X-Git-Url: http://git.lukelau.me/?a=blobdiff_plain;f=AST.hs;h=1e505af4f47116e401e2523f10c90ac1c466c360;hb=8d014ae9c14ab12436acad496ba2726e39b4d69a;hp=dfa43b7c60c0d0ced86a73268167b1b9584b9d74;hpb=4d1dcdc8a1187df7d2981d426f57f79be922358d;p=kaleidoscope-hs.git diff --git a/AST.hs b/AST.hs index dfa43b7..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,7 +18,10 @@ instance Read Expr where readPrec = parens $ choice [ parseNum , parseVar , parseCall + , parseIf , parseBinOp "<" 10 (Cmp LT) + , parseBinOp ">" 10 (Cmp GT) + , parseBinOp "==" 10 (Cmp EQ) , parseBinOp "+" 20 Add , parseBinOp "-" 20 Sub , parseBinOp "*" 40 Mul @@ -38,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