wip: working on analyzer
- Feat: add binary math analysis - Fix: check for function context in block analysis - Feat: add 'break' analysis - Fix: few fixes in call analysis - Feat: add class analysis - Feat: add construct analysis - Feat: add declaration analysis - Feat: add field analysis - Feat: add function analysis - Feat: add loop analysis - Ref: updated utilities and context functions
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char dec_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
// Analyze variable/field/argument declaration
|
||||
fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!decl || decl->type != FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof DECLARATION");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *name = (char *)decl->var.identifier->token->value;
|
||||
fzl_type_t type = an_toktotype(decl->var.type);
|
||||
char *classname = NULL;
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
classname = decl->var.type->value;
|
||||
|
||||
fzl_symtable_res_t res =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 1], decl, name,
|
||||
an_toktotype(decl->var.type), 0);
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
snprintf(dec_problem, sizeof(dec_problem),
|
||||
"'%s' already has been declared", name);
|
||||
else
|
||||
snprintf(dec_problem, sizeof(dec_problem), "cannot store '%s'", name);
|
||||
|
||||
an_push_error(analyzer, decl->token, dec_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
fzl_sym_t class;
|
||||
bool class_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((class_found = fzl_symt_get(analyzer->symbols[i],
|
||||
decl->var.type->value, &class)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!class_found || class.node->type != FZL_NODETYPE_CLASS)
|
||||
{
|
||||
snprintf(dec_problem, sizeof(dec_problem), "class '%s' does not exists",
|
||||
(char *)decl->var.type->value);
|
||||
an_push_error(analyzer, decl->var.type, dec_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_symt_setobjclass(analyzer->symbols[analyzer->symbols_top - 1], name,
|
||||
classname);
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
Reference in New Issue
Block a user