Skip to content

Commit

Permalink
feat: enhanced error messages for undeclared identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Jan 5, 2025
1 parent 3517214 commit eeb15be
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 12 deletions.
57 changes: 46 additions & 11 deletions ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,45 @@ TypeModifiers get_current_modifiers(void)
return mods;
}

/* Include the symbol table functions */
extern bool set_variable(char *name, int value, TypeModifiers mod);
extern int get_variable(char *name);
extern void yyerror(const char *s);
extern void yapping(const char *format, ...);
extern void yappin(const char *format, ...);
extern void baka(const char *format, ...);
extern TypeModifiers get_variable_modifiers(const char *name);
extern int yylineno;

/* Function implementations */

bool check_and_mark_identifier(ASTNode *node, const char *contextErrorMessage)
{
if (!node->alreadyChecked)
{
node->alreadyChecked = true;
node->isValidSymbol = false;

// Do the table lookup
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, node->data.name) == 0)
{
node->isValidSymbol = true;
break;
}
}

if (!node->isValidSymbol)
{
yylineno = yylineno - 2;
yyerror(contextErrorMessage);
}
}

return node->isValidSymbol;
}

void execute_switch_statement(ASTNode *node)
{
int switch_value = evaluate_expression(node->data.switch_stmt.expression);
Expand Down Expand Up @@ -145,17 +184,6 @@ void execute_switch_statement(ASTNode *node)
}
}

/* Include the symbol table functions */
extern bool set_variable(char *name, int value, TypeModifiers mod);
extern int get_variable(char *name);
extern void yyerror(const char *s);
extern void yapping(const char *format, ...);
extern void yappin(const char *format, ...);
extern void baka(const char *format, ...);
extern TypeModifiers get_variable_modifiers(const char *name);

/* Function implementations */

ASTNode *create_int_node(int value)
{
ASTNode *node = malloc(sizeof(ASTNode));
Expand Down Expand Up @@ -436,6 +464,9 @@ int evaluate_expression_int(ASTNode *node)
}
case NODE_IDENTIFIER:
{
if (!check_and_mark_identifier(node, "Undefined variable"))
exit(1);

char *name = node->data.name;
for (int i = 0; i < var_count; i++)
{
Expand Down Expand Up @@ -728,6 +759,8 @@ bool is_float_expression(ASTNode *node)
return false;
case NODE_IDENTIFIER:
{
if (!check_and_mark_identifier(node, "Undefined variable in type check"))
exit(1);
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, node->data.name) == 0)
Expand Down Expand Up @@ -764,6 +797,8 @@ bool is_double_expression(ASTNode *node)
return false;
case NODE_IDENTIFIER:
{
if (!check_and_mark_identifier(node, "Undefined variable in type check"))
exit(1);
for (int i = 0; i < var_count; i++)
{
if (strcmp(symbol_table[i].name, node->data.name) == 0)
Expand Down
3 changes: 3 additions & 0 deletions ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ struct ASTNode
{
NodeType type;
TypeModifiers modifiers;
bool alreadyChecked;
bool isValidSymbol;
union
{
int ivalue;
Expand Down Expand Up @@ -220,6 +222,7 @@ void execute_yappin_call(ArgumentList *args);
void execute_baka_call(ArgumentList *args);
void free_ast(ASTNode *node);
void reset_modifiers(void);
bool check_and_mark_identifier(ASTNode *node, const char *contextErrorMessage);

extern TypeModifiers current_modifiers;

Expand Down
4 changes: 3 additions & 1 deletion lang.y
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
%define parse.error verbose
%{
#include "ast.h"
#include <stdio.h>
Expand Down Expand Up @@ -381,7 +382,8 @@ int main(void) {
}

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

void yapping(const char* format, ...) {
Expand Down

0 comments on commit eeb15be

Please sign in to comment.