From: Luke Lau Date: Sun, 19 May 2019 13:16:31 +0000 (+0100) Subject: Parse call expressions X-Git-Url: https://git.lukelau.me/?p=kaleidoscope-hs.git;a=commitdiff_plain;h=0daa38ae816488b94b57154c11a7bff5d376a28d Parse call expressions The Call constructor stores the name of the callee and the list of arguments being passed to it. --- 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