-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
516 lines (451 loc) · 19.4 KB
/
Copy pathparser.py
File metadata and controls
516 lines (451 loc) · 19.4 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
from typing import Any, Optional
from lexer import Token, TokenType, tokenize
from ast import (
ASTNode, ProgramNode, BlockNode, FunctionNode, ParameterNode,
IdentifierNode, NumberNode, StringNode, BooleanNode,
BinaryNode, UnaryNode, GroupingNode, AssignmentNode, CallNode,
IfNode, WhileNode, ForNode, ReturnNode, PrintNode, TypeNode,
ListNode, DictNode, IndexNode, TryNode, ImportNode,
BreakNode, ContinueNode, PassNode, NodeType, dump_ast
)
class Parser:
def __init__(self, tokens: list[Token]):
self.tokens = tokens
self.current = 0
def is_at_end(self) -> bool:
return self.peek().type == TokenType.EOF
def peek(self) -> Token:
return self.tokens[self.current]
def previous(self) -> Token:
return self.tokens[self.current - 1]
def advance(self) -> Token:
if not self.is_at_end():
self.current += 1
return self.previous()
def check(self, type: TokenType) -> bool:
if self.is_at_end():
return False
return self.peek().type == type
def check_next(self, type: TokenType) -> bool:
if self.current + 1 >= len(self.tokens):
return False
return self.tokens[self.current + 1].type == type
def match(self, *types: TokenType) -> bool:
for type in types:
if self.check(type):
self.advance()
return True
return False
def consume(self, type: TokenType, message: str = None) -> Token:
if self.check(type):
return self.advance()
raise SyntaxError(f"{message or 'Expected'} {type} at {self.peek().line}:{self.peek().column}")
def synchronize(self):
self.advance()
while not self.is_at_end():
if self.previous().type == TokenType.NEWLINE:
return
if self.check(TokenType.FN) or self.check(TokenType.IF) or self.check(TokenType.WHILE):
return
self.advance()
def parse(self) -> ProgramNode:
statements = []
while not self.is_at_end():
if self.check(TokenType.NEWLINE):
self.advance()
continue
statements.append(self.statement())
return ProgramNode(statements)
def statement(self) -> ASTNode:
if self.match(TokenType.FN):
return self.function()
if self.match(TokenType.IF):
return self.if_statement()
if self.match(TokenType.WHILE):
return self.while_statement()
if self.match(TokenType.FOR):
return self.for_statement()
if self.match(TokenType.RETURN):
return self.return_statement()
if self.match(TokenType.TRY):
return self.try_statement()
if self.match(TokenType.BREAK):
return BreakNode(self.previous().line, self.previous().column)
if self.match(TokenType.CONTINUE):
return ContinueNode(self.previous().line, self.previous().column)
if self.match(TokenType.PRINT, TokenType.TYPE):
return self._parse_builtin_call(self.previous())
if self.match(TokenType.IMPORT):
return self.import_statement()
return self.expression_statement()
def function(self) -> FunctionNode:
name_token = self.consume(TokenType.IDENTIFIER, "Expected function name")
name = name_token.value
self.consume(TokenType.LPAREN, "Expected '(' after function name")
parameters = self.parameters()
self.consume(TokenType.RPAREN, "Expected ')' after parameters")
self.consume(TokenType.LPAREN, "Expected '(' for function body")
body = self.block()
self.consume(TokenType.RPAREN, "Expected ')' to close function body")
return FunctionNode(name, parameters, body, name_token.line, name_token.column)
def parameters(self) -> list[ParameterNode]:
params = []
if not self.check(TokenType.RPAREN):
while True:
param_token = self.consume(TokenType.IDENTIFIER, "Expected parameter name")
default_value = None
if self.match(TokenType.EQUAL):
default_value = self.expression()
params.append(ParameterNode(param_token.value, default_value))
if not self.match(TokenType.COMMA):
break
return params
def block(self) -> BlockNode:
statements = []
while self.match(TokenType.NEWLINE):
pass
while not self.check(TokenType.RPAREN) and not self.is_at_end():
if self.check(TokenType.NEWLINE):
self.advance()
continue
statements.append(self.statement())
return BlockNode(statements)
def if_statement(self) -> IfNode:
self.consume(TokenType.LPAREN, "Expected '(' after if")
condition = self.expression()
self.consume(TokenType.RPAREN, "Expected ')' after condition")
self.consume(TokenType.LPAREN, "Expected '(' for if body")
while self.match(TokenType.NEWLINE):
pass
then_branch = self.block()
self.consume(TokenType.RPAREN, "Expected ')' to close if body")
elif_branches = []
while self.match(TokenType.ELIF):
self.consume(TokenType.LPAREN, "Expected '(' after elif")
elif_condition = self.expression()
self.consume(TokenType.RPAREN, "Expected ')' after elif condition")
self.consume(TokenType.LPAREN, "Expected '(' for elif body")
while self.match(TokenType.NEWLINE):
pass
elif_body = self.block()
self.consume(TokenType.RPAREN, "Expected ')' to close elif body")
elif_branches.append((elif_condition, elif_body))
else_branch = None
if self.match(TokenType.ELSE):
self.consume(TokenType.LPAREN, "Expected '(' for else body")
while self.match(TokenType.NEWLINE):
pass
else_branch = self.block()
self.consume(TokenType.RPAREN, "Expected ')' to close else body")
return IfNode(condition, then_branch, elif_branches, else_branch)
def while_statement(self) -> WhileNode:
self.consume(TokenType.LPAREN, "Expected '(' after while")
condition = self.expression()
self.consume(TokenType.RPAREN, "Expected ')' after condition")
self.consume(TokenType.LPAREN, "Expected '(' for while body")
while self.match(TokenType.NEWLINE):
pass
body = self.block()
self.consume(TokenType.RPAREN, "Expected ')' to close while body")
return WhileNode(condition, body)
def for_statement(self) -> ForNode:
variable = self.consume(TokenType.IDENTIFIER, "Expected loop variable").value
self.consume(TokenType.IN, "Expected 'in'")
if self.check(TokenType.LBRACKET):
self.advance()
elements = []
while self.match(TokenType.NEWLINE):
pass
if not self.check(TokenType.RBRACKET):
while True:
elements.append(self.expression())
while self.match(TokenType.NEWLINE):
pass
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RBRACKET, "Expected ']'")
iterable = ListNode(elements)
else:
iterable = self.expression()
while self.match(TokenType.NEWLINE):
pass
if self.check(TokenType.LPAREN):
self.consume(TokenType.LPAREN)
body = self.block()
self.consume(TokenType.RPAREN, "Expected ')' to close for body")
else:
stmt = self.statement()
body = BlockNode([stmt])
return ForNode(variable, iterable, body)
def return_statement(self) -> ReturnNode:
value = None
if not self.check(TokenType.NEWLINE) and not self.check(TokenType.RPAREN):
value = self.expression()
return ReturnNode(value)
def try_statement(self) -> TryNode:
self.consume(TokenType.LPAREN, "Expected '(' after try")
while self.match(TokenType.NEWLINE):
pass
try_body = self.block()
self.consume(TokenType.RPAREN, "Expected ')' to close try body")
if not self.match(TokenType.CATCH):
raise SyntaxError(f"Expected 'catch' at {self.peek().line}:{self.peek().column}")
variable = "error"
if self.check(TokenType.IDENTIFIER):
variable = self.advance().value
self.consume(TokenType.LPAREN, "Expected '(' after catch")
# Skip newlines after catch LPAREN
while self.match(TokenType.NEWLINE):
pass
catch_body = self.block()
self.consume(TokenType.RPAREN, "Expected ')' to close catch body")
return TryNode(try_body, catch_body, variable)
def import_statement(self) -> ImportNode:
# Check if it's import("path") syntax
if self.check(TokenType.LPAREN):
self.consume(TokenType.LPAREN, "Expected '('")
module = self.consume(TokenType.STRING, "Expected module path").value
self.consume(TokenType.RPAREN, "Expected ')'")
return ImportNode(module, None)
# import x from y syntax
names = []
while True:
names.append(self.consume(TokenType.IDENTIFIER, "Expected name").value)
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.FROM, "Expected 'from'")
module = self.consume(TokenType.STRING, "Expected module name").value
return ImportNode(module, names)
def print_statement(self) -> PrintNode:
self.consume(TokenType.LPAREN, "Expected '(' after print")
args = []
if not self.check(TokenType.RPAREN):
while True:
args.append(self.expression())
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RPAREN, "Expected ')' after print args")
return PrintNode(args)
def type_statement(self) -> TypeNode:
self.consume(TokenType.LPAREN, "Expected '(' after type")
arg = self.expression()
self.consume(TokenType.RPAREN, "Expected ')' after type arg")
return TypeNode(arg)
def expression_statement(self) -> ASTNode:
expr = self.expression()
if self.match(TokenType.EQUAL):
if isinstance(expr, IdentifierNode):
value = self.expression()
return AssignmentNode(expr, value)
return expr
def expression(self) -> ASTNode:
return self.or_expression()
def or_expression(self) -> ASTNode:
left = self.and_expression()
while self.match(TokenType.OR):
right = self.and_expression()
left = BinaryNode("or", left, right)
return left
def and_expression(self) -> ASTNode:
left = self.equality()
while self.match(TokenType.AND):
right = self.equality()
left = BinaryNode("and", left, right)
return left
def equality(self) -> ASTNode:
left = self.comparison()
while self.match(TokenType.BANG_EQUAL, TokenType.EQUAL_EQUAL):
operator = self.previous().value
right = self.comparison()
left = BinaryNode(operator, left, right)
return left
def comparison(self) -> ASTNode:
left = self.term()
while self.match(TokenType.GREATER, TokenType.GREATER_EQUAL, TokenType.LESS, TokenType.LESS_EQUAL):
operator = self.previous().value
right = self.term()
left = BinaryNode(operator, left, right)
return left
def term(self) -> ASTNode:
left = self.factor()
while self.match(TokenType.MINUS, TokenType.PLUS):
operator = self.previous().value
right = self.factor()
left = BinaryNode(operator, left, right)
return left
def factor(self) -> ASTNode:
left = self.exponent()
while self.match(TokenType.SLASH, TokenType.STAR, TokenType.PERCENT):
operator = self.previous().value
right = self.exponent()
left = BinaryNode(operator, left, right)
return left
def exponent(self) -> ASTNode:
left = self.call()
while self.match(TokenType.STAR_STAR):
right = self.unary()
left = BinaryNode("**", left, right)
return left
def unary(self) -> ASTNode:
if self.match(TokenType.BANG, TokenType.MINUS, TokenType.NOT):
operator = self.previous().value
operand = self.unary()
return UnaryNode(operator, operand)
return self.exponent()
def call(self) -> ASTNode:
expr = self.primary()
while True:
if self.match(TokenType.DOT):
name_token = self.consume(TokenType.IDENTIFIER, "Expected method name")
if self.match(TokenType.LPAREN):
args = []
if not self.check(TokenType.RPAREN):
while True:
args.append(self.expression())
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RPAREN, "Expected ')' after arguments")
if isinstance(expr, IdentifierNode):
method_name = f"{expr.name}.{name_token.value}"
elif hasattr(expr, 'name'):
method_name = f"{expr.name}.{name_token.value}"
else:
method_name = f"{str(expr)}.{name_token.value}"
expr = CallNode(method_name, args)
else:
expr = CallNode(f"{expr.name}.{name_token.value}" if hasattr(expr, 'name') else f"{str(expr)}.{name_token.value}", [])
elif self.match(TokenType.LPAREN):
args = []
if not self.check(TokenType.RPAREN):
while True:
args.append(self.expression())
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RPAREN, "Expected ')' after arguments")
if isinstance(expr, IdentifierNode):
expr = CallNode(expr.name, args)
else:
expr = CallNode(str(expr), args)
elif self.match(TokenType.LBRACKET):
index = self.expression()
self.consume(TokenType.RBRACKET, "Expected ']' after index")
expr = IndexNode(expr, index)
else:
break
return expr
def primary(self) -> ASTNode:
while self.match(TokenType.NEWLINE):
pass
token = self.peek()
if self.match(TokenType.NUMBER):
return NumberNode(token.value)
if self.match(TokenType.STRING):
return StringNode(token.value)
if self.match(TokenType.TRUE):
return BooleanNode(True)
if self.match(TokenType.FALSE):
return BooleanNode(False)
if self.match(TokenType.IDENTIFIER):
return IdentifierNode(token.value)
if self.match(TokenType.PRINT, TokenType.TYPE):
return self._parse_builtin_call(token)
if self.match(TokenType.IMPORT):
path_token = self.consume(TokenType.STRING, "Expected module path")
return ImportNode(path_token.value)
if self.match(TokenType.LPAREN):
expr = self.expression()
self.consume(TokenType.RPAREN, "Expected ')'")
return GroupingNode(expr)
if self.match(TokenType.LBRACKET):
elements = []
while self.match(TokenType.NEWLINE):
pass
if not self.check(TokenType.RBRACKET):
while True:
elements.append(self.expression())
while self.match(TokenType.NEWLINE):
pass
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RBRACKET, "Expected ']'")
return ListNode(elements)
if self.match(TokenType.LBRACE):
pairs = {}
while self.match(TokenType.NEWLINE):
pass
if not self.check(TokenType.RBRACE):
while True:
key = self.expression()
self.consume(TokenType.COLON, "Expected ':'")
value = self.expression()
pairs[key] = value
while self.match(TokenType.NEWLINE):
pass
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RBRACE, "Expected '}'")
return DictNode(pairs)
raise SyntaxError(f"Unexpected token: {token.type.name} at {token.line}:{token.column}")
raise SyntaxError(f"Unexpected token: {token.type.name} at {token.line}:{token.column}")
def _parse_builtin_call(self, token: Token) -> ASTNode:
self.consume(TokenType.LPAREN, "Expected '('")
args = []
if not self.check(TokenType.RPAREN):
while True:
args.append(self.expression())
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RPAREN, "Expected ')'")
if token.type == TokenType.PRINT:
return PrintNode(args)
elif token.type == TokenType.TYPE:
return TypeNode(args[0] if args else None)
return CallNode(token.value, args)
if self.match(TokenType.LBRACKET):
elements = []
if not self.check(TokenType.RBRACKET):
while True:
elements.append(self.expression())
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RBRACKET, "Expected ']'")
return ListNode(elements)
if self.match(TokenType.LBRACE):
pairs = {}
if not self.check(TokenType.RBRACE):
while True:
key = self.expression()
self.consume(TokenType.COLON, "Expected ':'")
value = self.expression()
pairs[key] = value
if not self.match(TokenType.COMMA):
break
self.consume(TokenType.RBRACE, "Expected '}'")
return DictNode(pairs)
raise SyntaxError(f"Unexpected token: {token.type.name} at {token.line}:{token.column}")
def parse(source: str) -> ProgramNode:
tokens = tokenize(source)
parser = Parser(tokens)
return parser.parse()
if __name__ == "__main__":
test_code = '''
fn add(a, b) (
return a + b
)
fn factorial(n) (
if (n <= 1) (
return 1
) else (
return n * factorial(n - 1)
)
)
result = add(5, 10)
print(result)
print("Hello World")
x = [1, 2, 3]
print(x[0])
person = {"name": "Alice"}
print(person["name"])
'''
program = parse(test_code)
print(dump_ast(program))