38 lines
997 B
C
38 lines
997 B
C
#include <compiler/fzl_analyzer.h>
|
|
#include <compiler/fzl_parser.h>
|
|
#include <compiler/utils/fzl_symtable.h>
|
|
#include <stdio.h>
|
|
|
|
// Analyze identifier
|
|
fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id)
|
|
{
|
|
if (!analyzer)
|
|
return (fzl_an_res_t){.error = true};
|
|
|
|
if (!id || id->type != FZL_NODETYPE_IDENTIFIER)
|
|
{
|
|
an_push_error(analyzer, NULL, "node is not an identifier");
|
|
return (fzl_an_res_t){.error = true};
|
|
}
|
|
|
|
char *symname = id->token->value;
|
|
fzl_sym_t sym;
|
|
bool sym_found = false;
|
|
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
|
{
|
|
if ((sym_found = fzl_symt_get(analyzer->symbols[i], symname, &sym)))
|
|
break;
|
|
}
|
|
|
|
if (!sym_found)
|
|
{
|
|
snprintf(an_problem, sizeof(an_problem), "cannot find symbol '%s'",
|
|
symname);
|
|
an_push_error(analyzer, id->token, an_problem);
|
|
return (fzl_an_res_t){.error = true};
|
|
}
|
|
|
|
return (fzl_an_res_t){
|
|
.error = false, .type = sym.type, .classname = sym.classname};
|
|
}
|