@@ -17,17 +17,17 @@ def visitSimpleStatement(self, ctx: MyParser.SimpleStatementContext):
17
17
return self .visitChildren (ctx ) # todo
18
18
19
19
def visitIfThenElse (self , ctx : MyParser .IfThenElseContext ):
20
- condition = self .visit (ctx .getChild ( 0 ))
20
+ condition = self .visit (ctx .if_ ( ))
21
21
if condition :
22
- return self .visit (ctx .getChild ( 1 ))
23
- elif ctx .getChildCount () == 3 :
24
- return self .visit (ctx .getChild ( 2 ))
22
+ return self .visit (ctx .then ( ))
23
+ elif ctx .else_ () is not None :
24
+ return self .visit (ctx .else_ ( ))
25
25
26
26
def visitIf (self , ctx : MyParser .IfContext ):
27
- return self .visit (ctx .getChild ( 2 ))
27
+ return self .visit (ctx .comparison ( ))
28
28
29
29
def visitElse (self , ctx : MyParser .ElseContext ):
30
- return self .visit (ctx .getChild ( 1 ))
30
+ return self .visit (ctx .statement ( ))
31
31
32
32
def visitForLoop (self , ctx : MyParser .ForLoopContext ):
33
33
return self .visitChildren (ctx ) # todo
@@ -39,8 +39,8 @@ def visitWhileLoop(self, ctx: MyParser.WhileLoopContext):
39
39
return self .visitChildren (ctx ) # todo
40
40
41
41
def visitComparison (self , ctx : MyParser .ComparisonContext ):
42
- a = self .visit (ctx .getChild (0 ))
43
- b = self .visit (ctx .getChild ( 2 ))
42
+ a = self .visit (ctx .expression (0 ))
43
+ b = self .visit (ctx .expression ( 1 ))
44
44
match ctx .getChild (1 ).symbol .type :
45
45
case MyParser .EQ :
46
46
return a == b
@@ -62,8 +62,8 @@ def visitCompoundAssignment(self, ctx: MyParser.CompoundAssignmentContext):
62
62
return self .visitChildren (ctx ) # todo
63
63
64
64
def visitPrint (self , ctx : MyParser .PrintContext ):
65
- for i in range (1 , ctx .getChildCount () - 1 , 2 ):
66
- print (str (self .visit (ctx .getChild (i ))))
65
+ for i in range (ctx .getChildCount () // 2 ):
66
+ print (str (self .visit (ctx .expression (i ))))
67
67
68
68
def visitReturn (self , ctx : MyParser .ReturnContext ):
69
69
if ctx .expression () is not None :
@@ -74,8 +74,8 @@ def visitReturn(self, ctx: MyParser.ReturnContext):
74
74
sys .exit ()
75
75
76
76
def visitBinaryExpression (self , ctx : MyParser .BinaryExpressionContext ):
77
- a = self .visit (ctx .getChild (0 ))
78
- b = self .visit (ctx .getChild ( 2 ))
77
+ a = self .visit (ctx .expression (0 ))
78
+ b = self .visit (ctx .expression ( 1 ))
79
79
match ctx .op .type :
80
80
case MyParser .PLUS :
81
81
return a + b
@@ -88,21 +88,21 @@ def visitBinaryExpression(self, ctx: MyParser.BinaryExpressionContext):
88
88
# todo: MAT_* operations
89
89
90
90
def visitParenthesesExpression (self , ctx : MyParser .ParenthesesExpressionContext ):
91
- return self .visit (ctx .getChild ( 1 ))
91
+ return self .visit (ctx .expression ( ))
92
92
93
93
def visitTransposeExpression (self , ctx : MyParser .TransposeExpressionContext ):
94
- vector = self .visit (ctx .getChild ( 0 ))
94
+ vector = self .visit (ctx .expression ( ))
95
95
if not isinstance (vector , Vector ):
96
96
raise TypeError
97
97
return vector .transpose ()
98
98
99
99
def visitMinusExpression (self , ctx : MyParser .MinusExpressionContext ):
100
- return - self .visit (ctx .getChild ( 1 ))
100
+ return - self .visit (ctx .expression ( ))
101
101
102
102
def visitSpecialMatrixFunction (self , ctx : MyParser .SpecialMatrixFunctionContext ):
103
103
fname = ctx .getChild (0 ).symbol .type
104
104
if fname == MyParser .EYE :
105
- dim = self .visit (ctx .getChild ( 2 ))
105
+ dim = self .visit (ctx .expression ( 0 ))
106
106
if not isinstance (dim , Int ):
107
107
raise TypeError
108
108
rows = [
@@ -112,7 +112,7 @@ def visitSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext)
112
112
return Vector (rows )
113
113
else :
114
114
dims = [
115
- self .visit (ctx .getChild (i )) for i in range (2 , ctx .getChildCount (), 2 )
115
+ self .visit (ctx .expression (i )) for i in range (ctx .getChildCount () // 2 - 1 )
116
116
]
117
117
if {type (dim ) for dim in dims } != {Int }:
118
118
raise TypeError
@@ -129,7 +129,7 @@ def visitContinue(self, ctx: MyParser.ContinueContext):
129
129
130
130
def visitVector (self , ctx : MyParser .VectorContext ):
131
131
elements = [
132
- self .visit (ctx .getChild (i )) for i in range (1 , ctx .getChildCount () - 1 , 2 )
132
+ self .visit (ctx .expression (i )) for i in range (ctx .getChildCount () // 2 )
133
133
]
134
134
return Vector (elements )
135
135
0 commit comments