-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParse.hs
187 lines (166 loc) · 4.82 KB
/
Parse.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
module Parse
( program
, expr
) where
import Control.Applicative hiding (many, (<|>))
import Data.Char
import Text.ParserCombinators.Parsec
import qualified PowAST as Ast
-- We have many fun !!
program :: Parser Ast.Program
program = do
funs <- many fun
_ <- eof
return funs
fun :: Parser Ast.Fun
fun = do
sc <- lexeme scoping
fn <- lexeme $ funName
_ <- lexeme $ char '⊂'
ps <- lexeme $ params
_ <- lexeme $ char '⊃'
bd <- lexeme compoundStatement
return $ Ast.Fun fn sc ps bd
scoping :: Parser Ast.Scoping
scoping = (symbol "static" >> return Ast.StaticScoping) <|>
(symbol "dynamic" >> return Ast.DynamicScoping)
name :: Parser String
name = do
-- NOTE: Maybe allow name to start with numbers
-- [a-zA-Z_][a-zA-Z0-9_]*
first <- satisfy (orUnderscore isAlpha)
rest <- many $ satisfy (orUnderscore isAlphaNum)
return $ [first] ++ rest
funName :: Parser Ast.FunName
funName = name
lexeme :: Parser a -> Parser a
lexeme parser = parser <* spaces
symbol :: String -> Parser String
symbol = lexeme . string
params :: Parser [Ast.Param]
params = sepByCommas identifier
arguments :: Parser Ast.Args
arguments = sepByCommas expr
sepByCommas :: Parser a -> Parser [a]
sepByCommas = (flip sepBy) (symbol ",")
identifier :: Parser Ast.Id
identifier = lexeme $ do
_ <- char '~'
id <- name
return id
statement :: Parser Ast.Statement
statement = try statement' <|> conditional <|> loop
where
statement' = do
e <- expr
_ <- symbol ":"
return e
parens :: Parser a -> Parser a
parens p = symbol "(" *> p <* symbol ")"
expr :: Parser Ast.Expr
expr =
giveback
<|> write
<|> conditional
<|> loop
<|> binOpChain
where
binOpChain = assignment
assignment = chainr1 equal (symbol "←" >> return Ast.Assign)
equal = chainl1 notEqual (symbol "=" >> return Ast.Equal)
notEqual = chainl1 lessgreater (symbol "≠" >> return Ast.NotEqual)
lessgreater = chainl1 strConcat binOp
where
binOp =
(try $ (symbol ">=" <|> symbol "≥") >> return Ast.GreaterEq) <|>
(try $ (symbol "<=" <|> symbol "≤") >> return Ast.LessEq) <|>
(symbol "<" >> return Ast.Less) <|>
(symbol ">" >> return Ast.Greater)
strConcat = chainl1 plusminus binOp
where binOp = symbol "↔" >> return Ast.StrConcat
plusminus = chainl1 timesdivide binOp
where binOp = (symbol "+" >> return Ast.Plus) <|>
(symbol "-" >> return Ast.Minus)
timesdivide = chainl1 parseRealThing binOp
where binOp = (symbol "*" >> return Ast.Times) <|>
(symbol "/" >> return Ast.Divide)
parseRealThing =
try arrayIndex
<|> (parens expr)
<|> arrayLit
<|> numbers
<|> trool
<|> string
<|> variable
<|> call
trool = (symbol "⊥" >> return (Ast.TroolLit Ast.No)) <|>
(symbol "⊤" >> return (Ast.TroolLit Ast.Yes)) <|>
(symbol "⟛" >> return (Ast.TroolLit Ast.CouldHappen))
-- TODO: Add escaping?!
string = do
char '|'
s <- many $ noneOf "|"
symbol "|"
return $ Ast.StrLit s
arrayIndex = do
ns <- numbers
e <- parens expr
return $ Ast.ArrayIndex ns e
arrayLit = do
symbol "#"
exprs <- sepByCommas expr
return $ Ast.ArrayLit exprs
write = do
symbol "✎"
e <- expr
return $ Ast.Write e
variable = do
id <- identifier
return $ Ast.Var id
numbers = do
ns <- lexeme $ many1 digit
return $ Ast.IntLit (read ns)
giveback = do
_ <- symbol "giveback" <|> symbol "↜"
expr' <- expr
return $ Ast.GiveBack expr'
call = do
symbol "⊂"
args <- arguments
symbol "⊃"
symbol "↝"
funName <- lexeme name
return $ Ast.Call args funName
loop :: Parser Ast.Expr
loop = do
symbol "⟳"
condition <- expr
symbol "?"
body <- compoundStatement
return $ Ast.While condition body
-- TODO: Make this less ugly!
conditional :: Parser Ast.Expr
conditional = try conditionalWithElse <|> conditionalWithoutElse
where
conditionalWithoutElse = do
symbol "¿"
condition <- expr
symbol "?"
thenBranch <- compoundStatement
return $ Ast.If condition thenBranch []
conditionalWithElse = do
symbol "¿"
condition <- expr
symbol "?"
thenBranch <- compoundStatement
symbol "!"
elseBranch <- compoundStatement
return $ Ast.If condition thenBranch elseBranch
compoundStatement :: Parser [Ast.Statement]
compoundStatement = do
symbol "/"
body <- many statement
symbol "\\"
return body
orUnderscore :: (Char -> Bool) -> Char -> Bool
orUnderscore f c = f c || c == '_'