Impl analyzer #5
@@ -14,18 +14,39 @@ 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/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
|
||||
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/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
|
||||
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
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
#ifndef FZL_ANALYZER_H
|
||||
#define FZL_ANALYZER_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
// 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_CLASS, // when analyzing class
|
||||
} fzl_an_ctx_type_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_symtable_t *symbols;
|
||||
size_t symbols_level;
|
||||
|
||||
union
|
||||
{
|
||||
fzl_node_t *fn;
|
||||
char *classname;
|
||||
};
|
||||
} 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;
|
||||
|
||||
char *alloc_names[256];
|
||||
size_t alloc_names_top;
|
||||
|
||||
fzl_node_t *root;
|
||||
fzl_an_ctx_t *ctx;
|
||||
} fzl_analyzer_t;
|
||||
|
||||
// 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);
|
||||
|
||||
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);
|
||||
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);
|
||||
|
samsonium marked this conversation as resolved
Outdated
|
||||
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);
|
||||
|
||||
#endif // FZL_ANALYZER_H
|
||||
@@ -2,6 +2,7 @@
|
||||
#define FZL_SYMTABLE_H
|
||||
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdint.h>
|
||||
@@ -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,
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze assigns
|
||||
|
samsonium
commented
Make a common 2nd level string buffer for errors in Make a common 2nd level string buffer for errors in `analyze_*` functions
|
||||
fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
|
samsonium
commented
Move these Move these `extern`s to the same header as `analyze_*` function
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!assign_node || assign_node->type != FZL_NODETYPE_ASSIGN)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not assignment");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *left = assign_node->binary_op.left;
|
||||
fzl_an_res_t leftside = analyze_common(analyzer, left);
|
||||
if (leftside.error)
|
||||
|
samsonium
commented
"... is not assignment" "... is not assignment"
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t rightside =
|
||||
analyze_common(analyzer, assign_node->binary_op.right);
|
||||
if (rightside.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (left->type != FZL_NODETYPE_DECLARATION &&
|
||||
left->type != FZL_NODETYPE_IDENTIFIER && left->type != FZL_NODETYPE_FIELD)
|
||||
{
|
||||
an_push_error(analyzer, left->token,
|
||||
"left side must be one of these: declaration, identifier or "
|
||||
"field access");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(leftside.type, rightside.type, leftside.classname,
|
||||
rightside.classname))
|
||||
|
samsonium
commented
need more understandable message need more understandable message
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"incompatible types - '%s' is expected, got '%s'",
|
||||
an_typetostr(leftside.type, leftside.classname),
|
||||
an_typetostr(rightside.type, rightside.classname));
|
||||
an_push_error(analyzer, assign_node->binary_op.right->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze binary arithmetics
|
||||
|
samsonium
commented
Also look for another Also look for another `extern` functions in `an_*.c` files
|
||||
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 binary math operator");
|
||||
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)
|
||||
{
|
||||
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);
|
||||
|
samsonium
commented
"expected integer type, got M" "expected integer type, got M"
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (right.type < FZL_TYPE_INT8)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"integer type is expected, got '%s'",
|
||||
|
samsonium
commented
"expected integer type, got N" "expected integer type, got N"
|
||||
an_typetostr(right.type, right.classname));
|
||||
an_push_error(analyzer, bin_node->binary_op.right->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = left.type, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze block
|
||||
|
samsonium
commented
Also replace this "per-analyzer-buffers" with single one in other "an_*.c" files Also replace this "per-analyzer-buffers" with single one in other "an_*.c" files
|
||||
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(an_problem, sizeof(an_problem), "node is not a block");
|
||||
an_push_error(analyzer, NULL, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY);
|
||||
|
||||
|
samsonium
commented
"... is not a block" "... is not a block"
|
||||
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 statement will never execute");
|
||||
|
||||
fzl_an_res_t entry_res =
|
||||
analyze_common(analyzer, block_node->list.entries[i]);
|
||||
if (entry_res.error)
|
||||
error = true;
|
||||
|
||||
|
samsonium
commented
"statement", not "node" "statement", not "node"
|
||||
if (block_node->list.entries[i]->type == FZL_NODETYPE_RETURN)
|
||||
has_return = true;
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *fnctx = analyzer->ctx;
|
||||
while (fnctx != NULL)
|
||||
{
|
||||
if (fnctx && 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(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};
|
||||
}
|
||||
|
||||
|
samsonium
commented
"expect 'return <%s>' inside function with non-null return type" "expect 'return <%s>' inside function with non-null return type"
|
||||
fzl_node_t *last_node =
|
||||
block_node->list.entries[block_node->list.entry_count - 1];
|
||||
if (last_node->type != FZL_NODETYPE_RETURN)
|
||||
{
|
||||
// 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, an_problem);
|
||||
|
samsonium
commented
Replace with ghost walker to check if all branches returning a value Replace with ghost walker to check if all branches returning a value
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
|
||||
// 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 a break statement");
|
||||
|
samsonium
commented
"... is not break statement" "... is not break statement"
|
||||
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;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP)
|
||||
{
|
||||
an_push_error(analyzer, break_node->token,
|
||||
"cannot use 'break' outside the loop");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,238 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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)
|
||||
{
|
||||
*method = false;
|
||||
|
||||
if (callee->type == FZL_NODETYPE_IDENTIFIER)
|
||||
return (char *)callee->token->value;
|
||||
|
||||
if (callee->type != FZL_NODETYPE_FIELD)
|
||||
|
samsonium
commented
add an error for unknown callee type add an error for unknown callee type
|
||||
{
|
||||
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);
|
||||
if (parent_res.type != FZL_TYPE_OBJECT || !parent_res.classname)
|
||||
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, "out of memory");
|
||||
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;
|
||||
}
|
||||
|
||||
// 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, "node is not call");
|
||||
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);
|
||||
if (!callee_name)
|
||||
{
|
||||
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};
|
||||
}
|
||||
|
||||
// NOTE: move into `compiler/analyzer/builtins.c`
|
||||
|
samsonium
commented
Move builtins outside (f.e. Move builtins outside (f.e. `compiler/analyzer/builtins.c`)
|
||||
if (strcmp(callee_name, "printline") == 0 || strcmp(callee_name, "exit") == 0)
|
||||
{
|
||||
if (call_node->call.args_count != 1)
|
||||
{
|
||||
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, an_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};
|
||||
}
|
||||
|
||||
|
samsonium
commented
remove remove
|
||||
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(an_problem, sizeof(an_problem), "cannot resolve symbol '%s'",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (sym.node->type != FZL_NODETYPE_FUNCTION)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "'%s' is not callable",
|
||||
callee_name);
|
||||
an_push_error(analyzer, call_node->token, an_problem);
|
||||
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
size_t offset = is_method ? 1 : 0;
|
||||
if ((sym.node->fn.args_count - offset) != call_node->call.args_count)
|
||||
{
|
||||
if (is_method)
|
||||
|
samsonium
commented
rename variable to rename variable to `offset`
|
||||
{
|
||||
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,
|
||||
|
samsonium
commented
`function '%s' requires %zu argument(s), got %zu` (or `method`, if `is_method == true`)
|
||||
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)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
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);
|
||||
char *req_classname = NULL;
|
||||
if (req_type == FZL_TYPE_OBJECT)
|
||||
req_classname = arg->var.type->value;
|
||||
|
||||
fzl_an_res_t got =
|
||||
analyze_common(analyzer, call_node->call.args[i - offset]);
|
||||
if (got.error)
|
||||
{
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
|
samsonium
commented
`function '%s' is expecting argument %zu to be '%s', got '%s'`
|
||||
}
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(req_type, got.type, req_classname, got.classname))
|
||||
{
|
||||
if (is_method)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"method '%s.%s' is expecting %zu argument to have type '%s', "
|
||||
"got '%s'",
|
||||
tmp_classname, tmp_fnname, i + 1 - offset,
|
||||
an_typetostr(req_type, req_classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
}
|
||||
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)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
@@ -0,0 +1,71 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze class declaration
|
||||
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 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(an_problem, sizeof(an_problem),
|
||||
"name '%s' already has been declared", classname);
|
||||
an_push_error(analyzer, class_node->token, an_problem);
|
||||
}
|
||||
else
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot store name '%s'",
|
||||
classname);
|
||||
an_push_error(analyzer, class_node->token, an_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};
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stddef.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
// 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)
|
||||
{
|
||||
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:
|
||||
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;
|
||||
|
||||
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:
|
||||
res = analyze_block(analyzer, node);
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,114 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// Analyze binary conditional nodes
|
||||
fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!cond_node || (cond_node->type != FZL_NODETYPE_OR &&
|
||||
cond_node->type != FZL_NODETYPE_AND &&
|
||||
cond_node->type != FZL_NODETYPE_LESS &&
|
||||
cond_node->type != FZL_NODETYPE_LESS_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_GREATER &&
|
||||
cond_node->type != FZL_NODETYPE_GREATER_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_NEQUAL))
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not a binary conditional operator");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t left = analyze_common(analyzer, cond_node->binary_op.left);
|
||||
if (left.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t right = analyze_common(analyzer, cond_node->binary_op.right);
|
||||
if (right.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (cond_node->type == FZL_NODETYPE_OR || cond_node->type == FZL_NODETYPE_AND)
|
||||
{
|
||||
if (left.type != FZL_TYPE_BOOL)
|
||||
an_push_warning(analyzer, cond_node->binary_op.left->token,
|
||||
"left operand is not resulting with boolean type");
|
||||
|
||||
if (right.type != FZL_TYPE_BOOL)
|
||||
an_push_warning(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand is not resulting with boolean type");
|
||||
}
|
||||
else if (cond_node->type == FZL_NODETYPE_LESS ||
|
||||
cond_node->type == FZL_NODETYPE_LESS_EQUAL ||
|
||||
cond_node->type == FZL_NODETYPE_GREATER ||
|
||||
cond_node->type == FZL_NODETYPE_GREATER_EQUAL)
|
||||
{
|
||||
if (left.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"left operand must have integer type");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (left.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"right operand must have integer type");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (left.type >= FZL_TYPE_INT8)
|
||||
{
|
||||
if (right.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand must have integer type");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
else if (left.type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
if (!left.classname || !right.classname)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"unhandled error :(");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(left.type, right.type, left.classname,
|
||||
right.classname))
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "'%s' and '%s' are incompatible",
|
||||
an_typetostr(left.type, left.classname),
|
||||
an_typetostr(right.type, right.classname));
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (left.type == FZL_TYPE_STRING)
|
||||
{
|
||||
if (right.type != FZL_TYPE_STRING)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand must be a string");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (!an_is_compatible(left.type, right.type, left.classname,
|
||||
right.classname))
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem),
|
||||
"equality not supported between %s and %s",
|
||||
an_typetostr(left.type, left.classname),
|
||||
an_typetostr(right.type, right.classname));
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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 a condition");
|
||||
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);
|
||||
|
||||
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)
|
||||
an_push_warning(analyzer, cond->cond.condition->token,
|
||||
"resulting type is not bool");
|
||||
|
||||
if (cond->cond.condition->token->type == FZL_TOKTYPE_BOOL_LITERAL)
|
||||
{
|
||||
snprintf(an_problem, sizeof(an_problem), "condition is always %s",
|
||||
(char *)cond->cond.condition->token->value);
|
||||
an_push_warning(analyzer, cond->cond.condition->token, an_problem);
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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 a 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(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};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_OBJECT, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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 a 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(an_problem, sizeof(an_problem), "'%s' already has been declared",
|
||||
name);
|
||||
else
|
||||
snprintf(an_problem, sizeof(an_problem), "cannot store '%s'", name);
|
||||
|
||||
an_push_error(analyzer, decl->token, an_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(an_problem, sizeof(an_problem), "class '%s' does not exists",
|
||||
(char *)decl->var.type->value);
|
||||
an_push_error(analyzer, decl->var.type, an_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 = type, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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 field access");
|
||||
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(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};
|
||||
}
|
||||
|
||||
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(an_problem, sizeof(an_problem), "class '%s' not found",
|
||||
parent.classname);
|
||||
an_push_error(analyzer, field->parent->token, an_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(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};
|
||||
}
|
||||
|
||||
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};
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
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 a 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;
|
||||
|
||||
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)
|
||||
{
|
||||
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 in methods");
|
||||
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[ctx->symbols_level - 1], fn_node,
|
||||
methodname, rettype, 0);
|
||||
if (putres != FZL_SYMTABLE_RESULT_OK)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
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, an_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[ctx->symbols_level - 1], methodname,
|
||||
classname);
|
||||
}
|
||||
else
|
||||
{
|
||||
fzl_symtable_res_t putres =
|
||||
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)
|
||||
{
|
||||
if (putres == FZL_SYMTABLE_RESULT_EXISTS)
|
||||
{
|
||||
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, an_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};
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not an identifier");
|
||||
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(an_problem, sizeof(an_problem), "cannot find symbol '%s'",
|
||||
symname);
|
||||
an_push_error(analyzer, id->token, an_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = sym.type, .classname = sym.classname};
|
||||
}
|
||||
@@ -0,0 +1,455 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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(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, an_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(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, an_problem);
|
||||
}
|
||||
|
||||
if (cond_data.type == COND_TYPE_VAR_GREATER &&
|
||||
step_data.dir == STEP_DIR_TYPE_INCREASE)
|
||||
{
|
||||
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, an_problem);
|
||||
}
|
||||
else if (cond_data.type == COND_TYPE_VAR_LESS &&
|
||||
step_data.dir == STEP_DIR_TYPE_DECREASE)
|
||||
{
|
||||
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, 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(an_problem, sizeof(an_problem), "'%s' is stalled",
|
||||
cond_data.varname);
|
||||
an_push_warning(analyzer, loop_node->loop.step->token, an_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};
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stdio.h>
|
||||
|
||||
// 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 a return statement");
|
||||
return (fzl_an_res_t){.error = false};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *fnctx = analyzer->ctx;
|
||||
while (fnctx != NULL)
|
||||
{
|
||||
if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
break;
|
||||
|
||||
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 the 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)
|
||||
{
|
||||
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};
|
||||
}
|
||||
|
||||
fzl_an_res_t ret_res = analyze_common(analyzer, ret_node->ret.value);
|
||||
if (ret_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname))
|
||||
{
|
||||
if (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};
|
||||
}
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = rettype, .classname = classname};
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
|
||||
// Analyze unary condition
|
||||
fzl_an_res_t analyze_unarycond(fzl_analyzer_t *analyzer, fzl_node_t *unary)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!unary || unary->type != FZL_NODETYPE_NOT)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not an unary condition operator");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
|
||||
if (operand_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (operand_res.type != FZL_TYPE_BOOL && operand_res.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, unary->unary_op.operand->token,
|
||||
"unary condition only support bool and integer types");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
// Analyze unary arithmetic
|
||||
fzl_an_res_t analyze_unarymath(fzl_analyzer_t *analyzer, fzl_node_t *unary)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!unary || (unary->type != FZL_NODETYPE_UNARY_MINUS &&
|
||||
unary->type != FZL_NODETYPE_UNARY_PLUS))
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not an unary math operator");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
|
||||
if (operand_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (operand_res.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, unary->unary_op.operand->token,
|
||||
"unary operator only support integer types");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = operand_res.type, .classname = NULL};
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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;
|
||||
|
||||
memset(ctx, 0, sizeof(fzl_an_ctx_t));
|
||||
|
||||
ctx->type = type;
|
||||
if (analyzer->ctx != NULL)
|
||||
ctx->parent = analyzer->ctx;
|
||||
else
|
||||
ctx->parent = NULL;
|
||||
|
||||
ctx->symbols = fzl_mksymt();
|
||||
ctx->symbols_level = analyzer->symbols_top;
|
||||
analyzer->ctx = ctx;
|
||||
analyzer->symbols[analyzer->symbols_top++] = ctx->symbols;
|
||||
|
||||
return ctx;
|
||||
}
|
||||
|
||||
// Close context and return parent
|
||||
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer)
|
||||
{
|
||||
if (!analyzer || analyzer->ctx == NULL)
|
||||
return NULL;
|
||||
|
||||
fzl_an_ctx_t *current = analyzer->ctx;
|
||||
fzl_an_ctx_t *parent = current->parent;
|
||||
analyzer->ctx = parent;
|
||||
|
||||
free(current);
|
||||
fzl_freesymt(analyzer->symbols[--analyzer->symbols_top]);
|
||||
|
||||
return parent;
|
||||
}
|
||||
@@ -0,0 +1,124 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
|
||||
// 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 "<unknown>";
|
||||
}
|
||||
}
|
||||
|
||||
// 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(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)
|
||||
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;
|
||||
}
|
||||
@@ -0,0 +1,118 @@
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_token.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
char aerrors[256][256];
|
||||
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)
|
||||
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;
|
||||
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;
|
||||
}
|
||||
|
||||
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(aerrors[ei], sizeof(aerrors[ei]), "'%s':%zu:%zu => %s",
|
||||
tok->filename, tok->ln, tok->cn, error);
|
||||
else
|
||||
snprintf(aerrors[ei], sizeof(aerrors[ei]), "<unknown location> => %s",
|
||||
error);
|
||||
|
||||
analyzer->errors[analyzer->errors_top++] = aerrors[ei];
|
||||
return true;
|
||||
}
|
||||
|
||||
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(awarnings[wi], sizeof(awarnings[wi]), "'%s':%zu:%zu => %s",
|
||||
tok->filename, tok->ln, tok->cn, warning);
|
||||
else
|
||||
snprintf(awarnings[wi], sizeof(awarnings[wi]), "<unknown location> => %s",
|
||||
warning);
|
||||
|
||||
analyzer->warnings[analyzer->warnings_top++] = awarnings[wi];
|
||||
return true;
|
||||
}
|
||||
|
||||
void fzl_analyze(fzl_analyzer_t *analyzer)
|
||||
{
|
||||
if (!analyzer)
|
||||
return;
|
||||
|
||||
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)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, current);
|
||||
if (res.error)
|
||||
break;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
for (ssize_t i = analyzer->symbols_top; i > 0; i--)
|
||||
an_fin_ctx(analyzer);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <common/fzl_colors.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_debug.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/fzl_reader.h>
|
||||
@@ -122,7 +123,40 @@ 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_CWARNING, FZL_CRST,
|
||||
analyzer->warnings[i]);
|
||||
}
|
||||
|
||||
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
||||
@@ -131,6 +165,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);
|
||||
@@ -144,6 +179,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);
|
||||
@@ -190,6 +226,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);
|
||||
@@ -204,6 +241,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);
|
||||
@@ -217,6 +255,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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <compiler/utils/fzl_symtable.h>
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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");
|
||||
@@ -430,8 +431,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];
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
fn choose(bool flag) int
|
||||
{
|
||||
if (flag)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int invalid = "not an integer";
|
||||
exit(1);
|
||||
@@ -0,0 +1,12 @@
|
||||
fn doSomeWork()
|
||||
{
|
||||
printline("Some work");
|
||||
}
|
||||
|
||||
fn sum(int a, int b) int
|
||||
{
|
||||
return a + b;
|
||||
}
|
||||
|
||||
int result = sum(5, 6);
|
||||
printline(result);
|
||||
@@ -0,0 +1,19 @@
|
||||
class Node
|
||||
{
|
||||
Node next;
|
||||
|
||||
fn ping()
|
||||
{
|
||||
printline("ping");
|
||||
}
|
||||
|
||||
fn pingNext()
|
||||
{
|
||||
this.next.ping();
|
||||
}
|
||||
}
|
||||
|
||||
Node head = new Node;
|
||||
Node tail = new Node;
|
||||
head.next = tail;
|
||||
head.pingNext();
|
||||
Move these functions to another header