Skip to content

Commit 20d3b2e

Browse files
authored
Merge pull request #43 from Brainrotlang/feat/enhanced-error-handling-undeclared-identifier
feat: enhanced error messages for undeclared identifiers
2 parents f6ec53d + 7392300 commit 20d3b2e

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

ast.c

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,45 @@ TypeModifiers get_current_modifiers(void)
106106
return mods;
107107
}
108108

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+
109148
void execute_switch_statement(ASTNode *node)
110149
{
111150
int switch_value = evaluate_expression(node->data.switch_stmt.expression);
@@ -143,17 +182,6 @@ void execute_switch_statement(ASTNode *node)
143182
}
144183
}
145184

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-
157185
ASTNode *create_int_node(int value)
158186
{
159187
ASTNode *node = malloc(sizeof(ASTNode));
@@ -416,6 +444,9 @@ int evaluate_expression_int(ASTNode *node)
416444
}
417445
case NODE_IDENTIFIER:
418446
{
447+
if (!check_and_mark_identifier(node, "Undefined variable"))
448+
exit(1);
449+
419450
char *name = node->data.name;
420451
for (int i = 0; i < var_count; i++)
421452
{
@@ -708,6 +739,8 @@ bool is_float_expression(ASTNode *node)
708739
return false;
709740
case NODE_IDENTIFIER:
710741
{
742+
if (!check_and_mark_identifier(node, "Undefined variable in type check"))
743+
exit(1);
711744
for (int i = 0; i < var_count; i++)
712745
{
713746
if (strcmp(symbol_table[i].name, node->data.name) == 0)
@@ -744,6 +777,8 @@ bool is_double_expression(ASTNode *node)
744777
return false;
745778
case NODE_IDENTIFIER:
746779
{
780+
if (!check_and_mark_identifier(node, "Undefined variable in type check"))
781+
exit(1);
747782
for (int i = 0; i < var_count; i++)
748783
{
749784
if (strcmp(symbol_table[i].name, node->data.name) == 0)

ast.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ struct ASTNode
119119
{
120120
NodeType type;
121121
TypeModifiers modifiers;
122+
bool alreadyChecked;
123+
bool isValidSymbol;
122124
union
123125
{
124126
int ivalue;
@@ -220,6 +222,7 @@ void execute_yappin_call(ArgumentList *args);
220222
void execute_baka_call(ArgumentList *args);
221223
void free_ast(ASTNode *node);
222224
void reset_modifiers(void);
225+
bool check_and_mark_identifier(ASTNode *node, const char *contextErrorMessage);
223226

224227
extern TypeModifiers current_modifiers;
225228

lang.y

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
%define parse.error verbose
12
%{
23
#include "ast.h"
34
#include <stdio.h>
@@ -381,7 +382,8 @@ int main(void) {
381382
}
382383

383384
void yyerror(const char *s) {
384-
fprintf(stderr, "Error: %s at line %d\n", s, yylineno);
385+
extern char *yytext;
386+
fprintf(stderr, "Error: %s at line %d\n", s, yylineno - 1);
385387
}
386388

387389
void yapping(const char* format, ...) {

0 commit comments

Comments
 (0)