73 lines
2.0 KiB
C
73 lines
2.0 KiB
C
#include <common/fzl_types.h>
|
|
#include <compiler/fzl_analyzer.h>
|
|
#include <compiler/fzl_parser.h>
|
|
#include <compiler/internals/analyzer.h>
|
|
#include <compiler/utils/fzl_symtable.h>
|
|
#include <stdio.h>
|
|
|
|
// Analyze class declaration
|
|
fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node)
|
|
{
|
|
if (!analyzer)
|
|
return (fzl_an_res_t){.error = true};
|
|
|
|
if (!class_node || class_node->type != FZL_NODETYPE_CLASS)
|
|
{
|
|
an_push_error(analyzer, NULL, "node is not class");
|
|
return (fzl_an_res_t){.error = true};
|
|
}
|
|
|
|
char *classname = (char *)class_node->class.name->value;
|
|
fzl_an_ctx_t *ctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_CLASS);
|
|
ctx->classname = classname;
|
|
|
|
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], classname, &sym)))
|
|
break;
|
|
}
|
|
|
|
fzl_symtable_res_t res =
|
|
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], class_node,
|
|
classname, FZL_TYPE_NULL, 0);
|
|
|
|
if (res != FZL_SYMTABLE_RESULT_OK)
|
|
{
|
|
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
|
{
|
|
snprintf(an_problem, sizeof(an_problem),
|
|
"name '%s' already has been declared", classname);
|
|
an_push_error(analyzer, class_node->token, an_problem);
|
|
}
|
|
else
|
|
{
|
|
snprintf(an_problem, sizeof(an_problem), "cannot store name '%s'",
|
|
classname);
|
|
an_push_error(analyzer, class_node->token, an_problem);
|
|
}
|
|
|
|
an_fin_ctx(analyzer);
|
|
return (fzl_an_res_t){.error = true};
|
|
}
|
|
|
|
bool error = false;
|
|
for (size_t i = 0; i < class_node->class.fields_count; i++)
|
|
{
|
|
fzl_an_res_t res = analyze_common(analyzer, class_node->class.fields[i]);
|
|
if (res.error)
|
|
error = true;
|
|
}
|
|
|
|
for (size_t i = 0; i < class_node->class.methods_count; i++)
|
|
{
|
|
fzl_an_res_t res = analyze_common(analyzer, class_node->class.methods[i]);
|
|
if (res.error)
|
|
error = true;
|
|
}
|
|
|
|
an_fin_ctx(analyzer);
|
|
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};
|
|
}
|