Parse call expressions
authorLuke Lau <luke_lau@icloud.com>
Sun, 19 May 2019 13:16:31 +0000 (14:16 +0100)
committerLuke Lau <luke_lau@icloud.com>
Sun, 19 May 2019 13:16:31 +0000 (14:16 +0100)
The Call constructor stores the name of the callee and the list of
arguments being passed to it.

AST.hs

diff --git a/AST.hs b/AST.hs
index e491b648a7d45f7bd3b419b0e3ff6a8c215cbd83..b57d7cb8cede024a9c2bb0e366d8d066474d331d 100644 (file)
--- 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