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,83 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char field_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
// Analyze field access
|
||||
fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
|
||||
{
|
||||
if (analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!field || field->type != FZL_NODETYPE_FIELD)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof FIELD");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t parent = analyze_common(analyzer, field->memb.parent);
|
||||
if (parent.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (parent.type != FZL_TYPE_OBJECT || parent.classname == NULL)
|
||||
{
|
||||
snprintf(field_problem, sizeof(field_problem),
|
||||
"access to non-object value");
|
||||
an_push_error(analyzer, field->token, field_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *field_name = field->memb.field->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], parent.classname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (sym_found)
|
||||
{
|
||||
snprintf(field_problem, sizeof(field_problem), "class '%s' not found",
|
||||
parent.classname);
|
||||
an_push_error(analyzer, field->parent->token, field_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *field_node = NULL;
|
||||
for (size_t i = 0; i < sym.node->class.fields_count; i++)
|
||||
{
|
||||
if (strcmp(sym.node->class.fields[i]->var.identifier->token->value,
|
||||
field_name) == 0)
|
||||
{
|
||||
field_node = sym.node->class.fields[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (field_node == NULL)
|
||||
{
|
||||
snprintf(field_problem, sizeof(field_problem),
|
||||
"field '%s.%s' does not exists", parent.classname, field_name);
|
||||
an_push_error(analyzer, field->memb.field->token, field_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_type_t field_type = an_toktotype(field_node->var.type);
|
||||
char *classname = NULL;
|
||||
if (field_type == FZL_TYPE_OBJECT)
|
||||
classname = field_node->var.type->value;
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = field_type, .classname = classname};
|
||||
}
|
||||
Reference in New Issue
Block a user