Files
fz-lang/src/compiler/analyzer/an_conditional.c
T

46 lines
1.4 KiB
C

#include <common/fzl_types.h>
#include <compiler/fzl_analyzer.h>
#include <compiler/fzl_parser.h>
#include <compiler/fzl_token.h>
#include <compiler/internals/analyzer.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};
}