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,126 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char fn_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!fn_node || fn_node->type != FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof FUNCTION");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *fnctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_FUNCTION);
|
||||
fnctx->fn = fn_node;
|
||||
|
||||
fzl_an_ctx_t *ctx = analyzer->ctx;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
if (ctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
fzl_type_t rettype = an_toktotype(fn_node->fn.type);
|
||||
char *classname = NULL;
|
||||
if (rettype == FZL_TYPE_OBJECT)
|
||||
classname = (char *)fn_node->fn.type->value;
|
||||
|
||||
if (ctx != NULL && ctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||
{
|
||||
for (size_t i = 0; i < fn_node->fn.args_count; i++)
|
||||
{
|
||||
fzl_node_t *decl = fn_node->fn.args[i];
|
||||
if (decl->type != FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
an_push_error(analyzer, decl->token, "argument must be a declaration");
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *argname = (char *)decl->var.identifier->token->value;
|
||||
if (strcmp(argname, "this") == 0 && i > 0)
|
||||
{
|
||||
an_push_error(analyzer, decl->token,
|
||||
"manual declaration of 'this' is not allowed");
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
size_t classname_len = strlen(ctx->classname);
|
||||
size_t methodname_len = strlen((char *)fn_node->fn.name->value);
|
||||
char *methodname = malloc(classname_len + methodname_len + 2);
|
||||
|
||||
strcpy(methodname, ctx->classname);
|
||||
methodname[classname_len] = '_';
|
||||
strcpy(methodname + classname_len + 1, fn_node->fn.name->value);
|
||||
methodname[classname_len + methodname_len + 1] = 0;
|
||||
|
||||
analyzer->alloc_names[analyzer->alloc_names_top++] = methodname;
|
||||
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], fn_node,
|
||||
methodname, rettype, 0);
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(fn_problem, sizeof(fn_problem),
|
||||
"method '%s.%s' already has been declared", classname,
|
||||
(char *)fn_node->fn.name->value);
|
||||
an_push_error(analyzer, fn_node->token, fn_problem);
|
||||
}
|
||||
else
|
||||
an_push_error(analyzer, fn_node->token, "failed to store method");
|
||||
|
||||
free(analyzer->alloc_names[--analyzer->alloc_names_top]);
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
fzl_symt_setobjclass(analyzer->symbols[analyzer->symbols_top - 1],
|
||||
methodname, classname);
|
||||
}
|
||||
else
|
||||
{
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 1], fn_node,
|
||||
(char *)fn_node->fn.name->value, rettype, 0);
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(fn_problem, sizeof(fn_problem),
|
||||
"function '%s' already has been declared",
|
||||
(char *)fn_node->fn.name->value);
|
||||
an_push_error(analyzer, fn_node->token, fn_problem);
|
||||
}
|
||||
else
|
||||
an_push_error(analyzer, fn_node->token, "failed to store function");
|
||||
|
||||
free(analyzer->alloc_names[--analyzer->alloc_names_top]);
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
fzl_an_res_t body_res = analyze_common(analyzer, fn_node->fn.body);
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = body_res.error, .type = rettype, .classname = classname};
|
||||
}
|
||||
Reference in New Issue
Block a user