From 62ae05b4cdac1e4a0c70a665cabe983f25efe9d1 Mon Sep 17 00:00:00 2001 From: samsonium Date: Mon, 26 Oct 2026 20:47:24 +0300 Subject: [PATCH] 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; }