-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparse.py
More file actions
86 lines (77 loc) · 2.71 KB
/
parse.py
File metadata and controls
86 lines (77 loc) · 2.71 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
from nodes import *
from types_ import *
from printf import printf
from SymbolTable import global_symbol_table
class Parser:
def __init__(self, tokens):
self.tokens = iter(tokens)
self.advance()
def advance(self):
try:
self.current_token = next(self.tokens)
except StopIteration:
self.current_token = None
def generate_tree(self):
if self.current_token == None:
return None
result = self.expr()
if self.current_token != None:
print(f"ERROR: parsing error")
return result
def expr(self):
if self.current_token.type == TYPE_IDENTIFIER:
var_name_token = self.current_token
if global_symbol_table.get(var_name_token.value):
self.advance()
return VarAccessNode(var_name_token) #TODO: Make variables in the left hand size of the expression work.
self.advance()
if self.current_token.type != TYPE_EQUAL:
if not global_symbol_table.get(self.current_token.value): return print("ERROR: missing ':='")
self.advance()
var_value_token = self.expr()
return VarAssignNode(var_name_token, var_value_token)
else:
result = self.term() # Left part of expression
while self.current_token != None and self.current_token.type in (TYPE_PLUS, TYPE_MINUS):
if self.current_token.type == TYPE_PLUS:
self.advance()
result = AddNode(result, self.term())
elif self.current_token.type == TYPE_MINUS:
self.advance()
result = SubtractNode(result, self.term())
return result
def term(self):
result = self.factor() # Left part of expression
while self.current_token != None and self.current_token.type in (TYPE_MULTIPLY, TYPE_DIVIDE):
if self.current_token.type == TYPE_MULTIPLY:
self.advance()
result = MultiplyNode(result, self.factor())
elif self.current_token.type == TYPE_DIVIDE:
self.advance()
result = DivideNode(result, self.factor())
return result
def factor(self):
token = self.current_token
if token.type == TYPE_LPAR:
self.advance()
result = self.expr()
if self.current_token.type != TYPE_RPAR:
printf("ERROR: missing close parenthesis")
return;
self.advance()
return result
elif token.type == TYPE_NUMBER:
self.advance()
return NumberNode(token.value)
elif token.type == TYPE_PLUS:
self.advance()
return PlusNode(self.factor())
elif token.type == TYPE_MINUS:
self.advance()
return MinusNode(self.factor())
elif token.type == TYPE_NUMBER:
self.advance()
return NumberNode(self.factor())
elif token.type == TYPE_IDENTIFIER:
self.advance()
return VarAccessNode(self.factor())