wip: working on analyzer

- Feat: implement return analyzer
- Fix: add block return check if inside function
This commit is contained in:
samsonium
2026-10-26 22:20:09 +03:00
parent a70ad011f3
commit d7630dbb6a
4 changed files with 127 additions and 0 deletions
+47
View File
@@ -1,11 +1,16 @@
#include <common/fzl_types.h>
#include <compiler/fzl_analyzer.h>
#include <compiler/fzl_parser.h>
#include <compiler/fzl_token.h>
#include <stddef.h>
#include <stdio.h>
char block_problem[256];
// defined in `compiler/analyzer/utility.c`
extern fzl_type_t an_toktotype(fzl_token_t *tok);
extern char *an_typetostr(fzl_type_t type, char *classname);
// Analyze block
fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
{
@@ -38,6 +43,48 @@ fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
has_return = true;
}
fzl_an_ctx_t *fnctx = analyzer->ctx;
while (fnctx != NULL)
{
if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
break;
fnctx = fnctx->parent;
}
if (fnctx && fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
{
fzl_type_t rettype = an_toktotype(fnctx->fn->fn.type);
char *classname = NULL;
char *rettype_str = an_typetostr(rettype, classname);
if (rettype == FZL_TYPE_OBJECT)
classname = (char *)fnctx->fn->fn.type->value;
if (rettype != FZL_TYPE_NULL)
{
if (!has_return || block_node->list.entry_count < 1)
{
snprintf(block_problem, sizeof(block_problem),
"expected 'return <%s>' in function because return type is %s",
rettype_str, rettype_str);
an_push_error(analyzer, block_node->token, block_problem);
return (fzl_an_res_t){.error = true};
}
fzl_node_t *last_node =
block_node->list.entries[block_node->list.entry_count - 1];
if (last_node->type != FZL_NODETYPE_RETURN)
{
snprintf(block_problem, sizeof(block_problem),
"expected last statement in block to be 'return <%s>'",
rettype_str);
an_push_error(analyzer, last_node->token, block_problem);
return (fzl_an_res_t){.error = true};
}
}
}
an_fin_ctx(analyzer);
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};