wip: working on analyzer
- Feat: now symbols table store node on PUT - Feat: call analyzer with callee resolving and arguments validate - Feat: identifier analyzer - Feat: analyzer utils (types compat, tok-to-type, type-to-string)
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
#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 <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *tmp_method_name = NULL;
|
||||
char call_problem[256] = {0};
|
||||
|
||||
// Try to get symbol name from callee
|
||||
char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||
{
|
||||
*method = false;
|
||||
|
||||
if (callee->type == FZL_NODETYPE_IDENTIFIER)
|
||||
return (char *)callee->token->value;
|
||||
|
||||
if (callee->type != FZL_NODETYPE_FIELD)
|
||||
return NULL;
|
||||
|
||||
*method = true;
|
||||
fzl_an_res_t parent_res = analyze_common(analyzer, callee->memb.parent);
|
||||
if (parent_res.type != FZL_TYPE_OBJECT || !parent_res.classname)
|
||||
return NULL;
|
||||
|
||||
char *fn_name = callee->memb.field->token->value;
|
||||
size_t classname_len = strlen(parent_res.classname);
|
||||
size_t methodname_len = strlen(fn_name);
|
||||
tmp_method_name = malloc(classname_len + methodname_len + 2);
|
||||
if (tmp_method_name)
|
||||
{
|
||||
an_push_error(analyzer, callee->token,
|
||||
"failed to allocate space for method name");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
strcpy(tmp_method_name, parent_res.classname);
|
||||
tmp_method_name[classname_len] = '_';
|
||||
strcpy(tmp_method_name + classname_len + 1, fn_name);
|
||||
tmp_method_name[classname_len + methodname_len + 1] = 0;
|
||||
|
||||
return tmp_method_name;
|
||||
}
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc);
|
||||
extern char *an_typetostr(fzl_type_t type, char *classname);
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
// Analyze call node
|
||||
fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
{
|
||||
if (!call_node || call_node->type != FZL_NODETYPE_CALL)
|
||||
{
|
||||
an_push_error(analyzer, call_node->token,
|
||||
"provided node is not typeof CALL");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *callee = call_node->call.callee;
|
||||
fzl_an_res_t callee_res = analyze_common(analyzer, callee);
|
||||
if (callee_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
bool is_method = false;
|
||||
char *callee_name = resolve_callee(analyzer, callee, &is_method);
|
||||
if (!callee_name)
|
||||
{
|
||||
an_push_error(analyzer, callee->token, "expression is not callable");
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
free(tmp_method_name);
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
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], callee_name, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem), "cannot resolve symbol '%s'",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (sym.node->type != FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem), "'%s' is not callable",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (sym.node->fn.args_count != call_node->call.args_count)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem),
|
||||
"expected %zu argument(s) while %zu is specified",
|
||||
sym.node->fn.args_count, call_node->call.args_count);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < sym.node->fn.args_count; i++)
|
||||
{
|
||||
fzl_an_res_t req = analyze_common(analyzer, sym.node->fn.args[i]);
|
||||
fzl_an_res_t got = analyze_common(analyzer, call_node->call.args[i]);
|
||||
if (!an_is_compatible(req.type, got.type, req.classname, got.classname))
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem),
|
||||
"expect argument %zu to be %s, got '%s'", i + 1,
|
||||
an_typetostr(req.type, req.classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
fzl_type_t type = an_toktotype(sym.node->fn.type);
|
||||
char *classname = sym.classname;
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
|
||||
}
|
||||
Reference in New Issue
Block a user