X-Git-Url: https://git.lukelau.me/?p=kaleidoscope-hs-old.git;a=blobdiff_plain;f=AST.hs;h=424cfbe7b13010e0960e4b88a1675cdcfa3b3f0f;hp=6528215422482fbfa3111faece64c3ff80205983;hb=51aeade75d5289fff417414b04bae5be0862fd8f;hpb=a0b31174f4b7f6ac037dc4002c38cd61b09dabaa diff --git a/AST.hs b/AST.hs index 6528215..424cfbe 100644 --- a/AST.hs +++ b/AST.hs @@ -1,20 +1,26 @@ module AST where import Text.Read -import Text.ParserCombinators.ReadP hiding ((+++)) +import Text.ParserCombinators.ReadP hiding ((+++), choice) +data BinOpType = Add | Sub | Mul + deriving Show data Expr = Num Float - | Add Expr Expr + | BinOp BinOpType Expr Expr deriving Show instance Read Expr where - readPrec = parseNum +++ parseAdd + readPrec = choice [ parseNum + , parseBinOp '+' Add + , parseBinOp '-' Sub + , parseBinOp '*' Mul + ] where parseNum = Num <$> readPrec - parseAdd = step $ do + parseBinOp c typ = step $ do a <- prec 11 readPrec lift $ do skipSpaces - char '+' + char c skipSpaces b <- readPrec - return (Add a b) + return (BinOp typ a b)