@@ -106,6 +106,45 @@ TypeModifiers get_current_modifiers(void)
106
106
return mods ;
107
107
}
108
108
109
+ /* Include the symbol table functions */
110
+ extern bool set_variable (char * name , int value , TypeModifiers mod );
111
+ extern int get_variable (char * name );
112
+ extern void yyerror (const char * s );
113
+ extern void yapping (const char * format , ...);
114
+ extern void yappin (const char * format , ...);
115
+ extern void baka (const char * format , ...);
116
+ extern TypeModifiers get_variable_modifiers (const char * name );
117
+ extern int yylineno ;
118
+
119
+ /* Function implementations */
120
+
121
+ bool check_and_mark_identifier (ASTNode * node , const char * contextErrorMessage )
122
+ {
123
+ if (!node -> alreadyChecked )
124
+ {
125
+ node -> alreadyChecked = true;
126
+ node -> isValidSymbol = false;
127
+
128
+ // Do the table lookup
129
+ for (int i = 0 ; i < var_count ; i ++ )
130
+ {
131
+ if (strcmp (symbol_table [i ].name , node -> data .name ) == 0 )
132
+ {
133
+ node -> isValidSymbol = true;
134
+ break ;
135
+ }
136
+ }
137
+
138
+ if (!node -> isValidSymbol )
139
+ {
140
+ yylineno = yylineno - 2 ;
141
+ yyerror (contextErrorMessage );
142
+ }
143
+ }
144
+
145
+ return node -> isValidSymbol ;
146
+ }
147
+
109
148
void execute_switch_statement (ASTNode * node )
110
149
{
111
150
int switch_value = evaluate_expression (node -> data .switch_stmt .expression );
@@ -143,17 +182,6 @@ void execute_switch_statement(ASTNode *node)
143
182
}
144
183
}
145
184
146
- /* Include the symbol table functions */
147
- extern bool set_variable (char * name , int value , TypeModifiers mod );
148
- extern int get_variable (char * name );
149
- extern void yyerror (const char * s );
150
- extern void yapping (const char * format , ...);
151
- extern void yappin (const char * format , ...);
152
- extern void baka (const char * format , ...);
153
- extern TypeModifiers get_variable_modifiers (const char * name );
154
-
155
- /* Function implementations */
156
-
157
185
ASTNode * create_int_node (int value )
158
186
{
159
187
ASTNode * node = malloc (sizeof (ASTNode ));
@@ -416,6 +444,9 @@ int evaluate_expression_int(ASTNode *node)
416
444
}
417
445
case NODE_IDENTIFIER :
418
446
{
447
+ if (!check_and_mark_identifier (node , "Undefined variable" ))
448
+ exit (1 );
449
+
419
450
char * name = node -> data .name ;
420
451
for (int i = 0 ; i < var_count ; i ++ )
421
452
{
@@ -708,6 +739,8 @@ bool is_float_expression(ASTNode *node)
708
739
return false;
709
740
case NODE_IDENTIFIER :
710
741
{
742
+ if (!check_and_mark_identifier (node , "Undefined variable in type check" ))
743
+ exit (1 );
711
744
for (int i = 0 ; i < var_count ; i ++ )
712
745
{
713
746
if (strcmp (symbol_table [i ].name , node -> data .name ) == 0 )
@@ -744,6 +777,8 @@ bool is_double_expression(ASTNode *node)
744
777
return false;
745
778
case NODE_IDENTIFIER :
746
779
{
780
+ if (!check_and_mark_identifier (node , "Undefined variable in type check" ))
781
+ exit (1 );
747
782
for (int i = 0 ; i < var_count ; i ++ )
748
783
{
749
784
if (strcmp (symbol_table [i ].name , node -> data .name ) == 0 )
0 commit comments