wip: working on analyzer

- Feat: add assigns analysis
- Fix: change node check condition in binary math analysis
- Feat: add unary and binary conditions analysis
- Fix: add type passtrough in declaration analysis
- Feat: add unary math analysis
This commit is contained in:
samsonium
2026-10-27 12:21:49 +03:00
parent 25991c6f15
commit a263b6206f
9 changed files with 277 additions and 6 deletions
+122
View File
@@ -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};
}