projects
/
kaleidoscope-hs-old.git
/ commitdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
| commitdiff |
tree
raw
|
patch
|
inline
| side by side (parent:
7a64aef
)
Parse if statements
author
Luke Lau
<luke_lau@icloud.com>
Sun, 10 Mar 2019 15:52:06 +0000
(15:52 +0000)
committer
Luke Lau
<luke_lau@icloud.com>
Wed, 17 Apr 2019 22:38:30 +0000
(23:38 +0100)
AST.hs
patch
|
blob
|
history
diff --git
a/AST.hs
b/AST.hs
index 1965ae6ba8378bc8277b2b46fe031ea04a64a5d5..720413652e4343740ad6268e0a866b0576e4c4aa 100644
(file)
--- a/
AST.hs
+++ b/
AST.hs
@@
-8,7
+8,7
@@
newtype Program = Program [AST]
deriving Show
instance Read Program where
deriving Show
instance Read Program where
- readPrec = fmap Program $ lift $ sepBy1 (read
Prec_to_P readPrec 0
) $ do
+ readPrec = fmap Program $ lift $ sepBy1 (read
S_to_P reads
) $ do
skipSpaces
char ';'
skipSpaces
skipSpaces
char ';'
skipSpaces
@@
-20,8
+20,9
@@
data Expr = Num Float
| BinOp BinOpType Expr Expr
| Var String
| Call String [Expr]
| BinOp BinOpType Expr Expr
| Var String
| Call String [Expr]
+ | If Expr Expr Expr
deriving Show
deriving Show
-data BinOpType = Add | Sub | Mul
+data BinOpType = Add | Sub | Mul
| Cmp Ordering
deriving Show
instance Read AST where
deriving Show
instance Read AST where
@@
-41,9
+42,13
@@
instance Read Expr where
readPrec = choice [ parseNum
, parseVar
, parseCall
readPrec = choice [ parseNum
, parseVar
, parseCall
- , parseBinOp '+' Add
- , parseBinOp '-' Sub
- , parseBinOp '*' Mul
+ , parseIf
+ , parseBinOp "+" Add
+ , parseBinOp "-" Sub
+ , parseBinOp "*" Mul
+ , parseBinOp ">" (Cmp GT)
+ , parseBinOp "<" (Cmp LT)
+ , parseBinOp "==" (Cmp EQ)
]
where parseNum = Num <$> readPrec
parseVar = Var <$> lift (munch1 isAlpha)
]
where parseNum = Num <$> readPrec
parseVar = Var <$> lift (munch1 isAlpha)
@@
-53,11
+58,27
@@
instance Read Expr where
sepBy (readS_to_P reads)
(skipSpaces >> char ',' >> skipSpaces)
return (Call func params)
sepBy (readS_to_P reads)
(skipSpaces >> char ',' >> skipSpaces)
return (Call func params)
- parseBinOp
c
typ = step $ do
+ parseBinOp
s
typ = step $ do
a <- prec 11 readPrec
lift $ do
skipSpaces
a <- prec 11 readPrec
lift $ do
skipSpaces
- char c
+ string s
skipSpaces
b <- readPrec
return (BinOp typ a b)
skipSpaces
b <- readPrec
return (BinOp typ a b)
+ parseIf = do
+ lift $ do
+ string "if"
+ skipSpaces
+ cond <- step readPrec
+ lift $ do
+ skipSpaces
+ string "then"
+ skipSpaces
+ thenE <- step readPrec
+ lift $ do
+ skipSpaces
+ string "else"
+ skipSpaces
+ elseE <- step readPrec
+ return (If cond thenE elseE)