X-Git-Url: https://git.lukelau.me/?p=kaleidoscope-hs.git;a=blobdiff_plain;f=AST.hs;h=b57d7cb8cede024a9c2bb0e366d8d066474d331d;hp=e491b648a7d45f7bd3b419b0e3ff6a8c215cbd83;hb=0daa38ae816488b94b57154c11a7bff5d376a28d;hpb=2f3c8f5cb6c8b9e6f5be02b20910f8105a9011a6 diff --git a/AST.hs b/AST.hs index e491b64..b57d7cb 100644 --- a/AST.hs +++ b/AST.hs @@ -7,6 +7,7 @@ import Text.ParserCombinators.ReadP hiding ((+++), choice) data Expr = Num Float | Var String | BinOp BinOp Expr Expr + | Call String [Expr] deriving Show data BinOp = Add | Sub | Mul | Cmp Ordering @@ -15,6 +16,7 @@ data BinOp = Add | Sub | Mul | Cmp Ordering instance Read Expr where readPrec = parens $ choice [ parseNum , parseVar + , parseCall , parseBinOp "<" 10 (Cmp LT) , parseBinOp "+" 20 Add , parseBinOp "-" 20 Sub @@ -30,6 +32,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