From 4904f400370428d123c6125955d5007d2ee58e56 Mon Sep 17 00:00:00 2001 From: samsonium Date: Sun, 25 Oct 2026 19:24:02 +0300 Subject: [PATCH 01/11] wip: working on analyzer --- CMakeLists.txt | 13 +++---- include/compiler/fzl_analyzer.h | 28 +++++++++++++++ src/compiler/fzl_analyzer.c | 62 +++++++++++++++++++++++++++++++++ src/compiler/main.c | 35 ++++++++++++++++++- 4 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 include/compiler/fzl_analyzer.h create mode 100644 src/compiler/fzl_analyzer.c diff --git a/CMakeLists.txt b/CMakeLists.txt index 425124a..e7a7699 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,18 +14,19 @@ include_directories(include) # Compiler add_executable(fzc - src/compiler/fzl_token.c - src/compiler/fzl_reader.c - src/compiler/fzl_parser.c - src/compiler/fzl_debug.c - src/compiler/fzl_walker.c - src/compiler/fzl_writer.c src/compiler/utils/fzl_const_pool.c src/compiler/utils/fzl_symtable.c src/compiler/utils/fzl_linked.c src/compiler/utils/fzl_options.c src/compiler/utils/fzl_ctable.c src/compiler/checkers/fzl_loop_check.c + src/compiler/fzl_analyzer.c + src/compiler/fzl_debug.c + src/compiler/fzl_parser.c + src/compiler/fzl_reader.c + src/compiler/fzl_token.c + src/compiler/fzl_walker.c + src/compiler/fzl_writer.c src/compiler/main.c) # Virtual machine diff --git a/include/compiler/fzl_analyzer.h b/include/compiler/fzl_analyzer.h new file mode 100644 index 0000000..c196f3a --- /dev/null +++ b/include/compiler/fzl_analyzer.h @@ -0,0 +1,28 @@ +#ifndef FZL_ANALYZER_H +#define FZL_ANALYZER_H + +#include +#include + +// Analyzer descriptor +typedef struct +{ + char *warnings[256]; + size_t warnings_top; + + char *errors[256]; + size_t errors_top; + + fzl_node_t *root; +} fzl_analyzer_t; + +// Allocate analyzer +fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node); + +// Analyze AST +void fzl_analyze(fzl_analyzer_t *analyzer); + +// Free analyzer +void fzl_freeanalyzer(fzl_analyzer_t *analyzer); + +#endif // FZL_ANALYZER_H diff --git a/src/compiler/fzl_analyzer.c b/src/compiler/fzl_analyzer.c new file mode 100644 index 0000000..336f015 --- /dev/null +++ b/src/compiler/fzl_analyzer.c @@ -0,0 +1,62 @@ +#include +#include +#include +#include +#include + +fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node) +{ + if (!node) + return NULL; + + fzl_analyzer_t *analyzer = malloc(sizeof(fzl_analyzer_t)); + if (!analyzer) + return NULL; + + analyzer->root = node; + analyzer->warnings_top = 0; + analyzer->errors_top = 0; + + memset(analyzer->errors, 0, sizeof(analyzer->errors)); + memset(analyzer->warnings, 0, sizeof(analyzer->warnings)); + + return analyzer; +} + +// Push error to errors list +bool push_error(fzl_analyzer_t *analyzer, char *error) +{ + if (analyzer->errors_top >= 256) + return false; + + analyzer->errors[analyzer->errors_top++] = error; + return true; +} + +// Push warning to warnings list +bool push_warning(fzl_analyzer_t *analyzer, char *error) +{ + if (analyzer->warnings_top >= 256) + return false; + + analyzer->warnings[analyzer->warnings_top++] = error; + return true; +} + +void fzl_analyze(fzl_analyzer_t *analyzer) +{ + if (!analyzer) + return; + + // +} + +void fzl_freeanalyzer(fzl_analyzer_t *analyzer) +{ + if (!analyzer) + return; + + memset(analyzer->errors, 0, sizeof(analyzer->errors)); + memset(analyzer->warnings, 0, sizeof(analyzer->warnings)); + free(analyzer); +} diff --git a/src/compiler/main.c b/src/compiler/main.c index dfb486c..638960e 100644 --- a/src/compiler/main.c +++ b/src/compiler/main.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -122,7 +123,39 @@ int main(int argc, char **argv) printf("%sok%s!\n", FZL_COK, FZL_CRST); fzl_debug_print_ast(parser->root); printf("%s> building%s\n", FZL_CDEBUG, FZL_CRST); - printf("%s-> walking AST...%s\n", FZL_CDEBUG, FZL_CRST); + printf("%s-> analyzing AST...%s\n", FZL_CDEBUG, FZL_CRST); + } + + fzl_analyzer_t *analyzer = fzl_mkanalyzer(parser->root); + if (!analyzer) + { + printf("%sError%s: failed to allocate analyzer\n", FZL_CERROR, FZL_CRST); + fzl_freeoptions(opts); + fzl_freeast(parser->root); + fzl_freeparser(parser); + fzl_freereader(rd); + fzl_freelinked(linked); + } + + fzl_analyze(analyzer); + if (analyzer->errors_top > 0) + { + for (ssize_t i = analyzer->errors_top - 1; i >= 0; i--) + printf("%sError%s: %s\n", FZL_CERROR, FZL_CRST, analyzer->errors[i]); + + fzl_freeoptions(opts); + fzl_freeanalyzer(analyzer); + fzl_freeast(parser->root); + fzl_freeparser(parser); + fzl_freereader(rd); + fzl_freelinked(linked); + return -1; + } + + if (analyzer->warnings_top > 0) + { + for (ssize_t i = analyzer->warnings_top - 1; i >= 0; i--) + printf("%sWarning%s: %s\n", FZL_CERROR, FZL_CRST, analyzer->warnings[i]); } fzl_walker_t *walker = fzl_mkwalker(parser->root); From 62ae05b4cdac1e4a0c70a665cabe983f25efe9d1 Mon Sep 17 00:00:00 2001 From: samsonium Date: Mon, 26 Oct 2026 20:47:24 +0300 Subject: [PATCH 02/11] wip: working on analyzer - Feat: now symbols table store node on PUT - Feat: call analyzer with callee resolving and arguments validate - Feat: identifier analyzer - Feat: analyzer utils (types compat, tok-to-type, type-to-string) --- CMakeLists.txt | 4 + include/compiler/fzl_analyzer.h | 45 +++++++++ include/compiler/utils/fzl_symtable.h | 6 +- src/compiler/analyzer/an_call.c | 134 ++++++++++++++++++++++++++ src/compiler/analyzer/an_class.c | 8 ++ src/compiler/analyzer/an_common.c | 60 ++++++++++++ src/compiler/analyzer/an_identifier.c | 40 ++++++++ src/compiler/analyzer/utility.c | 121 +++++++++++++++++++++++ src/compiler/fzl_analyzer.c | 57 +++++++++-- src/compiler/main.c | 5 + src/compiler/utils/fzl_symtable.c | 9 +- 11 files changed, 474 insertions(+), 15 deletions(-) create mode 100644 src/compiler/analyzer/an_call.c create mode 100644 src/compiler/analyzer/an_class.c create mode 100644 src/compiler/analyzer/an_common.c create mode 100644 src/compiler/analyzer/an_identifier.c create mode 100644 src/compiler/analyzer/utility.c diff --git a/CMakeLists.txt b/CMakeLists.txt index e7a7699..c8db2e1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,6 +20,10 @@ add_executable(fzc src/compiler/utils/fzl_options.c src/compiler/utils/fzl_ctable.c src/compiler/checkers/fzl_loop_check.c + src/compiler/analyzer/utility.c + src/compiler/analyzer/an_call.c + src/compiler/analyzer/an_common.c + src/compiler/analyzer/an_identifier.c src/compiler/fzl_analyzer.c src/compiler/fzl_debug.c src/compiler/fzl_parser.c diff --git a/include/compiler/fzl_analyzer.h b/include/compiler/fzl_analyzer.h index c196f3a..0572478 100644 --- a/include/compiler/fzl_analyzer.h +++ b/include/compiler/fzl_analyzer.h @@ -1,9 +1,21 @@ #ifndef FZL_ANALYZER_H #define FZL_ANALYZER_H +#include #include +#include +#include +#include #include +// Analyzing context type +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_t; + // Analyzer descriptor typedef struct { @@ -13,16 +25,49 @@ typedef struct char *errors[256]; size_t errors_top; + fzl_symtable_t *symbols[256]; + size_t symbols_top; + fzl_node_t *root; } fzl_analyzer_t; +// Inner node analyzing result descriptor +typedef struct +{ + bool error; + + fzl_type_t type; + char *classname; +} fzl_an_res_t; + +// Current node context descriptor +typedef struct fzl_an_ctx +{ + struct fzl_an_ctx *parent; + + fzl_an_ctx_type_t type; +} fzl_an_ctx_t; + +// Temp container for error +extern char alast_error[256]; + // Allocate analyzer fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node); +// Push error to errors list +bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error); + +// Push warning to warnings list +bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning); + // Analyze AST void fzl_analyze(fzl_analyzer_t *analyzer); // Free analyzer void fzl_freeanalyzer(fzl_analyzer_t *analyzer); +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_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id); + #endif // FZL_ANALYZER_H diff --git a/include/compiler/utils/fzl_symtable.h b/include/compiler/utils/fzl_symtable.h index ebaacdc..949c5d1 100644 --- a/include/compiler/utils/fzl_symtable.h +++ b/include/compiler/utils/fzl_symtable.h @@ -2,6 +2,7 @@ #define FZL_SYMTABLE_H #include +#include #include #include #include @@ -19,6 +20,7 @@ typedef struct { char *name; fzl_type_t type; + fzl_node_t *node; size_t ip; char *classname; @@ -37,8 +39,8 @@ typedef struct fzl_symtable_t *fzl_mksymt(); // Put new entry into symtable -fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name, - fzl_type_t type, uint64_t ip); +fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, fzl_node_t *node, + char *name, fzl_type_t type, uint64_t ip); // Assign class name to object type fzl_symtable_res_t fzl_symt_setobjclass(fzl_symtable_t *symt, char *name, diff --git a/src/compiler/analyzer/an_call.c b/src/compiler/analyzer/an_call.c new file mode 100644 index 0000000..263a8f6 --- /dev/null +++ b/src/compiler/analyzer/an_call.c @@ -0,0 +1,134 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +char *tmp_method_name = NULL; +char call_problem[256] = {0}; + +// Try to get symbol name from callee +char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method) +{ + *method = false; + + if (callee->type == FZL_NODETYPE_IDENTIFIER) + return (char *)callee->token->value; + + if (callee->type != FZL_NODETYPE_FIELD) + return NULL; + + *method = true; + fzl_an_res_t parent_res = analyze_common(analyzer, callee->memb.parent); + if (parent_res.type != FZL_TYPE_OBJECT || !parent_res.classname) + return NULL; + + char *fn_name = callee->memb.field->token->value; + 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) + { + an_push_error(analyzer, callee->token, + "failed to allocate space for method name"); + return NULL; + } + + strcpy(tmp_method_name, parent_res.classname); + tmp_method_name[classname_len] = '_'; + strcpy(tmp_method_name + classname_len + 1, fn_name); + tmp_method_name[classname_len + methodname_len + 1] = 0; + + 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 +fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) +{ + if (!call_node || call_node->type != FZL_NODETYPE_CALL) + { + an_push_error(analyzer, call_node->token, + "provided node is not typeof CALL"); + 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}; + + bool is_method = false; + char *callee_name = resolve_callee(analyzer, callee, &is_method); + if (!callee_name) + { + an_push_error(analyzer, callee->token, "expression is not callable"); + + if (tmp_method_name != NULL) + free(tmp_method_name); + + 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--) + { + if ((sym_found = fzl_symt_get(analyzer->symbols[i], callee_name, &sym))) + break; + } + + if (!sym_found) + { + snprintf(call_problem, sizeof(call_problem), "cannot resolve symbol '%s'", + callee_name); + an_push_error(analyzer, call_node->token, call_problem); + return (fzl_an_res_t){.error = true}; + } + + if (sym.node->type != FZL_NODETYPE_FUNCTION) + { + snprintf(call_problem, sizeof(call_problem), "'%s' is not callable", + callee_name); + an_push_error(analyzer, call_node->token, call_problem); + return (fzl_an_res_t){.error = true}; + } + + if (sym.node->fn.args_count != call_node->call.args_count) + { + snprintf(call_problem, sizeof(call_problem), + "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); + return (fzl_an_res_t){.error = true}; + } + + for (size_t i = 0; i < sym.node->fn.args_count; i++) + { + fzl_an_res_t req = analyze_common(analyzer, sym.node->fn.args[i]); + fzl_an_res_t got = analyze_common(analyzer, call_node->call.args[i]); + if (!an_is_compatible(req.type, got.type, req.classname, got.classname)) + { + snprintf(call_problem, sizeof(call_problem), + "expect argument %zu to be %s, got '%s'", i + 1, + an_typetostr(req.type, req.classname), + an_typetostr(got.type, got.classname)); + an_push_error(analyzer, call_node->token, call_problem); + return (fzl_an_res_t){.error = true}; + } + } + + fzl_type_t type = an_toktotype(sym.node->fn.type); + char *classname = sym.classname; + + return (fzl_an_res_t){.error = false, .type = type, .classname = classname}; +} diff --git a/src/compiler/analyzer/an_class.c b/src/compiler/analyzer/an_class.c new file mode 100644 index 0000000..55c59f4 --- /dev/null +++ b/src/compiler/analyzer/an_class.c @@ -0,0 +1,8 @@ +// TODO: +// 1. check for manual `this` declaration + +#include +#include + +// Analyze class declaration +fzl_an_res_t analyze_class(fzl_node_t *class_node) {} diff --git a/src/compiler/analyzer/an_common.c b/src/compiler/analyzer/an_common.c new file mode 100644 index 0000000..66581a7 --- /dev/null +++ b/src/compiler/analyzer/an_common.c @@ -0,0 +1,60 @@ +#include "compiler/fzl_token.h" +#include +#include +#include +#include + +// Analyzer main function +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_IDENTIFIER: + res = analyze_identifier(analyzer, node); + break; + + case FZL_NODETYPE_BLOCK: + for (size_t i = 0; i < node->list.entry_count; i++) + { + fzl_an_res_t entry_res = + analyze_common(analyzer, node->list.entries[i]); + if (entry_res.error) + res.error = true; + } + break; + + case FZL_NODETYPE_CALL: + res = analyze_call(analyzer, node); + break; + + case FZL_NODETYPE_LITERAL: + switch (node->token->type) + { + case FZL_TOKTYPE_STR_LITERAL: + res.type = FZL_TYPE_STRING; + break; + + case FZL_TOKTYPE_NUM_LITERAL: + res.type = FZL_TYPE_INT; + break; + + case FZL_TOKTYPE_BOOL_LITERAL: + res.type = FZL_TYPE_BOOL; + break; + + case FZL_TOKTYPE_NULL_LITERAL: + res.type = FZL_TYPE_OBJECT; + res.classname = "null"; + break; + + default: + an_push_error(analyzer, node->token, "unknown literal type"); + res.error = true; + break; + } + break; + } + + return res; +} diff --git a/src/compiler/analyzer/an_identifier.c b/src/compiler/analyzer/an_identifier.c new file mode 100644 index 0000000..203d956 --- /dev/null +++ b/src/compiler/analyzer/an_identifier.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include + +char id_problem[256]; + +// Analyze identifier +fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id) +{ + if (!analyzer) + return (fzl_an_res_t){.error = true}; + + if (!id || id->type != FZL_NODETYPE_IDENTIFIER) + { + snprintf(id_problem, sizeof(id_problem), "cannot parse identifier"); + an_push_error(analyzer, NULL, id_problem); + return (fzl_an_res_t){.error = true}; + } + + char *symname = id->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], symname, &sym))) + break; + } + + if (!sym_found) + { + snprintf(id_problem, sizeof(id_problem), "cannot find symbol '%s'", + symname); + an_push_error(analyzer, id->token, id_problem); + return (fzl_an_res_t){.error = true}; + } + + return (fzl_an_res_t){ + .error = false, .type = sym.type, .classname = sym.classname}; +} diff --git a/src/compiler/analyzer/utility.c b/src/compiler/analyzer/utility.c new file mode 100644 index 0000000..03679a1 --- /dev/null +++ b/src/compiler/analyzer/utility.c @@ -0,0 +1,121 @@ +#include +#include +#include +#include + +// Check compatibility between A and B types (and classnames) +bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc) +{ + if (a == b) + return true; + + if (a == FZL_TYPE_OBJECT && b == FZL_TYPE_OBJECT) + { + if (strcmp(ac, "null") == 0 || strcmp(bc, "null") == 0) + return true; + + return strcmp(ac, bc) == 0; + } + + if (a >= FZL_TYPE_INT8 && b >= FZL_TYPE_INT8) + return true; + + return false; +} + +// Convert type to string +char *an_typetostr(fzl_type_t type, char *classname) +{ + if (classname) + return classname; + + switch (type) + { + case FZL_TYPE_NULL: + return "null"; + + case FZL_TYPE_BOOL: + return "boolean"; + + case FZL_TYPE_STRING: + return "string"; + + case FZL_TYPE_CHAR: + return "char"; + + case FZL_TYPE_INT8: + return "i8"; + + case FZL_TYPE_INT16: + return "i16"; + + case FZL_TYPE_INT32: + return "i32"; + + case FZL_TYPE_INT64: + return "i64"; + + case FZL_TYPE_UINT8: + return "u8"; + + case FZL_TYPE_UINT16: + return "u16"; + + case FZL_TYPE_UINT32: + return "u32"; + + case FZL_TYPE_UINT64: + return "u64"; + + case FZL_TYPE_INT: + return "int"; + + case FZL_TYPE_UINT: + return "uint"; + + default: + return ""; + } +} + +// Convert type token to type +fzl_type_t an_toktotype(fzl_token_t *tok) +{ + if (!tok) + return FZL_TYPE_NULL; + + if (tok->type == FZL_TOKTYPE_NAME) + return FZL_TYPE_OBJECT; + + if (strcmp((char *)tok->value, "i8") == 0) + return FZL_TYPE_INT8; + if (strcmp((char *)tok->value, "i16") == 0) + return FZL_TYPE_INT16; + if (strcmp((char *)tok->value, "i32") == 0) + return FZL_TYPE_INT32; + if (strcmp((char *)tok->value, "i64") == 0) + return FZL_TYPE_INT64; + + if (strcmp((char *)tok->value, "u8") == 0) + return FZL_TYPE_UINT8; + if (strcmp((char *)tok->value, "u16") == 0) + return FZL_TYPE_UINT16; + if (strcmp((char *)tok->value, "u32") == 0) + return FZL_TYPE_UINT32; + if (strcmp((char *)tok->value, "u64") == 0) + return FZL_TYPE_UINT64; + + if (strcmp((char *)tok->value, "int") == 0) + return FZL_TYPE_INT; + if (strcmp((char *)tok->value, "uint") == 0) + return FZL_TYPE_UINT; + + if (strcmp((char *)tok->value, "bool") == 0) + return FZL_TYPE_BOOL; + if (strcmp((char *)tok->value, "str") == 0) + return FZL_TYPE_STRING; + if (strcmp((char *)tok->value, "char") == 0) + return FZL_TYPE_CHAR; + + return FZL_TYPE_NULL; +} diff --git a/src/compiler/fzl_analyzer.c b/src/compiler/fzl_analyzer.c index 336f015..cb8f7f8 100644 --- a/src/compiler/fzl_analyzer.c +++ b/src/compiler/fzl_analyzer.c @@ -1,9 +1,14 @@ #include #include +#include +#include #include +#include #include #include +char alast_err[256]; + fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node) { if (!node) @@ -16,30 +21,44 @@ fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node) analyzer->root = node; analyzer->warnings_top = 0; analyzer->errors_top = 0; + analyzer->symbols_top = 0; memset(analyzer->errors, 0, sizeof(analyzer->errors)); memset(analyzer->warnings, 0, sizeof(analyzer->warnings)); + memset(analyzer->symbols, 0, sizeof(analyzer->symbols)); return analyzer; } -// Push error to errors list -bool push_error(fzl_analyzer_t *analyzer, char *error) +bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error) { - if (analyzer->errors_top >= 256) + if (analyzer->errors_top >= 256 || !analyzer || !error) return false; - analyzer->errors[analyzer->errors_top++] = error; + if (tok != NULL) + snprintf(alast_error, sizeof(alast_error), "%s:%zu:%zu => %s", + tok->filename, tok->ln, tok->cn, error); + else + snprintf(alast_error, sizeof(alast_error), " => %s", + error); + + analyzer->errors[analyzer->errors_top++] = alast_error; return true; } -// Push warning to warnings list -bool push_warning(fzl_analyzer_t *analyzer, char *error) +bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning) { - if (analyzer->warnings_top >= 256) + if (analyzer->warnings_top >= 256 || !analyzer || !warning) return false; - analyzer->warnings[analyzer->warnings_top++] = error; + if (tok != NULL) + snprintf(alast_error, sizeof(alast_error), "%s:%zu:%zu => %s", + tok->filename, tok->ln, tok->cn, warning); + else + snprintf(alast_error, sizeof(alast_error), " => %s", + warning); + + analyzer->warnings[analyzer->warnings_top++] = alast_error; return true; } @@ -48,7 +67,26 @@ void fzl_analyze(fzl_analyzer_t *analyzer) if (!analyzer) return; - // + analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt(); + + fzl_node_t *current = analyzer->root; + while (current != NULL) + { + fzl_an_res_t res = analyze_common(analyzer, current); + if (res.error) + break; + + current = current->next; + } + + if (analyzer->symbols_top < 0) + { + an_push_error(analyzer, NULL, + "undefined error (early finish of global environment)"); + return; + } + + fzl_freesymt(analyzer->symbols[--analyzer->symbols_top]); } void fzl_freeanalyzer(fzl_analyzer_t *analyzer) @@ -58,5 +96,6 @@ void fzl_freeanalyzer(fzl_analyzer_t *analyzer) memset(analyzer->errors, 0, sizeof(analyzer->errors)); memset(analyzer->warnings, 0, sizeof(analyzer->warnings)); + memset(analyzer->symbols, 0, sizeof(analyzer->symbols)); free(analyzer); } diff --git a/src/compiler/main.c b/src/compiler/main.c index 638960e..0210e3d 100644 --- a/src/compiler/main.c +++ b/src/compiler/main.c @@ -164,6 +164,7 @@ int main(int argc, char **argv) printf("%sError%s: failed to allocate writer\n", FZL_CERROR, FZL_CRST); fzl_freeoptions(opts); fzl_freeast(parser->root); + fzl_freeanalyzer(analyzer); fzl_freeparser(parser); fzl_freereader(rd); fzl_freelinked(linked); @@ -177,6 +178,7 @@ int main(int argc, char **argv) fzl_freeoptions(opts); fzl_freewalker(walker); fzl_freeast(parser->root); + fzl_freeanalyzer(analyzer); fzl_freeparser(parser); fzl_freereader(rd); fzl_freelinked(linked); @@ -223,6 +225,7 @@ int main(int argc, char **argv) fzl_freeoptions(opts); fzl_freewalker(walker); fzl_freeast(parser->root); + fzl_freeanalyzer(analyzer); fzl_freeparser(parser); fzl_freereader(rd); fzl_freelinked(linked); @@ -237,6 +240,7 @@ int main(int argc, char **argv) fzl_freewriter(writer); fzl_freewalker(walker); fzl_freeast(parser->root); + fzl_freeanalyzer(analyzer); fzl_freeparser(parser); fzl_freereader(rd); fzl_freelinked(linked); @@ -250,6 +254,7 @@ int main(int argc, char **argv) fzl_freelinked(linked); fzl_freereader(rd); fzl_freeast(parser->root); + fzl_freeanalyzer(analyzer); fzl_freeparser(parser); fzl_freewalker(walker); fzl_freewriter(writer); diff --git a/src/compiler/utils/fzl_symtable.c b/src/compiler/utils/fzl_symtable.c index a1c5f5d..b425962 100644 --- a/src/compiler/utils/fzl_symtable.c +++ b/src/compiler/utils/fzl_symtable.c @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -21,8 +22,8 @@ fzl_symtable_t *fzl_mksymt() return tbl; } -fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name, - fzl_type_t type, uint64_t ip) +fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, fzl_node_t *node, + char *name, fzl_type_t type, uint64_t ip) { if (!symt) return FZL_SYMTABLE_RESULT_ERR; @@ -37,8 +38,8 @@ fzl_symtable_res_t fzl_symt_put(fzl_symtable_t *symt, char *name, symt->symbols = realloc(symt->symbols, symt->remainder * sizeof(fzl_sym_t)); } - symt->symbols[symt->size++] = - (fzl_sym_t){.name = name, .type = type, .ip = ip, .classname = NULL}; + symt->symbols[symt->size++] = (fzl_sym_t){ + .name = name, .node = node, .type = type, .ip = ip, .classname = NULL}; return FZL_SYMTABLE_RESULT_OK; } From a70ad011f361993d1e71e26ad549e0989ca6bd4e Mon Sep 17 00:00:00 2001 From: samsonium Date: Mon, 26 Oct 2026 21:34:54 +0300 Subject: [PATCH 03/11] wip: working on analyzer - Feat: add `nest_ctx` and `fin_ctx` fns for working with context and environment - Feat: add block analyzer - Feat: add conditional statement analyzer --- CMakeLists.txt | 3 ++ include/compiler/fzl_analyzer.h | 37 +++++++++++++--------- src/compiler/analyzer/an_block.c | 44 ++++++++++++++++++++++++++ src/compiler/analyzer/an_call.c | 3 +- src/compiler/analyzer/an_common.c | 17 ++++++---- src/compiler/analyzer/an_conditional.c | 40 +++++++++++++++++++++++ src/compiler/analyzer/an_identifier.c | 3 +- src/compiler/analyzer/context.c | 38 ++++++++++++++++++++++ src/compiler/fzl_analyzer.c | 15 ++++----- 9 files changed, 165 insertions(+), 35 deletions(-) create mode 100644 src/compiler/analyzer/an_block.c create mode 100644 src/compiler/analyzer/an_conditional.c create mode 100644 src/compiler/analyzer/context.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c8db2e1..d4e31cc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -21,8 +21,11 @@ add_executable(fzc src/compiler/utils/fzl_ctable.c src/compiler/checkers/fzl_loop_check.c src/compiler/analyzer/utility.c + src/compiler/analyzer/context.c + src/compiler/analyzer/an_block.c src/compiler/analyzer/an_call.c src/compiler/analyzer/an_common.c + src/compiler/analyzer/an_conditional.c src/compiler/analyzer/an_identifier.c src/compiler/fzl_analyzer.c src/compiler/fzl_debug.c diff --git a/include/compiler/fzl_analyzer.h b/include/compiler/fzl_analyzer.h index 0572478..546b140 100644 --- a/include/compiler/fzl_analyzer.h +++ b/include/compiler/fzl_analyzer.h @@ -16,21 +16,6 @@ typedef enum FZL_AN_CTX_TYPE_LOOP, // when analyzing loop } fzl_an_ctx_type_t; -// Analyzer descriptor -typedef struct -{ - char *warnings[256]; - size_t warnings_top; - - char *errors[256]; - size_t errors_top; - - fzl_symtable_t *symbols[256]; - size_t symbols_top; - - fzl_node_t *root; -} fzl_analyzer_t; - // Inner node analyzing result descriptor typedef struct { @@ -48,6 +33,22 @@ typedef struct fzl_an_ctx fzl_an_ctx_type_t type; } fzl_an_ctx_t; +// Analyzer descriptor +typedef struct +{ + char *warnings[256]; + size_t warnings_top; + + char *errors[256]; + size_t errors_top; + + fzl_symtable_t *symbols[256]; + size_t symbols_top; + + fzl_node_t *root; + fzl_an_ctx_t *ctx; +} fzl_analyzer_t; + // Temp container for error extern char alast_error[256]; @@ -69,5 +70,11 @@ void fzl_freeanalyzer(fzl_analyzer_t *analyzer); 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_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id); +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_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); #endif // FZL_ANALYZER_H diff --git a/src/compiler/analyzer/an_block.c b/src/compiler/analyzer/an_block.c new file mode 100644 index 0000000..9a88b35 --- /dev/null +++ b/src/compiler/analyzer/an_block.c @@ -0,0 +1,44 @@ +#include +#include +#include +#include +#include + +char block_problem[256]; + +// Analyze block +fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node) +{ + if (!analyzer) + return (fzl_an_res_t){.error = true}; + + if (!block_node || block_node->type != FZL_NODETYPE_BLOCK) + { + snprintf(block_problem, sizeof(block_problem), "node is not typeof BLOCK"); + an_push_error(analyzer, NULL, block_problem); + return (fzl_an_res_t){.error = true}; + } + + an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY); + + bool has_return = false; + bool error = false; + for (size_t i = 0; i < block_node->list.entry_count; i++) + { + if (has_return) + an_push_warning(analyzer, block_node->list.entries[i]->token, + "this node will never execute"); + + fzl_an_res_t entry_res = + analyze_common(analyzer, block_node->list.entries[i]); + if (entry_res.error) + error = true; + + if (block_node->list.entries[i]->type == FZL_NODETYPE_RETURN) + has_return = true; + } + + an_fin_ctx(analyzer); + + return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL}; +} diff --git a/src/compiler/analyzer/an_call.c b/src/compiler/analyzer/an_call.c index 263a8f6..10ea7e8 100644 --- a/src/compiler/analyzer/an_call.c +++ b/src/compiler/analyzer/an_call.c @@ -57,8 +57,7 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) { if (!call_node || call_node->type != FZL_NODETYPE_CALL) { - an_push_error(analyzer, call_node->token, - "provided node is not typeof CALL"); + an_push_error(analyzer, NULL, "provided node is not typeof CALL"); return (fzl_an_res_t){.error = true}; } diff --git a/src/compiler/analyzer/an_common.c b/src/compiler/analyzer/an_common.c index 66581a7..490c6c9 100644 --- a/src/compiler/analyzer/an_common.c +++ b/src/compiler/analyzer/an_common.c @@ -3,6 +3,7 @@ #include #include #include +#include // Analyzer main function fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node) @@ -10,18 +11,20 @@ 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_FUNCTION: + res = analyze_function(analyzer, node); + break; + + case FZL_NODETYPE_CONDITIONAL: + res = analyze_conditional(analyzer, node); + break; + case FZL_NODETYPE_IDENTIFIER: res = analyze_identifier(analyzer, node); break; case FZL_NODETYPE_BLOCK: - for (size_t i = 0; i < node->list.entry_count; i++) - { - fzl_an_res_t entry_res = - analyze_common(analyzer, node->list.entries[i]); - if (entry_res.error) - res.error = true; - } + res = analyze_block(analyzer, node); break; case FZL_NODETYPE_CALL: diff --git a/src/compiler/analyzer/an_conditional.c b/src/compiler/analyzer/an_conditional.c new file mode 100644 index 0000000..f37dd48 --- /dev/null +++ b/src/compiler/analyzer/an_conditional.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include + +char cond_problem[256]; + +// Analyze conditional statement +fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond) +{ + if (!analyzer) + return (fzl_an_res_t){.error = true}; + + if (!cond || cond->type != FZL_NODETYPE_CONDITIONAL) + { + an_push_error(analyzer, NULL, "provided node is not typeof CONDITIONAL"); + return (fzl_an_res_t){.error = true}; + } + + 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 else_res = analyze_common(analyzer, cond->cond.else_block); + + if (cond_res.error || true_res.error || else_res.error) + return (fzl_an_res_t){.error = true}; + + if (cond_res.type != FZL_TYPE_BOOL) + an_push_warning(analyzer, cond->cond.condition->token, + "resulting type is not bool"); + + if (cond->cond.condition->token->type == FZL_TOKTYPE_BOOL_LITERAL) + { + snprintf(cond_problem, sizeof(cond_problem), "condition is always %s", + (char *)cond->cond.condition->token->value); + an_push_warning(analyzer, cond->cond.condition->token, cond_problem); + } + + return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL}; +} diff --git a/src/compiler/analyzer/an_identifier.c b/src/compiler/analyzer/an_identifier.c index 203d956..a56c2cd 100644 --- a/src/compiler/analyzer/an_identifier.c +++ b/src/compiler/analyzer/an_identifier.c @@ -13,8 +13,7 @@ fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id) if (!id || id->type != FZL_NODETYPE_IDENTIFIER) { - snprintf(id_problem, sizeof(id_problem), "cannot parse identifier"); - an_push_error(analyzer, NULL, id_problem); + an_push_error(analyzer, NULL, "provided node is not typeof IDENTIFIER"); return (fzl_an_res_t){.error = true}; } diff --git a/src/compiler/analyzer/context.c b/src/compiler/analyzer/context.c new file mode 100644 index 0000000..472bfb3 --- /dev/null +++ b/src/compiler/analyzer/context.c @@ -0,0 +1,38 @@ +#include +#include +#include + +// Nest context +fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type) +{ + if (!analyzer) + return NULL; + + fzl_an_ctx_t *ctx = malloc(sizeof(fzl_an_ctx_t)); + if (!ctx) + return NULL; + + ctx->type = type; + if (analyzer->ctx != NULL) + ctx->parent = analyzer->ctx; + + analyzer->ctx = ctx; + analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt(); + + return ctx; +} + +// Close context and return parent +fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer) +{ + if (!analyzer || !analyzer->ctx) + 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); + return parent; +} diff --git a/src/compiler/fzl_analyzer.c b/src/compiler/fzl_analyzer.c index cb8f7f8..4c9560d 100644 --- a/src/compiler/fzl_analyzer.c +++ b/src/compiler/fzl_analyzer.c @@ -67,7 +67,11 @@ void fzl_analyze(fzl_analyzer_t *analyzer) if (!analyzer) return; - analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt(); + if (an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY) == NULL) + { + an_push_error(analyzer, NULL, "failed to make global context"); + return; + } fzl_node_t *current = analyzer->root; while (current != NULL) @@ -79,14 +83,7 @@ void fzl_analyze(fzl_analyzer_t *analyzer) current = current->next; } - if (analyzer->symbols_top < 0) - { - an_push_error(analyzer, NULL, - "undefined error (early finish of global environment)"); - return; - } - - fzl_freesymt(analyzer->symbols[--analyzer->symbols_top]); + an_fin_ctx(analyzer); } void fzl_freeanalyzer(fzl_analyzer_t *analyzer) From d7630dbb6a77a30470619f589ba2366390fcf293 Mon Sep 17 00:00:00 2001 From: samsonium Date: Mon, 26 Oct 2026 22:20:09 +0300 Subject: [PATCH 04/11] wip: working on analyzer - Feat: implement return analyzer - Fix: add block return check if inside function --- include/compiler/fzl_analyzer.h | 6 +++ src/compiler/analyzer/an_block.c | 47 +++++++++++++++++++++ src/compiler/analyzer/an_common.c | 4 ++ src/compiler/analyzer/an_return.c | 70 +++++++++++++++++++++++++++++++ 4 files changed, 127 insertions(+) create mode 100644 src/compiler/analyzer/an_return.c diff --git a/include/compiler/fzl_analyzer.h b/include/compiler/fzl_analyzer.h index 546b140..5b727f3 100644 --- a/include/compiler/fzl_analyzer.h +++ b/include/compiler/fzl_analyzer.h @@ -31,6 +31,11 @@ typedef struct fzl_an_ctx struct fzl_an_ctx *parent; fzl_an_ctx_type_t type; + + union + { + fzl_node_t *fn; + }; } fzl_an_ctx_t; // Analyzer descriptor @@ -73,6 +78,7 @@ fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id); 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_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); diff --git a/src/compiler/analyzer/an_block.c b/src/compiler/analyzer/an_block.c index 9a88b35..aad92d5 100644 --- a/src/compiler/analyzer/an_block.c +++ b/src/compiler/analyzer/an_block.c @@ -1,11 +1,16 @@ #include #include #include +#include #include #include char block_problem[256]; +// defined in `compiler/analyzer/utility.c` +extern fzl_type_t an_toktotype(fzl_token_t *tok); +extern char *an_typetostr(fzl_type_t type, char *classname); + // Analyze block fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node) { @@ -38,6 +43,48 @@ fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node) has_return = true; } + fzl_an_ctx_t *fnctx = analyzer->ctx; + while (fnctx != NULL) + { + if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION) + break; + + fnctx = fnctx->parent; + } + + if (fnctx && fnctx->type == FZL_AN_CTX_TYPE_FUNCTION) + { + fzl_type_t rettype = an_toktotype(fnctx->fn->fn.type); + char *classname = NULL; + char *rettype_str = an_typetostr(rettype, classname); + + if (rettype == FZL_TYPE_OBJECT) + classname = (char *)fnctx->fn->fn.type->value; + + if (rettype != FZL_TYPE_NULL) + { + if (!has_return || block_node->list.entry_count < 1) + { + snprintf(block_problem, sizeof(block_problem), + "expected 'return <%s>' in function because return type is %s", + rettype_str, rettype_str); + an_push_error(analyzer, block_node->token, block_problem); + return (fzl_an_res_t){.error = true}; + } + + fzl_node_t *last_node = + block_node->list.entries[block_node->list.entry_count - 1]; + if (last_node->type != FZL_NODETYPE_RETURN) + { + snprintf(block_problem, sizeof(block_problem), + "expected last statement in block to be 'return <%s>'", + rettype_str); + an_push_error(analyzer, last_node->token, block_problem); + return (fzl_an_res_t){.error = true}; + } + } + } + an_fin_ctx(analyzer); return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL}; diff --git a/src/compiler/analyzer/an_common.c b/src/compiler/analyzer/an_common.c index 490c6c9..4b837c8 100644 --- a/src/compiler/analyzer/an_common.c +++ b/src/compiler/analyzer/an_common.c @@ -11,6 +11,10 @@ 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_RETURN: + res = analyze_return(analyzer, node); + break; + case FZL_NODETYPE_FUNCTION: res = analyze_function(analyzer, node); break; diff --git a/src/compiler/analyzer/an_return.c b/src/compiler/analyzer/an_return.c new file mode 100644 index 0000000..e2a285a --- /dev/null +++ b/src/compiler/analyzer/an_return.c @@ -0,0 +1,70 @@ +#include "common/fzl_types.h" +#include +#include +#include +#include + +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 +fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) +{ + if (!analyzer) + return (fzl_an_res_t){.error = true}; + + if (!ret_node || ret_node->type != FZL_NODETYPE_RETURN) + { + an_push_error(analyzer, NULL, "node is not typeof RETURN"); + return (fzl_an_res_t){.error = false}; + } + + fzl_an_ctx_t *fnctx = analyzer->ctx; + while (!fnctx) + { + if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION) + break; + + fnctx = fnctx->parent; + } + + if (!fnctx) + { + an_push_error(analyzer, ret_node->token, + "cannot use 'return' outside function"); + return (fzl_an_res_t){.error = true}; + } + + fzl_type_t rettype = an_toktotype(fnctx->fn->fn.type); + char *classname = NULL; + if (rettype == FZL_TYPE_OBJECT) + classname = (char *)fnctx->fn->fn.type->value; + + char *rettype_str = an_typetostr(rettype, classname); + if (rettype != FZL_TYPE_NULL) + { + if (ret_node->ret.value == NULL) + { + an_push_error(analyzer, ret_node->token, + "return must be used with value in this function"); + return (fzl_an_res_t){.error = true}; + } + + fzl_an_res_t ret_res = analyze_common(analyzer, ret_node->ret.value); + if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname)) + { + snprintf(ret_problem, sizeof(ret_problem), + "expected 'return <%s>' instead of 'return <%s>'", rettype_str, + an_typetostr(ret_res.type, ret_res.classname)); + an_push_error(analyzer, ret_node->token, ret_problem); + return (fzl_an_res_t){.error = true}; + } + } + + return (fzl_an_res_t){ + .error = false, .type = rettype, .classname = classname}; +} From 25991c6f1532b759c70a4e1b6f1c19cc53c07f53 Mon Sep 17 00:00:00 2001 From: samsonium Date: Tue, 27 Oct 2026 05:18:36 +0300 Subject: [PATCH 05/11] 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 --- CMakeLists.txt | 9 + include/compiler/fzl_analyzer.h | 15 +- src/compiler/analyzer/an_binmath.c | 47 +++ src/compiler/analyzer/an_block.c | 2 +- src/compiler/analyzer/an_break.c | 33 ++ src/compiler/analyzer/an_call.c | 76 +++- src/compiler/analyzer/an_class.c | 73 +++- src/compiler/analyzer/an_common.c | 35 +- src/compiler/analyzer/an_construct.c | 40 +++ src/compiler/analyzer/an_declaration.c | 71 ++++ src/compiler/analyzer/an_field.c | 83 +++++ src/compiler/analyzer/an_function.c | 126 +++++++ src/compiler/analyzer/an_loop.c | 460 +++++++++++++++++++++++++ src/compiler/analyzer/context.c | 5 +- src/compiler/analyzer/utility.c | 3 + src/compiler/fzl_analyzer.c | 42 ++- src/compiler/fzl_walker.c | 4 +- 17 files changed, 1094 insertions(+), 30 deletions(-) create mode 100644 src/compiler/analyzer/an_binmath.c create mode 100644 src/compiler/analyzer/an_break.c create mode 100644 src/compiler/analyzer/an_construct.c create mode 100644 src/compiler/analyzer/an_declaration.c create mode 100644 src/compiler/analyzer/an_field.c create mode 100644 src/compiler/analyzer/an_function.c create mode 100644 src/compiler/analyzer/an_loop.c diff --git a/CMakeLists.txt b/CMakeLists.txt index d4e31cc..fecc083 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/include/compiler/fzl_analyzer.h b/include/compiler/fzl_analyzer.h index 5b727f3..56a5794 100644 --- a/include/compiler/fzl_analyzer.h +++ b/include/compiler/fzl_analyzer.h @@ -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); diff --git a/src/compiler/analyzer/an_binmath.c b/src/compiler/analyzer/an_binmath.c new file mode 100644 index 0000000..3ceb96f --- /dev/null +++ b/src/compiler/analyzer/an_binmath.c @@ -0,0 +1,47 @@ +#include +#include +#include + +// 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}; +} diff --git a/src/compiler/analyzer/an_block.c b/src/compiler/analyzer/an_block.c index aad92d5..639dbe0 100644 --- a/src/compiler/analyzer/an_block.c +++ b/src/compiler/analyzer/an_block.c @@ -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; diff --git a/src/compiler/analyzer/an_break.c b/src/compiler/analyzer/an_break.c new file mode 100644 index 0000000..3e8d80b --- /dev/null +++ b/src/compiler/analyzer/an_break.c @@ -0,0 +1,33 @@ +#include +#include +#include + +// 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}; +} diff --git a/src/compiler/analyzer/an_call.c b/src/compiler/analyzer/an_call.c index 10ea7e8..3b1f218 100644 --- a/src/compiler/analyzer/an_call.c +++ b/src/compiler/analyzer/an_call.c @@ -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}; } diff --git a/src/compiler/analyzer/an_class.c b/src/compiler/analyzer/an_class.c index 55c59f4..da264b6 100644 --- a/src/compiler/analyzer/an_class.c +++ b/src/compiler/analyzer/an_class.c @@ -1,8 +1,73 @@ -// TODO: -// 1. check for manual `this` declaration - +#include #include #include +#include +#include + +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}; +} diff --git a/src/compiler/analyzer/an_common.c b/src/compiler/analyzer/an_common.c index 4b837c8..c893867 100644 --- a/src/compiler/analyzer/an_common.c +++ b/src/compiler/analyzer/an_common.c @@ -1,7 +1,7 @@ -#include "compiler/fzl_token.h" #include #include #include +#include #include #include @@ -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; diff --git a/src/compiler/analyzer/an_construct.c b/src/compiler/analyzer/an_construct.c new file mode 100644 index 0000000..5c9202d --- /dev/null +++ b/src/compiler/analyzer/an_construct.c @@ -0,0 +1,40 @@ +#include +#include +#include +#include +#include +#include + +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}; +} diff --git a/src/compiler/analyzer/an_declaration.c b/src/compiler/analyzer/an_declaration.c new file mode 100644 index 0000000..a6bc786 --- /dev/null +++ b/src/compiler/analyzer/an_declaration.c @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include +#include +#include + +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}; +} diff --git a/src/compiler/analyzer/an_field.c b/src/compiler/analyzer/an_field.c new file mode 100644 index 0000000..15e728d --- /dev/null +++ b/src/compiler/analyzer/an_field.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +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}; +} diff --git a/src/compiler/analyzer/an_function.c b/src/compiler/analyzer/an_function.c new file mode 100644 index 0000000..6e96be8 --- /dev/null +++ b/src/compiler/analyzer/an_function.c @@ -0,0 +1,126 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +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}; +} diff --git a/src/compiler/analyzer/an_loop.c b/src/compiler/analyzer/an_loop.c new file mode 100644 index 0000000..3568233 --- /dev/null +++ b/src/compiler/analyzer/an_loop.c @@ -0,0 +1,460 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +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}; +} diff --git a/src/compiler/analyzer/context.c b/src/compiler/analyzer/context.c index 472bfb3..7bdaaef 100644 --- a/src/compiler/analyzer/context.c +++ b/src/compiler/analyzer/context.c @@ -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; } diff --git a/src/compiler/analyzer/utility.c b/src/compiler/analyzer/utility.c index 03679a1..db4792e 100644 --- a/src/compiler/analyzer/utility.c +++ b/src/compiler/analyzer/utility.c @@ -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) diff --git a/src/compiler/fzl_analyzer.c b/src/compiler/fzl_analyzer.c index 4c9560d..e2a1d43 100644 --- a/src/compiler/fzl_analyzer.c +++ b/src/compiler/fzl_analyzer.c @@ -3,11 +3,16 @@ #include #include #include +#include #include #include #include -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), " => %s", + snprintf(aerrors[ei], sizeof(aerrors[ei]), " => %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), " => %s", + snprintf(awarnings[wi], sizeof(awarnings[wi]), " => %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); } diff --git a/src/compiler/fzl_walker.c b/src/compiler/fzl_walker.c index b1414c2..b5d903b 100644 --- a/src/compiler/fzl_walker.c +++ b/src/compiler/fzl_walker.c @@ -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); From a263b6206ffde882cef2034a8beae96cc8e12690 Mon Sep 17 00:00:00 2001 From: samsonium Date: Tue, 27 Oct 2026 12:21:49 +0300 Subject: [PATCH 06/11] 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 --- CMakeLists.txt | 4 + include/compiler/fzl_analyzer.h | 4 + src/compiler/analyzer/an_assign.c | 54 +++++++++++ src/compiler/analyzer/an_binmath.c | 11 ++- src/compiler/analyzer/an_common.c | 24 +++++ src/compiler/analyzer/an_condition.c | 122 +++++++++++++++++++++++++ src/compiler/analyzer/an_declaration.c | 2 +- src/compiler/analyzer/an_unarycond.c | 30 ++++++ src/compiler/analyzer/an_unarymath.c | 32 +++++++ 9 files changed, 277 insertions(+), 6 deletions(-) create mode 100644 src/compiler/analyzer/an_assign.c create mode 100644 src/compiler/analyzer/an_condition.c create mode 100644 src/compiler/analyzer/an_unarycond.c create mode 100644 src/compiler/analyzer/an_unarymath.c diff --git a/CMakeLists.txt b/CMakeLists.txt index fecc083..e388003 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,12 +22,14 @@ add_executable(fzc src/compiler/checkers/fzl_loop_check.c src/compiler/analyzer/utility.c src/compiler/analyzer/context.c + src/compiler/analyzer/an_assign.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_condition.c src/compiler/analyzer/an_conditional.c src/compiler/analyzer/an_construct.c src/compiler/analyzer/an_declaration.c @@ -36,6 +38,8 @@ add_executable(fzc src/compiler/analyzer/an_identifier.c src/compiler/analyzer/an_loop.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_debug.c src/compiler/fzl_parser.c diff --git a/include/compiler/fzl_analyzer.h b/include/compiler/fzl_analyzer.h index 56a5794..76cced5 100644 --- a/include/compiler/fzl_analyzer.h +++ b/include/compiler/fzl_analyzer.h @@ -88,6 +88,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_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_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_fin_ctx(fzl_analyzer_t *analyzer); diff --git a/src/compiler/analyzer/an_assign.c b/src/compiler/analyzer/an_assign.c new file mode 100644 index 0000000..927e997 --- /dev/null +++ b/src/compiler/analyzer/an_assign.c @@ -0,0 +1,54 @@ +#include "common/fzl_types.h" +#include "compiler/fzl_analyzer.h" +#include "compiler/fzl_parser.h" +#include + +char asn_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 char *an_typetostr(fzl_type_t type, char *classname); + +// 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 typeof ASSIGN"); + 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, + "this type of assignment left is not supported"); + return (fzl_an_res_t){.error = true}; + } + + if (!an_is_compatible(leftside.type, rightside.type, leftside.classname, + rightside.classname)) + { + snprintf(asn_problem, sizeof(asn_problem), "cannot assign %s to %s", + an_typetostr(rightside.type, rightside.classname), + an_typetostr(leftside.type, leftside.classname)); + an_push_error(analyzer, assign_node->binary_op.right->token, asn_problem); + return (fzl_an_res_t){.error = true}; + } + + return (fzl_an_res_t){ + .error = false, .type = FZL_TYPE_NULL, .classname = NULL}; +} diff --git a/src/compiler/analyzer/an_binmath.c b/src/compiler/analyzer/an_binmath.c index 3ceb96f..2e89aac 100644 --- a/src/compiler/analyzer/an_binmath.c +++ b/src/compiler/analyzer/an_binmath.c @@ -11,11 +11,12 @@ 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) + 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}; diff --git a/src/compiler/analyzer/an_common.c b/src/compiler/analyzer/an_common.c index c893867..a8f43fb 100644 --- a/src/compiler/analyzer/an_common.c +++ b/src/compiler/analyzer/an_common.c @@ -11,6 +11,30 @@ 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_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_SUB: case FZL_NODETYPE_MUL: diff --git a/src/compiler/analyzer/an_condition.c b/src/compiler/analyzer/an_condition.c new file mode 100644 index 0000000..0a0505b --- /dev/null +++ b/src/compiler/analyzer/an_condition.c @@ -0,0 +1,122 @@ +#include +#include +#include +#include + +char bincond_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 char *an_typetostr(fzl_type_t type, char *classname); + +// 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 typeof binary condition"); + 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 be number"); + 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 be number"); + 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 be an integer"); + 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(bincond_problem, sizeof(bincond_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, + bincond_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(bincond_problem, sizeof(bincond_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}; +} diff --git a/src/compiler/analyzer/an_declaration.c b/src/compiler/analyzer/an_declaration.c index a6bc786..efc0300 100644 --- a/src/compiler/analyzer/an_declaration.c +++ b/src/compiler/analyzer/an_declaration.c @@ -67,5 +67,5 @@ fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl) classname); } - return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL}; + return (fzl_an_res_t){.error = false, .type = type, .classname = classname}; } diff --git a/src/compiler/analyzer/an_unarycond.c b/src/compiler/analyzer/an_unarycond.c new file mode 100644 index 0000000..29123dc --- /dev/null +++ b/src/compiler/analyzer/an_unarycond.c @@ -0,0 +1,30 @@ +#include +#include +#include + +// 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 typeof unary condition"); + 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}; +} diff --git a/src/compiler/analyzer/an_unarymath.c b/src/compiler/analyzer/an_unarymath.c new file mode 100644 index 0000000..6df52a5 --- /dev/null +++ b/src/compiler/analyzer/an_unarymath.c @@ -0,0 +1,32 @@ +#include +#include +#include +#include + +// 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 typeof unary math"); + 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}; +} From aa7a06bd223a61b6d1cbee3ce5dc8be82d04c48f Mon Sep 17 00:00:00 2001 From: samsonium Date: Tue, 27 Oct 2026 12:50:58 +0300 Subject: [PATCH 07/11] feat: finish implementing analyzer --- src/compiler/analyzer/an_assign.c | 6 +++--- src/compiler/analyzer/an_break.c | 2 ++ src/compiler/analyzer/an_call.c | 24 ++++++++++++++++++++---- src/compiler/analyzer/an_common.c | 3 +++ src/compiler/analyzer/an_function.c | 7 +++++++ src/compiler/analyzer/an_return.c | 7 +++++-- src/compiler/analyzer/context.c | 5 +++++ src/compiler/main.c | 3 ++- 8 files changed, 47 insertions(+), 10 deletions(-) diff --git a/src/compiler/analyzer/an_assign.c b/src/compiler/analyzer/an_assign.c index 927e997..a4109d7 100644 --- a/src/compiler/analyzer/an_assign.c +++ b/src/compiler/analyzer/an_assign.c @@ -1,6 +1,6 @@ -#include "common/fzl_types.h" -#include "compiler/fzl_analyzer.h" -#include "compiler/fzl_parser.h" +#include +#include +#include #include char asn_problem[256]; diff --git a/src/compiler/analyzer/an_break.c b/src/compiler/analyzer/an_break.c index 3e8d80b..b9a1f6d 100644 --- a/src/compiler/analyzer/an_break.c +++ b/src/compiler/analyzer/an_break.c @@ -19,6 +19,8 @@ fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node) { if (ctx && ctx->type == FZL_AN_CTX_TYPE_LOOP) break; + + ctx = ctx->parent; } if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP) diff --git a/src/compiler/analyzer/an_call.c b/src/compiler/analyzer/an_call.c index 3b1f218..96d1bf9 100644 --- a/src/compiler/analyzer/an_call.c +++ b/src/compiler/analyzer/an_call.c @@ -164,15 +164,31 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) return (fzl_an_res_t){.error = true}; } - for (size_t i = 0; i < sym.node->fn.args_count; i++) + size_t start_i = is_method ? 1 : 0; + for (size_t i = start_i; i < sym.node->fn.args_count; i++) { - fzl_an_res_t req = analyze_common(analyzer, sym.node->fn.args[i]); + fzl_node_t *arg = sym.node->fn.args[i]; + fzl_type_t req_type = an_toktotype(arg->var.type); + char *req_classname = NULL; + if (req_type == FZL_TYPE_OBJECT) + req_classname = arg->var.type->value; + fzl_an_res_t got = analyze_common(analyzer, call_node->call.args[i]); - if (!an_is_compatible(req.type, got.type, req.classname, got.classname)) + if (got.error) + { + if (tmp_method_name != NULL) + { + free(tmp_method_name); + tmp_method_name = NULL; + } + return (fzl_an_res_t){.error = true}; + } + + if (!an_is_compatible(req_type, got.type, req_classname, got.classname)) { snprintf(call_problem, sizeof(call_problem), "expect argument %zu to be %s, got '%s'", i + 1, - an_typetostr(req.type, req.classname), + an_typetostr(req_type, req_classname), an_typetostr(got.type, got.classname)); an_push_error(analyzer, call_node->token, call_problem); diff --git a/src/compiler/analyzer/an_common.c b/src/compiler/analyzer/an_common.c index a8f43fb..7a2d077 100644 --- a/src/compiler/analyzer/an_common.c +++ b/src/compiler/analyzer/an_common.c @@ -8,6 +8,9 @@ // Analyzer main function fzl_an_res_t analyze_common(fzl_analyzer_t *analyzer, fzl_node_t *node) { + if (!analyzer || !node) + return (fzl_an_res_t){.error = true}; + fzl_an_res_t res = {.error = false, .type = FZL_TYPE_NULL, .classname = NULL}; switch (node->type) { diff --git a/src/compiler/analyzer/an_function.c b/src/compiler/analyzer/an_function.c index 6e96be8..ee6bcc4 100644 --- a/src/compiler/analyzer/an_function.c +++ b/src/compiler/analyzer/an_function.c @@ -26,6 +26,13 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node) fzl_an_ctx_t *fnctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_FUNCTION); fnctx->fn = fn_node; + for (size_t i = 0; i < fn_node->fn.args_count; i++) + { + fzl_an_res_t res = analyze_common(analyzer, fn_node->fn.args[i]); + if (res.error) + return (fzl_an_res_t){.error = true}; + } + fzl_an_ctx_t *ctx = analyzer->ctx; while (ctx != NULL) { diff --git a/src/compiler/analyzer/an_return.c b/src/compiler/analyzer/an_return.c index e2a285a..032e04e 100644 --- a/src/compiler/analyzer/an_return.c +++ b/src/compiler/analyzer/an_return.c @@ -24,7 +24,7 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) } fzl_an_ctx_t *fnctx = analyzer->ctx; - while (!fnctx) + while (fnctx != NULL) { if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION) break; @@ -32,7 +32,7 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) fnctx = fnctx->parent; } - if (!fnctx) + if (!fnctx || fnctx->type != FZL_AN_CTX_TYPE_FUNCTION) { an_push_error(analyzer, ret_node->token, "cannot use 'return' outside function"); @@ -55,6 +55,9 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) } fzl_an_res_t ret_res = analyze_common(analyzer, ret_node->ret.value); + if (ret_res.error) + return (fzl_an_res_t){.error = true}; + if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname)) { snprintf(ret_problem, sizeof(ret_problem), diff --git a/src/compiler/analyzer/context.c b/src/compiler/analyzer/context.c index 7bdaaef..e2242e0 100644 --- a/src/compiler/analyzer/context.c +++ b/src/compiler/analyzer/context.c @@ -1,6 +1,7 @@ #include #include #include +#include // Nest context fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type) @@ -12,9 +13,13 @@ fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type) if (!ctx) return NULL; + memset(ctx, 0, sizeof(fzl_an_ctx_t)); + ctx->type = type; if (analyzer->ctx != NULL) ctx->parent = analyzer->ctx; + else + ctx->parent = NULL; analyzer->ctx = ctx; analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt(); diff --git a/src/compiler/main.c b/src/compiler/main.c index 0210e3d..8abd8b8 100644 --- a/src/compiler/main.c +++ b/src/compiler/main.c @@ -155,7 +155,8 @@ int main(int argc, char **argv) if (analyzer->warnings_top > 0) { for (ssize_t i = analyzer->warnings_top - 1; i >= 0; i--) - printf("%sWarning%s: %s\n", FZL_CERROR, FZL_CRST, analyzer->warnings[i]); + printf("%sWarning%s: %s\n", FZL_CWARNING, FZL_CRST, + analyzer->warnings[i]); } fzl_walker_t *walker = fzl_mkwalker(parser->root); From 411d291a2c6c0562c65e67d11eb3e0567d7f09cf Mon Sep 17 00:00:00 2001 From: samsonium Date: Tue, 27 Oct 2026 12:55:46 +0300 Subject: [PATCH 08/11] feat: add VM protection for classes w/o fields --- src/vm/fzl_vm.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/vm/fzl_vm.c b/src/vm/fzl_vm.c index 3537808..0e94957 100644 --- a/src/vm/fzl_vm.c +++ b/src/vm/fzl_vm.c @@ -430,8 +430,10 @@ int fzl_vm_run(fzl_vm_t *vm, bool allow_infinite, uint64_t max_iter) { uint64_t values_count = vm->ops[++vm->ip]; fzl_vm_object_t *obj = malloc(sizeof(fzl_vm_object_t)); - obj->values = malloc(values_count * sizeof(fzl_value_t)); - obj->values_count = values_count; + + if (values_count > 0) + obj->values = malloc(values_count * sizeof(fzl_value_t)); + obj->values_count = values_count; memset(obj->values, 0, values_count * sizeof(fzl_value_t)); fzl_vm_env_t *current_env = vm->estack[vm->etop - 1]; From c61322718d1eacf7b92d70ab752a36ccf3512ffa Mon Sep 17 00:00:00 2001 From: samsonium Date: Tue, 27 Oct 2026 14:12:09 +0300 Subject: [PATCH 09/11] fix: some analysis stage problems --- include/compiler/fzl_analyzer.h | 2 ++ src/compiler/analyzer/an_call.c | 15 ++++++++------- src/compiler/analyzer/an_conditional.c | 10 ++++++++-- src/compiler/analyzer/an_field.c | 4 ++-- src/compiler/analyzer/an_function.c | 8 ++++---- src/compiler/analyzer/context.c | 4 +++- src/compiler/fzl_analyzer.c | 4 ++-- ...alyzer_continues_after_if_without_else.fzs | 9 +++++++++ tests/programs/pos_function_external_call.fzs | 12 ++++++++++++ tests/programs/pos_method_call_via_field.fzs | 19 +++++++++++++++++++ 10 files changed, 69 insertions(+), 18 deletions(-) create mode 100644 tests/programs/neg_analyzer_continues_after_if_without_else.fzs create mode 100644 tests/programs/pos_function_external_call.fzs create mode 100644 tests/programs/pos_method_call_via_field.fzs diff --git a/include/compiler/fzl_analyzer.h b/include/compiler/fzl_analyzer.h index 76cced5..37a1659 100644 --- a/include/compiler/fzl_analyzer.h +++ b/include/compiler/fzl_analyzer.h @@ -32,6 +32,8 @@ typedef struct fzl_an_ctx struct fzl_an_ctx *parent; fzl_an_ctx_type_t type; + fzl_symtable_t *symbols; + size_t symbols_level; union { diff --git a/src/compiler/analyzer/an_call.c b/src/compiler/analyzer/an_call.c index 96d1bf9..4c4fb0c 100644 --- a/src/compiler/analyzer/an_call.c +++ b/src/compiler/analyzer/an_call.c @@ -106,9 +106,9 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) .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_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; @@ -148,11 +148,12 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) return (fzl_an_res_t){.error = true}; } - if (sym.node->fn.args_count != call_node->call.args_count) + size_t start_i = is_method ? 1 : 0; + if ((sym.node->fn.args_count - start_i) != call_node->call.args_count) { snprintf(call_problem, sizeof(call_problem), "expected %zu argument(s) while %zu is specified", - sym.node->fn.args_count, call_node->call.args_count); + sym.node->fn.args_count, call_node->call.args_count - start_i); an_push_error(analyzer, call_node->token, call_problem); if (tmp_method_name != NULL) @@ -164,7 +165,6 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) return (fzl_an_res_t){.error = true}; } - size_t start_i = is_method ? 1 : 0; for (size_t i = start_i; i < sym.node->fn.args_count; i++) { fzl_node_t *arg = sym.node->fn.args[i]; @@ -173,7 +173,8 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) if (req_type == FZL_TYPE_OBJECT) req_classname = arg->var.type->value; - fzl_an_res_t got = analyze_common(analyzer, call_node->call.args[i]); + fzl_an_res_t got = + analyze_common(analyzer, call_node->call.args[i - start_i]); if (got.error) { if (tmp_method_name != NULL) diff --git a/src/compiler/analyzer/an_conditional.c b/src/compiler/analyzer/an_conditional.c index f37dd48..1e5ec96 100644 --- a/src/compiler/analyzer/an_conditional.c +++ b/src/compiler/analyzer/an_conditional.c @@ -20,9 +20,15 @@ fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond) 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 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}; if (cond_res.type != FZL_TYPE_BOOL) diff --git a/src/compiler/analyzer/an_field.c b/src/compiler/analyzer/an_field.c index 15e728d..29fea9c 100644 --- a/src/compiler/analyzer/an_field.c +++ b/src/compiler/analyzer/an_field.c @@ -15,7 +15,7 @@ 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) + if (!analyzer) return (fzl_an_res_t){.error = true}; if (!field || field->type != FZL_NODETYPE_FIELD) @@ -46,7 +46,7 @@ fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field) break; } - if (sym_found) + if (!sym_found) { snprintf(field_problem, sizeof(field_problem), "class '%s' not found", parent.classname); diff --git a/src/compiler/analyzer/an_function.c b/src/compiler/analyzer/an_function.c index ee6bcc4..e2f619c 100644 --- a/src/compiler/analyzer/an_function.c +++ b/src/compiler/analyzer/an_function.c @@ -81,7 +81,7 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node) analyzer->alloc_names[analyzer->alloc_names_top++] = methodname; 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); if (putres != FZL_SYMTABLE_RESULT_OK) { @@ -99,13 +99,13 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node) an_fin_ctx(analyzer); return (fzl_an_res_t){.error = true}; } - fzl_symt_setobjclass(analyzer->symbols[analyzer->symbols_top - 1], - methodname, classname); + fzl_symt_setobjclass(analyzer->symbols[ctx->symbols_level - 1], methodname, + classname); } else { 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); if (putres != FZL_SYMTABLE_RESULT_OK) { diff --git a/src/compiler/analyzer/context.c b/src/compiler/analyzer/context.c index e2242e0..2c3a361 100644 --- a/src/compiler/analyzer/context.c +++ b/src/compiler/analyzer/context.c @@ -21,8 +21,10 @@ fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type) else ctx->parent = NULL; + ctx->symbols = fzl_mksymt(); + ctx->symbols_level = analyzer->symbols_top; analyzer->ctx = ctx; - analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt(); + analyzer->symbols[analyzer->symbols_top++] = ctx->symbols; return ctx; } diff --git a/src/compiler/fzl_analyzer.c b/src/compiler/fzl_analyzer.c index e2a1d43..b35d0cc 100644 --- a/src/compiler/fzl_analyzer.c +++ b/src/compiler/fzl_analyzer.c @@ -47,7 +47,7 @@ bool an_push_error(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *error) size_t ei = aerrors_top++; 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); else snprintf(aerrors[ei], sizeof(aerrors[ei]), " => %s", @@ -64,7 +64,7 @@ bool an_push_warning(fzl_analyzer_t *analyzer, fzl_token_t *tok, char *warning) size_t wi = awarnings_top++; 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); else snprintf(awarnings[wi], sizeof(awarnings[wi]), " => %s", diff --git a/tests/programs/neg_analyzer_continues_after_if_without_else.fzs b/tests/programs/neg_analyzer_continues_after_if_without_else.fzs new file mode 100644 index 0000000..f43f3e6 --- /dev/null +++ b/tests/programs/neg_analyzer_continues_after_if_without_else.fzs @@ -0,0 +1,9 @@ +fn choose(bool flag) int +{ + if (flag) + return 1; + return 0; +} + +int invalid = "not an integer"; +exit(1); diff --git a/tests/programs/pos_function_external_call.fzs b/tests/programs/pos_function_external_call.fzs new file mode 100644 index 0000000..d5d76ad --- /dev/null +++ b/tests/programs/pos_function_external_call.fzs @@ -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); diff --git a/tests/programs/pos_method_call_via_field.fzs b/tests/programs/pos_method_call_via_field.fzs new file mode 100644 index 0000000..c2f057c --- /dev/null +++ b/tests/programs/pos_method_call_via_field.fzs @@ -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(); From dc80de66e4f606636b94b3a55c94866fad141726 Mon Sep 17 00:00:00 2001 From: samsonium Date: Tue, 27 Oct 2026 15:40:25 +0300 Subject: [PATCH 10/11] fix: review problems --- include/compiler/fzl_analyzer.h | 6 ++ src/compiler/analyzer/an_assign.c | 20 +++--- src/compiler/analyzer/an_binmath.c | 18 +++--- src/compiler/analyzer/an_block.c | 27 ++++---- src/compiler/analyzer/an_break.c | 4 +- src/compiler/analyzer/an_call.c | 88 ++++++++++++++++---------- src/compiler/analyzer/an_class.c | 12 ++-- src/compiler/analyzer/an_condition.c | 22 ++----- src/compiler/analyzer/an_conditional.c | 8 +-- src/compiler/analyzer/an_construct.c | 8 +-- src/compiler/analyzer/an_declaration.c | 19 ++---- src/compiler/analyzer/an_field.c | 23 +++---- src/compiler/analyzer/an_function.c | 17 ++--- src/compiler/analyzer/an_identifier.c | 8 +-- src/compiler/analyzer/an_loop.c | 25 +++----- src/compiler/analyzer/an_return.c | 60 +++++++++++++----- src/compiler/analyzer/an_unarycond.c | 2 +- src/compiler/analyzer/an_unarymath.c | 2 +- src/compiler/fzl_analyzer.c | 2 + src/compiler/utils/fzl_linked.c | 2 +- src/vm/fzl_vm.c | 3 +- 21 files changed, 197 insertions(+), 179 deletions(-) diff --git a/include/compiler/fzl_analyzer.h b/include/compiler/fzl_analyzer.h index 37a1659..4050ba4 100644 --- a/include/compiler/fzl_analyzer.h +++ b/include/compiler/fzl_analyzer.h @@ -76,6 +76,12 @@ void fzl_analyze(fzl_analyzer_t *analyzer); // Free 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_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node); fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id); diff --git a/src/compiler/analyzer/an_assign.c b/src/compiler/analyzer/an_assign.c index a4109d7..120a6cd 100644 --- a/src/compiler/analyzer/an_assign.c +++ b/src/compiler/analyzer/an_assign.c @@ -3,12 +3,6 @@ #include #include -char asn_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 char *an_typetostr(fzl_type_t type, char *classname); - // Analyze assigns fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node) { @@ -17,7 +11,7 @@ fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node) if (!assign_node || assign_node->type != FZL_NODETYPE_ASSIGN) { - an_push_error(analyzer, NULL, "node is not typeof ASSIGN"); + an_push_error(analyzer, NULL, "node is not assignment"); return (fzl_an_res_t){.error = true}; } @@ -35,17 +29,19 @@ fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node) left->type != FZL_NODETYPE_IDENTIFIER && left->type != FZL_NODETYPE_FIELD) { an_push_error(analyzer, left->token, - "this type of assignment left is not supported"); + "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(asn_problem, sizeof(asn_problem), "cannot assign %s to %s", - an_typetostr(rightside.type, rightside.classname), - an_typetostr(leftside.type, leftside.classname)); - an_push_error(analyzer, assign_node->binary_op.right->token, asn_problem); + 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}; } diff --git a/src/compiler/analyzer/an_binmath.c b/src/compiler/analyzer/an_binmath.c index 2e89aac..2d2f522 100644 --- a/src/compiler/analyzer/an_binmath.c +++ b/src/compiler/analyzer/an_binmath.c @@ -1,9 +1,7 @@ #include #include #include - -// defined in `compiler/analyzer/utility.c` -extern bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc); +#include // Analyze binary arithmetics fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node) @@ -18,7 +16,7 @@ fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node) 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}; } @@ -32,15 +30,19 @@ fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node) if (left.type < FZL_TYPE_INT8) { - an_push_error(analyzer, bin_node->binary_op.left->token, - "left side is not an integer type"); + snprintf(an_problem, sizeof(an_problem), + "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}; } if (right.type < FZL_TYPE_INT8) { - an_push_error(analyzer, bin_node->binary_op.right->token, - "right side is not an integer type"); + snprintf(an_problem, sizeof(an_problem), + "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}; } diff --git a/src/compiler/analyzer/an_block.c b/src/compiler/analyzer/an_block.c index 639dbe0..f4ce9be 100644 --- a/src/compiler/analyzer/an_block.c +++ b/src/compiler/analyzer/an_block.c @@ -5,12 +5,6 @@ #include #include -char block_problem[256]; - -// defined in `compiler/analyzer/utility.c` -extern fzl_type_t an_toktotype(fzl_token_t *tok); -extern char *an_typetostr(fzl_type_t type, char *classname); - // Analyze block fzl_an_res_t analyze_block(fzl_analyzer_t *analyzer, fzl_node_t *block_node) { @@ -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) { - snprintf(block_problem, sizeof(block_problem), "node is not typeof BLOCK"); - an_push_error(analyzer, NULL, block_problem); + snprintf(an_problem, sizeof(an_problem), "node is not a block"); + an_push_error(analyzer, NULL, an_problem); 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) 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 = 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) { - snprintf(block_problem, sizeof(block_problem), - "expected 'return <%s>' in function because return type is %s", - rettype_str, rettype_str); - an_push_error(analyzer, block_node->token, block_problem); + snprintf(an_problem, sizeof(an_problem), + "'return <%s>' is expected in the function with non-null " + "return type", + rettype_str); + an_push_error(analyzer, block_node->token, an_problem); 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]; 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>'", 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}; } } diff --git a/src/compiler/analyzer/an_break.c b/src/compiler/analyzer/an_break.c index b9a1f6d..fb83f44 100644 --- a/src/compiler/analyzer/an_break.c +++ b/src/compiler/analyzer/an_break.c @@ -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) { - 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}; } @@ -26,7 +26,7 @@ fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node) if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP) { 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}; } diff --git a/src/compiler/analyzer/an_call.c b/src/compiler/analyzer/an_call.c index 4c4fb0c..3776b8d 100644 --- a/src/compiler/analyzer/an_call.c +++ b/src/compiler/analyzer/an_call.c @@ -9,8 +9,9 @@ #include #include -char *tmp_method_name = NULL; -char call_problem[256] = {0}; +char *tmp_method_name = NULL; +char *tmp_fnname = NULL; +char *tmp_classname = NULL; // Try to get symbol name from callee 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; if (callee->type != FZL_NODETYPE_FIELD) + { + an_push_error(analyzer, callee->token, "unknown call format"); return NULL; + } *method = true; 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; 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 methodname_len = strlen(fn_name); tmp_method_name = malloc(classname_len + methodname_len + 2); if (!tmp_method_name) { - an_push_error(analyzer, callee->token, - "failed to allocate space for method name"); + an_push_error(analyzer, callee->token, "out of memory"); return NULL; } @@ -47,17 +52,12 @@ char *resolve_callee(fzl_analyzer_t *analyzer, fzl_node_t *callee, bool *method) 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 fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) { 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}; } @@ -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}; } + // NOTE: move into `compiler/analyzer/builtins.c` if (strcmp(callee_name, "printline") == 0 || strcmp(callee_name, "exit") == 0) { 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", 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) { @@ -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}; } - // 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--) @@ -120,9 +117,9 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) 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); - an_push_error(analyzer, call_node->token, call_problem); + an_push_error(analyzer, call_node->token, an_problem); 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) { - snprintf(call_problem, sizeof(call_problem), "'%s' is not callable", + snprintf(an_problem, sizeof(an_problem), "'%s' is not callable", 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) { @@ -148,13 +145,24 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) return (fzl_an_res_t){.error = true}; } - size_t start_i = is_method ? 1 : 0; - if ((sym.node->fn.args_count - start_i) != 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), - "expected %zu argument(s) while %zu is specified", - sym.node->fn.args_count, call_node->call.args_count - start_i); - an_push_error(analyzer, call_node->token, call_problem); + if (is_method) + { + snprintf(an_problem, sizeof(an_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) { @@ -165,7 +173,7 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) return (fzl_an_res_t){.error = true}; } - for (size_t i = start_i; i < sym.node->fn.args_count; i++) + for (size_t i = offset; i < sym.node->fn.args_count; i++) { fzl_node_t *arg = sym.node->fn.args[i]; fzl_type_t req_type = an_toktotype(arg->var.type); @@ -174,7 +182,7 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) req_classname = arg->var.type->value; fzl_an_res_t got = - analyze_common(analyzer, call_node->call.args[i - start_i]); + analyze_common(analyzer, call_node->call.args[i - offset]); if (got.error) { if (tmp_method_name != NULL) @@ -187,11 +195,25 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node) if (!an_is_compatible(req_type, got.type, req_classname, got.classname)) { - snprintf(call_problem, sizeof(call_problem), - "expect argument %zu to be %s, got '%s'", i + 1, - an_typetostr(req_type, req_classname), - an_typetostr(got.type, got.classname)); - an_push_error(analyzer, call_node->token, call_problem); + 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)); + } + 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) { diff --git a/src/compiler/analyzer/an_class.c b/src/compiler/analyzer/an_class.c index da264b6..ceccd86 100644 --- a/src/compiler/analyzer/an_class.c +++ b/src/compiler/analyzer/an_class.c @@ -4,8 +4,6 @@ #include #include -char class_problem[256]; - // Analyze class declaration 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) { - 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}; } @@ -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) { - snprintf(class_problem, sizeof(class_problem), + snprintf(an_problem, sizeof(an_problem), "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 { - snprintf(class_problem, sizeof(class_problem), "cannot store name '%s'", + snprintf(an_problem, sizeof(an_problem), "cannot store name '%s'", classname); - an_push_error(analyzer, class_node->token, class_problem); + an_push_error(analyzer, class_node->token, an_problem); } an_fin_ctx(analyzer); diff --git a/src/compiler/analyzer/an_condition.c b/src/compiler/analyzer/an_condition.c index 0a0505b..8473b54 100644 --- a/src/compiler/analyzer/an_condition.c +++ b/src/compiler/analyzer/an_condition.c @@ -3,12 +3,6 @@ #include #include -char bincond_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 char *an_typetostr(fzl_type_t type, char *classname); - // Analyze binary conditional nodes fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node) { @@ -24,7 +18,7 @@ fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node) cond_node->type != FZL_NODETYPE_EQUAL && cond_node->type != FZL_NODETYPE_NEQUAL)) { - an_push_error(analyzer, NULL, "node is not typeof binary condition"); + an_push_error(analyzer, NULL, "node is not a binary conditional operator"); return (fzl_an_res_t){.error = true}; } @@ -54,14 +48,14 @@ fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node) if (left.type < FZL_TYPE_INT8) { an_push_error(analyzer, cond_node->binary_op.left->token, - "left operand must be number"); + "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 be number"); + "right operand must have integer type"); return (fzl_an_res_t){.error = true}; } } @@ -70,7 +64,7 @@ fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node) if (right.type < FZL_TYPE_INT8) { an_push_error(analyzer, cond_node->binary_op.right->token, - "right operand must be an integer"); + "right operand must have integer type"); return (fzl_an_res_t){.error = true}; } @@ -89,12 +83,10 @@ fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node) if (!an_is_compatible(left.type, right.type, left.classname, right.classname)) { - snprintf(bincond_problem, sizeof(bincond_problem), - "'%s' and '%s' are incompatible", + 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, - bincond_problem); + an_push_error(analyzer, cond_node->binary_op.right->token, an_problem); return (fzl_an_res_t){.error = true}; } } @@ -110,7 +102,7 @@ fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node) else if (!an_is_compatible(left.type, right.type, left.classname, right.classname)) { - snprintf(bincond_problem, sizeof(bincond_problem), + 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)); diff --git a/src/compiler/analyzer/an_conditional.c b/src/compiler/analyzer/an_conditional.c index 1e5ec96..5a4002d 100644 --- a/src/compiler/analyzer/an_conditional.c +++ b/src/compiler/analyzer/an_conditional.c @@ -4,8 +4,6 @@ #include #include -char cond_problem[256]; - // Analyze conditional statement fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond) { @@ -14,7 +12,7 @@ fzl_an_res_t analyze_conditional(fzl_analyzer_t *analyzer, fzl_node_t *cond) 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}; } @@ -37,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) { - 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); - 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}; diff --git a/src/compiler/analyzer/an_construct.c b/src/compiler/analyzer/an_construct.c index 5c9202d..3529aaf 100644 --- a/src/compiler/analyzer/an_construct.c +++ b/src/compiler/analyzer/an_construct.c @@ -5,8 +5,6 @@ #include #include -char c_problem[256]; - // Analyze class 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) { - 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}; } @@ -30,8 +28,8 @@ fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct) if (!sym_found) { - snprintf(c_problem, sizeof(c_problem), "class '%s' not found", classname); - an_push_error(analyzer, construct->token, c_problem); + snprintf(an_problem, sizeof(an_problem), "class '%s' not found", classname); + an_push_error(analyzer, construct->token, an_problem); return (fzl_an_res_t){.error = true}; } diff --git a/src/compiler/analyzer/an_declaration.c b/src/compiler/analyzer/an_declaration.c index efc0300..4f950dc 100644 --- a/src/compiler/analyzer/an_declaration.c +++ b/src/compiler/analyzer/an_declaration.c @@ -6,11 +6,6 @@ #include #include -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) { @@ -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) { - 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}; } @@ -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_EXISTS) - snprintf(dec_problem, sizeof(dec_problem), - "'%s' already has been declared", name); + snprintf(an_problem, sizeof(an_problem), "'%s' already has been declared", + name); 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}; } @@ -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) { - 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); - 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}; } diff --git a/src/compiler/analyzer/an_field.c b/src/compiler/analyzer/an_field.c index 29fea9c..e231f5d 100644 --- a/src/compiler/analyzer/an_field.c +++ b/src/compiler/analyzer/an_field.c @@ -7,11 +7,6 @@ #include #include -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) { @@ -20,7 +15,7 @@ fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *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}; } @@ -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) { - snprintf(field_problem, sizeof(field_problem), - "access to non-object value"); - an_push_error(analyzer, field->token, field_problem); + snprintf(an_problem, sizeof(an_problem), "access to non-object value (%s)", + an_typetostr(parent.type, parent.classname)); + an_push_error(analyzer, field->token, an_problem); return (fzl_an_res_t){.error = true}; } @@ -48,9 +43,9 @@ fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field) 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); - 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}; } @@ -67,9 +62,9 @@ fzl_an_res_t analyze_field(fzl_analyzer_t *analyzer, fzl_node_t *field) 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); + snprintf(an_problem, sizeof(an_problem), "field '%s.%s' does not exists", + parent.classname, field_name); + an_push_error(analyzer, field->memb.field->token, an_problem); return (fzl_an_res_t){.error = true}; } diff --git a/src/compiler/analyzer/an_function.c b/src/compiler/analyzer/an_function.c index e2f619c..a555fed 100644 --- a/src/compiler/analyzer/an_function.c +++ b/src/compiler/analyzer/an_function.c @@ -7,11 +7,6 @@ #include #include -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) @@ -19,7 +14,7 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node) 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}; } @@ -63,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) { 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); return (fzl_an_res_t){.error = true}; } @@ -87,10 +82,10 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node) { 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, (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 an_push_error(analyzer, fn_node->token, "failed to store method"); @@ -111,10 +106,10 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node) { if (putres == FZL_SYMTABLE_RESULT_EXISTS) { - snprintf(fn_problem, sizeof(fn_problem), + snprintf(an_problem, sizeof(an_problem), "function '%s' already has been declared", (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 an_push_error(analyzer, fn_node->token, "failed to store function"); diff --git a/src/compiler/analyzer/an_identifier.c b/src/compiler/analyzer/an_identifier.c index a56c2cd..da80f41 100644 --- a/src/compiler/analyzer/an_identifier.c +++ b/src/compiler/analyzer/an_identifier.c @@ -3,8 +3,6 @@ #include #include -char id_problem[256]; - // Analyze identifier 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) { - 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}; } @@ -28,9 +26,9 @@ fzl_an_res_t analyze_identifier(fzl_analyzer_t *analyzer, fzl_node_t *id) if (!sym_found) { - snprintf(id_problem, sizeof(id_problem), "cannot find symbol '%s'", + snprintf(an_problem, sizeof(an_problem), "cannot find symbol '%s'", symname); - an_push_error(analyzer, id->token, id_problem); + an_push_error(analyzer, id->token, an_problem); return (fzl_an_res_t){.error = true}; } diff --git a/src/compiler/analyzer/an_loop.c b/src/compiler/analyzer/an_loop.c index 3568233..d2e1aa7 100644 --- a/src/compiler/analyzer/an_loop.c +++ b/src/compiler/analyzer/an_loop.c @@ -7,11 +7,6 @@ #include #include -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 { @@ -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 && 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 " "mistake?", 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 && 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'", 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 && 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'", 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 && 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'", 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 || cond_data.type == COND_TYPE_VAR_GREATER) && 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); - an_push_warning(analyzer, loop_node->loop.step->token, loop_problem); + an_push_warning(analyzer, loop_node->loop.step->token, an_problem); } } } diff --git a/src/compiler/analyzer/an_return.c b/src/compiler/analyzer/an_return.c index 032e04e..543bc6f 100644 --- a/src/compiler/analyzer/an_return.c +++ b/src/compiler/analyzer/an_return.c @@ -1,16 +1,9 @@ -#include "common/fzl_types.h" +#include #include #include #include #include -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 fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) { @@ -19,7 +12,7 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) 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}; } @@ -32,10 +25,22 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) fnctx = fnctx->parent; } + 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, - "cannot use 'return' outside function"); + "cannot use 'return' outside the function"); return (fzl_an_res_t){.error = true}; } @@ -49,8 +54,20 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) { if (ret_node->ret.value == NULL) { - an_push_error(analyzer, ret_node->token, - "return must be used with value in this function"); + if (is_in_method) + { + 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}; } @@ -60,10 +77,21 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node) if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname)) { - snprintf(ret_problem, sizeof(ret_problem), - "expected 'return <%s>' instead of 'return <%s>'", rettype_str, - an_typetostr(ret_res.type, ret_res.classname)); - an_push_error(analyzer, ret_node->token, ret_problem); + if (is_in_method) + { + 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_push_error(analyzer, ret_node->token, an_problem); return (fzl_an_res_t){.error = true}; } } diff --git a/src/compiler/analyzer/an_unarycond.c b/src/compiler/analyzer/an_unarycond.c index 29123dc..3a7cc68 100644 --- a/src/compiler/analyzer/an_unarycond.c +++ b/src/compiler/analyzer/an_unarycond.c @@ -10,7 +10,7 @@ fzl_an_res_t analyze_unarycond(fzl_analyzer_t *analyzer, fzl_node_t *unary) if (!unary || unary->type != FZL_NODETYPE_NOT) { - an_push_error(analyzer, NULL, "node is not typeof unary condition"); + an_push_error(analyzer, NULL, "node is not an unary condition operator"); return (fzl_an_res_t){.error = true}; } diff --git a/src/compiler/analyzer/an_unarymath.c b/src/compiler/analyzer/an_unarymath.c index 6df52a5..3f592aa 100644 --- a/src/compiler/analyzer/an_unarymath.c +++ b/src/compiler/analyzer/an_unarymath.c @@ -12,7 +12,7 @@ fzl_an_res_t analyze_unarymath(fzl_analyzer_t *analyzer, fzl_node_t *unary) if (!unary || (unary->type != FZL_NODETYPE_UNARY_MINUS && unary->type != FZL_NODETYPE_UNARY_PLUS)) { - an_push_error(analyzer, NULL, "node is not typeof unary math"); + an_push_error(analyzer, NULL, "node is not an unary math operator"); return (fzl_an_res_t){.error = true}; } diff --git a/src/compiler/fzl_analyzer.c b/src/compiler/fzl_analyzer.c index b35d0cc..9b4603f 100644 --- a/src/compiler/fzl_analyzer.c +++ b/src/compiler/fzl_analyzer.c @@ -14,6 +14,8 @@ size_t aerrors_top; char awarnings[256][256]; size_t awarnings_top; +char an_problem[256]; + fzl_analyzer_t *fzl_mkanalyzer(fzl_node_t *node) { if (!node) diff --git a/src/compiler/utils/fzl_linked.c b/src/compiler/utils/fzl_linked.c index 409455b..26a6f10 100644 --- a/src/compiler/utils/fzl_linked.c +++ b/src/compiler/utils/fzl_linked.c @@ -51,7 +51,7 @@ bool fzl_linked_add_files(fzl_linked_t *l, char **files, size_t files_count) rewind(fd); source = malloc(file_sz + 1); - fread(source, file_sz, 1, fd); + fread(source, file_sz, 1, fd); // NOTE: add red bytes check fclose(fd); source[file_sz] = 0; diff --git a/src/vm/fzl_vm.c b/src/vm/fzl_vm.c index 0e94957..c3e4552 100644 --- a/src/vm/fzl_vm.c +++ b/src/vm/fzl_vm.c @@ -50,7 +50,8 @@ fzl_vm_t *fzl_mkvm(const char *filepath) } 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) { snprintf(last_err, sizeof(last_err), "magic check error"); From 6498c73960f439f31ffae5da704282814aa43b04 Mon Sep 17 00:00:00 2001 From: samsonium Date: Tue, 27 Oct 2026 15:42:02 +0300 Subject: [PATCH 11/11] ref: move linked list example into tests folder --- exl/linked-list.fzs => tests/programs/pos_linked_list.fzs | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename exl/linked-list.fzs => tests/programs/pos_linked_list.fzs (100%) diff --git a/exl/linked-list.fzs b/tests/programs/pos_linked_list.fzs similarity index 100% rename from exl/linked-list.fzs rename to tests/programs/pos_linked_list.fzs