@@ -48,7 +48,13 @@ def __init__(self):
48
48
49
49
def enterForLoop (self , ctx : MyParser .ForLoopContext ):
50
50
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
52
58
53
59
def exitForLoop (self , ctx : MyParser .ForLoopContext ):
54
60
self .nested_loop_counter -= 1
@@ -99,8 +105,15 @@ def exitSimpleAssignment(self, ctx: MyParser.SimpleAssignmentContext):
99
105
ctx .getChild (1 ).getSymbol (),
100
106
)
101
107
self .variables [var ] = None
108
+ self .expr_type [ctx .getChild (0 )] = None
102
109
else :
103
110
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
+ )
104
117
105
118
def exitCompoundAssignment (self , ctx : MyParser .CompoundAssignmentContext ):
106
119
type_1 = self .expr_type [ctx .getChild (0 )]
0 commit comments