-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_builder.py
78 lines (54 loc) · 1.81 KB
/
ast_builder.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
from dataclasses import dataclass
import decode as d
class Expression:
pass
@dataclass
class VariableExpression(Expression):
variable: int
@dataclass
class ValueExpression(Expression):
value: str | int | bool | VariableExpression
@dataclass
class UnaryExpression(Expression):
operator: d.UnaryOperator
operand: Expression
@dataclass
class BinaryExpression(Expression):
operator: d.BinaryOperator
left: Expression
right: Expression
@dataclass
class LambdaExpression(Expression):
variable: int
body: Expression
@dataclass
class IfExpression(Expression):
condition: Expression
then_branch: Expression
else_branch: Expression
def build_ast(tokens: list[d.Token]) -> Expression:
if not tokens:
raise ValueError("Empty token list")
token = tokens.pop(0)
if isinstance(token, d.ValueToken):
return ValueExpression(value=token.value)
if isinstance(token, d.VariableToken):
return VariableExpression(variable=token.variable)
if isinstance(token, d.UnaryOperatorToken):
operand = build_ast(tokens)
return UnaryExpression(operator=token.operator, operand=operand)
if isinstance(token, d.BinaryOperatorToken):
left = build_ast(tokens)
right = build_ast(tokens)
return BinaryExpression(operator=token.operator, left=left, right=right)
if isinstance(token, d.LambdaToken):
body = build_ast(tokens)
return LambdaExpression(variable=token.variable, body=body)
if isinstance(token, d.IfToken):
condition = build_ast(tokens)
then_branch = build_ast(tokens)
else_branch = build_ast(tokens)
return IfExpression(
condition=condition, then_branch=then_branch, else_branch=else_branch
)
raise ValueError(f"Unknown token {token}")