X-Git-Url: https://git.lukelau.me/?p=kaleidoscope-hs.git;a=blobdiff_plain;f=AST.hs;h=c766afc1570140a77690e9f19259a68def0ac967;hp=5b22ea6c662714d483f1dd1ec51755e5f4b7dc7a;hb=45afeea77cd017d2ac37cf330c595c22c90a7f87;hpb=d6cecbbf067560322e391363250fd3c5d45bfbec 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