X-Git-Url: https://git.lukelau.me/?p=kaleidoscope-hs.git;a=blobdiff_plain;f=AST.hs;h=1e505af4f47116e401e2523f10c90ac1c466c360;hp=e491b648a7d45f7bd3b419b0e3ff6a8c215cbd83;hb=HEAD;hpb=2f3c8f5cb6c8b9e6f5be02b20910f8105a9011a6 diff --git a/AST.hs b/AST.hs index e491b64..9ff555a 100644 --- a/AST.hs +++ b/AST.hs @@ -4,9 +4,10 @@ import Data.Char import Text.Read import Text.ParserCombinators.ReadP hiding ((+++), choice) -data Expr = Num Float +data Expr = Num Double | Var String | BinOp BinOp Expr Expr + | Call String [Expr] deriving Show data BinOp = Add | Sub | Mul | Cmp Ordering @@ -15,7 +16,10 @@ data BinOp = Add | Sub | Mul | Cmp Ordering instance Read Expr where readPrec = parens $ choice [ parseNum , parseVar + , parseCall , parseBinOp "<" 10 (Cmp LT) + , parseBinOp ">" 10 (Cmp GT) + , parseBinOp "==" 10 (Cmp EQ) , parseBinOp "+" 20 Add , parseBinOp "-" 20 Sub , parseBinOp "*" 40 Mul @@ -30,6 +34,12 @@ instance Read Expr where skipSpaces b <- readPrec return (BinOp op a b) + parseCall = do + func <- lift (munch1 isAlpha) + params <- lift $ between (char '(') (char ')') $ + sepBy (readS_to_P reads) + (skipSpaces >> char ',' >> skipSpaces) + return (Call func params) data Prototype = Prototype String [String] deriving Show