From 4904f400370428d123c6125955d5007d2ee58e56 Mon Sep 17 00:00:00 2001 From: samsonium Date: Sun, 25 Oct 2026 19:24:02 +0300 Subject: [PATCH] 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);