Commit 069ce8aa
069ce8aabe5bMerged
Ryan Seys <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
feat(codegen): support redo in while, until, and for loops
Wraps compile_while_stmt, compile_until_stmt, and both branches of
compile_for_stmt (range + array) with the same push_redo_label /
emit_redo_label / pop_redo_label dance that compile_times_block
already uses. redo re-runs the body without re-evaluating the
loop guard.
For the array branch of for, the lv_<vname> = sp_<pfx>_get(...)
assignment stays OUTSIDE the redo label so redo re-runs the body
with the same iterator value — matches MRI's "don't advance the
iterator" semantic.
Each emit site gets its own push/emit/pop pair (the counter-driven sp_redo_ name guarantees uniqueness across nested loops; emitting the same C label twice in one function would fail to compile).
Addresses gemini-code-assist's high-priority comment on PR matz/spinel#283.
🤖 Generated with Claude Code
Details
- Commit
- 069ce8aabe5b
- Parent
- 71afc0ef046a
spinel_codegen.rbmodifiedView file
cond = compile_cond_expr(@nd_predicate[nid]) emit(" while (" + cond + ") {") @indent = @indent + 1 redo_label = push_redo_label emit_redo_label(redo_label) compile_stmts_body(@nd_body[nid]) pop_redo_label @indent = @indent - 1 emit(" }") @hoisted_strlen_var = saved_var cond = compile_cond_expr(@nd_predicate[nid]) emit(" while (!(" + cond + ")) {") @indent = @indent + 1 redo_label = push_redo_label emit_redo_label(redo_label) compile_stmts_body(@nd_body[nid]) pop_redo_label @indent = @indent - 1 emit(" }") @in_loop = old cmp = range_excl_end(coll) == 1 ? "<" : "<=" emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " " + cmp + " " + right + "; lv_" + vname + "++) {") @indent = @indent + 1 redo_label = push_redo_label emit_redo_label(redo_label) compile_stmts_body(@nd_body[nid]) pop_redo_label @indent = @indent - 1 emit(" }") else tmp = new_temp pfx = array_c_prefix(ct) emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {") # Loop-var assignment stays OUTSIDE the redo label so `redo` re-runs # the body with the same value (matches MRI: `redo` does not advance). emit(" lv_" + vname + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");") @indent = @indent + 1 redo_label = push_redo_label emit_redo_label(redo_label) compile_stmts_body(@nd_body[nid]) pop_redo_label @indent = @indent - 1 emit(" }") end