projects
/
kaleidoscope-hs-old.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Add support for more binary ops
[kaleidoscope-hs-old.git]
/
AST.hs
diff --git
a/AST.hs
b/AST.hs
index 6528215422482fbfa3111faece64c3ff80205983..424cfbe7b13010e0960e4b88a1675cdcfa3b3f0f 100644
(file)
--- a/
AST.hs
+++ b/
AST.hs
@@
-1,20
+1,26
@@
module AST where
import Text.Read
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
data Expr = Num Float
- |
Add
Expr Expr
+ |
BinOp BinOpType
Expr Expr
deriving Show
instance Read Expr where
deriving Show
instance Read Expr where
- readPrec = parseNum +++ parseAdd
+ readPrec = choice [ parseNum
+ , parseBinOp '+' Add
+ , parseBinOp '-' Sub
+ , parseBinOp '*' Mul
+ ]
where parseNum = Num <$> readPrec
where parseNum = Num <$> readPrec
- parse
Add
= step $ do
+ parse
BinOp c typ
= step $ do
a <- prec 11 readPrec
lift $ do
skipSpaces
a <- prec 11 readPrec
lift $ do
skipSpaces
- char
'+'
+ char
c
skipSpaces
b <- readPrec
skipSpaces
b <- readPrec
- return (
Add
a b)
+ return (
BinOp typ
a b)