Impl analyzer #5
+7
-6
@@ -14,18 +14,19 @@ include_directories(include)
|
|||||||
|
|
||||||
# Compiler
|
# Compiler
|
||||||
add_executable(fzc
|
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_const_pool.c
|
||||||
src/compiler/utils/fzl_symtable.c
|
src/compiler/utils/fzl_symtable.c
|
||||||
src/compiler/utils/fzl_linked.c
|
src/compiler/utils/fzl_linked.c
|
||||||
src/compiler/utils/fzl_options.c
|
src/compiler/utils/fzl_options.c
|
||||||
src/compiler/utils/fzl_ctable.c
|
src/compiler/utils/fzl_ctable.c
|
||||||
src/compiler/checkers/fzl_loop_check.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)
|
src/compiler/main.c)
|
||||||
|
|
||||||
# Virtual machine
|
# Virtual machine
|
||||||
|
|||||||
@@ -0,0 +1,28 @@
|
|||||||
|
#ifndef FZL_ANALYZER_H
|
||||||
|
#define FZL_ANALYZER_H
|
||||||
|
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
|
||||||
|
// 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
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
|
#include <compiler/fzl_parser.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
+34
-1
@@ -1,4 +1,5 @@
|
|||||||
#include <common/fzl_colors.h>
|
#include <common/fzl_colors.h>
|
||||||
|
#include <compiler/fzl_analyzer.h>
|
||||||
#include <compiler/fzl_debug.h>
|
#include <compiler/fzl_debug.h>
|
||||||
#include <compiler/fzl_parser.h>
|
#include <compiler/fzl_parser.h>
|
||||||
#include <compiler/fzl_reader.h>
|
#include <compiler/fzl_reader.h>
|
||||||
@@ -122,7 +123,39 @@ int main(int argc, char **argv)
|
|||||||
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
printf("%sok%s!\n", FZL_COK, FZL_CRST);
|
||||||
fzl_debug_print_ast(parser->root);
|
fzl_debug_print_ast(parser->root);
|
||||||
printf("%s> building%s\n", FZL_CDEBUG, FZL_CRST);
|
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);
|
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
||||||
|
|||||||
Reference in New Issue
Block a user