Impl analyzer #5

Merged
samsonium merged 11 commits from impl-analyzer into v1.1 2026-10-27 12:43:08 +00:00
8 changed files with 47 additions and 10 deletions
Showing only changes of commit aa7a06bd22 - Show all commits
+3 -3
View File
@@ -1,6 +1,6 @@
#include "common/fzl_types.h" #include <common/fzl_types.h>
#include "compiler/fzl_analyzer.h" #include <compiler/fzl_analyzer.h>
#include "compiler/fzl_parser.h" #include <compiler/fzl_parser.h>
#include <stdio.h> #include <stdio.h>
char asn_problem[256]; char asn_problem[256];
Review

Make a common 2nd level string buffer for errors in analyze_* functions

Make a common 2nd level string buffer for errors in `analyze_*` functions
3
+2
View File
1
@@ -19,6 +19,8 @@ fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node)
{ {
if (ctx && ctx->type == FZL_AN_CTX_TYPE_LOOP) if (ctx && ctx->type == FZL_AN_CTX_TYPE_LOOP)
break; break;
ctx = ctx->parent;
} }
if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP) if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP)
+20 -4
View File
5
@@ -164,15 +164,31 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
for (size_t i = 0; i < sym.node->fn.args_count; i++) size_t start_i = is_method ? 1 : 0;
for (size_t i = start_i; i < sym.node->fn.args_count; i++)
{ {
fzl_an_res_t req = analyze_common(analyzer, sym.node->fn.args[i]); fzl_node_t *arg = sym.node->fn.args[i];
fzl_type_t req_type = an_toktotype(arg->var.type);
char *req_classname = NULL;
if (req_type == FZL_TYPE_OBJECT)
req_classname = arg->var.type->value;
fzl_an_res_t got = analyze_common(analyzer, call_node->call.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)) if (got.error)
{
if (tmp_method_name != NULL)
{
free(tmp_method_name);
tmp_method_name = NULL;
}
return (fzl_an_res_t){.error = true};
}
if (!an_is_compatible(req_type, got.type, req_classname, got.classname))
{ {
snprintf(call_problem, sizeof(call_problem), snprintf(call_problem, sizeof(call_problem),
"expect argument %zu to be %s, got '%s'", i + 1, "expect argument %zu to be %s, got '%s'", i + 1,
an_typetostr(req.type, req.classname), an_typetostr(req_type, req_classname),
Outdated
Review

function '%s' is expecting argument %zu to be '%s', got '%s'

`function '%s' is expecting argument %zu to be '%s', got '%s'`
an_typetostr(got.type, got.classname)); an_typetostr(got.type, got.classname));
an_push_error(analyzer, call_node->token, call_problem); an_push_error(analyzer, call_node->token, call_problem);
+3
View File
@@ -8,6 +8,9 @@
// Analyzer main function // Analyzer main function
fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node) fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node)
{ {
if (!analyzer || !node)
return (fzl_an_res_t){.error = true};
fzl_an_res_t res = {.error = false, .type = FZL_TYPE_NULL, .classname = NULL}; fzl_an_res_t res = {.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
switch (node->type) switch (node->type)
{ {
+7
View File
@@ -26,6 +26,13 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
fzl_an_ctx_t *fnctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_FUNCTION); fzl_an_ctx_t *fnctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_FUNCTION);
fnctx->fn = fn_node; fnctx->fn = fn_node;
for (size_t i = 0; i < fn_node->fn.args_count; i++)
{
fzl_an_res_t res = analyze_common(analyzer, fn_node->fn.args[i]);
if (res.error)
return (fzl_an_res_t){.error = true};
}
fzl_an_ctx_t *ctx = analyzer->ctx; fzl_an_ctx_t *ctx = analyzer->ctx;
while (ctx != NULL) while (ctx != NULL)
{ {
+5 -2
View File
@@ -24,7 +24,7 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
} }
fzl_an_ctx_t *fnctx = analyzer->ctx; fzl_an_ctx_t *fnctx = analyzer->ctx;
while (!fnctx) while (fnctx != NULL)
{ {
if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION) if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
break; break;
@@ -32,7 +32,7 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
fnctx = fnctx->parent; fnctx = fnctx->parent;
} }
if (!fnctx) if (!fnctx || fnctx->type != FZL_AN_CTX_TYPE_FUNCTION)
{ {
an_push_error(analyzer, ret_node->token, an_push_error(analyzer, ret_node->token,
"cannot use 'return' outside function"); "cannot use 'return' outside function");
@@ -55,6 +55,9 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
} }
fzl_an_res_t ret_res = analyze_common(analyzer, ret_node->ret.value); fzl_an_res_t ret_res = analyze_common(analyzer, ret_node->ret.value);
if (ret_res.error)
return (fzl_an_res_t){.error = true};
if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname)) if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname))
{ {
snprintf(ret_problem, sizeof(ret_problem), snprintf(ret_problem, sizeof(ret_problem),
+5
View File
@@ -1,6 +1,7 @@
#include <compiler/fzl_analyzer.h> #include <compiler/fzl_analyzer.h>
#include <compiler/utils/fzl_symtable.h> #include <compiler/utils/fzl_symtable.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
// Nest context // Nest context
fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type) fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type)
@@ -12,9 +13,13 @@ fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type)
if (!ctx) if (!ctx)
return NULL; return NULL;
memset(ctx, 0, sizeof(fzl_an_ctx_t));
ctx->type = type; ctx->type = type;
if (analyzer->ctx != NULL) if (analyzer->ctx != NULL)
ctx->parent = analyzer->ctx; ctx->parent = analyzer->ctx;
else
ctx->parent = NULL;
analyzer->ctx = ctx; analyzer->ctx = ctx;
analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt(); analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt();
+2 -1
View File
@@ -155,7 +155,8 @@ int main(int argc, char **argv)
if (analyzer->warnings_top > 0) if (analyzer->warnings_top > 0)
{ {
for (ssize_t i = analyzer->warnings_top - 1; i >= 0; i--) for (ssize_t i = analyzer->warnings_top - 1; i >= 0; i--)
printf("%sWarning%s: %s\n", FZL_CERROR, FZL_CRST, analyzer->warnings[i]); printf("%sWarning%s: %s\n", FZL_CWARNING, FZL_CRST,
analyzer->warnings[i]);
} }
fzl_walker_t *walker = fzl_mkwalker(parser->root); fzl_walker_t *walker = fzl_mkwalker(parser->root);