From: Luke Lau Date: Wed, 5 Jun 2019 21:09:41 +0000 (+0100) Subject: Parse if expressions X-Git-Url: http://git.lukelau.me/?p=kaleidoscope-hs.git;a=commitdiff_plain;h=3d32ea6f2146d033b27a28907ad32ca6c7a7279d Parse if expressions Back to the AST. --- 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