-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
548 lines (487 loc) · 16.5 KB
/
parser.py
File metadata and controls
548 lines (487 loc) · 16.5 KB
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
import sys
import const
from typing import Dict, Any, List, Optional, Tuple
from lexer import Lexer, Token
from const import KEYWORDS, syserr
"""
parses the list of tokens into an abstract syntax tree
each node is a dictionary to tell the evaluator how to execute the code
"""
Node = Dict[str, Any]
AST = List[Node]
CondBody = List[Optional[Node]]
EOF = Token("EOF", "EOF")
class ParseError(Exception):
"""
for generic errors while parsing
"""
def __init__(self, message: Optional[str] = None) -> None:
if message:
super().__init__(message)
else:
super().__init__()
class Parser:
def __init__(self, tokens: List[Token]) -> None:
self.tokens = tokens
self.pos = 0
# priority for the operators for bodmas
self.operatorpriority = {
"MULTIPLY": 3,
"DIVIDE": 3,
"ADD": 2,
"SUBTRACT": 2,
"EQUALS": 1,
"GREATER": 1,
"LESS": 1,
"NOT": 1,
}
def curr(self) -> Token:
"""returns the token at the current index"""
return self.tokens[self.pos] if self.pos < len(self.tokens) else EOF
def peek(self) -> Token:
"""returns the token at the next index"""
return self.tokens[self.pos + 1] if self.pos + 1 < len(self.tokens) else EOF
def consume(
self,
expected: Optional[str] = None,
errormsg: Optional[str] = "",
error: Optional[type] = ParseError,
) -> Token:
"""
returns the token at the current index then moves on to the next index
if the token is not of the expected type (if the expected type is given),
raises an error of type error (if given) with the given error message
"""
token = self.curr()
if token.type == "EOF":
raise EOFError("unexpected end of file")
if error is not None:
if expected and token.type != expected:
if errormsg:
raise error(errormsg)
raise error(f"expected {expected} but {token} is {token.type}")
self.pos += 1
return token
def parse(self) -> AST:
"""entry point"""
ast: AST = []
while self.curr().type != "EOF":
parsed: Optional[Node] = self.parseline()
if parsed is not None:
ast.append(parsed)
return ast
def parseline(self) -> Optional[Node]:
'''parses a "line"'''
token: Token = self.curr()
if not token:
return None
# parses tokens based on its type
match token.type:
# code ends
case "EOF":
raise SystemError(f"unexpected EOF{syserr}")
# variable declaration
case "NEW_VAR_IDENT":
return self.decl()
# if statements
case "IF_OPEN":
return self.ifelse()
# while loops
case "WHILE_OPEN":
return self.whileloop()
# print statements
case "OUTPUT":
return self.output()
# input statements
case "INPUT":
return self.receive()
# variable reassignment
case "REASSIGNMENT_IDENT":
return self.reassign()
# variables
case "ID":
return self.expr()
# comments
case "COMMENT_OPEN":
self.consume("COMMENT_OPEN")
return None
# \n
case "NEWLINE":
self.consume("NEWLINE")
return None
# try except
case "TRY_OPEN":
return self.tryexcept()
# flow control
case "BREAK" | "CONTINUE":
self.consume()
return {"type": "flow", "name": token.type.lower()}
# warnings
case "VAR":
raise ParseError(
f"unexpected token {KEYWORDS['NEW_VAR_IDENT']}\ndid you mean to declare a variable?"
)
case _:
raise ParseError(f"unexpected token {token}{syserr}")
def decl(self) -> Node:
"""
handles declaration of new variables
type: str
name: str
vartype: str
isconst: bool
value: Optional[Node]
"""
self.consume(
"NEW_VAR_IDENT",
errormsg=f"expected {KEYWORDS['NEW_VAR_IDENT']}{syserr}",
error=SystemError,
)
# tries to get variable type
try:
vartypetok: Token = self.consume(
"TYPE", errormsg="expected a type declaration", error=SyntaxError
)
except SyntaxError as e:
if self.peek().type in {"CONST", "VAR"}:
raise SyntaxError(
f"expected a type declaration after {KEYWORDS['NEW_VAR_IDENT']}\ndid you forget to declare a type?"
) from e
raise SyntaxError(
f"expected a type declaration after {KEYWORDS['NEW_VAR_IDENT']}"
) from e
vartype: str = vartypetok.value
# tries to get variable mutability
constvar: Token = self.consume()
isconst: bool = False
# verifies if constexist is set to true
if const.constexist:
match constvar.type:
case "CONST":
isconst = True
case "VAR":
isconst = False
case _:
raise SyntaxError(
f"expected {KEYWORDS['CONST']} or {KEYWORDS['VAR']} after {KEYWORDS['NEW_VAR_IDENT']}"
)
# tries to get variable name + value
idtok: Token = self.consume(
"ID", errormsg="expected a variable", error=SyntaxError
)
varname: str = idtok.value
value: Optional[Node] = None
if self.curr().type == "ASSIGNMENT_OPERATOR":
self.consume(
"ASSIGNMENT_OPERATOR",
errormsg=f"{KEYWORDS['ASSIGNMENT_OPERATOR']} expected",
error=SyntaxError,
)
value = self.expr()
return {
"type": "declaration",
"name": varname,
"vartype": vartype,
"isconst": isconst,
"value": value,
}
def tryexcept(self) -> Node:
"""
parses try-except functions
type: str
try: CondBody
except: CondBody
"""
self.consume(
"TRY_OPEN",
errormsg=f"expected {KEYWORDS['TRY_OPEN']}{syserr}",
error=SystemError,
)
self.consume("DO", errormsg=f"expected {KEYWORDS['DO']}", error=SyntaxError)
# consumes the newline if it exists
if self.curr().type == "NEWLINE":
self.consume(
"NEWLINE", errormsg=f"expected a newline{syserr}", error=SystemError
)
trybody: CondBody = []
while self.curr().type != "EXCEPT":
trybody.append(self.parseline())
self.consume(
"EXCEPT", errormsg=f"expected {KEYWORDS['EXCEPT']}", error=SyntaxError
)
self.consume("DO", errormsg=f"expected {KEYWORDS['DO']}", error=SyntaxError)
exceptbody: CondBody = []
while self.curr().type != "TRY_CLOSE":
exceptbody.append(self.parseline())
self.consume(
"TRY_CLOSE",
errormsg=f"expected {KEYWORDS['TRY_CLOSE']} to end try-except",
error=SyntaxError,
)
return {
"type": "try",
"try": trybody,
"except": exceptbody,
}
def ifelse(self) -> Node:
"""
parses if else
type: str
condition: Node
body: CondBody
elifs: List[Tuple[Node, CondBody]]
elsebody: CondBody
"""
self.consume(
"IF_OPEN",
errormsg=f"expected {KEYWORDS['IF_OPEN']}{syserr}",
error=SystemError,
)
condition: Node = self.evalcond()
self.consume(
"THEN",
errormsg=f"expected {KEYWORDS['THEN']} after {KEYWORDS['IF_OPEN']}",
error=SyntaxError,
)
self.consume(
"DO",
errormsg=f"expected {KEYWORDS['DO']} after {KEYWORDS['THEN']}",
error=SyntaxError,
)
# gets the code that gets run in the if clause
body: CondBody = []
while self.curr().type not in {"ELIF", "ELSE", "IF_CLOSE"}:
body.append(self.parseline())
elifs: List[Tuple[Node, CondBody]] = []
elsebody: CondBody = []
# gets the condition for the elif statement and the code that runs in the elif clause
while self.curr().type == "ELIF":
self.consume(
"ELIF",
errormsg=f"expected {KEYWORDS['ELIF']}{syserr}",
error=SystemError,
)
elifcond: Node = self.evalcond()
self.consume(
"THEN",
errormsg=f"expected {KEYWORDS['THEN']} after {KEYWORDS['ELIF']}",
error=SyntaxError,
)
self.consume(
"DO",
errormsg=f"expected {KEYWORDS['DO']} after {KEYWORDS['THEN']}",
error=SyntaxError,
)
elifbody: CondBody = []
while self.curr().type not in {"ELIF", "ELSE", "IF_CLOSE"}:
elifbody.append(self.parseline())
elifs.append((elifcond, elifbody))
# gets the code that gets run in the else clause
if self.curr().type == "ELSE":
self.consume(
"ELSE",
errormsg=f"expected {KEYWORDS['ELSE']}{syserr}",
error=SystemError,
)
self.consume(
"DO",
errormsg=f"expected {KEYWORDS['DO']} after {KEYWORDS['ELSE']}",
error=SyntaxError,
)
while self.curr().type != "IF_CLOSE":
elsebody.append(self.parseline())
self.consume(
"IF_CLOSE",
errormsg=f"expected {KEYWORDS['IF_CLOSE']} to end if statement",
error=SyntaxError,
)
return {
"type": "if",
"condition": condition,
"body": body,
"elifs": elifs,
"elsebody": elsebody,
}
def whileloop(self) -> Node:
"""
handles while loops
type: str
condition: Node
body: CondBody
"""
self.consume(
"WHILE_OPEN",
errormsg=f"expected {KEYWORDS['WHILE_OPEN']}{syserr}",
error=SystemError,
)
condition: Node = self.expr()
self.consume(
"DO",
errormsg=f"expected {KEYWORDS['DO']} after {KEYWORDS['WHILE_OPEN']}",
error=SyntaxError,
)
# gets the code in the while loop
body: CondBody = []
while self.curr().type != "WHILE_CLOSE":
body.append(self.parseline())
self.consume(
"WHILE_CLOSE",
errormsg=f"expected {KEYWORDS['WHILE_CLOSE']} to end while loop",
error=SyntaxError,
)
return {"type": "while", "condition": condition, "body": body}
def output(self) -> Node:
"""
handles print statements
type: str
value: Optional[Node]
newline: bool
"""
self.consume(
"OUTPUT",
errormsg=f"expected {KEYWORDS['OUTPUT']}{syserr}",
error=SystemError,
)
newline: bool = False
value: Optional[Node] = None
match self.curr().type:
case "OUTPUT_NEWLINE":
self.consume(
"OUTPUT_NEWLINE",
errormsg=f"expected {KEYWORDS['OUTPUT_NEWLINE']}{syserr}",
error=SystemError,
)
value = self.expr()
newline = True
case _:
value = self.expr()
return {"type": "output", "value": value, "newline": newline}
def receive(self) -> Node:
"""
handles user input
type: str
target: Optional[str]
"""
self.consume(
"INPUT", errormsg=f"expected {KEYWORDS['INPUT']}{syserr}", error=SystemError
)
target: Optional[str] = None
if self.curr().type == "TO":
self.consume(
"TO", errormsg=f"expected {KEYWORDS['TO']}{syserr}", error=SystemError
)
target = self.consume(
"ID", errormsg="expected a variable", error=SyntaxError
).value
return {"type": "input", "target": target}
def reassign(self) -> Node:
"""
handles variable reassignment
type: str
name: str
value: Node
"""
self.consume(
"REASSIGNMENT_IDENT",
errormsg=f"expected {KEYWORDS['REASSIGNMENT_IDENT']}{syserr}",
error=SystemError,
)
varname: str = self.consume(
"ID", errormsg="expected a variable name", error=SyntaxError
).value
self.consume(
"TO",
errormsg=f"expected {KEYWORDS['TO']} after variable name",
error=SyntaxError,
)
value: Node = self.expr()
return {"type": "reassignment", "name": varname, "value": value}
def expr(self, priority: int = 0) -> Node:
"""
parses conditionals and math
ai helped a lot with this sob
type: str
op: str
left: Node
right: Node
"""
left: Node = self.exprhelper()
while (
self.curr().type not in {"RIGHT_BRACKET", "NEWLINE", "DO", "THEN", "EOF"}
and self.priority(self.curr().type) > priority
):
operator: str = self.consume().type
right: Node = self.expr(self.priority(operator))
left = {"type": "operation", "op": operator, "left": left, "right": right}
return left
def exprhelper(self) -> Node:
"""
helper function for expr()
basically just a bunch of if else statements
type: str
valtype: Optional[str]
value: Optional[str]
name: Optional[str]
"""
token = self.consume()
match token.type:
case "LEFT_BRACKET":
expr = self.expr()
self.consume(
"RIGHT_BRACKET",
errormsg=f"expected {KEYWORDS['CLOSE_PAREN']}",
error=SyntaxError,
)
return expr
case _ if token.type in self.operatorpriority.keys():
expr = self.expr()
return expr
case "INT" | "FLOAT":
return {"type": "literal", "valtype": token.type, "value": token.value}
case "BOOL":
return {
"type": "literal",
"valtype": "BOOL",
"value": token.value == "true",
}
case "STRING":
return {"type": "literal", "valtype": "STRING", "value": token.value}
case "ID":
return {"type": "variable", "name": token.value}
case _:
raise ParseError(f"invalid expression token {token.type}")
def evalcond(self) -> Node:
"""
evaluates a condition
type: str
op: str
left: Node
right: Node
"""
node: Node = self.expr()
while self.curr().type not in ["RIGHT_BRACKET", "NEWLINE", "THEN"]:
operator: str = self.consume().type
right: Node = self.expr()
node = {"type": "comparison", "op": operator, "left": node, "right": right}
return node
def priority(self, toktype: str) -> int:
"""returns operator priority"""
res = self.operatorpriority.get(toktype, 0)
if not res:
raise RuntimeError(f"{toktype} is not an operator")
return res
# for easier debugging
if __name__ == "__main__":
flag = sys.argv[1] if len(sys.argv) > 1 else None
args = sys.argv[2:]
code = None
if flag == "--debug":
code = "\n".join(open(file, "r").read() for file in args)
if code:
print(f"code: \n{code}")
lexer = Lexer(code)
tokens = lexer.tokenize()
print(f"tokens: {tokens}\n\n")
parser = Parser(tokens)
ast = parser.parse()
print(f"ast: {ast}\n\n")