-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
131 lines (113 loc) · 4.45 KB
/
parser.py
File metadata and controls
131 lines (113 loc) · 4.45 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
import time
import javax_tokens
def parse_tokens(tokens):
idx = 0
ast = []
def parse_expression():
nonlocal idx
expression = []
while idx < len(tokens):
token_type = tokens[idx][0]
token_value = tokens[idx][1]
if (token_type == "KEYWORD") and (token_value in javax_tokens.keywords):
if token_value == "return":
break
function_name = token_value
idx += 2
arguments = [] # making a list of arguments to pass to the function
while tokens[idx] != ("SYMBOL", ")"):
arguments.append(parse_expression())
idx += 1 # This is to skip the closing bracket
expression.append(
{
"type": "FUNCTION_CALL",
"name": function_name,
"arguments": arguments
}
)
elif token_type in javax_tokens.variables:
expression.append({"type": token_type, "value": token_value})
idx += 1
elif token_type == "IDENTIFIER":
if (idx + 1 < len(tokens)) and (tokens[idx + 1] == ("ASSIGN", "=")):
idx += 2 # Skip the assignment operator
expression.append(
{
"type": "VAR_DECL",
"name": token_value,
"value": parse_expression()
}
)
else:
expression.append({"type": token_type, "value": token_value})
idx += 1 # IMPORTANT TO BREAK THE LOOP
elif token_type == "OPERATOR":
idx += 1
expression = {
"type": "BIN_EXPR",
"operator": token_value,
"left": expression,
"right": parse_expression()
}
elif token_type == "IFSTATE":
idx += 2 # Skip the opening bracket
node_exp = {
"type": "IF_STATEMENT",
"condition": parse_expression(),
"child": []
}
idx += 2
while tokens[idx] != ("SYMBOL", "}"):
node_exp["child"].append(parse_expression())
if tokens[idx][1] == ";":
idx += 1
expression.append(node_exp)
idx += 1
elif token_type == "FUNCTION":
idx += 1
name = tokens[idx][1]
node_exp = {
"type": "FUNC_DECL",
"name": name,
"params": None,
"child": None,
"return": None
}
idx += 1
if tokens[idx] == ("SYMBOL", "("):
idx += 1
arguments = []
while tokens[idx] != ("SYMBOL", ")"):
arguments.append(tokens[idx][1])
idx += 1
node_exp["params"] = arguments
idx += 1
node_exp["child"] = []
while tokens[idx] != ("SYMBOL", "}"):
node_exp["child"].append(parse_expression())
if tokens[idx][1] == ";":
idx += 1
if tokens[idx][1] == "return":
idx += 1
return_value = parse_expression()
node_exp["return"] = return_value
break
expression.append(node_exp)
print(expression)
elif (token_type == "SYMBOL") and (token_value in (")", ";")):
break
elif (token_type == "SYMBOL") and (token_value in "("):
idx += 1
expression = parse_expression()
idx += 1
else:
idx += 1
return expression[0] if len(expression) == 1 else expression
while idx < len(tokens):
node = {"NODE": parse_expression()}
# Check if node is not empty and append to ast list
if node:
ast.append(node)
if idx < len(tokens) and tokens[idx] == ("SYMBOL", ";"):
idx += 1
return ast