-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast.py
132 lines (98 loc) · 3.17 KB
/
ast.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
class Node:
pass
class Program(Node):
def __init__(self, statements):
self.statements = statements
def __repr__(self):
return f'Program(statements={self.statements})'
class Print(Node):
def __init__(self, expression):
self.expression = expression
def __repr__(self):
return f'Print(expression={self.expression})'
class Assign(Node):
def __init__(self, name, value):
self.name = name
self.value = value
def __repr__(self):
return f'Assign(name={self.name}, value={self.value})'
class BinOp(Node):
def __init__(self, left, op, right):
self.left = left
self.op = op
self.right = right
def __repr__(self):
return f'BinOp(left={self.left}, op={self.op}, right={self.right})'
class Number(Node):
def __init__(self, value):
self.value = value
def __repr__(self):
return f'Number(value={self.value})'
class Float(Node):
def __init__(self, value):
self.value = value
def __repr__(self):
return f'Float(value={self.value})'
class Identifier(Node):
def __init__(self, name):
self.name = name
def __repr__(self):
return f'Identifier(name={self.name})'
class String(Node):
def __init__(self, value):
self.value = value
def __repr__(self):
return f'String(value={self.value})'
class If(Node):
def __init__(self, condition, true_branch, false_branch=None):
self.condition = condition
self.true_branch = true_branch
self.false_branch = false_branch
def __repr__(self):
return f'If(condition={self.condition}, true_branch={self.true_branch}, false_branch={self.false_branch})'
class Comparison(Node):
def __init__(self, left, op, right):
self.left = left
self.op = op
self.right = right
def __repr__(self):
return f'Comparison(left={self.left}, op={self.op}, right={self.right})'
class While(Node):
def __init__(self, condition, body):
self.condition = condition
self.body = body
def __repr__(self):
return f'While(condition={self.condition}, body={self.body})'
class Block(Node):
def __init__(self, statements):
self.statements = statements
def __repr__(self):
return f'Block(statements={self.statements})'
class Break(Node):
def __repr__(self):
return 'Break()'
class Continue(Node):
def __repr__(self):
return 'Continue()'
class Array(Node):
def __init__(self, elements):
self.elements = elements
def __repr__(self):
return f'Array(elements={self.elements})'
class ArrayAccess(Node):
def __init__(self, array, index):
self.array = array
self.index = index
def __repr__(self):
return f'ArrayAccess(array={self.array}, index={self.index})'
class FunctionDef(Node):
def __init__(self, name, body):
self.name = name
self.body = body
def __repr__(self):
return f'FunctionDef(name={self.name}, body={self.body})'
class FunctionCall(Node):
def __init__(self, name):
self.name = name
def __repr__(self):
return f'FunctionCall(name={self.name})'