wip: working on generator

- Feat: add break handler
- Feat: add class handler
- Feat: add format string handler
- Feat: add loop handler
This commit is contained in:
samsonium
2026-11-04 20:04:33 +03:00
parent 7fe3fe3ff3
commit b278e8bb74
4 changed files with 192 additions and 0 deletions
+61
View File
@@ -0,0 +1,61 @@
#include <compiler/fzl_gen.h>
#include <compiler/internals/gen.h>
#include <stdbool.h>
// While or for loops
bool gen_loop(fzl_gen_t *gen, fzl_node_t *node)
{
if (!gen || !node)
return false;
fzl_gen_ctx_t *ctx = gen_ctx(gen, FZL_GEN_CTX_TYPE_LOOP, node);
if (!ctx)
return false;
gen_op(gen, FZL_OPCODE_MK_ENVTABLE);
if (node->loop.init)
if (!gen_common(gen, node->loop.init))
return false;
size_t start_ip = gen->ops_count;
if (node->loop.cond)
{
if (!gen_common(gen, node->loop.cond))
return false;
}
else
{
gen_op(gen, FZL_OPCODE_PUSHNUM);
gen_word(gen, 1);
}
gen_op(gen, FZL_OPCODE_JZ);
ctx->loop.breaks[ctx->loop.breaks_count++] = gen->ops_count;
gen_word(gen, 0);
if (!gen_common(gen, node->loop.body))
return false;
if (node->loop.step)
if (!gen_common(gen, node->loop.step))
return false;
gen_op(gen, FZL_OPCODE_JMP);
gen_word(gen, start_ip);
// backpatch `break`s
for (size_t i = 0; i < ctx->loop.breaks_count; i++)
gen->ops[ctx->loop.breaks[i]] = gen->ops_count;
gen_op(gen, FZL_OPCODE_RM_ENVTABLE);
for (size_t i = gen->syms_top - 1; i > ctx->level; i--)
fzl_freesymt(gen->syms[--gen->syms_top]);
ctx = gen_fin_ctx(gen);
if (!ctx)
return false;
return true;
}