@@ -48,7 +48,13 @@ def __init__(self):
4848
4949 def enterForLoop (self , ctx : MyParser .ForLoopContext ):
5050 self .nested_loop_counter += 1
51- self .variables [ctx .getChild (1 ).getText ()] = Type .INT
51+ var = ctx .getChild (1 ).getText ()
52+ if var in self .variables :
53+ ctx .parser .notifyErrorListeners (
54+ f"Variable { var } already declared" , ctx .getChild (2 ).getSymbol ()
55+ )
56+ else :
57+ self .variables [var ] = Type .INT
5258
5359 def exitForLoop (self , ctx : MyParser .ForLoopContext ):
5460 self .nested_loop_counter -= 1
@@ -99,8 +105,15 @@ def exitSimpleAssignment(self, ctx: MyParser.SimpleAssignmentContext):
99105 ctx .getChild (1 ).getSymbol (),
100106 )
101107 self .variables [var ] = None
108+ self .expr_type [ctx .getChild (0 )] = None
102109 else :
103110 self .variables [var ] = self .expr_type [ctx .getChild (2 )]
111+ self .expr_type [ctx .getChild (0 )] = self .expr_type [ctx .getChild (2 )]
112+
113+ elif self .expr_type [ctx .getChild (0 )] != self .expr_type [ctx .getChild (2 )]:
114+ ctx .parser .notifyErrorListeners (
115+ "Incompatible types in an assignment" , ctx .getChild (1 ).getSymbol ()
116+ )
104117
105118 def exitCompoundAssignment (self , ctx : MyParser .CompoundAssignmentContext ):
106119 type_1 = self .expr_type [ctx .getChild (0 )]
0 commit comments