-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.py
295 lines (266 loc) · 15.9 KB
/
parse.py
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
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.current_token_index = 0
def current_token(self):
if self.current_token_index < len(self.tokens):
return self.tokens[self.current_token_index]
return None
def next_token(self):
self.current_token_index += 1
return self.current_token()
def peek_token(self):
if self.current_token_index + 1 < len(self.tokens):
return self.tokens[self.current_token_index + 1]
return None
def peek_next_token(self):
if self.current_token_index + 2 < len(self.tokens):
return self.tokens[self.current_token_index + 2]
return None
def raise_error(self, message):
token = self.current_token()
if token:
error_msg = f"Error at line {token['line']}, column {token['column']}: {message}"
else:
error_msg = "Error: " + message
raise SyntaxError(error_msg)
def parse(self):
results = []
while self.current_token() is not None:
if self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] in ['entero', 'decimal', 'booleano', 'cadena']:
# Peek to see if next token is an identifier followed by `(`, indicates a function declaration
if self.peek_token() and self.peek_token()['type'] == 'IDENTIFIER' and self.peek_next_token() and self.peek_next_token()['value'] == '(':
results.append(self.parse_function_declaration())
else:
results.append(self.parse_variable_declaration())
elif self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] == 'si':
results.append(self.parse_if_statement())
elif self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] == 'mientras':
results.append(self.parse_while_loop())
elif self.current_token()['type'] == 'IDENTIFIER':
results.append(self.parse_expression())
else:
self.raise_error(f"Unexpected token {self.current_token()['value']}")
return results
def parse_expression(self):
if self.current_token()['type'] != 'IDENTIFIER':
self.raise_error("Expected identifier at start of expression.")
left = self.current_token()['value']
self.next_token()
if self.current_token()['type'] == 'OPERATOR' and self.current_token()['value'] == '=':
self.next_token()
expression = self.parse_complex_expression()
else:
self.raise_error("Expected '=' after identifier.")
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == ';':
self.next_token()
else:
self.raise_error("Expected ';' at end of expression.")
return {'type': 'assignment', 'identifier': left, 'expression': expression}
def parse_complex_expression(self):
elements = []
if self.current_token()['type'] in ['NUMBER', 'IDENTIFIER']:
elements.append(self.current_token()['value'])
self.next_token()
else:
self.raise_error("Expected a number or identifier after '='.")
while self.current_token()['value'] in ['+', '-', '*', '/', '%']:
operator = self.current_token()['value']
self.next_token()
if self.current_token()['type'] in ['NUMBER', 'IDENTIFIER']:
elements.append(operator)
elements.append(self.current_token()['value'])
self.next_token()
else:
self.raise_error(f"Expected a number or identifier after operator '{operator}'.")
return elements
def parse_variable_declaration(self):
node = {'type': 'variable_declaration', 'data': {}}
if self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] in ['entero', 'decimal', 'booleano', 'cadena']:
node['data']['type'] = self.current_token()['value']
self.next_token() # Move to IDENTIFIER
if self.current_token()['type'] == 'IDENTIFIER':
node['data']['identifier'] = self.current_token()['value']
self.next_token() # Move to OPERATOR(=)
if self.current_token()['type'] == 'OPERATOR' and self.current_token()['value'] == '=':
self.next_token() # Move to VALUE
if (self.current_token()['type'] in ['STRING', 'NUMBER', 'IDENTIFIER']) or (self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] in ['verdadero', 'falso']):
node['data']['value'] = self.current_token()['value']
self.next_token() # Move to expected semicolon
print(self.current_token())
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == ';':
self.next_token() # Prepare for the next statement
return node
else:
self.raise_error("Missing semicolon in variable declaration.")
else:
self.raise_error("Invalid or missing value in variable declaration.")
else:
self.raise_error("Missing '=' in variable declaration.")
else:
self.raise_error("Invalid or missing identifier in variable declaration.")
else:
self.raise_error("Unexpected keyword; expected 'entero', 'decimal', 'booleano', or 'cadena'")
return node
def parse_condition(self):
node = {'type': 'condition', 'data': {}}
if self.current_token()['type'] == 'IDENTIFIER':
node['data']['identifier'] = self.current_token()['value']
self.next_token()
if self.current_token()['type'] == 'OPERATOR' and self.current_token()['value'] in ['==', '<=', '>=', '<', '>']:
node['data']['operation'] = self.current_token()['value']
self.next_token()
if (self.current_token()['type'] in ['NUMBER', 'IDENTIFIER']) or (self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] in ['verdadero', 'falso']):
self.next_token()
node['data']['comparison'] = self.current_token()['value']
return node
else:
self.raise_error("Missing comparison on the condition.")
else:
self.raise_error("Missing operator on the condition.")
else:
self.raise_error("Missing identifier on the condition.")
return node
def parse_statements(self, block_node):
"""Parse statements until a closing brace '}' or end of tokens."""
while self.current_token() and (self.current_token()['type'] != 'SIGN' or self.current_token()['value'] != '}'):
if self.current_token()['type'] == 'IDENTIFIER':
block_node['statements'].append(self.parse_expression())
elif self.current_token()['type'] == 'KEYWORD':
if self.current_token()['value'] in ['si', 'mientras']:
if self.current_token()['value'] == 'si':
block_node['statements'].append(self.parse_if_statement())
else:
block_node['statements'].append(self.parse_while_loop())
elif self.current_token()['value'] in ['entero', 'decimal', 'booleano', 'cadena']:
block_node['statements'].append(self.parse_variable_declaration())
else:
self.raise_error(f"Unexpected keyword {self.current_token()['value']} in statement block.")
else:
self.next_token() # Skip unknown tokens or handle errors
# After processing all statements, ensure the loop exited because of '}' and not because of a missing brace.
if not self.current_token() or self.current_token()['type'] != 'SIGN' or self.current_token()['value'] != '}':
self.raise_error("Expected '}' at the end of the block.")
def parse_if_statement(self):
node = {'type': 'if_statement', 'if_block': {'statements': []}, 'else_block': {'statements': []}}
if self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] == 'si':
self.next_token() # Move past 'si'
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == '(':
self.next_token()
condition = self.parse_condition()
node['if_block']['condition'] = condition
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == ')':
self.next_token()
if self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] == 'entonces':
self.next_token()
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == '{':
self.next_token()
self.parse_statements(node['if_block'])
if self.current_token() and self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == '}':
self.next_token()
# Check for 'sino'
if self.current_token() and self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] == 'sino':
self.next_token()
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == '{':
self.next_token()
self.parse_statements(node['else_block'])
if self.current_token() and self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == '}':
self.next_token()
else:
self.raise_error("Expected '}' at the end of else block.")
else:
self.raise_error("Expected '{' after 'sino'.")
return node
else:
self.raise_error("Expected '}' at the end of if block.")
else:
self.raise_error("Expected '{' after 'entonces'.")
else:
self.raise_error("Expected 'entonces' after condition.")
else:
self.raise_error("Expected ')' after condition.")
else:
self.raise_error("Expected '(' after 'si'.")
return node
def parse_while_loop(self):
node = {'type': 'while_loop', 'statements': [], 'data': {}}
if self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] == 'mientras':
self.next_token()
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == '(':
self.next_token()
node['data']['condition'] = self.parse_condition()
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == ')':
self.next_token()
if self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] == 'hacer':
self.next_token()
if self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == '{':
self.next_token()
self.parse_statements(node)
if self.current_token() and self.current_token()['type'] == 'SIGN' and self.current_token()['value'] == '}':
self.next_token()
return node
else:
self.raise_error("Expected '}' at the end of statements block.")
else:
self.raise_error("Expected '{' after 'hacer'.")
else:
self.raise_error("Expected 'hacer' after condition.")
else:
self.raise_error("Expected ')' after condition.")
else:
self.raise_error("Expected '(' after 'mientras'.")
return node
def parse_function_declaration(self):
node = {'type': 'function_declaration', 'data': {}}
if self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] in ['entero', 'decimal', 'booleano', 'cadena']:
node['data']['return_type'] = self.current_token()['value']
self.next_token() # Move to function name
if self.current_token()['type'] == 'IDENTIFIER':
node['data']['function_name'] = self.current_token()['value']
self.next_token() # Move to `(`
if self.current_token()['value'] == '(':
self.next_token() # Skip `(` and check for parameters or `)`
node['data']['parameters'] = self.parse_parameters()
if self.current_token()['value'] == ')':
self.next_token() # Skip `)` and move to `{`
if self.current_token()['value'] == '{':
self.next_token() # Enter function body
node['statements'] = []
while self.current_token() and self.current_token()['value'] != '}':
self.parse_statements(node)
if self.current_token() and self.current_token()['value'] == '}':
self.next_token() # Close function body
return node
else:
self.raise_error("Expected '}' at the end of function declaration.")
else:
self.raise_error("Expected '{' after function parameters.")
else:
self.raise_error("Expected ')' after function parameters.")
else:
self.raise_error("Expected '(' after function name.")
else:
self.raise_error("Expected function name identifier after return type.")
return node
def parse_parameters(self):
parameters = []
if self.current_token()['value'] != ')': # Check if there are any parameters
while True:
if self.current_token()['type'] == 'KEYWORD' and self.current_token()['value'] in ['entero', 'decimal', 'booleano', 'cadena']:
param_type = self.current_token()['value']
self.next_token()
if self.current_token()['type'] == 'IDENTIFIER':
param_name = self.current_token()['value']
parameters.append({'type': param_type, 'name': param_name})
self.next_token()
if self.current_token()['value'] == ',':
self.next_token() # Move past the comma for the next parameter
elif self.current_token()['value'] == ')':
break # End of parameters list
else:
self.raise_error("Expected ',' or ')' in parameter list.")
else:
self.raise_error("Expected an identifier for parameter name.")
else:
self.raise_error("Expected a type keyword in parameters.")
return parameters