wip: working on analyzer
- Feat: add binary math analysis - Fix: check for function context in block analysis - Feat: add 'break' analysis - Fix: few fixes in call analysis - Feat: add class analysis - Feat: add construct analysis - Feat: add declaration analysis - Feat: add field analysis - Feat: add function analysis - Feat: add loop analysis - Ref: updated utilities and context functions
This commit is contained in:
@@ -22,11 +22,20 @@ add_executable(fzc
|
||||
src/compiler/checkers/fzl_loop_check.c
|
||||
src/compiler/analyzer/utility.c
|
||||
src/compiler/analyzer/context.c
|
||||
src/compiler/analyzer/an_binmath.c
|
||||
src/compiler/analyzer/an_block.c
|
||||
src/compiler/analyzer/an_break.c
|
||||
src/compiler/analyzer/an_call.c
|
||||
src/compiler/analyzer/an_class.c
|
||||
src/compiler/analyzer/an_common.c
|
||||
src/compiler/analyzer/an_conditional.c
|
||||
src/compiler/analyzer/an_construct.c
|
||||
src/compiler/analyzer/an_declaration.c
|
||||
src/compiler/analyzer/an_field.c
|
||||
src/compiler/analyzer/an_function.c
|
||||
src/compiler/analyzer/an_identifier.c
|
||||
src/compiler/analyzer/an_loop.c
|
||||
src/compiler/analyzer/an_return.c
|
||||
src/compiler/fzl_analyzer.c
|
||||
src/compiler/fzl_debug.c
|
||||
src/compiler/fzl_parser.c
|
||||
|
||||
@@ -14,6 +14,7 @@ typedef enum
|
||||
FZL_AN_CTX_TYPE_UNNECESSARY, // default type
|
||||
FZL_AN_CTX_TYPE_FUNCTION, // when analyzing function
|
||||
FZL_AN_CTX_TYPE_LOOP, // when analyzing loop
|
||||
FZL_AN_CTX_TYPE_CLASS, // when analyzing class
|
||||
} fzl_an_ctx_type_t;
|
||||
|
||||
// Inner node analyzing result descriptor
|
||||
@@ -35,6 +36,7 @@ typedef struct fzl_an_ctx
|
||||
union
|
||||
{
|
||||
fzl_node_t *fn;
|
||||
char *classname;
|
||||
};
|
||||
} fzl_an_ctx_t;
|
||||
|
||||
@@ -50,13 +52,13 @@ typedef struct
|
||||
fzl_symtable_t *symbols[256];
|
||||
size_t symbols_top;
|
||||
|
||||
char *alloc_names[256];
|
||||
size_t alloc_names_top;
|
||||
|
||||
fzl_node_t *root;
|
||||
fzl_an_ctx_t *ctx;
|
||||
} fzl_analyzer_t;
|
||||
|
||||
// Temp container for error
|
||||
extern char alast_error[256];
|
||||
|
||||
// Allocate analyzer
|
||||
fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node);
|
||||
|
||||
@@ -79,6 +81,13 @@ fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond);
|
||||
fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node);
|
||||
fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node);
|
||||
fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node);
|
||||
fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node);
|
||||
fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field);
|
||||
fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl);
|
||||
fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct);
|
||||
fzl_an_res_t analyze_loop(fzl_analyzer_t *analyzer, fzl_node_t *loop_node);
|
||||
fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node);
|
||||
fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node);
|
||||
|
||||
fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type);
|
||||
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer);
|
||||
|
||||
@@ -0,0 +1,47 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc);
|
||||
|
||||
// Analyze binary arithmetics
|
||||
fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!bin_node || bin_node->type != FZL_NODETYPE_ADD ||
|
||||
bin_node->type != FZL_NODETYPE_SUB ||
|
||||
bin_node->type != FZL_NODETYPE_MUL ||
|
||||
bin_node->type != FZL_NODETYPE_DIV ||
|
||||
bin_node->type != FZL_NODETYPE_POW || bin_node->type != FZL_NODETYPE_MOD)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof binary math");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t left = analyze_common(analyzer, bin_node->binary_op.left);
|
||||
if (left.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t right = analyze_common(analyzer, bin_node->binary_op.right);
|
||||
if (right.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (left.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, bin_node->binary_op.left->token,
|
||||
"left side is not an integer type");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (right.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, bin_node->binary_op.right->token,
|
||||
"right side is not an integer type");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = left.type, .classname = NULL};
|
||||
}
|
||||
@@ -46,7 +46,7 @@ fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
|
||||
fzl_an_ctx_t *fnctx = analyzer->ctx;
|
||||
while (fnctx != NULL)
|
||||
{
|
||||
if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
if (fnctx && fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
break;
|
||||
|
||||
fnctx = fnctx->parent;
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
|
||||
// Analyze break node
|
||||
fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!break_node || break_node->type != FZL_NODETYPE_BREAK)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof BREAK");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *ctx = analyzer->ctx;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
if (ctx && ctx->type == FZL_AN_CTX_TYPE_LOOP)
|
||||
break;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP)
|
||||
{
|
||||
an_push_error(analyzer, break_node->token,
|
||||
"cannot use 'break' outside loop");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
@@ -32,7 +32,7 @@ char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
|
||||
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)
|
||||
if (!tmp_method_name)
|
||||
{
|
||||
an_push_error(analyzer, callee->token,
|
||||
"failed to allocate space for method name");
|
||||
@@ -61,10 +61,7 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
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};
|
||||
fzl_node_t *callee = call_node->call.callee;
|
||||
|
||||
bool is_method = false;
|
||||
char *callee_name = resolve_callee(analyzer, callee, &is_method);
|
||||
@@ -73,11 +70,46 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
an_push_error(analyzer, callee->token, "expression is not callable");
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (strcmp(callee_name, "printline") == 0 || strcmp(callee_name, "exit") == 0)
|
||||
{
|
||||
if (call_node->call.args_count != 1)
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_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);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.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--)
|
||||
@@ -91,6 +123,13 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
snprintf(call_problem, sizeof(call_problem), "cannot resolve symbol '%s'",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
@@ -99,6 +138,13 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
snprintf(call_problem, sizeof(call_problem), "'%s' is not callable",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
@@ -108,6 +154,13 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
"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);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
@@ -122,6 +175,13 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
an_typetostr(req.type, req.classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
@@ -129,5 +189,11 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
fzl_type_t type = an_toktotype(sym.node->fn.type);
|
||||
char *classname = sym.classname;
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
|
||||
}
|
||||
|
||||
@@ -1,8 +1,73 @@
|
||||
// TODO:
|
||||
// 1. check for manual `this` declaration
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char class_problem[256];
|
||||
|
||||
// Analyze class declaration
|
||||
fzl_an_res_t analyze_class(fzl_node_t *class_node) {}
|
||||
fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!class_node || class_node->type != FZL_NODETYPE_CLASS)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof CLASS");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *classname = (char *)class_node->class.name->value;
|
||||
fzl_an_ctx_t *ctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_CLASS);
|
||||
ctx->classname = classname;
|
||||
|
||||
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], classname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
fzl_symtable_res_t res =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], class_node,
|
||||
classname, FZL_TYPE_NULL, 0);
|
||||
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(class_problem, sizeof(class_problem),
|
||||
"name '%s' already has been declared", classname);
|
||||
an_push_error(analyzer, class_node->token, class_problem);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(class_problem, sizeof(class_problem), "cannot store name '%s'",
|
||||
classname);
|
||||
an_push_error(analyzer, class_node->token, class_problem);
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
bool error = false;
|
||||
for (size_t i = 0; i < class_node->class.fields_count; i++)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, class_node->class.fields[i]);
|
||||
if (res.error)
|
||||
error = true;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < class_node->class.methods_count; i++)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, class_node->class.methods[i]);
|
||||
if (res.error)
|
||||
error = true;
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include "compiler/fzl_token.h"
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
@@ -11,6 +11,39 @@ fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node)
|
||||
fzl_an_res_t res = {.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
switch (node->type)
|
||||
{
|
||||
case FZL_NODETYPE_ADD:
|
||||
case FZL_NODETYPE_SUB:
|
||||
case FZL_NODETYPE_MUL:
|
||||
case FZL_NODETYPE_DIV:
|
||||
case FZL_NODETYPE_POW:
|
||||
case FZL_NODETYPE_MOD:
|
||||
res = analyze_binmath(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BREAK:
|
||||
res = analyze_break(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LOOP:
|
||||
res = analyze_loop(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONSTRUCT:
|
||||
res = analyze_construct(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_DECLARATION:
|
||||
res = analyze_declaration(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_FIELD:
|
||||
res = analyze_field(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CLASS:
|
||||
res = analyze_class(analyzer, node);
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_RETURN:
|
||||
res = analyze_return(analyzer, node);
|
||||
break;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char c_problem[256];
|
||||
|
||||
// Analyze class construct
|
||||
fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!construct || construct->type != FZL_NODETYPE_CONSTRUCT)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof CONSTRUCT");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *classname = (char *)construct->construct.name->token->value;
|
||||
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], classname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
{
|
||||
snprintf(c_problem, sizeof(c_problem), "class '%s' not found", classname);
|
||||
an_push_error(analyzer, construct->token, c_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_OBJECT, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#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 <stdio.h>
|
||||
|
||||
char dec_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
// Analyze variable/field/argument declaration
|
||||
fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!decl || decl->type != FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof DECLARATION");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *name = (char *)decl->var.identifier->token->value;
|
||||
fzl_type_t type = an_toktotype(decl->var.type);
|
||||
char *classname = NULL;
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
classname = decl->var.type->value;
|
||||
|
||||
fzl_symtable_res_t res =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 1], decl, name,
|
||||
an_toktotype(decl->var.type), 0);
|
||||
if (res != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (res == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
snprintf(dec_problem, sizeof(dec_problem),
|
||||
"'%s' already has been declared", name);
|
||||
else
|
||||
snprintf(dec_problem, sizeof(dec_problem), "cannot store '%s'", name);
|
||||
|
||||
an_push_error(analyzer, decl->token, dec_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
fzl_sym_t class;
|
||||
bool class_found = false;
|
||||
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--)
|
||||
{
|
||||
if ((class_found = fzl_symt_get(analyzer->symbols[i],
|
||||
decl->var.type->value, &class)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!class_found || class.node->type != FZL_NODETYPE_CLASS)
|
||||
{
|
||||
snprintf(dec_problem, sizeof(dec_problem), "class '%s' does not exists",
|
||||
(char *)decl->var.type->value);
|
||||
an_push_error(analyzer, decl->var.type, dec_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_symt_setobjclass(analyzer->symbols[analyzer->symbols_top - 1], name,
|
||||
classname);
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char field_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
// Analyze field access
|
||||
fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
|
||||
{
|
||||
if (analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!field || field->type != FZL_NODETYPE_FIELD)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof FIELD");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t parent = analyze_common(analyzer, field->memb.parent);
|
||||
if (parent.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (parent.type != FZL_TYPE_OBJECT || parent.classname == NULL)
|
||||
{
|
||||
snprintf(field_problem, sizeof(field_problem),
|
||||
"access to non-object value");
|
||||
an_push_error(analyzer, field->token, field_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *field_name = field->memb.field->token->value;
|
||||
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], parent.classname, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (sym_found)
|
||||
{
|
||||
snprintf(field_problem, sizeof(field_problem), "class '%s' not found",
|
||||
parent.classname);
|
||||
an_push_error(analyzer, field->parent->token, field_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *field_node = NULL;
|
||||
for (size_t i = 0; i < sym.node->class.fields_count; i++)
|
||||
{
|
||||
if (strcmp(sym.node->class.fields[i]->var.identifier->token->value,
|
||||
field_name) == 0)
|
||||
{
|
||||
field_node = sym.node->class.fields[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (field_node == NULL)
|
||||
{
|
||||
snprintf(field_problem, sizeof(field_problem),
|
||||
"field '%s.%s' does not exists", parent.classname, field_name);
|
||||
an_push_error(analyzer, field->memb.field->token, field_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_type_t field_type = an_toktotype(field_node->var.type);
|
||||
char *classname = NULL;
|
||||
if (field_type == FZL_TYPE_OBJECT)
|
||||
classname = field_node->var.type->value;
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = field_type, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char fn_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!fn_node || fn_node->type != FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof FUNCTION");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *fnctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_FUNCTION);
|
||||
fnctx->fn = fn_node;
|
||||
|
||||
fzl_an_ctx_t *ctx = analyzer->ctx;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
if (ctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
fzl_type_t rettype = an_toktotype(fn_node->fn.type);
|
||||
char *classname = NULL;
|
||||
if (rettype == FZL_TYPE_OBJECT)
|
||||
classname = (char *)fn_node->fn.type->value;
|
||||
|
||||
if (ctx != NULL && ctx->type == FZL_AN_CTX_TYPE_CLASS)
|
||||
{
|
||||
for (size_t i = 0; i < fn_node->fn.args_count; i++)
|
||||
{
|
||||
fzl_node_t *decl = fn_node->fn.args[i];
|
||||
if (decl->type != FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
an_push_error(analyzer, decl->token, "argument must be a declaration");
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
char *argname = (char *)decl->var.identifier->token->value;
|
||||
if (strcmp(argname, "this") == 0 && i > 0)
|
||||
{
|
||||
an_push_error(analyzer, decl->token,
|
||||
"manual declaration of 'this' is not allowed");
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
size_t classname_len = strlen(ctx->classname);
|
||||
size_t methodname_len = strlen((char *)fn_node->fn.name->value);
|
||||
char *methodname = malloc(classname_len + methodname_len + 2);
|
||||
|
||||
strcpy(methodname, ctx->classname);
|
||||
methodname[classname_len] = '_';
|
||||
strcpy(methodname + classname_len + 1, fn_node->fn.name->value);
|
||||
methodname[classname_len + methodname_len + 1] = 0;
|
||||
|
||||
analyzer->alloc_names[analyzer->alloc_names_top++] = methodname;
|
||||
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], fn_node,
|
||||
methodname, rettype, 0);
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(fn_problem, sizeof(fn_problem),
|
||||
"method '%s.%s' already has been declared", classname,
|
||||
(char *)fn_node->fn.name->value);
|
||||
an_push_error(analyzer, fn_node->token, fn_problem);
|
||||
}
|
||||
else
|
||||
an_push_error(analyzer, fn_node->token, "failed to store method");
|
||||
|
||||
free(analyzer->alloc_names[--analyzer->alloc_names_top]);
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
fzl_symt_setobjclass(analyzer->symbols[analyzer->symbols_top - 1],
|
||||
methodname, classname);
|
||||
}
|
||||
else
|
||||
{
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 1], fn_node,
|
||||
(char *)fn_node->fn.name->value, rettype, 0);
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
snprintf(fn_problem, sizeof(fn_problem),
|
||||
"function '%s' already has been declared",
|
||||
(char *)fn_node->fn.name->value);
|
||||
an_push_error(analyzer, fn_node->token, fn_problem);
|
||||
}
|
||||
else
|
||||
an_push_error(analyzer, fn_node->token, "failed to store function");
|
||||
|
||||
free(analyzer->alloc_names[--analyzer->alloc_names_top]);
|
||||
an_fin_ctx(analyzer);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
fzl_an_res_t body_res = analyze_common(analyzer, fn_node->fn.body);
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = body_res.error, .type = rettype, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,460 @@
|
||||
#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 <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
char loop_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern fzl_type_t an_toktotype(fzl_token_t *tok);
|
||||
|
||||
// Init block analysis result
|
||||
typedef struct
|
||||
{
|
||||
bool has_var;
|
||||
char *name;
|
||||
fzl_type_t type;
|
||||
char *classname;
|
||||
} init_analysis;
|
||||
|
||||
// Check init block
|
||||
init_analysis check_init(fzl_analyzer_t *analyzer, fzl_node_t *init)
|
||||
{
|
||||
if (!analyzer || !init)
|
||||
return (init_analysis){.has_var = false};
|
||||
|
||||
if (init->type == FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
char *name = init->token->value;
|
||||
|
||||
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], name, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
return (init_analysis){.has_var = false};
|
||||
|
||||
char *classname = NULL;
|
||||
if (sym.type == FZL_TYPE_OBJECT)
|
||||
classname = sym.classname;
|
||||
|
||||
return (init_analysis){.has_var = true,
|
||||
.name = name,
|
||||
.type = sym.type,
|
||||
.classname = classname};
|
||||
}
|
||||
else if (init->type == FZL_NODETYPE_DECLARATION)
|
||||
{
|
||||
char *name = init->var.identifier->token->value;
|
||||
fzl_type_t type = an_toktotype(init->var.type);
|
||||
char *classname = NULL;
|
||||
if (type == FZL_TYPE_OBJECT)
|
||||
classname = init->var.type->value;
|
||||
|
||||
return (init_analysis){
|
||||
.has_var = true, .name = name, .type = type, .classname = classname};
|
||||
}
|
||||
else if (init->type == FZL_NODETYPE_ASSIGN)
|
||||
{
|
||||
return (init_analysis)check_init(analyzer, init->binary_op.left);
|
||||
}
|
||||
|
||||
return (init_analysis){.has_var = false};
|
||||
}
|
||||
|
||||
// Condition block analysis result type
|
||||
typedef enum
|
||||
{
|
||||
COND_TYPE_CONST_TRUE, // `true` is used as condition
|
||||
COND_TYPE_CONST_FALSE, // `false` is used as condition
|
||||
COND_TYPE_VAR_LESS, // until variable value is less
|
||||
COND_TYPE_VAR_GREATER, // until variable value is greater
|
||||
COND_TYPE_UNKNOWN, // cannot determine in compilation time
|
||||
} cond_type;
|
||||
|
||||
// Condition block analysis result
|
||||
typedef struct
|
||||
{
|
||||
cond_type type;
|
||||
bool result_bool;
|
||||
char *varname;
|
||||
fzl_type_t vartype;
|
||||
char *classname;
|
||||
} cond_analysis;
|
||||
|
||||
// Check condition block
|
||||
cond_analysis check_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond,
|
||||
bool report)
|
||||
{
|
||||
if (!cond)
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||
|
||||
switch (cond->type)
|
||||
{
|
||||
case FZL_NODETYPE_LITERAL:
|
||||
if (cond->token->type == FZL_TOKTYPE_STR_LITERAL)
|
||||
{
|
||||
if (report)
|
||||
an_push_warning(analyzer, cond->token,
|
||||
"string literal is used as condition");
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||
}
|
||||
else if (cond->token->type == FZL_TOKTYPE_NULL_LITERAL)
|
||||
{
|
||||
if (report)
|
||||
an_push_warning(analyzer, cond->token,
|
||||
"null literal is used as condition");
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||
}
|
||||
else if (cond->token->type == FZL_TOKTYPE_BOOL_LITERAL)
|
||||
{
|
||||
if (strcmp(cond->token->value, "true") == 0)
|
||||
return (cond_analysis){.type = COND_TYPE_CONST_TRUE,
|
||||
.result_bool = true};
|
||||
else
|
||||
return (cond_analysis){.type = COND_TYPE_CONST_FALSE,
|
||||
.result_bool = true};
|
||||
}
|
||||
else if (cond->token->type == FZL_TOKTYPE_NUM_LITERAL)
|
||||
{
|
||||
if (strcmp(cond->token->value, "0") == 0)
|
||||
return (cond_analysis){.type = COND_TYPE_CONST_FALSE,
|
||||
.result_bool = false};
|
||||
else
|
||||
return (cond_analysis){.type = COND_TYPE_CONST_TRUE,
|
||||
.result_bool = false};
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LESS:
|
||||
case FZL_NODETYPE_LESS_EQUAL:
|
||||
{
|
||||
fzl_node_t *comp_left = cond->binary_op.left;
|
||||
cond_analysis left_check = check_condition(analyzer, comp_left, false);
|
||||
if (left_check.varname != NULL)
|
||||
return (cond_analysis){.type = COND_TYPE_VAR_LESS,
|
||||
.result_bool = true,
|
||||
.varname = left_check.varname,
|
||||
.vartype = left_check.vartype,
|
||||
.classname = left_check.classname};
|
||||
|
||||
fzl_node_t *comp_right = cond->binary_op.right;
|
||||
cond_analysis right_check = check_condition(analyzer, comp_right, false);
|
||||
if (right_check.varname != NULL)
|
||||
return (cond_analysis){.type = COND_TYPE_VAR_GREATER,
|
||||
.result_bool = true,
|
||||
.varname = right_check.varname,
|
||||
.vartype = right_check.vartype,
|
||||
.classname = right_check.classname};
|
||||
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN};
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_GREATER:
|
||||
case FZL_NODETYPE_GREATER_EQUAL:
|
||||
{
|
||||
fzl_node_t *comp_left = cond->binary_op.left;
|
||||
cond_analysis left_check = check_condition(analyzer, comp_left, false);
|
||||
if (left_check.varname != NULL)
|
||||
return (cond_analysis){.type = COND_TYPE_VAR_GREATER,
|
||||
.result_bool = true,
|
||||
.varname = left_check.varname,
|
||||
.vartype = left_check.vartype,
|
||||
.classname = left_check.classname};
|
||||
|
||||
fzl_node_t *comp_right = cond->binary_op.right;
|
||||
cond_analysis right_check = check_condition(analyzer, comp_right, false);
|
||||
if (right_check.varname != NULL)
|
||||
return (cond_analysis){.type = COND_TYPE_VAR_LESS,
|
||||
.result_bool = true,
|
||||
.varname = right_check.varname,
|
||||
.vartype = right_check.vartype,
|
||||
.classname = right_check.classname};
|
||||
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN};
|
||||
}
|
||||
|
||||
case FZL_NODETYPE_IDENTIFIER:
|
||||
{
|
||||
char *name = cond->token->value;
|
||||
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], name, &sym)))
|
||||
break;
|
||||
}
|
||||
|
||||
if (!sym_found)
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .varname = NULL};
|
||||
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN,
|
||||
.varname = name,
|
||||
.vartype = sym.type,
|
||||
.classname = sym.classname};
|
||||
}
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
fzl_an_res_t custom_res = analyze_common(analyzer, cond);
|
||||
if (custom_res.type == FZL_TYPE_BOOL)
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = true};
|
||||
|
||||
return (cond_analysis){.type = COND_TYPE_UNKNOWN, .result_bool = false};
|
||||
}
|
||||
|
||||
// Step block change direction
|
||||
typedef enum
|
||||
{
|
||||
STEP_DIR_TYPE_INCREASE,
|
||||
STEP_DIR_TYPE_DECREASE,
|
||||
STEP_DIR_TYPE_STALL,
|
||||
STEP_DIR_UNKNOWN,
|
||||
} step_dir_type;
|
||||
|
||||
// Step block analysis
|
||||
typedef struct
|
||||
{
|
||||
bool changing;
|
||||
char *changing_var;
|
||||
step_dir_type dir;
|
||||
} step_analysis;
|
||||
|
||||
// Check step block
|
||||
step_analysis check_step(fzl_node_t *step)
|
||||
{
|
||||
if (!step)
|
||||
return (step_analysis){.changing = false};
|
||||
|
||||
if (step->type == FZL_NODETYPE_ASSIGN)
|
||||
{
|
||||
if (step->binary_op.left->type != FZL_NODETYPE_IDENTIFIER)
|
||||
return (step_analysis){
|
||||
.changing = true, .changing_var = "-", .dir = STEP_DIR_UNKNOWN};
|
||||
|
||||
int dir = 0;
|
||||
fzl_node_t *right = step->binary_op.right;
|
||||
fzl_node_t *num = NULL;
|
||||
char *varname = NULL;
|
||||
if (right->binary_op.left->type != FZL_NODETYPE_IDENTIFIER)
|
||||
{
|
||||
num = right->binary_op.left;
|
||||
if (right->binary_op.right->type == FZL_NODETYPE_IDENTIFIER)
|
||||
varname = right->binary_op.right->token->value;
|
||||
}
|
||||
else
|
||||
{
|
||||
num = right->binary_op.right;
|
||||
if (right->binary_op.left->type == FZL_NODETYPE_IDENTIFIER)
|
||||
varname = right->binary_op.left->token->value;
|
||||
}
|
||||
|
||||
if (right->type == FZL_NODETYPE_ADD || right->type == FZL_NODETYPE_MUL ||
|
||||
right->type == FZL_NODETYPE_POW)
|
||||
dir = 1;
|
||||
else if (right->type == FZL_NODETYPE_SUB ||
|
||||
right->type == FZL_NODETYPE_DIV || right->type == FZL_NODETYPE_MOD)
|
||||
dir = -1;
|
||||
|
||||
if (num && num->type == FZL_NODETYPE_UNARY_MINUS)
|
||||
dir *= -1;
|
||||
|
||||
step_dir_type dirtype = varname == NULL ? STEP_DIR_UNKNOWN
|
||||
: dir > 0 ? STEP_DIR_TYPE_INCREASE
|
||||
: dir < 0 ? STEP_DIR_TYPE_DECREASE
|
||||
: STEP_DIR_TYPE_STALL;
|
||||
|
||||
return (step_analysis){.changing = varname != NULL && dir != 0,
|
||||
.changing_var = varname,
|
||||
.dir = dirtype};
|
||||
}
|
||||
|
||||
return (step_analysis){.changing = false, .dir = STEP_DIR_UNKNOWN};
|
||||
}
|
||||
|
||||
// Body analysis result
|
||||
typedef struct
|
||||
{
|
||||
bool has_exit;
|
||||
} body_analysis;
|
||||
|
||||
body_analysis check_body(fzl_node_t *body, bool nested_loop)
|
||||
{
|
||||
body_analysis result = {.has_exit = false};
|
||||
switch (body->type)
|
||||
{
|
||||
case FZL_NODETYPE_RETURN:
|
||||
result.has_exit = true;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BREAK:
|
||||
if (!nested_loop)
|
||||
result.has_exit = true;
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_LOOP:
|
||||
if (body->loop.body)
|
||||
{
|
||||
body_analysis res = check_body(body->loop.body, true);
|
||||
if (res.has_exit)
|
||||
result.has_exit = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_CONDITIONAL:
|
||||
if (check_body(body->cond.true_block, nested_loop).has_exit)
|
||||
{
|
||||
result.has_exit = true;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case FZL_NODETYPE_BLOCK:
|
||||
for (size_t i = 0; i < body->list.entry_count; i++)
|
||||
{
|
||||
body_analysis res = check_body(body->list.entries[i], nested_loop);
|
||||
if (res.has_exit)
|
||||
{
|
||||
result.has_exit = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Analyze loop
|
||||
fzl_an_res_t analyze_loop(fzl_analyzer_t *analyzer, fzl_node_t *loop_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!loop_node || loop_node->type != FZL_NODETYPE_LOOP)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof LOOP");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_LOOP);
|
||||
|
||||
init_analysis init_data = {
|
||||
.has_var = false, .type = FZL_TYPE_NULL, .name = NULL, .classname = NULL};
|
||||
if (loop_node->loop.init)
|
||||
{
|
||||
fzl_an_res_t init_res = analyze_common(analyzer, loop_node->loop.init);
|
||||
if (init_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
init_data = check_init(analyzer, loop_node->loop.init);
|
||||
}
|
||||
|
||||
cond_analysis cond_data = {.type = COND_TYPE_CONST_TRUE,
|
||||
.varname = NULL,
|
||||
.vartype = FZL_TYPE_NULL,
|
||||
.classname = NULL};
|
||||
if (loop_node->loop.cond)
|
||||
{
|
||||
fzl_an_res_t cond_res = analyze_common(analyzer, loop_node->loop.cond);
|
||||
if (cond_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
cond_data = check_condition(analyzer, loop_node->loop.cond, true);
|
||||
|
||||
if (!cond_data.result_bool && cond_data.type != COND_TYPE_UNKNOWN)
|
||||
an_push_warning(analyzer, loop_node->loop.cond->token,
|
||||
"resulting type is not boolean");
|
||||
|
||||
if (cond_data.type == COND_TYPE_CONST_FALSE)
|
||||
an_push_warning(analyzer, loop_node->loop.cond->token,
|
||||
"loop will never execute");
|
||||
|
||||
if (init_data.name && cond_data.varname &&
|
||||
strcmp(init_data.name, cond_data.varname) != 0)
|
||||
{
|
||||
snprintf(loop_problem, sizeof(loop_problem),
|
||||
"'%s' is used in condition but '%s' is declared. Maybe there is "
|
||||
"mistake?",
|
||||
cond_data.varname, init_data.name);
|
||||
an_push_warning(analyzer, loop_node->loop.cond->token, loop_problem);
|
||||
}
|
||||
}
|
||||
|
||||
step_analysis step_data = {
|
||||
.changing = false, .changing_var = NULL, .dir = STEP_DIR_UNKNOWN};
|
||||
if (loop_node->loop.step)
|
||||
{
|
||||
fzl_an_res_t step_res = analyze_common(analyzer, loop_node->loop.step);
|
||||
if (step_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
step_data = check_step(loop_node->loop.step);
|
||||
|
||||
if (step_data.dir != STEP_DIR_UNKNOWN)
|
||||
{
|
||||
if (step_data.changing_var && cond_data.varname &&
|
||||
strcmp(step_data.changing_var, cond_data.varname) != 0)
|
||||
{
|
||||
snprintf(loop_problem, sizeof(loop_problem),
|
||||
"changing '%s' but condition checking '%s'",
|
||||
step_data.changing_var, cond_data.varname);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, loop_problem);
|
||||
}
|
||||
|
||||
if (cond_data.type == COND_TYPE_VAR_GREATER &&
|
||||
step_data.dir == STEP_DIR_TYPE_INCREASE)
|
||||
{
|
||||
snprintf(loop_problem, sizeof(loop_problem),
|
||||
"'%s' is increasing while condition is 'greater than'",
|
||||
step_data.changing_var);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, loop_problem);
|
||||
}
|
||||
else if (cond_data.type == COND_TYPE_VAR_LESS &&
|
||||
step_data.dir == STEP_DIR_TYPE_DECREASE)
|
||||
{
|
||||
snprintf(loop_problem, sizeof(loop_problem),
|
||||
"'%s' is decreasing while condition is 'less than'",
|
||||
step_data.changing_var);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, loop_problem);
|
||||
}
|
||||
else if ((cond_data.type == COND_TYPE_VAR_LESS ||
|
||||
cond_data.type == COND_TYPE_VAR_GREATER) &&
|
||||
step_data.dir == STEP_DIR_TYPE_STALL)
|
||||
{
|
||||
snprintf(loop_problem, sizeof(loop_problem), "'%s' is stalled",
|
||||
cond_data.varname);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, loop_problem);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
body_analysis body_data = {.has_exit = false};
|
||||
if (cond_data.type != COND_TYPE_CONST_FALSE)
|
||||
{
|
||||
fzl_an_res_t body_res = analyze_common(analyzer, loop_node->loop.body);
|
||||
if (body_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
body_data = check_body(loop_node->loop.body, false);
|
||||
if (cond_data.type == COND_TYPE_CONST_TRUE && !body_data.has_exit)
|
||||
an_push_warning(analyzer, loop_node->token, "possibly infinite loop");
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
@@ -25,14 +25,15 @@ fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type)
|
||||
// Close context and return parent
|
||||
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer)
|
||||
{
|
||||
if (!analyzer || !analyzer->ctx)
|
||||
if (!analyzer || analyzer->ctx == NULL)
|
||||
return NULL;
|
||||
|
||||
fzl_an_ctx_t *current = analyzer->ctx;
|
||||
fzl_an_ctx_t *parent = current->parent;
|
||||
analyzer->ctx = parent;
|
||||
fzl_freesymt(analyzer->symbols[--analyzer->symbols_top]);
|
||||
|
||||
free(current);
|
||||
fzl_freesymt(analyzer->symbols[--analyzer->symbols_top]);
|
||||
|
||||
return parent;
|
||||
}
|
||||
|
||||
@@ -87,6 +87,9 @@ fzl_type_t an_toktotype(fzl_token_t *tok)
|
||||
if (tok->type == FZL_TOKTYPE_NAME)
|
||||
return FZL_TYPE_OBJECT;
|
||||
|
||||
if (strcmp(tok->value, "null") == 0)
|
||||
return FZL_TYPE_NULL;
|
||||
|
||||
if (strcmp((char *)tok->value, "i8") == 0)
|
||||
return FZL_TYPE_INT8;
|
||||
if (strcmp((char *)tok->value, "i16") == 0)
|
||||
|
||||
+30
-12
@@ -3,11 +3,16 @@
|
||||
#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 alast_err[256];
|
||||
char aerrors[256][256];
|
||||
size_t aerrors_top;
|
||||
|
||||
char awarnings[256][256];
|
||||
size_t awarnings_top;
|
||||
|
||||
fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node)
|
||||
{
|
||||
@@ -18,14 +23,19 @@ fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node)
|
||||
if (!analyzer)
|
||||
return NULL;
|
||||
|
||||
analyzer->root = node;
|
||||
analyzer->warnings_top = 0;
|
||||
analyzer->errors_top = 0;
|
||||
analyzer->symbols_top = 0;
|
||||
analyzer->root = node;
|
||||
analyzer->warnings_top = 0;
|
||||
analyzer->errors_top = 0;
|
||||
analyzer->symbols_top = 0;
|
||||
analyzer->alloc_names_top = 0;
|
||||
|
||||
memset(analyzer->errors, 0, sizeof(analyzer->errors));
|
||||
memset(analyzer->warnings, 0, sizeof(analyzer->warnings));
|
||||
memset(analyzer->symbols, 0, sizeof(analyzer->symbols));
|
||||
memset(analyzer->alloc_names, 0, sizeof(analyzer->alloc_names));
|
||||
|
||||
memset(aerrors, 0, sizeof(aerrors));
|
||||
memset(awarnings, 0, sizeof(awarnings));
|
||||
|
||||
return analyzer;
|
||||
}
|
||||
@@ -35,14 +45,15 @@ bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error)
|
||||
if (analyzer->errors_top >= 256 || !analyzer || !error)
|
||||
return false;
|
||||
|
||||
size_t ei = aerrors_top++;
|
||||
if (tok != NULL)
|
||||
snprintf(alast_error, sizeof(alast_error), "%s:%zu:%zu => %s",
|
||||
snprintf(aerrors[ei], sizeof(aerrors[ei]), "'%s'(%zu:%zu) => %s",
|
||||
tok->filename, tok->ln, tok->cn, error);
|
||||
else
|
||||
snprintf(alast_error, sizeof(alast_error), "<unknown location> => %s",
|
||||
snprintf(aerrors[ei], sizeof(aerrors[ei]), "<unknown location> => %s",
|
||||
error);
|
||||
|
||||
analyzer->errors[analyzer->errors_top++] = alast_error;
|
||||
analyzer->errors[analyzer->errors_top++] = aerrors[ei];
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -51,14 +62,15 @@ bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning)
|
||||
if (analyzer->warnings_top >= 256 || !analyzer || !warning)
|
||||
return false;
|
||||
|
||||
size_t wi = awarnings_top++;
|
||||
if (tok != NULL)
|
||||
snprintf(alast_error, sizeof(alast_error), "%s:%zu:%zu => %s",
|
||||
snprintf(awarnings[wi], sizeof(awarnings[wi]), "'%s'(%zu:%zu) => %s",
|
||||
tok->filename, tok->ln, tok->cn, warning);
|
||||
else
|
||||
snprintf(alast_error, sizeof(alast_error), "<unknown location> => %s",
|
||||
snprintf(awarnings[wi], sizeof(awarnings[wi]), "<unknown location> => %s",
|
||||
warning);
|
||||
|
||||
analyzer->warnings[analyzer->warnings_top++] = alast_error;
|
||||
analyzer->warnings[analyzer->warnings_top++] = awarnings[wi];
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -83,7 +95,8 @@ void fzl_analyze(fzl_analyzer_t *analyzer)
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
for (ssize_t i = analyzer->symbols_top; i > 0; i--)
|
||||
an_fin_ctx(analyzer);
|
||||
}
|
||||
|
||||
void fzl_freeanalyzer(fzl_analyzer_t *analyzer)
|
||||
@@ -91,8 +104,13 @@ void fzl_freeanalyzer(fzl_analyzer_t *analyzer)
|
||||
if (!analyzer)
|
||||
return;
|
||||
|
||||
if (analyzer->alloc_names_top > 0)
|
||||
for (ssize_t i = analyzer->alloc_names_top - 1; i >= 0; i--)
|
||||
free(analyzer->alloc_names[i]);
|
||||
|
||||
memset(analyzer->errors, 0, sizeof(analyzer->errors));
|
||||
memset(analyzer->warnings, 0, sizeof(analyzer->warnings));
|
||||
memset(analyzer->symbols, 0, sizeof(analyzer->symbols));
|
||||
memset(analyzer->alloc_names, 0, sizeof(analyzer->alloc_names));
|
||||
free(analyzer);
|
||||
}
|
||||
|
||||
@@ -513,7 +513,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
|
||||
fzl_type_t fntype = tok_to_type(node->fn.type);
|
||||
fzl_symtable_res_t putres =
|
||||
fzl_symt_put(walker->syms[walker->sym_top - 1],
|
||||
fzl_symt_put(walker->syms[walker->sym_top - 1], node,
|
||||
node->fn.name->value, fntype, walker->op_count);
|
||||
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
@@ -1198,7 +1198,7 @@ fzl_walk_res_t walk(fzl_walker_t *walker, fzl_node_t *node)
|
||||
}
|
||||
|
||||
fzl_symtable_res_t res = fzl_symt_put(
|
||||
walker->syms[walker->sym_top - 1],
|
||||
walker->syms[walker->sym_top - 1], node,
|
||||
node->var.identifier->token->value,
|
||||
ct != NULL ? FZL_TYPE_OBJECT : tok_to_type(node->var.type), 0);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user