feat: finish implementing analyzer
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include "common/fzl_types.h"
|
||||
#include "compiler/fzl_analyzer.h"
|
||||
#include "compiler/fzl_parser.h"
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char asn_problem[256];
|
||||
|
||||
@@ -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)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP)
|
||||
|
||||
@@ -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};
|
||||
}
|
||||
|
||||
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]);
|
||||
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),
|
||||
"expect argument %zu to be %s, got '%s'", i + 1,
|
||||
an_typetostr(req.type, req.classname),
|
||||
an_typetostr(req_type, req_classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@
|
||||
// Analyzer main function
|
||||
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};
|
||||
switch (node->type)
|
||||
{
|
||||
|
||||
@@ -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);
|
||||
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;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
|
||||
@@ -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;
|
||||
while (!fnctx)
|
||||
while (fnctx != NULL)
|
||||
{
|
||||
if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
break;
|
||||
@@ -32,7 +32,7 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
|
||||
fnctx = fnctx->parent;
|
||||
}
|
||||
|
||||
if (!fnctx)
|
||||
if (!fnctx || fnctx->type != FZL_AN_CTX_TYPE_FUNCTION)
|
||||
{
|
||||
an_push_error(analyzer, ret_node->token,
|
||||
"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);
|
||||
if (ret_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname))
|
||||
{
|
||||
snprintf(ret_problem, sizeof(ret_problem),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// Nest context
|
||||
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)
|
||||
return NULL;
|
||||
|
||||
memset(ctx, 0, sizeof(fzl_an_ctx_t));
|
||||
|
||||
ctx->type = type;
|
||||
if (analyzer->ctx != NULL)
|
||||
ctx->parent = analyzer->ctx;
|
||||
else
|
||||
ctx->parent = NULL;
|
||||
|
||||
analyzer->ctx = ctx;
|
||||
analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt();
|
||||
|
||||
+2
-1
@@ -155,7 +155,8 @@ int main(int argc, char **argv)
|
||||
if (analyzer->warnings_top > 0)
|
||||
{
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user