fix: review problems
This commit is contained in:
@@ -9,8 +9,9 @@
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char *tmp_method_name = NULL;
|
||||
char call_problem[256] = {0};
|
||||
char *tmp_method_name = NULL;
|
||||
char *tmp_fnname = NULL;
|
||||
char *tmp_classname = NULL;
|
||||
|
||||
// Try to get symbol name from callee
|
||||
char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||
@@ -21,7 +22,10 @@ char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||
return (char *)callee->token->value;
|
||||
|
||||
if (callee->type != FZL_NODETYPE_FIELD)
|
||||
{
|
||||
an_push_error(analyzer, callee->token, "unknown call format");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
*method = true;
|
||||
fzl_an_res_t parent_res = analyze_common(analyzer, callee->memb.parent);
|
||||
@@ -29,13 +33,14 @@ char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||
return NULL;
|
||||
|
||||
char *fn_name = callee->memb.field->token->value;
|
||||
tmp_fnname = fn_name;
|
||||
tmp_classname = parent_res.classname;
|
||||
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");
|
||||
an_push_error(analyzer, callee->token, "out of memory");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
@@ -47,17 +52,12 @@ char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||
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, NULL, "provided node is not typeof CALL");
|
||||
an_push_error(analyzer, NULL, "node is not call");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
@@ -78,14 +78,15 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
// NOTE: move into `compiler/analyzer/builtins.c`
|
||||
if (strcmp(callee_name, "printline") == 0 || strcmp(callee_name, "exit") == 0)
|
||||
{
|
||||
if (call_node->call.args_count != 1)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem),
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"'%s' builtin should be called with 1 argument instead of %zu",
|
||||
callee_name, call_node->call.args_count);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
@@ -106,10 +107,6 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
|
||||
// fzl_an_res_t callee_res = analyze_common(analyzer, callee);
|
||||
// if (callee_res.error)
|
||||
// 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--)
|
||||
@@ -120,9 +117,9 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem), "cannot resolve symbol '%s'",
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot resolve symbol '%s'",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
@@ -135,9 +132,9 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
|
||||
if (sym.node->type != FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem), "'%s' is not callable",
|
||||
snprintf(an_problem, sizeof(an_problem), "'%s' is not callable",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
@@ -148,13 +145,24 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
size_t start_i = is_method ? 1 : 0;
|
||||
if ((sym.node->fn.args_count - start_i) != call_node->call.args_count)
|
||||
size_t offset = is_method ? 1 : 0;
|
||||
if ((sym.node->fn.args_count - offset) != 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 - start_i);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
if (is_method)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"method '%s.%s' is expecting %zu argument(s), got %zu",
|
||||
tmp_classname, tmp_fnname, sym.node->fn.args_count,
|
||||
call_node->call.args_count - offset);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"function '%s' is expecting %zu argument(s), got %zu",
|
||||
callee_name, sym.node->fn.args_count,
|
||||
call_node->call.args_count - offset);
|
||||
}
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
@@ -165,7 +173,7 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
for (size_t i = start_i; i < sym.node->fn.args_count; i++)
|
||||
for (size_t i = offset; i < sym.node->fn.args_count; i++)
|
||||
{
|
||||
fzl_node_t *arg = sym.node->fn.args[i];
|
||||
fzl_type_t req_type = an_toktotype(arg->var.type);
|
||||
@@ -174,7 +182,7 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
req_classname = arg->var.type->value;
|
||||
|
||||
fzl_an_res_t got =
|
||||
analyze_common(analyzer, call_node->call.args[i - start_i]);
|
||||
analyze_common(analyzer, call_node->call.args[i - offset]);
|
||||
if (got.error)
|
||||
{
|
||||
if (tmp_method_name != NULL)
|
||||
@@ -187,11 +195,25 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
|
||||
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);
|
||||
if (is_method)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"method '%s.%s' is expecting %zu argument to have type '%s', "
|
||||
"got '%s'",
|
||||
tmp_classname, tmp_fnname, i + 1 - offset,
|
||||
an_typetostr(req_type, req_classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"function '%s' is expecting %zu argument to have type '%s', "
|
||||
"got '%s'",
|
||||
callee_name, i + 1 - offset,
|
||||
an_typetostr(req_type, req_classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
}
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user