Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa7a06bd22 | ||
|
|
a263b6206f |
@@ -22,12 +22,14 @@ add_executable(fzc
|
||||
src/compiler/checkers/fzl_loop_check.c
|
||||
src/compiler/analyzer/utility.c
|
||||
src/compiler/analyzer/context.c
|
||||
src/compiler/analyzer/an_assign.c
|
||||
src/compiler/analyzer/an_binmath.c
|
||||
src/compiler/analyzer/an_block.c
|
||||
src/compiler/analyzer/an_break.c
|
||||
src/compiler/analyzer/an_call.c
|
||||
src/compiler/analyzer/an_class.c
|
||||
src/compiler/analyzer/an_common.c
|
||||
src/compiler/analyzer/an_condition.c
|
||||
src/compiler/analyzer/an_conditional.c
|
||||
src/compiler/analyzer/an_construct.c
|
||||
src/compiler/analyzer/an_declaration.c
|
||||
@@ -36,6 +38,8 @@ add_executable(fzc
|
||||
src/compiler/analyzer/an_identifier.c
|
||||
src/compiler/analyzer/an_loop.c
|
||||
src/compiler/analyzer/an_return.c
|
||||
src/compiler/analyzer/an_unarycond.c
|
||||
src/compiler/analyzer/an_unarymath.c
|
||||
src/compiler/fzl_analyzer.c
|
||||
src/compiler/fzl_debug.c
|
||||
src/compiler/fzl_parser.c
|
||||
|
||||
@@ -88,6 +88,10 @@ fzl_an_res_t analyze_construct(fzl_analyzer_t *analyzer, fzl_node_t *construct);
|
||||
fzl_an_res_t analyze_loop(fzl_analyzer_t *analyzer, fzl_node_t *loop_node);
|
||||
fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node);
|
||||
fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node);
|
||||
fzl_an_res_t analyze_unarymath(fzl_analyzer_t *analyzer, fzl_node_t *unary);
|
||||
fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node);
|
||||
fzl_an_res_t analyze_unarycond(fzl_analyzer_t *analyzer, fzl_node_t *unary);
|
||||
fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node);
|
||||
|
||||
fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type);
|
||||
fzl_an_ctx_t *an_fin_ctx(fzl_analyzer_t *analyzer);
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char asn_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc);
|
||||
extern char *an_typetostr(fzl_type_t type, char *classname);
|
||||
|
||||
// Analyze assigns
|
||||
fzl_an_res_t analyze_assign(fzl_analyzer_t *analyzer, fzl_node_t *assign_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!assign_node || assign_node->type != FZL_NODETYPE_ASSIGN)
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof ASSIGN");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_node_t *left = assign_node->binary_op.left;
|
||||
fzl_an_res_t leftside = analyze_common(analyzer, left);
|
||||
if (leftside.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t rightside =
|
||||
analyze_common(analyzer, assign_node->binary_op.right);
|
||||
if (rightside.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (left->type != FZL_NODETYPE_DECLARATION &&
|
||||
left->type != FZL_NODETYPE_IDENTIFIER && left->type != FZL_NODETYPE_FIELD)
|
||||
{
|
||||
an_push_error(analyzer, left->token,
|
||||
"this type of assignment left is not supported");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(leftside.type, rightside.type, leftside.classname,
|
||||
rightside.classname))
|
||||
{
|
||||
snprintf(asn_problem, sizeof(asn_problem), "cannot assign %s to %s",
|
||||
an_typetostr(rightside.type, rightside.classname),
|
||||
an_typetostr(leftside.type, leftside.classname));
|
||||
an_push_error(analyzer, assign_node->binary_op.right->token, asn_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_NULL, .classname = NULL};
|
||||
}
|
||||
@@ -11,11 +11,12 @@ fzl_an_res_t analyze_binmath(fzl_analyzer_t *analyzer, fzl_node_t *bin_node)
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!bin_node || bin_node->type != FZL_NODETYPE_ADD ||
|
||||
bin_node->type != FZL_NODETYPE_SUB ||
|
||||
bin_node->type != FZL_NODETYPE_MUL ||
|
||||
bin_node->type != FZL_NODETYPE_DIV ||
|
||||
bin_node->type != FZL_NODETYPE_POW || bin_node->type != FZL_NODETYPE_MOD)
|
||||
if (!bin_node || (bin_node->type != FZL_NODETYPE_ADD &&
|
||||
bin_node->type != FZL_NODETYPE_SUB &&
|
||||
bin_node->type != FZL_NODETYPE_MUL &&
|
||||
bin_node->type != FZL_NODETYPE_DIV &&
|
||||
bin_node->type != FZL_NODETYPE_POW &&
|
||||
bin_node->type != FZL_NODETYPE_MOD))
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof binary math");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
@@ -19,6 +19,8 @@ fzl_an_res_t analyze_break(fzl_analyzer_t *analyzer, fzl_node_t *break_node)
|
||||
{
|
||||
if (ctx && ctx->type == FZL_AN_CTX_TYPE_LOOP)
|
||||
break;
|
||||
|
||||
ctx = ctx->parent;
|
||||
}
|
||||
|
||||
if (!ctx || ctx->type != FZL_AN_CTX_TYPE_LOOP)
|
||||
|
||||
@@ -164,15 +164,31 @@ fzl_an_res_t analyze_call(fzl_analyzer_t *analyzer, fzl_node_t *call_node)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < sym.node->fn.args_count; i++)
|
||||
size_t start_i = is_method ? 1 : 0;
|
||||
for (size_t i = start_i; i < sym.node->fn.args_count; i++)
|
||||
{
|
||||
fzl_an_res_t req = analyze_common(analyzer, sym.node->fn.args[i]);
|
||||
fzl_node_t *arg = sym.node->fn.args[i];
|
||||
fzl_type_t req_type = an_toktotype(arg->var.type);
|
||||
char *req_classname = NULL;
|
||||
if (req_type == FZL_TYPE_OBJECT)
|
||||
req_classname = arg->var.type->value;
|
||||
|
||||
fzl_an_res_t got = analyze_common(analyzer, call_node->call.args[i]);
|
||||
if (!an_is_compatible(req.type, got.type, req.classname, got.classname))
|
||||
if (got.error)
|
||||
{
|
||||
if (tmp_method_name != NULL)
|
||||
{
|
||||
free(tmp_method_name);
|
||||
tmp_method_name = NULL;
|
||||
}
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(req_type, got.type, req_classname, got.classname))
|
||||
{
|
||||
snprintf(call_problem, sizeof(call_problem),
|
||||
"expect argument %zu to be %s, got '%s'", i + 1,
|
||||
an_typetostr(req.type, req.classname),
|
||||
an_typetostr(req_type, req_classname),
|
||||
an_typetostr(got.type, got.classname));
|
||||
an_push_error(analyzer, call_node->token, call_problem);
|
||||
|
||||
|
||||
@@ -8,9 +8,36 @@
|
||||
// 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:
|
||||
|
||||
@@ -0,0 +1,122 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char bincond_problem[256];
|
||||
|
||||
// defined in `compiler/analyzer/utility.c`
|
||||
extern bool an_is_compatible(fzl_type_t a, fzl_type_t b, char *ac, char *bc);
|
||||
extern char *an_typetostr(fzl_type_t type, char *classname);
|
||||
|
||||
// Analyze binary conditional nodes
|
||||
fzl_an_res_t analyze_condition(fzl_analyzer_t *analyzer, fzl_node_t *cond_node)
|
||||
{
|
||||
if (!analyzer)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!cond_node || (cond_node->type != FZL_NODETYPE_OR &&
|
||||
cond_node->type != FZL_NODETYPE_AND &&
|
||||
cond_node->type != FZL_NODETYPE_LESS &&
|
||||
cond_node->type != FZL_NODETYPE_LESS_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_GREATER &&
|
||||
cond_node->type != FZL_NODETYPE_GREATER_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_EQUAL &&
|
||||
cond_node->type != FZL_NODETYPE_NEQUAL))
|
||||
{
|
||||
an_push_error(analyzer, NULL, "node is not typeof binary condition");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t left = analyze_common(analyzer, cond_node->binary_op.left);
|
||||
if (left.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
fzl_an_res_t right = analyze_common(analyzer, cond_node->binary_op.right);
|
||||
if (right.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (cond_node->type == FZL_NODETYPE_OR || cond_node->type == FZL_NODETYPE_AND)
|
||||
{
|
||||
if (left.type != FZL_TYPE_BOOL)
|
||||
an_push_warning(analyzer, cond_node->binary_op.left->token,
|
||||
"left operand is not resulting with boolean type");
|
||||
|
||||
if (right.type != FZL_TYPE_BOOL)
|
||||
an_push_warning(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand is not resulting with boolean type");
|
||||
}
|
||||
else if (cond_node->type == FZL_NODETYPE_LESS ||
|
||||
cond_node->type == FZL_NODETYPE_LESS_EQUAL ||
|
||||
cond_node->type == FZL_NODETYPE_GREATER ||
|
||||
cond_node->type == FZL_NODETYPE_GREATER_EQUAL)
|
||||
{
|
||||
if (left.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"left operand must be number");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (left.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"right operand must be number");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (left.type >= FZL_TYPE_INT8)
|
||||
{
|
||||
if (right.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand must be an integer");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
else if (left.type == FZL_TYPE_OBJECT)
|
||||
{
|
||||
if (!left.classname || !right.classname)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.left->token,
|
||||
"unhandled error :(");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
if (!an_is_compatible(left.type, right.type, left.classname,
|
||||
right.classname))
|
||||
{
|
||||
snprintf(bincond_problem, sizeof(bincond_problem),
|
||||
"'%s' and '%s' are incompatible",
|
||||
an_typetostr(left.type, left.classname),
|
||||
an_typetostr(right.type, right.classname));
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||
bincond_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (left.type == FZL_TYPE_STRING)
|
||||
{
|
||||
if (right.type != FZL_TYPE_STRING)
|
||||
{
|
||||
an_push_error(analyzer, cond_node->binary_op.right->token,
|
||||
"right operand must be a string");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
}
|
||||
else if (!an_is_compatible(left.type, right.type, left.classname,
|
||||
right.classname))
|
||||
{
|
||||
snprintf(bincond_problem, sizeof(bincond_problem),
|
||||
"equality not supported between %s and %s",
|
||||
an_typetostr(left.type, left.classname),
|
||||
an_typetostr(right.type, right.classname));
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -67,5 +67,5 @@ fzl_an_res_t analyze_declaration(fzl_analyzer_t *analyzer, fzl_node_t *decl)
|
||||
classname);
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){.error = false, .type = FZL_TYPE_NULL};
|
||||
return (fzl_an_res_t){.error = false, .type = type, .classname = classname};
|
||||
}
|
||||
|
||||
@@ -26,6 +26,13 @@ fzl_an_res_t analyze_function(fzl_analyzer_t *analyzer, fzl_node_t *fn_node)
|
||||
fzl_an_ctx_t *fnctx = an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_FUNCTION);
|
||||
fnctx->fn = fn_node;
|
||||
|
||||
for (size_t i = 0; i < fn_node->fn.args_count; i++)
|
||||
{
|
||||
fzl_an_res_t res = analyze_common(analyzer, fn_node->fn.args[i]);
|
||||
if (res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *ctx = analyzer->ctx;
|
||||
while (ctx != NULL)
|
||||
{
|
||||
|
||||
@@ -24,7 +24,7 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
|
||||
}
|
||||
|
||||
fzl_an_ctx_t *fnctx = analyzer->ctx;
|
||||
while (!fnctx)
|
||||
while (fnctx != NULL)
|
||||
{
|
||||
if (fnctx->type == FZL_AN_CTX_TYPE_FUNCTION)
|
||||
break;
|
||||
@@ -32,7 +32,7 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
|
||||
fnctx = fnctx->parent;
|
||||
}
|
||||
|
||||
if (!fnctx)
|
||||
if (!fnctx || fnctx->type != FZL_AN_CTX_TYPE_FUNCTION)
|
||||
{
|
||||
an_push_error(analyzer, ret_node->token,
|
||||
"cannot use 'return' outside function");
|
||||
@@ -55,6 +55,9 @@ fzl_an_res_t analyze_return(fzl_analyzer_t *analyzer, fzl_node_t *ret_node)
|
||||
}
|
||||
|
||||
fzl_an_res_t ret_res = analyze_common(analyzer, ret_node->ret.value);
|
||||
if (ret_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (!an_is_compatible(ret_res.type, rettype, ret_res.classname, classname))
|
||||
{
|
||||
snprintf(ret_problem, sizeof(ret_problem),
|
||||
|
||||
@@ -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 typeof unary condition");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
|
||||
if (operand_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (operand_res.type != FZL_TYPE_BOOL && operand_res.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, unary->unary_op.operand->token,
|
||||
"unary condition only support bool and integer types");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = FZL_TYPE_BOOL, .classname = NULL};
|
||||
}
|
||||
@@ -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 typeof unary math");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
fzl_an_res_t operand_res = analyze_common(analyzer, unary->unary_op.operand);
|
||||
if (operand_res.error)
|
||||
return (fzl_an_res_t){.error = true};
|
||||
|
||||
if (operand_res.type < FZL_TYPE_INT8)
|
||||
{
|
||||
an_push_error(analyzer, unary->unary_op.operand->token,
|
||||
"unary operator only support integer types");
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
return (fzl_an_res_t){
|
||||
.error = false, .type = operand_res.type, .classname = NULL};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#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)
|
||||
@@ -12,9 +13,13 @@ fzl_an_ctx_t *an_nest_ctx(fzl_analyzer_t *analyzer, fzl_an_ctx_type_t type)
|
||||
if (!ctx)
|
||||
return NULL;
|
||||
|
||||
memset(ctx, 0, sizeof(fzl_an_ctx_t));
|
||||
|
||||
ctx->type = type;
|
||||
if (analyzer->ctx != NULL)
|
||||
ctx->parent = analyzer->ctx;
|
||||
else
|
||||
ctx->parent = NULL;
|
||||
|
||||
analyzer->ctx = ctx;
|
||||
analyzer->symbols[analyzer->symbols_top++] = fzl_mksymt();
|
||||
|
||||
+2
-1
@@ -155,7 +155,8 @@ int main(int argc, char **argv)
|
||||
if (analyzer->warnings_top > 0)
|
||||
{
|
||||
for (ssize_t i = analyzer->warnings_top - 1; i >= 0; i--)
|
||||
printf("%sWarning%s: %s\n", FZL_CERROR, FZL_CRST, analyzer->warnings[i]);
|
||||
printf("%sWarning%s: %s\n", FZL_CWARNING, FZL_CRST,
|
||||
analyzer->warnings[i]);
|
||||
}
|
||||
|
||||
fzl_walker_t *walker = fzl_mkwalker(parser->root);
|
||||
|
||||
Reference in New Issue
Block a user