-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_listener.py
147 lines (104 loc) · 4.57 KB
/
ast_listener.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
from generated.MyParser import MyParser
from generated.MyParserListener import MyParserListener
IND = "| "
class ASTListener(MyParserListener):
level = 0
def enterIf(self, ctx: MyParser.IfContext):
print(IND * self.level + "if")
self.level += 1
def exitIf(self, ctx: MyParser.IfContext):
self.level -= 1
def enterThen(self, ctx: MyParser.ThenContext):
print(IND * self.level + "then")
self.level += 1
def exitThen(self, ctx: MyParser.ThenContext):
self.level -= 1
def enterElse(self, ctx: MyParser.ElseContext):
print(IND * self.level + "else")
self.level += 1
def exitElse(self, ctx: MyParser.ElseContext):
self.level -= 1
def enterForLoop(self, ctx: MyParser.ForLoopContext):
print(IND * self.level + "for")
self.level += 1
def exitForLoop(self, ctx: MyParser.ForLoopContext):
self.level -= 1
def enterRange(self, ctx: MyParser.RangeContext):
print(IND * self.level + "range")
self.level += 1
def exitRange(self, ctx: MyParser.RangeContext):
self.level -= 1
def enterWhileLoop(self, ctx: MyParser.WhileLoopContext):
print(IND * self.level + "while")
self.level += 1
def exitWhileLoop(self, ctx: MyParser.WhileLoopContext):
self.level -= 1
def enterComparison(self, ctx: MyParser.ComparisonContext):
print(IND * self.level + ctx.getChild(1).getText())
self.level += 1
def exitComparison(self, ctx: MyParser.ComparisonContext):
self.level -= 1
def enterSimpleAssignment(self, ctx: MyParser.SimpleAssignmentContext):
print(IND * self.level + ctx.getChild(1).getText())
self.level += 1
def exitSimpleAssignment(self, ctx: MyParser.SimpleAssignmentContext):
self.level -= 1
def enterCompoundAssignment(self, ctx: MyParser.CompoundAssignmentContext):
print(IND * self.level + ctx.getChild(1).getText())
self.level += 1
def exitCompoundAssignment(self, ctx: MyParser.CompoundAssignmentContext):
self.level -= 1
def enterPrint(self, ctx: MyParser.PrintContext):
print(IND * self.level + "print")
self.level += 1
def exitPrint(self, ctx: MyParser.PrintContext):
self.level -= 1
def enterReturn(self, ctx: MyParser.ReturnContext):
print(IND * self.level + "return")
self.level += 1
def exitReturn(self, ctx: MyParser.ReturnContext):
self.level -= 1
def enterBinaryExpression(self, ctx: MyParser.BinaryExpressionContext):
operator = ctx.getChild(1)
print(IND * self.level + operator.getText())
self.level += 1
def exitBinaryExpression(self, ctx: MyParser.BinaryExpressionContext):
self.level -= 1
def enterTransposeExpression(self, ctx: MyParser.TransposeExpressionContext):
print(IND * self.level + "transpose")
self.level += 1
def exitTransposeExpression(self, ctx: MyParser.TransposeExpressionContext):
self.level -= 1
def enterMinusExpression(self, ctx: MyParser.MinusExpressionContext):
print(IND * self.level + "-")
self.level += 1
def exitMinusExpression(self, ctx: MyParser.MinusExpressionContext):
self.level -= 1
def enterSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext):
function_name = ctx.getChild(0)
print(IND * self.level + function_name.getText())
self.level += 1
def exitSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext):
self.level -= 1
def enterBreak(self, ctx: MyParser.BreakContext):
print(IND * self.level + "break")
def enterContinue(self, ctx: MyParser.ContinueContext):
print(IND * self.level + "continue")
def enterVector(self, ctx: MyParser.VectorContext):
print(IND * self.level + "vector")
self.level += 1
def exitVector(self, ctx: MyParser.VectorContext):
self.level -= 1
def enterElementReference(self, ctx: MyParser.ElementReferenceContext):
print(IND * self.level + "ref")
self.level += 1
def exitElementReference(self, ctx: MyParser.ElementReferenceContext):
self.level -= 1
def enterId(self, ctx: MyParser.IdContext):
print(IND * self.level + ctx.getText())
def enterInt(self, ctx: MyParser.IntContext):
print(IND * self.level + ctx.getText())
def enterFloat(self, ctx: MyParser.FloatContext):
print(IND * self.level + ctx.getText())
def enterString(self, ctx: MyParser.StringContext):
print(IND * self.level + ctx.getText())