wip: working on analyzer
- Feat: add `nest_ctx` and `fin_ctx` fns for working with context and environment - Feat: add block analyzer - Feat: add conditional statement analyzer
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
#include <common/fzl_types.h>
|
||||
#include <compiler/fzl_analyzer.h>
|
||||
#include <compiler/fzl_parser.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
|
||||
char block_problem[256];
|
||||
|
||||
// Analyze block
|
||||
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(block_problem, sizeof(block_problem), "node is not typeof BLOCK");
|
||||
an_push_error(analyzer, NULL, block_problem);
|
||||
return (fzl_an_res_t){.error = true};
|
||||
}
|
||||
|
||||
an_nest_ctx(analyzer, FZL_AN_CTX_TYPE_UNNECESSARY);
|
||||
|
||||
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 node will never execute");
|
||||
|
||||
fzl_an_res_t entry_res =
|
||||
analyze_common(analyzer, block_node->list.entries[i]);
|
||||
if (entry_res.error)
|
||||
error = true;
|
||||
|
||||
if (block_node->list.entries[i]->type == FZL_NODETYPE_RETURN)
|
||||
has_return = true;
|
||||
}
|
||||
|
||||
an_fin_ctx(analyzer);
|
||||
|
||||
return (fzl_an_res_t){.error = error, .type = FZL_TYPE_NULL};
|
||||
}
|
||||
Reference in New Issue
Block a user