X-Git-Url: https://git.lukelau.me/?p=kaleidoscope-hs.git;a=blobdiff_plain;f=AST.hs;h=b57d7cb8cede024a9c2bb0e366d8d066474d331d;hp=7dad3a19e91eab9f12657101bd224b5c81742d07;hb=0daa38ae816488b94b57154c11a7bff5d376a28d;hpb=8a66ac4f81c3d1eab3b13a9e58cee0098573eff1 diff --git a/AST.hs b/AST.hs index 7dad3a1..b57d7cb 100644 --- a/AST.hs +++ b/AST.hs @@ -7,14 +7,16 @@ 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 deriving Show instance Read Expr where - readPrec = choice [ parseNum + 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