7 Commits
Author SHA1 Message Date
samsonium b24cefee96 Merge pull request 'Impl analyzer' (#5) from impl-analyzer into v1.1
Reviewed-on: #5
2026-10-27 12:43:08 +00:00
samsonium 6498c73960 ref: move linked list example into tests folder 2026-10-27 15:42:02 +03:00
samsonium dc80de66e4 fix: review problems 2026-10-27 15:40:25 +03:00
samsonium c61322718d fix: some analysis stage problems 2026-10-27 14:12:09 +03:00
samsonium 411d291a2c feat: add VM protection for classes w/o fields 2026-10-27 12:55:46 +03:00
samsonium aa7a06bd22 feat: finish implementing analyzer 2026-10-27 12:50:58 +03:00
samsonium a263b6206f wip: working on analyzer
- Feat: add assigns analysis
- Fix: change node check condition in binary math analysis
- Feat: add unary and binary conditions analysis
- Fix: add type passtrough in declaration analysis
- Feat: add unary math analysis
2026-10-27 12:21:49 +03:00
29 changed files with 552 additions and 173 deletions
+4
View File
@@ -22,12 +22,14 @@ add_executable(fzc
src/compiler/checkers/fzl_loop_check.c src/compiler/checkers/fzl_loop_check.c
src/compiler/analyzer/utility.c src/compiler/analyzer/utility.c
src/compiler/analyzer/context.c src/compiler/analyzer/context.c
src/compiler/analyzer/an_assign.c
src/compiler/analyzer/an_binmath.c src/compiler/analyzer/an_binmath.c
src/compiler/analyzer/an_block.c src/compiler/analyzer/an_block.c
src/compiler/analyzer/an_break.c src/compiler/analyzer/an_break.c
src/compiler/analyzer/an_call.c src/compiler/analyzer/an_call.c
src/compiler/analyzer/an_class.c src/compiler/analyzer/an_class.c
src/compiler/analyzer/an_common.c src/compiler/analyzer/an_common.c
src/compiler/analyzer/an_condition.c
src/compiler/analyzer/an_conditional.c src/compiler/analyzer/an_conditional.c
src/compiler/analyzer/an_construct.c src/compiler/analyzer/an_construct.c
src/compiler/analyzer/an_declaration.c src/compiler/analyzer/an_declaration.c
@@ -36,6 +38,8 @@ add_executable(fzc
src/compiler/analyzer/an_identifier.c src/compiler/analyzer/an_identifier.c
src/compiler/analyzer/an_loop.c src/compiler/analyzer/an_loop.c
src/compiler/analyzer/an_return.c src/compiler/analyzer/an_return.c
src/compiler/analyzer/an_unarycond.c
src/compiler/analyzer/an_unarymath.c
src/compiler/fzl_analyzer.c src/compiler/fzl_analyzer.c
src/compiler/fzl_debug.c src/compiler/fzl_debug.c
src/compiler/fzl_parser.c src/compiler/fzl_parser.c
+12
View File
@@ -32,6 +32,8 @@ typedef struct fzl_an_ctx
struct fzl_an_ctx *parent; struct fzl_an_ctx *parent;
fzl_an_ctx_type_t type; fzl_an_ctx_type_t type;
fzl_symtable_t *symbols;
size_t symbols_level;
union union
{ {
@@ -74,6 +76,12 @@ void fzl_analyze(fzl_analyzer_t *analyzer);
// Free analyzer // Free analyzer
void fzl_freeanalyzer(fzl_analyzer_t *analyzer); void fzl_freeanalyzer(fzl_analyzer_t *analyzer);
extern char an_problem[256];
bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc);
char *an_typetostr(fzl_type_t type, char *classname);
fzl_type_t an_toktotype(fzl_token_t *tok);
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);
fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node); fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node);
fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id); fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id);
@@ -88,6 +96,10 @@ 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_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_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_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node);
fzl_an_res_t analyze_unarymath(fzl_analyzer_t *analyzer, fzl_node_t *unary);
fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node);
fzl_an_res_t analyze_unarycond(fzl_analyzer_t *analyzer, fzl_node_t *unary);
fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node);
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);
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer); fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer);
+50
View File
@@ -0,0 +1,50 @@
#include <common/fzl_types.h>
#include <compiler/fzl_analyzer.h>
#include <compiler/fzl_parser.h>
#include <stdio.h>
// Analyze assigns
fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node)
{
if (!analyzer)
return (fzl_an_res_t){.error = true};
if (!assign_node || assign_node->type != FZL_NODETYPE_ASSIGN)
{
an_push_error(analyzer, NULL, "node is not assignment");
return (fzl_an_res_t){.error = true};
}
fzl_node_t *left = assign_node->binary_op.left;
fzl_an_res_t leftside = analyze_common(analyzer, left);
if (leftside.error)
return (fzl_an_res_t){.error = true};
fzl_an_res_t rightside =
analyze_common(analyzer, assign_node->binary_op.right);
if (rightside.error)
return (fzl_an_res_t){.error = true};
if (left->type != FZL_NODETYPE_DECLARATION &&
left->type != FZL_NODETYPE_IDENTIFIER && left->type != FZL_NODETYPE_FIELD)
{
an_push_error(analyzer, left->token,
"left side must be one of these: declaration, identifier or "
"field access");
return (fzl_an_res_t){.error = true};
}
if (!an_is_compatible(leftside.type, rightside.type, leftside.classname,
rightside.classname))
{
snprintf(an_problem, sizeof(an_problem),
"incompatible types - '%s' is expected, got '%s'",
an_typetostr(leftside.type, leftside.classname),
an_typetostr(rightside.type, rightside.classname));
an_push_error(analyzer, assign_node->binary_op.right->token, an_problem);
return (fzl_an_res_t){.error = true};
}
return (fzl_an_res_t){
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
}
+16 -13
View File
@@ -1,9 +1,7 @@
#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>
// 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 // Analyze binary arithmetics
fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node) fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node)
@@ -11,13 +9,14 @@ fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node)
if (!analyzer) if (!analyzer)
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
if (!bin_node || bin_node->type != FZL_NODETYPE_ADD || if (!bin_node || (bin_node->type != FZL_NODETYPE_ADD &&
bin_node->type != FZL_NODETYPE_SUB || bin_node->type != FZL_NODETYPE_SUB &&
bin_node->type != FZL_NODETYPE_MUL || bin_node->type != FZL_NODETYPE_MUL &&
bin_node->type != FZL_NODETYPE_DIV || bin_node->type != FZL_NODETYPE_DIV &&
bin_node->type != FZL_NODETYPE_POW || bin_node->type != FZL_NODETYPE_MOD) bin_node->type != FZL_NODETYPE_POW &&
bin_node->type != FZL_NODETYPE_MOD))
{ {
an_push_error(analyzer, NULL, "node is not typeof binary math"); an_push_error(analyzer, NULL, "node is not binary math operator");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -31,15 +30,19 @@ fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node)
if (left.type < FZL_TYPE_INT8) if (left.type < FZL_TYPE_INT8)
{ {
an_push_error(analyzer, bin_node->binary_op.left->token, snprintf(an_problem, sizeof(an_problem),
"left side is not an integer type"); "integer type is expected, got '%s'",
an_typetostr(left.type, left.classname));
an_push_error(analyzer, bin_node->binary_op.left->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
if (right.type < FZL_TYPE_INT8) if (right.type < FZL_TYPE_INT8)
{ {
an_push_error(analyzer, bin_node->binary_op.right->token, snprintf(an_problem, sizeof(an_problem),
"right side is not an integer type"); "integer type is expected, got '%s'",
an_typetostr(right.type, right.classname));
an_push_error(analyzer, bin_node->binary_op.right->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
+12 -15
View File
@@ -5,12 +5,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdio.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 // Analyze block
fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node) fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
{ {
@@ -19,8 +13,8 @@ fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
if (!block_node || block_node->type != FZL_NODETYPE_BLOCK) if (!block_node || block_node->type != FZL_NODETYPE_BLOCK)
{ {
snprintf(block_problem, sizeof(block_problem), "node is not typeof BLOCK"); snprintf(an_problem, sizeof(an_problem), "node is not a block");
an_push_error(analyzer, NULL, block_problem); an_push_error(analyzer, NULL, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -32,7 +26,7 @@ fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
{ {
if (has_return) if (has_return)
an_push_warning(analyzer, block_node->list.entries[i]->token, an_push_warning(analyzer, block_node->list.entries[i]->token,
"this node will never execute"); "this statement will never execute");
fzl_an_res_t entry_res = fzl_an_res_t entry_res =
analyze_common(analyzer, block_node->list.entries[i]); analyze_common(analyzer, block_node->list.entries[i]);
@@ -65,10 +59,11 @@ fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
{ {
if (!has_return || block_node->list.entry_count < 1) if (!has_return || block_node->list.entry_count < 1)
{ {
snprintf(block_problem, sizeof(block_problem), snprintf(an_problem, sizeof(an_problem),
"expected 'return <%s>' in function because return type is %s", "'return <%s>' is expected in the function with non-null "
rettype_str, rettype_str); "return type",
an_push_error(analyzer, block_node->token, block_problem); rettype_str);
an_push_error(analyzer, block_node->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -76,10 +71,12 @@ fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node)
block_node->list.entries[block_node->list.entry_count - 1]; block_node->list.entries[block_node->list.entry_count - 1];
if (last_node->type != FZL_NODETYPE_RETURN) if (last_node->type != FZL_NODETYPE_RETURN)
{ {
snprintf(block_problem, sizeof(block_problem), // NOTE: replace with ghost walker to check if all branch return an
// expected value type
snprintf(an_problem, sizeof(an_problem),
"expected last statement in block to be 'return <%s>'", "expected last statement in block to be 'return <%s>'",
rettype_str); rettype_str);
an_push_error(analyzer, last_node->token, block_problem); an_push_error(analyzer, last_node->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
} }
+4 -2
View File
@@ -10,7 +10,7 @@ fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node)
if (!break_node || break_node->type != FZL_NODETYPE_BREAK) if (!break_node || break_node->type != FZL_NODETYPE_BREAK)
{ {
an_push_error(analyzer, NULL, "node is not typeof BREAK"); an_push_error(analyzer, NULL, "node is not a break statement");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -19,12 +19,14 @@ 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)
{ {
an_push_error(analyzer, break_node->token, an_push_error(analyzer, break_node->token,
"cannot use 'break' outside loop"); "cannot use 'break' outside the loop");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
+71 -32
View File
@@ -10,7 +10,8 @@
#include <string.h> #include <string.h>
char *tmp_method_name = NULL; char *tmp_method_name = NULL;
char call_problem[256] = {0}; char *tmp_fnname = NULL;
char *tmp_classname = NULL;
// Try to get symbol name from callee // Try to get symbol name from callee
char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method) 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; return (char *)callee->token->value;
if (callee->type != FZL_NODETYPE_FIELD) if (callee->type != FZL_NODETYPE_FIELD)
{
an_push_error(analyzer, callee->token, "unknown call format");
return NULL; return NULL;
}
*method = true; *method = true;
fzl_an_res_t parent_res = analyze_common(analyzer, callee->memb.parent); 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; return NULL;
char *fn_name = callee->memb.field->token->value; 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 classname_len = strlen(parent_res.classname);
size_t methodname_len = strlen(fn_name); size_t methodname_len = strlen(fn_name);
tmp_method_name = malloc(classname_len + methodname_len + 2); tmp_method_name = malloc(classname_len + methodname_len + 2);
if (!tmp_method_name) if (!tmp_method_name)
{ {
an_push_error(analyzer, callee->token, an_push_error(analyzer, callee->token, "out of memory");
"failed to allocate space for method name");
return NULL; return NULL;
} }
@@ -47,17 +52,12 @@ char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method)
return tmp_method_name; 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 // Analyze call node
fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *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) 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}; 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}; 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 (strcmp(callee_name, "printline") == 0 || strcmp(callee_name, "exit") == 0)
{ {
if (call_node->call.args_count != 1) 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", "'%s' builtin should be called with 1 argument instead of %zu",
callee_name, call_node->call.args_count); 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) 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}; .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; fzl_sym_t sym;
bool sym_found = false; bool sym_found = false;
for (ssize_t i = analyzer->symbols_top - 1; i >= 0; i--) 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) 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); 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) 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) 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); 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) if (tmp_method_name != NULL)
{ {
@@ -148,12 +145,24 @@ 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};
} }
if (sym.node->fn.args_count != 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), if (is_method)
"expected %zu argument(s) while %zu is specified", {
sym.node->fn.args_count, call_node->call.args_count); snprintf(an_problem, sizeof(an_problem),
an_push_error(analyzer, call_node->token, call_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) if (tmp_method_name != NULL)
{ {
@@ -164,17 +173,47 @@ 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++) for (size_t i = offset; 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_an_res_t got = analyze_common(analyzer, call_node->call.args[i]); fzl_type_t req_type = an_toktotype(arg->var.type);
if (!an_is_compatible(req.type, got.type, req.classname, got.classname)) 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 - offset]);
if (got.error)
{ {
snprintf(call_problem, sizeof(call_problem), if (tmp_method_name != NULL)
"expect argument %zu to be %s, got '%s'", i + 1, {
an_typetostr(req.type, req.classname), 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))
{
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)); an_typetostr(got.type, got.classname));
an_push_error(analyzer, call_node->token, call_problem); }
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) if (tmp_method_name != NULL)
{ {
+5 -7
View File
@@ -4,8 +4,6 @@
#include <compiler/utils/fzl_symtable.h> #include <compiler/utils/fzl_symtable.h>
#include <stdio.h> #include <stdio.h>
char class_problem[256];
// Analyze class declaration // Analyze class declaration
fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node) fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node)
{ {
@@ -14,7 +12,7 @@ fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node)
if (!class_node || class_node->type != FZL_NODETYPE_CLASS) if (!class_node || class_node->type != FZL_NODETYPE_CLASS)
{ {
an_push_error(analyzer, NULL, "node is not typeof CLASS"); an_push_error(analyzer, NULL, "node is not class");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -38,15 +36,15 @@ fzl_an_res_t analyze_class(fzl_analyzer_t *analyzer, fzl_node_t *class_node)
{ {
if (res == FZL_SYMTABLE_RESULT_EXISTS) if (res == FZL_SYMTABLE_RESULT_EXISTS)
{ {
snprintf(class_problem, sizeof(class_problem), snprintf(an_problem, sizeof(an_problem),
"name '%s' already has been declared", classname); "name '%s' already has been declared", classname);
an_push_error(analyzer, class_node->token, class_problem); an_push_error(analyzer, class_node->token, an_problem);
} }
else else
{ {
snprintf(class_problem, sizeof(class_problem), "cannot store name '%s'", snprintf(an_problem, sizeof(an_problem), "cannot store name '%s'",
classname); classname);
an_push_error(analyzer, class_node->token, class_problem); an_push_error(analyzer, class_node->token, an_problem);
} }
an_fin_ctx(analyzer); an_fin_ctx(analyzer);
+27
View File
@@ -8,9 +8,36 @@
// 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)
{ {
case FZL_NODETYPE_ASSIGN:
res = analyze_assign(analyzer, node);
break;
case FZL_NODETYPE_NOT:
res = analyze_unarycond(analyzer, node);
break;
case FZL_NODETYPE_OR:
case FZL_NODETYPE_AND:
case FZL_NODETYPE_LESS:
case FZL_NODETYPE_LESS_EQUAL:
case FZL_NODETYPE_GREATER:
case FZL_NODETYPE_GREATER_EQUAL:
case FZL_NODETYPE_EQUAL:
case FZL_NODETYPE_NEQUAL:
res = analyze_condition(analyzer, node);
break;
case FZL_NODETYPE_UNARY_MINUS:
case FZL_NODETYPE_UNARY_PLUS:
res = analyze_unarymath(analyzer, node);
break;
case FZL_NODETYPE_ADD: case FZL_NODETYPE_ADD:
case FZL_NODETYPE_SUB: case FZL_NODETYPE_SUB:
case FZL_NODETYPE_MUL: case FZL_NODETYPE_MUL:
+114
View File
@@ -0,0 +1,114 @@
#include <common/fzl_types.h>
#include <compiler/fzl_analyzer.h>
#include <compiler/fzl_parser.h>
#include <stdio.h>
// Analyze binary conditional nodes
fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node)
{
if (!analyzer)
return (fzl_an_res_t){.error = true};
if (!cond_node || (cond_node->type != FZL_NODETYPE_OR &&
cond_node->type != FZL_NODETYPE_AND &&
cond_node->type != FZL_NODETYPE_LESS &&
cond_node->type != FZL_NODETYPE_LESS_EQUAL &&
cond_node->type != FZL_NODETYPE_GREATER &&
cond_node->type != FZL_NODETYPE_GREATER_EQUAL &&
cond_node->type != FZL_NODETYPE_EQUAL &&
cond_node->type != FZL_NODETYPE_NEQUAL))
{
an_push_error(analyzer, NULL, "node is not a binary conditional operator");
return (fzl_an_res_t){.error = true};
}
fzl_an_res_t left = analyze_common(analyzer, cond_node->binary_op.left);
if (left.error)
return (fzl_an_res_t){.error = true};
fzl_an_res_t right = analyze_common(analyzer, cond_node->binary_op.right);
if (right.error)
return (fzl_an_res_t){.error = true};
if (cond_node->type == FZL_NODETYPE_OR || cond_node->type == FZL_NODETYPE_AND)
{
if (left.type != FZL_TYPE_BOOL)
an_push_warning(analyzer, cond_node->binary_op.left->token,
"left operand is not resulting with boolean type");
if (right.type != FZL_TYPE_BOOL)
an_push_warning(analyzer, cond_node->binary_op.right->token,
"right operand is not resulting with boolean type");
}
else if (cond_node->type == FZL_NODETYPE_LESS ||
cond_node->type == FZL_NODETYPE_LESS_EQUAL ||
cond_node->type == FZL_NODETYPE_GREATER ||
cond_node->type == FZL_NODETYPE_GREATER_EQUAL)
{
if (left.type < FZL_TYPE_INT8)
{
an_push_error(analyzer, cond_node->binary_op.left->token,
"left operand must have integer type");
return (fzl_an_res_t){.error = true};
}
if (left.type < FZL_TYPE_INT8)
{
an_push_error(analyzer, cond_node->binary_op.left->token,
"right operand must have integer type");
return (fzl_an_res_t){.error = true};
}
}
else if (left.type >= FZL_TYPE_INT8)
{
if (right.type < FZL_TYPE_INT8)
{
an_push_error(analyzer, cond_node->binary_op.right->token,
"right operand must have integer type");
return (fzl_an_res_t){.error = true};
}
return (fzl_an_res_t){
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
}
else if (left.type == FZL_TYPE_OBJECT)
{
if (!left.classname || !right.classname)
{
an_push_error(analyzer, cond_node->binary_op.left->token,
"unhandled error :(");
return (fzl_an_res_t){.error = true};
}
if (!an_is_compatible(left.type, right.type, left.classname,
right.classname))
{
snprintf(an_problem, sizeof(an_problem), "'%s' and '%s' are incompatible",
an_typetostr(left.type, left.classname),
an_typetostr(right.type, right.classname));
an_push_error(analyzer, cond_node->binary_op.right->token, an_problem);
return (fzl_an_res_t){.error = true};
}
}
else if (left.type == FZL_TYPE_STRING)
{
if (right.type != FZL_TYPE_STRING)
{
an_push_error(analyzer, cond_node->binary_op.right->token,
"right operand must be a string");
return (fzl_an_res_t){.error = true};
}
}
else if (!an_is_compatible(left.type, right.type, left.classname,
right.classname))
{
snprintf(an_problem, sizeof(an_problem),
"equality not supported between %s and %s",
an_typetostr(left.type, left.classname),
an_typetostr(right.type, right.classname));
return (fzl_an_res_t){.error = true};
}
return (fzl_an_res_t){
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
}
+11 -7
View File
@@ -4,8 +4,6 @@
#include <compiler/fzl_token.h> #include <compiler/fzl_token.h>
#include <stdio.h> #include <stdio.h>
char cond_problem[256];
// Analyze conditional statement // Analyze conditional statement
fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond) fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond)
{ {
@@ -14,15 +12,21 @@ fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond)
if (!cond || cond->type != FZL_NODETYPE_CONDITIONAL) if (!cond || cond->type != FZL_NODETYPE_CONDITIONAL)
{ {
an_push_error(analyzer, NULL, "provided node is not typeof CONDITIONAL"); an_push_error(analyzer, NULL, "provided node is a condition");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
fzl_an_res_t cond_res = analyze_common(analyzer, cond->cond.condition); fzl_an_res_t cond_res = analyze_common(analyzer, cond->cond.condition);
fzl_an_res_t true_res = analyze_common(analyzer, cond->cond.true_block); fzl_an_res_t true_res = analyze_common(analyzer, cond->cond.true_block);
fzl_an_res_t else_res = analyze_common(analyzer, cond->cond.else_block);
if (cond_res.error || true_res.error || else_res.error) if (cond->cond.else_block)
{
fzl_an_res_t else_res = analyze_common(analyzer, cond->cond.else_block);
if (else_res.error)
return (fzl_an_res_t){.error = true};
}
if (cond_res.error || true_res.error)
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
if (cond_res.type != FZL_TYPE_BOOL) if (cond_res.type != FZL_TYPE_BOOL)
@@ -31,9 +35,9 @@ fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond)
if (cond->cond.condition->token->type == FZL_TOKTYPE_BOOL_LITERAL) if (cond->cond.condition->token->type == FZL_TOKTYPE_BOOL_LITERAL)
{ {
snprintf(cond_problem, sizeof(cond_problem), "condition is always %s", snprintf(an_problem, sizeof(an_problem), "condition is always %s",
(char *)cond->cond.condition->token->value); (char *)cond->cond.condition->token->value);
an_push_warning(analyzer, cond->cond.condition->token, cond_problem); an_push_warning(analyzer, cond->cond.condition->token, an_problem);
} }
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL}; return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
+3 -5
View File
@@ -5,8 +5,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
char c_problem[256];
// Analyze class construct // Analyze class construct
fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct) fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct)
{ {
@@ -15,7 +13,7 @@ fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct)
if (!construct || construct->type != FZL_NODETYPE_CONSTRUCT) if (!construct || construct->type != FZL_NODETYPE_CONSTRUCT)
{ {
an_push_error(analyzer, NULL, "node is not typeof CONSTRUCT"); an_push_error(analyzer, NULL, "node is not a construct");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -30,8 +28,8 @@ fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct)
if (!sym_found) if (!sym_found)
{ {
snprintf(c_problem, sizeof(c_problem), "class '%s' not found", classname); snprintf(an_problem, sizeof(an_problem), "class '%s' not found", classname);
an_push_error(analyzer, construct->token, c_problem); an_push_error(analyzer, construct->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
+8 -13
View File
@@ -6,11 +6,6 @@
#include <stdbool.h> #include <stdbool.h>
#include <stdio.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 // Analyze variable/field/argument declaration
fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl) fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
{ {
@@ -19,7 +14,7 @@ fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
if (!decl || decl->type != FZL_NODETYPE_DECLARATION) if (!decl || decl->type != FZL_NODETYPE_DECLARATION)
{ {
an_push_error(analyzer, NULL, "node is not typeof DECLARATION"); an_push_error(analyzer, NULL, "node is not a declaration");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -35,12 +30,12 @@ fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
if (res != FZL_SYMTABLE_RESULT_OK) if (res != FZL_SYMTABLE_RESULT_OK)
{ {
if (res == FZL_SYMTABLE_RESULT_EXISTS) if (res == FZL_SYMTABLE_RESULT_EXISTS)
snprintf(dec_problem, sizeof(dec_problem), snprintf(an_problem, sizeof(an_problem), "'%s' already has been declared",
"'%s' already has been declared", name); name);
else else
snprintf(dec_problem, sizeof(dec_problem), "cannot store '%s'", name); snprintf(an_problem, sizeof(an_problem), "cannot store '%s'", name);
an_push_error(analyzer, decl->token, dec_problem); an_push_error(analyzer, decl->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -57,9 +52,9 @@ fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
if (!class_found || class.node->type != FZL_NODETYPE_CLASS) if (!class_found || class.node->type != FZL_NODETYPE_CLASS)
{ {
snprintf(dec_problem, sizeof(dec_problem), "class '%s' does not exists", snprintf(an_problem, sizeof(an_problem), "class '%s' does not exists",
(char *)decl->var.type->value); (char *)decl->var.type->value);
an_push_error(analyzer, decl->var.type, dec_problem); an_push_error(analyzer, decl->var.type, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -67,5 +62,5 @@ fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
classname); classname);
} }
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL}; return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
} }
+11 -16
View File
@@ -7,20 +7,15 @@
#include <stdio.h> #include <stdio.h>
#include <string.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 // Analyze field access
fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field) fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
{ {
if (analyzer) if (!analyzer)
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
if (!field || field->type != FZL_NODETYPE_FIELD) if (!field || field->type != FZL_NODETYPE_FIELD)
{ {
an_push_error(analyzer, NULL, "node is not typeof FIELD"); an_push_error(analyzer, NULL, "node is not field access");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -30,9 +25,9 @@ fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
if (parent.type != FZL_TYPE_OBJECT || parent.classname == NULL) if (parent.type != FZL_TYPE_OBJECT || parent.classname == NULL)
{ {
snprintf(field_problem, sizeof(field_problem), snprintf(an_problem, sizeof(an_problem), "access to non-object value (%s)",
"access to non-object value"); an_typetostr(parent.type, parent.classname));
an_push_error(analyzer, field->token, field_problem); an_push_error(analyzer, field->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -46,11 +41,11 @@ fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
break; break;
} }
if (sym_found) if (!sym_found)
{ {
snprintf(field_problem, sizeof(field_problem), "class '%s' not found", snprintf(an_problem, sizeof(an_problem), "class '%s' not found",
parent.classname); parent.classname);
an_push_error(analyzer, field->parent->token, field_problem); an_push_error(analyzer, field->parent->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -67,9 +62,9 @@ fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field)
if (field_node == NULL) if (field_node == NULL)
{ {
snprintf(field_problem, sizeof(field_problem), snprintf(an_problem, sizeof(an_problem), "field '%s.%s' does not exists",
"field '%s.%s' does not exists", parent.classname, field_name); parent.classname, field_name);
an_push_error(analyzer, field->memb.field->token, field_problem); an_push_error(analyzer, field->memb.field->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
+17 -15
View File
@@ -7,11 +7,6 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.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) fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
{ {
if (!analyzer) if (!analyzer)
@@ -19,13 +14,20 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
if (!fn_node || fn_node->type != FZL_NODETYPE_FUNCTION) if (!fn_node || fn_node->type != FZL_NODETYPE_FUNCTION)
{ {
an_push_error(analyzer, NULL, "node is not typeof FUNCTION"); an_push_error(analyzer, NULL, "node is not a function");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
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)
{ {
@@ -56,7 +58,7 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
if (strcmp(argname, "this") == 0 && i > 0) if (strcmp(argname, "this") == 0 && i > 0)
{ {
an_push_error(analyzer, decl->token, an_push_error(analyzer, decl->token,
"manual declaration of 'this' is not allowed"); "manual declaration of 'this' is not allowed in methods");
an_fin_ctx(analyzer); an_fin_ctx(analyzer);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -74,16 +76,16 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
analyzer->alloc_names[analyzer->alloc_names_top++] = methodname; analyzer->alloc_names[analyzer->alloc_names_top++] = methodname;
fzl_symtable_res_t putres = fzl_symtable_res_t putres =
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], fn_node, fzl_symt_put(analyzer->symbols[ctx->symbols_level - 1], fn_node,
methodname, rettype, 0); methodname, rettype, 0);
if (putres != FZL_SYMTABLE_RESULT_OK) if (putres != FZL_SYMTABLE_RESULT_OK)
{ {
if (putres == FZL_SYMTABLE_RESULT_EXISTS) if (putres == FZL_SYMTABLE_RESULT_EXISTS)
{ {
snprintf(fn_problem, sizeof(fn_problem), snprintf(an_problem, sizeof(an_problem),
"method '%s.%s' already has been declared", classname, "method '%s.%s' already has been declared", classname,
(char *)fn_node->fn.name->value); (char *)fn_node->fn.name->value);
an_push_error(analyzer, fn_node->token, fn_problem); an_push_error(analyzer, fn_node->token, an_problem);
} }
else else
an_push_error(analyzer, fn_node->token, "failed to store method"); an_push_error(analyzer, fn_node->token, "failed to store method");
@@ -92,22 +94,22 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
an_fin_ctx(analyzer); an_fin_ctx(analyzer);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
fzl_symt_setobjclass(analyzer->symbols[analyzer->symbols_top - 1], fzl_symt_setobjclass(analyzer->symbols[ctx->symbols_level - 1], methodname,
methodname, classname); classname);
} }
else else
{ {
fzl_symtable_res_t putres = fzl_symtable_res_t putres =
fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 1], fn_node, fzl_symt_put(analyzer->symbols[analyzer->symbols_top - 2], fn_node,
(char *)fn_node->fn.name->value, rettype, 0); (char *)fn_node->fn.name->value, rettype, 0);
if (putres != FZL_SYMTABLE_RESULT_OK) if (putres != FZL_SYMTABLE_RESULT_OK)
{ {
if (putres == FZL_SYMTABLE_RESULT_EXISTS) if (putres == FZL_SYMTABLE_RESULT_EXISTS)
{ {
snprintf(fn_problem, sizeof(fn_problem), snprintf(an_problem, sizeof(an_problem),
"function '%s' already has been declared", "function '%s' already has been declared",
(char *)fn_node->fn.name->value); (char *)fn_node->fn.name->value);
an_push_error(analyzer, fn_node->token, fn_problem); an_push_error(analyzer, fn_node->token, an_problem);
} }
else else
an_push_error(analyzer, fn_node->token, "failed to store function"); an_push_error(analyzer, fn_node->token, "failed to store function");
+3 -5
View File
@@ -3,8 +3,6 @@
#include <compiler/utils/fzl_symtable.h> #include <compiler/utils/fzl_symtable.h>
#include <stdio.h> #include <stdio.h>
char id_problem[256];
// Analyze identifier // Analyze identifier
fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id) fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id)
{ {
@@ -13,7 +11,7 @@ fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id)
if (!id || id->type != FZL_NODETYPE_IDENTIFIER) if (!id || id->type != FZL_NODETYPE_IDENTIFIER)
{ {
an_push_error(analyzer, NULL, "provided node is not typeof IDENTIFIER"); an_push_error(analyzer, NULL, "node is not an identifier");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -28,9 +26,9 @@ fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id)
if (!sym_found) if (!sym_found)
{ {
snprintf(id_problem, sizeof(id_problem), "cannot find symbol '%s'", snprintf(an_problem, sizeof(an_problem), "cannot find symbol '%s'",
symname); symname);
an_push_error(analyzer, id->token, id_problem); an_push_error(analyzer, id->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
+10 -15
View File
@@ -7,11 +7,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.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 // Init block analysis result
typedef struct typedef struct
{ {
@@ -386,11 +381,11 @@ fzl_an_res_t analyze_loop(fzl_analyzer_t *analyzer, fzl_node_t *loop_node)
if (init_data.name && cond_data.varname && if (init_data.name && cond_data.varname &&
strcmp(init_data.name, cond_data.varname) != 0) strcmp(init_data.name, cond_data.varname) != 0)
{ {
snprintf(loop_problem, sizeof(loop_problem), snprintf(an_problem, sizeof(an_problem),
"'%s' is used in condition but '%s' is declared. Maybe there is " "'%s' is used in condition but '%s' is declared. Maybe there is "
"mistake?", "mistake?",
cond_data.varname, init_data.name); cond_data.varname, init_data.name);
an_push_warning(analyzer, loop_node->loop.cond->token, loop_problem); an_push_warning(analyzer, loop_node->loop.cond->token, an_problem);
} }
} }
@@ -409,35 +404,35 @@ fzl_an_res_t analyze_loop(fzl_analyzer_t *analyzer, fzl_node_t *loop_node)
if (step_data.changing_var && cond_data.varname && if (step_data.changing_var && cond_data.varname &&
strcmp(step_data.changing_var, cond_data.varname) != 0) strcmp(step_data.changing_var, cond_data.varname) != 0)
{ {
snprintf(loop_problem, sizeof(loop_problem), snprintf(an_problem, sizeof(an_problem),
"changing '%s' but condition checking '%s'", "changing '%s' but condition checking '%s'",
step_data.changing_var, cond_data.varname); step_data.changing_var, cond_data.varname);
an_push_warning(analyzer, loop_node->loop.step->token, loop_problem); an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
} }
if (cond_data.type == COND_TYPE_VAR_GREATER && if (cond_data.type == COND_TYPE_VAR_GREATER &&
step_data.dir == STEP_DIR_TYPE_INCREASE) step_data.dir == STEP_DIR_TYPE_INCREASE)
{ {
snprintf(loop_problem, sizeof(loop_problem), snprintf(an_problem, sizeof(an_problem),
"'%s' is increasing while condition is 'greater than'", "'%s' is increasing while condition is 'greater than'",
step_data.changing_var); step_data.changing_var);
an_push_warning(analyzer, loop_node->loop.step->token, loop_problem); an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
} }
else if (cond_data.type == COND_TYPE_VAR_LESS && else if (cond_data.type == COND_TYPE_VAR_LESS &&
step_data.dir == STEP_DIR_TYPE_DECREASE) step_data.dir == STEP_DIR_TYPE_DECREASE)
{ {
snprintf(loop_problem, sizeof(loop_problem), snprintf(an_problem, sizeof(an_problem),
"'%s' is decreasing while condition is 'less than'", "'%s' is decreasing while condition is 'less than'",
step_data.changing_var); step_data.changing_var);
an_push_warning(analyzer, loop_node->loop.step->token, loop_problem); an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
} }
else if ((cond_data.type == COND_TYPE_VAR_LESS || else if ((cond_data.type == COND_TYPE_VAR_LESS ||
cond_data.type == COND_TYPE_VAR_GREATER) && cond_data.type == COND_TYPE_VAR_GREATER) &&
step_data.dir == STEP_DIR_TYPE_STALL) step_data.dir == STEP_DIR_TYPE_STALL)
{ {
snprintf(loop_problem, sizeof(loop_problem), "'%s' is stalled", snprintf(an_problem, sizeof(an_problem), "'%s' is stalled",
cond_data.varname); cond_data.varname);
an_push_warning(analyzer, loop_node->loop.step->token, loop_problem); an_push_warning(analyzer, loop_node->loop.step->token, an_problem);
} }
} }
} }
+48 -17
View File
@@ -1,16 +1,9 @@
#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 <compiler/fzl_token.h> #include <compiler/fzl_token.h>
#include <stdio.h> #include <stdio.h>
char ret_problem[256];
// defined in `compiler/analyzer/utility.c`
extern bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc);
extern fzl_type_t an_toktotype(fzl_token_t *tok);
extern char *an_typetostr(fzl_type_t type, char *classname);
// Analyze return statement // Analyze return statement
fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
{ {
@@ -19,12 +12,12 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
if (!ret_node || ret_node->type != FZL_NODETYPE_RETURN) if (!ret_node || ret_node->type != FZL_NODETYPE_RETURN)
{ {
an_push_error(analyzer, NULL, "node is not typeof RETURN"); an_push_error(analyzer, NULL, "node is not a return statement");
return (fzl_an_res_t){.error = false}; return (fzl_an_res_t){.error = false};
} }
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,10 +25,22 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
fnctx = fnctx->parent; fnctx = fnctx->parent;
} }
if (!fnctx) fzl_an_ctx_t *classctx = analyzer->ctx;
while (classctx != NULL)
{
if (classctx && classctx->type == FZL_AN_CTX_TYPE_CLASS)
break;
classctx = classctx->parent;
}
bool is_in_method =
classctx != NULL && classctx->type == FZL_AN_CTX_TYPE_CLASS;
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 the function");
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
@@ -49,18 +54,44 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
{ {
if (ret_node->ret.value == NULL) if (ret_node->ret.value == NULL)
{ {
an_push_error(analyzer, ret_node->token, if (is_in_method)
"return must be used with value in this function"); {
snprintf(an_problem, sizeof(an_problem),
"cannot return null as method '%s.%s' have return type '%s'",
classctx->classname, (char *)fnctx->fn->fn.name->value,
rettype_str);
}
else
{
snprintf(an_problem, sizeof(an_problem),
"cannot return null as function '%s' have return type '%s'",
(char *)fnctx->fn->fn.name->value, rettype_str);
}
an_push_error(analyzer, ret_node->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
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), if (is_in_method)
"expected 'return <%s>' instead of 'return <%s>'", rettype_str, {
snprintf(an_problem, sizeof(an_problem),
"method '%s.%s' is expecting return type '%s', got '%s'",
classctx->classname, (char *)fnctx->fn->fn.name->value,
rettype_str, an_typetostr(ret_res.type, ret_res.classname));
}
else
{
snprintf(an_problem, sizeof(an_problem),
"function '%s' is expecting return type '%s', got '%s'",
(char *)fnctx->fn->fn.name->value, rettype_str,
an_typetostr(ret_res.type, ret_res.classname)); an_typetostr(ret_res.type, ret_res.classname));
an_push_error(analyzer, ret_node->token, ret_problem); }
an_push_error(analyzer, ret_node->token, an_problem);
return (fzl_an_res_t){.error = true}; return (fzl_an_res_t){.error = true};
} }
} }
+30
View File
@@ -0,0 +1,30 @@
#include <common/fzl_types.h>
#include <compiler/fzl_analyzer.h>
#include <compiler/fzl_parser.h>
// Analyze unary condition
fzl_an_res_t analyze_unarycond(fzl_analyzer_t *analyzer, fzl_node_t *unary)
{
if (!analyzer)
return (fzl_an_res_t){.error = true};
if (!unary || unary->type != FZL_NODETYPE_NOT)
{
an_push_error(analyzer, NULL, "node is not an unary condition operator");
return (fzl_an_res_t){.error = true};
}
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
if (operand_res.error)
return (fzl_an_res_t){.error = true};
if (operand_res.type != FZL_TYPE_BOOL && operand_res.type < FZL_TYPE_INT8)
{
an_push_error(analyzer, unary->unary_op.operand->token,
"unary condition only support bool and integer types");
return (fzl_an_res_t){.error = true};
}
return (fzl_an_res_t){
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
}
+32
View File
@@ -0,0 +1,32 @@
#include <common/fzl_types.h>
#include <compiler/fzl_analyzer.h>
#include <compiler/fzl_parser.h>
#include <stdbool.h>
// Analyze unary arithmetic
fzl_an_res_t analyze_unarymath(fzl_analyzer_t *analyzer, fzl_node_t *unary)
{
if (!analyzer)
return (fzl_an_res_t){.error = true};
if (!unary || (unary->type != FZL_NODETYPE_UNARY_MINUS &&
unary->type != FZL_NODETYPE_UNARY_PLUS))
{
an_push_error(analyzer, NULL, "node is not an unary math operator");
return (fzl_an_res_t){.error = true};
}
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
if (operand_res.error)
return (fzl_an_res_t){.error = true};
if (operand_res.type < FZL_TYPE_INT8)
{
an_push_error(analyzer, unary->unary_op.operand->token,
"unary operator only support integer types");
return (fzl_an_res_t){.error = true};
}
return (fzl_an_res_t){
.error = false, .type = operand_res.type, .classname = NULL};
}
+8 -1
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,12 +13,18 @@ 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;
ctx->symbols = fzl_mksymt();
ctx->symbols_level = analyzer->symbols_top;
analyzer->ctx = ctx; analyzer->ctx = ctx;
analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt(); analyzer->symbols[analyzer->symbols_top++] = ctx->symbols;
return ctx; return ctx;
} }
+4 -2
View File
@@ -14,6 +14,8 @@ size_t aerrors_top;
char awarnings[256][256]; char awarnings[256][256];
size_t awarnings_top; size_t awarnings_top;
char an_problem[256];
fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node) fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node)
{ {
if (!node) if (!node)
@@ -47,7 +49,7 @@ bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error)
size_t ei = aerrors_top++; size_t ei = aerrors_top++;
if (tok != NULL) if (tok != NULL)
snprintf(aerrors[ei], sizeof(aerrors[ei]), "'%s'(%zu:%zu) => %s", snprintf(aerrors[ei], sizeof(aerrors[ei]), "'%s':%zu:%zu => %s",
tok->filename, tok->ln, tok->cn, error); tok->filename, tok->ln, tok->cn, error);
else else
snprintf(aerrors[ei], sizeof(aerrors[ei]), "<unknown location> => %s", snprintf(aerrors[ei], sizeof(aerrors[ei]), "<unknown location> => %s",
@@ -64,7 +66,7 @@ bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning)
size_t wi = awarnings_top++; size_t wi = awarnings_top++;
if (tok != NULL) if (tok != NULL)
snprintf(awarnings[wi], sizeof(awarnings[wi]), "'%s'(%zu:%zu) => %s", snprintf(awarnings[wi], sizeof(awarnings[wi]), "'%s':%zu:%zu => %s",
tok->filename, tok->ln, tok->cn, warning); tok->filename, tok->ln, tok->cn, warning);
else else
snprintf(awarnings[wi], sizeof(awarnings[wi]), "<unknown location> => %s", snprintf(awarnings[wi], sizeof(awarnings[wi]), "<unknown location> => %s",
+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);
+1 -1
View File
@@ -51,7 +51,7 @@ bool fzl_linked_add_files(fzl_linked_t *l, char **files, size_t files_count)
rewind(fd); rewind(fd);
source = malloc(file_sz + 1); source = malloc(file_sz + 1);
fread(source, file_sz, 1, fd); fread(source, file_sz, 1, fd); // NOTE: add red bytes check
fclose(fd); fclose(fd);
source[file_sz] = 0; source[file_sz] = 0;
+4 -1
View File
@@ -50,7 +50,8 @@ fzl_vm_t *fzl_mkvm(const char *filepath)
} }
char hdr[5]; char hdr[5];
fread(hdr, sizeof(char), 5, fd); fread(hdr, sizeof(char), 5,
fd); // NOTE: add red bytes check (also for freads below)
if (memcmp(hdr, expect_hdr, 5) != 0) if (memcmp(hdr, expect_hdr, 5) != 0)
{ {
snprintf(last_err, sizeof(last_err), "magic check error"); snprintf(last_err, sizeof(last_err), "magic check error");
@@ -430,6 +431,8 @@ int fzl_vm_run(fzl_vm_t *vm, bool allow_infinite, uint64_t max_iter)
{ {
uint64_t values_count = vm->ops[++vm->ip]; uint64_t values_count = vm->ops[++vm->ip];
fzl_vm_object_t *obj = malloc(sizeof(fzl_vm_object_t)); fzl_vm_object_t *obj = malloc(sizeof(fzl_vm_object_t));
if (values_count > 0)
obj->values = malloc(values_count * sizeof(fzl_value_t)); obj->values = malloc(values_count * sizeof(fzl_value_t));
obj->values_count = values_count; obj->values_count = values_count;
memset(obj->values, 0, values_count * sizeof(fzl_value_t)); memset(obj->values, 0, values_count * sizeof(fzl_value_t));
@@ -0,0 +1,9 @@
fn choose(bool flag) int
{
if (flag)
return 1;
return 0;
}
int invalid = "not an integer";
exit(1);
@@ -0,0 +1,12 @@
fn doSomeWork()
{
printline("Some work");
}
fn sum(int a, int b) int
{
return a + b;
}
int result = sum(5, 6);
printline(result);
@@ -0,0 +1,19 @@
class Node
{
Node next;
fn ping()
{
printline("ping");
}
fn pingNext()
{
this.next.ping();
}
}
Node head = new Node;
Node tail = new Node;
head.next = tail;
head.pingNext();