parser+codegen: Add support for loops

Signed-off-by: Ian Moffett <ian@mirocom.org>
This commit is contained in:
2026-02-17 01:49:22 -05:00
parent 197965f173
commit 9be4ce179e
4 changed files with 87 additions and 1 deletions

View File

@@ -67,6 +67,29 @@ resolve_return(struct rifle_state *state, struct ast_node *root)
return 0;
}
static int
resolve_loop(struct rifle_state *state, struct ast_node *root)
{
char label[32];
if (state == NULL || root == NULL) {
return -1;
}
if (root->epilogue) {
snprintf(label, sizeof(label), "L.%zu", state->loop_count - 1);
mu_gen_blab(state, label);
snprintf(label, sizeof(label), "L.%zu.1", state->loop_count - 1);
mu_gen_label(state, label, false);
} else {
snprintf(label, sizeof(label), "L.%zu", state->loop_count++);
mu_gen_label(state, label, false);
}
return 0;
}
int
cg_resolve_node(struct rifle_state *state, struct ast_node *root)
{
@@ -86,6 +109,12 @@ cg_resolve_node(struct rifle_state *state, struct ast_node *root)
return -1;
}
break;
case AST_LOOP:
if (resolve_loop(state, root) < 0) {
return -1;
}
break;
default:
trace_error(state, "unknown ast node %d\n", root->type);