X-Git-Url: http://git.lukelau.me/?a=blobdiff_plain;f=AST.hs;h=ae18a52347ce91c37336a06f891bae690ba691ec;hb=e3ba36b0fd73d28d041de61ed7d6bf590ef34f18;hp=9ff555a74d10c6d970fc8b7df0a37c1d98fdbffc;hpb=de8c7223c79f10c69f9916db1f15b34d20938e2c;p=kaleidoscope-hs.git diff --git a/AST.hs b/AST.hs index 9ff555a..ae18a52 100644 --- a/AST.hs +++ b/AST.hs @@ -8,6 +8,7 @@ data Expr = Num Double | Var String | BinOp BinOp Expr Expr | Call String [Expr] + | If Expr Expr Expr deriving Show data BinOp = Add | Sub | Mul | Cmp Ordering @@ -17,6 +18,7 @@ instance Read Expr where readPrec = parens $ choice [ parseNum , parseVar , parseCall + , parseIf , parseBinOp "<" 10 (Cmp LT) , parseBinOp ">" 10 (Cmp GT) , parseBinOp "==" 10 (Cmp EQ) @@ -28,10 +30,7 @@ instance Read Expr where parseVar = Var <$> lift (munch1 isAlpha) parseBinOp s prc op = prec prc $ do a <- step readPrec - lift $ do - skipSpaces - string s - skipSpaces + spaced $ string s b <- readPrec return (BinOp op a b) parseCall = do @@ -40,6 +39,15 @@ instance Read Expr where sepBy (readS_to_P reads) (skipSpaces >> char ',' >> skipSpaces) return (Call func params) + parseIf = do + spaced $ string "if" + cond <- readPrec + spaced $ string "then" + thenE <- readPrec + spaced $ string "else" + elseE <- readPrec + return (If cond thenE elseE) + spaced f = lift $ skipSpaces >> f >> skipSpaces data Prototype = Prototype String [String] deriving Show