@@ -37,7 +37,7 @@ def get_resulting_type(
37
37
38
38
39
39
class SemanticListener (MyParserListener ):
40
- """Checks break and continue statements, variable declarations,types and assignments."""
40
+ """Checks break and continue statements, variable declarations, types, assignments, etc ."""
41
41
42
42
def __init__ (self ):
43
43
self .nested_loop_counter = 0
@@ -46,43 +46,27 @@ def __init__(self):
46
46
ParserRuleContext , Type | tuple | None
47
47
] = {} # values should be either Type or (Type, int | None, int | None, ...)
48
48
49
- # LOOP CHECKING
50
-
51
49
def enterForLoop (self , ctx : MyParser .ForLoopContext ):
52
50
self .nested_loop_counter += 1
51
+ self .variables [ctx .getChild (1 ).getText ()] = Type .INT
53
52
54
53
def exitForLoop (self , ctx : MyParser .ForLoopContext ):
55
54
self .nested_loop_counter -= 1
56
55
56
+ def exitRange (self , ctx : MyParser .RangeContext ):
57
+ children_types = {self .expr_type [ctx .getChild (i )] for i in [0 , 2 ]}
58
+ if children_types != {Type .INT }:
59
+ ctx .parser .notifyErrorListeners (
60
+ "Range bounds must be integers" , ctx .getChild (1 ).getSymbol ()
61
+ )
62
+ self .expr_type [ctx ] = None
63
+
57
64
def enterWhileLoop (self , ctx : MyParser .WhileLoopContext ):
58
65
self .nested_loop_counter += 1
59
66
60
67
def exitWhileLoop (self , ctx : MyParser .WhileLoopContext ):
61
68
self .nested_loop_counter -= 1
62
69
63
- def enterBreak (self , ctx : MyParser .BreakContext ):
64
- if self .nested_loop_counter == 0 :
65
- ctx .parser .notifyErrorListeners (
66
- "Break statement outside of loop" , ctx .BREAK ().getSymbol ()
67
- )
68
-
69
- def enterContinue (self , ctx : MyParser .ContinueContext ):
70
- if self .nested_loop_counter == 0 :
71
- ctx .parser .notifyErrorListeners (
72
- "Continue statement outside of loop" , ctx .CONTINUE ().getSymbol ()
73
- )
74
-
75
- # VARIABLES & TYPES CHECKING
76
-
77
- def enterRange (self , ctx : MyParser .RangeContext ):
78
- pass
79
-
80
- def exitRange (self , ctx : MyParser .RangeContext ):
81
- pass
82
-
83
- def enterComparison (self , ctx : MyParser .ComparisonContext ):
84
- pass
85
-
86
70
def exitComparison (self , ctx : MyParser .ComparisonContext ):
87
71
children_types = {self .expr_type [ctx .getChild (i )] for i in [0 , 2 ]}
88
72
if not (
@@ -194,6 +178,18 @@ def exitSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext):
194
178
else :
195
179
self .expr_type [ctx ] = (Type .INT , * type_dimentions )
196
180
181
+ def enterBreak (self , ctx : MyParser .BreakContext ):
182
+ if self .nested_loop_counter == 0 :
183
+ ctx .parser .notifyErrorListeners (
184
+ "Break statement outside of loop" , ctx .BREAK ().getSymbol ()
185
+ )
186
+
187
+ def enterContinue (self , ctx : MyParser .ContinueContext ):
188
+ if self .nested_loop_counter == 0 :
189
+ ctx .parser .notifyErrorListeners (
190
+ "Continue statement outside of loop" , ctx .CONTINUE ().getSymbol ()
191
+ )
192
+
197
193
def exitVector (self , ctx : MyParser .VectorContext ):
198
194
elements = ctx .children [1 ::2 ]
199
195
for i in range (1 , len (elements )):
0 commit comments