4
4
from generated .MyParser import MyParser
5
5
from generated .MyParserVisitor import MyParserVisitor
6
6
from utils .memory import MemoryStack
7
- from utils .values import Value , Int , Float , String , Vector
7
+ from utils .values import Int , Float , String , Vector
8
8
9
9
10
10
class Break (Exception ):
@@ -15,13 +15,6 @@ class Continue(Exception):
15
15
pass
16
16
17
17
18
- def not_same_type (a : Value , b : Value ):
19
- return type (a ) is not type (b ) or (
20
- isinstance (a , Vector )
21
- and (a .dims != b .dims or a .primitive_type != b .primitive_type )
22
- )
23
-
24
-
25
18
class Interpreter (MyParserVisitor ):
26
19
def __init__ (self ):
27
20
self .memory_stack = MemoryStack ()
@@ -60,8 +53,6 @@ def visitForLoop(self, ctx: MyParser.ForLoopContext):
60
53
def visitRange (self , ctx : MyParser .RangeContext ):
61
54
a = self .visit (ctx .expression (0 ))
62
55
b = self .visit (ctx .expression (1 ))
63
- if {type (a ), type (b )} != {Int }:
64
- raise TypeError
65
56
return (a .value , b .value )
66
57
67
58
def visitWhileLoop (self , ctx : MyParser .WhileLoopContext ):
@@ -96,8 +87,6 @@ def visitSimpleAssignment(self, ctx: MyParser.SimpleAssignmentContext):
96
87
else : # a[0] = 1
97
88
ref_value = self .visit (ctx .elementReference ())
98
89
new_value = self .visit (ctx .expression ())
99
- if not_same_type (ref_value , new_value ):
100
- raise TypeError
101
90
ref_value .value = new_value .value
102
91
103
92
def visitCompoundAssignment (self , ctx : MyParser .CompoundAssignmentContext ):
@@ -117,8 +106,6 @@ def visitCompoundAssignment(self, ctx: MyParser.CompoundAssignmentContext):
117
106
else : # a[0] += 1
118
107
ref_value = self .visit (ctx .elementReference ())
119
108
new_value = self .visit (ctx .expression ())
120
- if not_same_type (ref_value , new_value ):
121
- raise TypeError
122
109
match ctx .getChild (1 ).symbol .type :
123
110
case MyParser .ASSIGN_PLUS :
124
111
new_value = ref_value + new_value
@@ -137,8 +124,6 @@ def visitPrint(self, ctx: MyParser.PrintContext):
137
124
def visitReturn (self , ctx : MyParser .ReturnContext ):
138
125
if ctx .expression ():
139
126
return_value = self .visit (ctx .expression ())
140
- if not isinstance (return_value , Int ):
141
- raise TypeError
142
127
sys .exit (return_value .value )
143
128
sys .exit ()
144
129
@@ -167,10 +152,7 @@ def visitParenthesesExpression(self, ctx: MyParser.ParenthesesExpressionContext)
167
152
return self .visit (ctx .expression ())
168
153
169
154
def visitTransposeExpression (self , ctx : MyParser .TransposeExpressionContext ):
170
- vector = self .visit (ctx .expression ())
171
- if not isinstance (vector , Vector ):
172
- raise TypeError
173
- return vector .transpose ()
155
+ return self .visit (ctx .expression ()).transpose ()
174
156
175
157
def visitMinusExpression (self , ctx : MyParser .MinusExpressionContext ):
176
158
return - self .visit (ctx .expression ())
@@ -179,8 +161,6 @@ def visitSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext)
179
161
fname = ctx .getChild (0 ).symbol .type
180
162
if fname == MyParser .EYE :
181
163
dim = self .visit (ctx .expression (0 ))
182
- if not isinstance (dim , Int ):
183
- raise TypeError
184
164
rows = [
185
165
Vector ([Int (i == j ) for j in range (dim .value )])
186
166
for i in range (dim .value )
@@ -191,8 +171,6 @@ def visitSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext)
191
171
self .visit (ctx .expression (i ))
192
172
for i in range (ctx .getChildCount () // 2 - 1 )
193
173
]
194
- if {type (dim ) for dim in dims } != {Int }:
195
- raise TypeError
196
174
vector = {MyParser .ZEROS : Int (0 ), MyParser .ONES : Int (1 )}[fname ]
197
175
for dim in reversed (dims ):
198
176
vector = Vector ([deepcopy (vector ) for _ in range (dim .value )])
@@ -214,12 +192,8 @@ def visitElementReference(self, ctx: MyParser.ElementReferenceContext):
214
192
indices = [
215
193
self .visit (ctx .expression (i )) for i in range (ctx .getChildCount () // 2 - 1 )
216
194
]
217
- if {type (idx ) for idx in indices } != {Int }:
218
- raise TypeError
219
195
result = self .visit (ctx .id_ ())
220
196
for idx in indices :
221
- if not isinstance (result , Vector ):
222
- raise TypeError
223
197
result = result .value [idx .value ]
224
198
return result
225
199
0 commit comments