6
6
from utils .memory import MemoryStack
7
7
from utils .values import Int , Float , String , Vector
8
8
9
+ class Break (Exception ):
10
+ pass
11
+
12
+ class Continue (Exception ):
13
+ pass
9
14
10
15
class Interpreter (MyParserVisitor ):
11
16
def __init__ (self ):
@@ -36,7 +41,12 @@ def visitForLoop(self, ctx: MyParser.ForLoopContext):
36
41
while a <= b :
37
42
self .memory_stack .put (variable , Int (a ))
38
43
a = a + 1 # to increment enumerateor and disregard changes inside the loop
39
- self .visit (ctx .statement ())
44
+ try :
45
+ self .visit (ctx .statement ())
46
+ except Continue :
47
+ continue
48
+ except Break :
49
+ break
40
50
41
51
def visitRange (self , ctx : MyParser .RangeContext ):
42
52
a = self .visit (ctx .expression (0 ))
@@ -47,7 +57,12 @@ def visitRange(self, ctx: MyParser.RangeContext):
47
57
48
58
def visitWhileLoop (self , ctx : MyParser .WhileLoopContext ):
49
59
while self .visit (ctx .comparison ()):
50
- self .visit (ctx .statement ())
60
+ try :
61
+ self .visit (ctx .statement ())
62
+ except Continue :
63
+ continue
64
+ except Break :
65
+ break
51
66
52
67
def visitComparison (self , ctx : MyParser .ComparisonContext ):
53
68
a = self .visit (ctx .expression (0 ))
@@ -153,10 +168,10 @@ def visitSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext)
153
168
return vector
154
169
155
170
def visitBreak (self , ctx : MyParser .BreakContext ):
156
- return self . visitChildren ( ctx ) # todo
171
+ raise Break ()
157
172
158
173
def visitContinue (self , ctx : MyParser .ContinueContext ):
159
- return self . visitChildren ( ctx ) # todo
174
+ raise Continue ()
160
175
161
176
def visitVector (self , ctx : MyParser .VectorContext ):
162
177
elements = [
0 commit comments