forked from byorgey/it-lang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.hs
44 lines (33 loc) · 1.01 KB
/
Parser.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
module Parser where
import Control.Applicative hiding (many, (<|>))
import ItLang
import Text.Parsec
import Text.Parsec.Language
import qualified Text.Parsec.Token as P
lexer = P.makeTokenParser javaStyle
parens = P.parens lexer
braces = P.braces lexer
identifier = P.identifier lexer
natural = P.natural lexer
reserved = P.reserved lexer
symbol = P.symbol lexer
semi = P.semi lexer
semiSep = P.semiSep lexer
parseNat = toNat <$> natural
toNat 0 = Z
toNat n = S (toNat (n-1))
parseProg = braces (semiSep parseStmt)
parseStmt =
Assign <$> identifier <*> (symbol "=" *> parseExp)
<|> Block <$> parseProg
<|> If <$> parseBExp
<*> (reserved "then" *> parseStmt)
<*> (reserved "else" *> parseStmt <* reserved "endif")
<|> Repeat <$> (reserved "repeat" *> parseExp)
<*> (reserved "times" *> parseStmt <* reserved "done")
-- parseExp =
-- Lit <$> parseNat
-- <|> V <$> identifier
-- <|> Plus
parseExp = undefined
parseBExp = undefined