Skip to content

Commit dd20bfe

Browse files
committed
Minor changes
1 parent 1fc3315 commit dd20bfe

File tree

1 file changed

+19
-23
lines changed

1 file changed

+19
-23
lines changed

semantic_listener.py

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ def get_resulting_type(
3737

3838

3939
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."""
4141

4242
def __init__(self):
4343
self.nested_loop_counter = 0
@@ -46,35 +46,13 @@ def __init__(self):
4646
ParserRuleContext, Type | tuple | None
4747
] = {} # values should be either Type or (Type, int | None, int | None, ...)
4848

49-
# LOOP CHECKING
50-
5149
def enterForLoop(self, ctx: MyParser.ForLoopContext):
5250
self.nested_loop_counter += 1
5351
self.variables[ctx.getChild(1).getText()] = Type.INT
5452

5553
def exitForLoop(self, ctx: MyParser.ForLoopContext):
5654
self.nested_loop_counter -= 1
5755

58-
def enterWhileLoop(self, ctx: MyParser.WhileLoopContext):
59-
self.nested_loop_counter += 1
60-
61-
def exitWhileLoop(self, ctx: MyParser.WhileLoopContext):
62-
self.nested_loop_counter -= 1
63-
64-
def enterBreak(self, ctx: MyParser.BreakContext):
65-
if self.nested_loop_counter == 0:
66-
ctx.parser.notifyErrorListeners(
67-
"Break statement outside of loop", ctx.BREAK().getSymbol()
68-
)
69-
70-
def enterContinue(self, ctx: MyParser.ContinueContext):
71-
if self.nested_loop_counter == 0:
72-
ctx.parser.notifyErrorListeners(
73-
"Continue statement outside of loop", ctx.CONTINUE().getSymbol()
74-
)
75-
76-
# VARIABLES & TYPES CHECKING
77-
7856
def exitRange(self, ctx: MyParser.RangeContext):
7957
children_types = {self.expr_type[ctx.getChild(i)] for i in [0, 2]}
8058
if children_types != {Type.INT}:
@@ -83,6 +61,12 @@ def exitRange(self, ctx: MyParser.RangeContext):
8361
)
8462
self.expr_type[ctx] = None
8563

64+
def enterWhileLoop(self, ctx: MyParser.WhileLoopContext):
65+
self.nested_loop_counter += 1
66+
67+
def exitWhileLoop(self, ctx: MyParser.WhileLoopContext):
68+
self.nested_loop_counter -= 1
69+
8670
def exitComparison(self, ctx: MyParser.ComparisonContext):
8771
children_types = {self.expr_type[ctx.getChild(i)] for i in [0, 2]}
8872
if not (
@@ -194,6 +178,18 @@ def exitSpecialMatrixFunction(self, ctx: MyParser.SpecialMatrixFunctionContext):
194178
else:
195179
self.expr_type[ctx] = (Type.INT, *type_dimentions)
196180

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+
197193
def exitVector(self, ctx: MyParser.VectorContext):
198194
elements = ctx.children[1::2]
199195
for i in range(1, len(elements)):

0 commit comments

Comments
 (0)