forked from pgrabiec/Interpreter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAST.py
More file actions
177 lines (129 loc) · 4.48 KB
/
AST.py
File metadata and controls
177 lines (129 loc) · 4.48 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
class Node(object):
#def __str__(self):
# return self.printTree(0)
def __init__(self, line):
self.lineno = line
def accept(self, visitor):
return visitor.visit(self)
class Program(Node):
def __init__(self, line, segments):
super().__init__(line)
self.segments = segments
class Declaration(Node):
def __init__(self, line, variable_type, inits):
super().__init__(line)
self.variable_type = variable_type
self.inits = inits
class Init(Node):
def __init__(self, line, identifier, expression):
""":arg identifier : AST.Identifier
:arg expression : AST.Expression"""
super().__init__(line)
self.identifier = identifier
self.expression = expression
class PrintInstruction(Node):
def __init__(self, line, args):
""":arg args : AST.ExpressionList"""
super().__init__(line)
self.args = args
class LabeledInstruction(Node):
def __init__(self, line, identifier, instruction):
""":arg identifier : string
:arg instruction : AST.Instruction"""
super().__init__(line)
self.identifier = identifier
self.instruction = instruction
class Assignment(Node):
def __init__(self, line, identifier, expression):
""":arg identifier : AST.Identifier
:arg expression : AST.Expression"""
super().__init__(line)
self.identifier = identifier
self.expression = expression
class ChoiceInstruction(Node):
def __init__(self, line, condition, instruction_true, instruction_false):
super().__init__(line)
self.condition = condition
self.instruction_true = instruction_true
self.instruction_false = instruction_false
class WhileInstruction(Node):
def __init__(self, line, condition, instruction):
super().__init__(line)
self.condition = condition
self.instruction = instruction
class RepeatInstruction(Node):
def __init__(self, line, instruction, condition):
super().__init__(line)
self.instruction = instruction
self.condition = condition
class ReturnInstruction(Node):
def __init__(self, line, expression):
super().__init__(line)
self.expression = expression
class BreakInstruction(Node):
pass
class ContinueInstruction(Node):
pass
class CompoundInstructions(Node):
def __init__(self, line, instructions, end_lineno):
super().__init__(line)
self.instructions = instructions
self.end_lineno = end_lineno
class Const(Node):
def __init__(self, line, value):
super().__init__(line)
self.value = value
# Terminal
class Integer(Const):
pass
# Terminal
class Float(Const):
pass
# Terminal
class String(Const):
pass
class Identifier(Node):
"""Matches ID terminals when they are associated with a identifier
Also, matches the production: expression -> ID"""
def __init__(self, line, identifier):
""":arg identifier : string"""
super().__init__(line)
self.identifier = identifier
class FunctionCallExpression(Node):
def __init__(self, line, identifier, arguments):
""":arg identifier : string
:arg arguments : AST.ExpressionList"""
super().__init__(line)
self.identifier = identifier
self.arguments = arguments
class BinExpr(Node):
def __init__(self, line, left, op, right):
""":arg left : AST.Expression
:arg op : string
:arg right : AST.Expression"""
super().__init__(line)
self.left = left
self.op = op
self.right = right
class FunctionDefinition(Node):
def __init__(self, line, return_type, identifier, arguments, instructions, end_lineno):
""":arg return_type : string
:arg identifier : string"""
super().__init__(line)
self.type = return_type
self.identifier = identifier
self.arguments = arguments
self.instructions = instructions
self.end_lineno = end_lineno
class Argument(Node):
def __init__(self, line, argument_type, argument_identifier):
""":arg argument_type : string
:arg argument_identifier : string"""
super().__init__(line)
self.argument_type = argument_type
self.argument_identifier = argument_identifier
class Variable(Node):
def __init__(self, line, identifier, type):
super().__init__(line)
self.identifier = identifier
self.type = type