X-Git-Url: https://git.lukelau.me/?a=blobdiff_plain;f=AST.hs;h=c766afc1570140a77690e9f19259a68def0ac967;hb=e7171a5ebd7d1089dbf8d5465edcd74375df02d6;hp=5b22ea6c662714d483f1dd1ec51755e5f4b7dc7a;hpb=dfaf05671c2c0363d80bdbc6f737a536ed620a80;p=kaleidoscope-hs.git diff --git a/AST.hs b/AST.hs index 5b22ea6..c766afc 100644 --- a/AST.hs +++ b/AST.hs @@ -4,6 +4,7 @@ import Text.Read import Text.ParserCombinators.ReadP hiding ((+++), choice) data Expr = Num Float + | Var String | BinOp BinOp Expr Expr deriving Show @@ -12,12 +13,14 @@ data BinOp = Add | Sub | Mul | Cmp Ordering instance Read Expr where readPrec = choice [ parseNum + , parseVar , parseBinOp "<" 10 (Cmp LT) , parseBinOp "+" 20 Add , parseBinOp "-" 20 Sub , parseBinOp "*" 40 Mul ] where parseNum = Num <$> readPrec + parseVar = Var <$> lift (munch1 isAlpha) -- use 'prec 1' and 'step' so that parsing 'a' -- can only go one step deep, to prevent ininfite -- recursion