From: Luke Lau Date: Sat, 18 May 2019 16:51:22 +0000 (+0100) Subject: Parse variables in expressions X-Git-Url: http://git.lukelau.me/?p=kaleidoscope-hs.git;a=commitdiff_plain;h=45afeea77cd017d2ac37cf330c595c22c90a7f87 Parse variables in expressions --- 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