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:
@@ -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};
|
||||
}
|
||||
Reference in New Issue
Block a user