projects
/
kaleidoscope-hs.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Parse variables in expressions
[kaleidoscope-hs.git]
/
AST.hs
diff --git
a/AST.hs
b/AST.hs
index 5b22ea6c662714d483f1dd1ec51755e5f4b7dc7a..c766afc1570140a77690e9f19259a68def0ac967 100644
(file)
--- a/
AST.hs
+++ b/
AST.hs
@@
-4,6
+4,7
@@
import Text.Read
import Text.ParserCombinators.ReadP hiding ((+++), choice)
data Expr = Num Float
import Text.ParserCombinators.ReadP hiding ((+++), choice)
data Expr = Num Float
+ | Var String
| BinOp BinOp Expr Expr
deriving Show
| BinOp BinOp Expr Expr
deriving Show
@@
-12,12
+13,14
@@
data BinOp = Add | Sub | Mul | Cmp Ordering
instance Read Expr where
readPrec = choice [ parseNum
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
, 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
-- use 'prec 1' and 'step' so that parsing 'a'
-- can only go one step deep, to prevent ininfite
-- recursion