X-Git-Url: https://git.lukelau.me/?a=blobdiff_plain;f=AST.hs;h=ae18a52347ce91c37336a06f891bae690ba691ec;hb=023adfd1ca927790be8335c0df3334a861129da3;hp=b57d7cb8cede024a9c2bb0e366d8d066474d331d;hpb=7527d7269557b5199bb1e661150ef958d97e2c15;p=kaleidoscope-hs.git diff --git a/AST.hs b/AST.hs index b57d7cb..ae18a52 100644 --- a/AST.hs +++ b/AST.hs @@ -4,10 +4,11 @@ import Data.Char import Text.Read import Text.ParserCombinators.ReadP hiding ((+++), choice) -data Expr = Num Float +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 @@ -26,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 @@ -38,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