-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.hs
309 lines (263 loc) · 8.88 KB
/
Main.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
import Control.Monad
import Text.Parsec.Language
import Text.ParserCombinators.Parsec
import Text.ParserCombinators.Parsec.Expr
import Text.ParserCombinators.Parsec.Language
import qualified Text.ParserCombinators.Parsec.Token as Token
import System.IO
import System.Environment
import Data.Time
type Id = String
type Type = String
data Op = Or
| And
| BoolNeg
| Eq
| Neq
| Leq
| Le
| Geq
| Ge
| Pow
| Neg
| Add
| Sub
| Mul
| Div
| ExprMerge
| ApplyFunc
deriving (Eq, Show)
data Expr = EmptyExpr
| Var Id
| Num Integer
| StringLiteral String
| BoolLiteral Bool
| BinOp Op Expr Expr
| UnOp Op Expr
deriving (Eq, Show)
data Statement = Empty
| Assign Id Expr
| Decl Type Id Expr
| If Expr Statement Statement
| While Expr Statement
| Union Statement Statement
| ExprSt Expr
| Return Expr
deriving (Eq, Show)
data Arg = Arg Type Id
deriving (Eq, Show)
data Func = Func Type Id [Arg] Statement
deriving (Eq, Show)
languageDefinition =
emptyDef { Token.nestedComments = False
, Token.caseSensitive = True
, Token.identStart = oneOf "qwertyuiopasdfghjklzxcvbnm"
, Token.identLetter = alphaNum
, Token.reservedNames = [ "{"
, "}"
, "("
, ")"
, ";"
, "->"
, "Func"
, "While"
, "If"
, "Else"
, "Var"
, "Int"
, "String"
, "Bool"
, "Return"
]
, Token.reservedOpNames = [ "||"
, "&&"
, "!"
, "=="
, "/="
, "<="
, "<"
, ">="
, ">"
, "^"
, "+"
, "-"
, "*"
, "/"
, ","
, "$"
, ":="
]
}
lexer = Token.makeTokenParser languageDefinition
identifier = Token.identifier lexer
reserved = Token.reserved lexer
reservedOp = Token.reservedOp lexer
parens = Token.parens lexer
braces = Token.braces lexer
integer = Token.integer lexer
whiteSpace = Token.whiteSpace lexer
stringL = Token.stringLiteral lexer
boolLiter :: Parser Expr
boolLiter = (do
reserved "True"
return $ BoolLiteral True)
<|> (do
reserved "False"
return $ BoolLiteral False)
fromBinToInteger :: String -> Integer
fromBinToInteger = acc 0
where acc num (x:xs) | x == '0' = acc (num * 2) xs
| x == '1' = acc (num * 2 + 1) xs
acc num [] = num
binNumber :: Parser Expr
binNumber = do
prefix <- char 'B'
str <- many1 ((char '0') <|> (char '1'))
whiteSpace
return $ (Num . fromBinToInteger) str
varOrFuncCall :: Parser Expr
varOrFuncCall = do
name <- identifier
expr <- ((parens expression) <|> (return EmptyExpr))
return $ if expr == EmptyExpr then Var name else BinOp ApplyFunc (Var name) expr
term = fmap StringLiteral stringL
<|> fmap Num integer
<|> varOrFuncCall
<|> boolLiter
<|> parens expression
<|> binNumber
<|> (return EmptyExpr)
expression :: Parser Expr
expression = buildExpressionParser operators term
operators = [
[Infix (reservedOp "^" >> return (BinOp Pow)) AssocRight]
, [Prefix (reservedOp "-" >> return (UnOp Neg))]
, [Infix (reservedOp "*" >> return (BinOp Mul)) AssocLeft,
Infix (reservedOp "/" >> return (BinOp Div)) AssocLeft]
, [Infix (reservedOp "+" >> return (BinOp Add)) AssocLeft,
Infix (reservedOp "-" >> return (BinOp Sub)) AssocLeft]
, [Infix (reservedOp "==" >> return (BinOp Eq)) AssocNone,
Infix (reservedOp "/=" >> return (BinOp Neq)) AssocNone,
Infix (reservedOp "<=" >> return (BinOp Leq)) AssocNone,
Infix (reservedOp "<" >> return (BinOp Le)) AssocNone,
Infix (reservedOp ">=" >> return (BinOp Geq)) AssocNone,
Infix (reservedOp ">" >> return (BinOp Ge)) AssocNone]
, [Prefix (reservedOp "!" >> return (UnOp BoolNeg))]
, [Infix (reservedOp "&&" >> return (BinOp And)) AssocRight]
, [Infix (reservedOp "||" >> return (BinOp Or)) AssocRight]
, [Infix (reservedOp "," >> return (BinOp ExprMerge)) AssocLeft]
, [Infix (reservedOp "$" >> return (BinOp ApplyFunc)) AssocRight]
]
assignStatement :: Parser Statement
assignStatement = do
name <- identifier
reservedOp ":="
expr <- expression
reserved ";"
return $ Assign name expr
declType :: Parser Type
declType = (do
reserved "Int"
return "Int") <|>
(do
reserved "Bool"
return "Bool") <|>
(do
reserved "String"
return "String")
declStatement :: Parser Statement
declStatement = do
reserved "Var"
myType <- declType
name <- identifier
reservedOp ":="
expr <- expression
reserved ";"
return $ Decl myType name expr
exprStatement :: Parser Statement
exprStatement = do
expr <- expression
reserved ";"
return $ ExprSt expr
statement' :: Parser Statement
statement' = assignStatement
<|> declStatement
<|> ifStatement
<|> whileStatement
<|> exprStatement
<|> returnStatement
statement :: Parser Statement
statement = do
firstStatement <- statement'
other <- (statement <|> return Empty)
return $ Union firstStatement other
returnStatement :: Parser Statement
returnStatement = do
reserved "Return"
expr <- expression
reserved ";"
return $ Return expr
elseStatement :: Parser Statement
elseStatement = (do
reserved "Else"
action <- (braces statement)
return action)
<|> (return Empty)
ifStatement :: Parser Statement
ifStatement = do
reserved "If"
condition <- (parens expression)
ifAction <- (braces statement)
elseAction <- elseStatement
return $ If condition ifAction elseAction
whileStatement :: Parser Statement
whileStatement = do
reserved "While"
condition <- (parens expression)
action <- (braces statement)
return $ While condition action
arg :: Parser Arg
arg = do
myType <- declType
name <- identifier
return $ Arg myType name
typedArgList :: Parser [Arg]
typedArgList = (do
firstArg <- arg
other <- ((do
reserved ","
typedArgList) <|> return [])
return $ firstArg : other) <|> (return [])
funcDecl :: Parser Func
funcDecl = do
reserved "Func"
name <- identifier
args <- (parens typedArgList)
reserved "->"
myType <- declType
action <- (braces statement)
whiteSpace
return $ Func myType name args action
funcList :: Parser [Func]
funcList = do
firstFunc <- funcDecl
other <- (funcList <|> return [])
return $ firstFunc : other
parseAnyResult parser str =
case parse (whiteSpace >> parser) "" str of
Left err -> error $ show err
Right result -> "Succes!"
parseAny parser str =
case parse (whiteSpace >> parser) "" str of
Left err -> error $ show err
Right result -> result
checkFile file = do
contents <- readFile file
start <- getCurrentTime
print (parseAnyResult funcList contents)
stop <- getCurrentTime
print $ diffUTCTime stop start
buildAST :: String -> IO()
buildAST file = do
contents <- readFile file
print (parseAny funcList contents)