X-Git-Url: http://git.lukelau.me/?p=kaleidoscope-hs.git;a=blobdiff_plain;f=AST.hs;fp=AST.hs;h=1e505af4f47116e401e2523f10c90ac1c466c360;hp=9ff555a74d10c6d970fc8b7df0a37c1d98fdbffc;hb=3d32ea6f2146d033b27a28907ad32ca6c7a7279d;hpb=7b8ef6725099f09b81f8c39ca6f00dec14213bed diff --git a/AST.hs b/AST.hs index 9ff555a..1e505af 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) @@ -40,6 +42,14 @@ instance Read Expr where sepBy (readS_to_P reads) (skipSpaces >> char ',' >> skipSpaces) return (Call func params) + parseIf = do + lift $ skipSpaces >> string "if" >> skipSpaces + cond <- readPrec + lift $ skipSpaces >> string "then" >> skipSpaces + thenE <- readPrec + lift $ skipSpaces >> string "else" >> skipSpaces + elseE <- readPrec + return (If cond thenE elseE) data Prototype = Prototype String [String] deriving Show