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

@@ -26009,7 +26009,10 @@
2600926009 cond = compile_cond_expr(@nd_predicate[nid])
2601026010 emit(" while (" + cond + ") {")
2601126011 @indent = @indent + 1
26012+ redo_label = push_redo_label
26013+ emit_redo_label(redo_label)
2601226014 compile_stmts_body(@nd_body[nid])
26015+ pop_redo_label
2601326016 @indent = @indent - 1
2601426017 emit(" }")
2601526018 @hoisted_strlen_var = saved_var
@@ -26023,7 +26026,10 @@
2602326026 cond = compile_cond_expr(@nd_predicate[nid])
2602426027 emit(" while (!(" + cond + ")) {")
2602526028 @indent = @indent + 1
26029+ redo_label = push_redo_label
26030+ emit_redo_label(redo_label)
2602626031 compile_stmts_body(@nd_body[nid])
26032+ pop_redo_label
2602726033 @indent = @indent - 1
2602826034 emit(" }")
2602926035 @in_loop = old
@@ -26047,7 +26053,10 @@
2604726053 cmp = range_excl_end(coll) == 1 ? "<" : "<="
2604826054 emit(" for (lv_" + vname + " = " + left + "; lv_" + vname + " " + cmp + " " + right + "; lv_" + vname + "++) {")
2604926055 @indent = @indent + 1
26056+ redo_label = push_redo_label
26057+ emit_redo_label(redo_label)
2605026058 compile_stmts_body(@nd_body[nid])
26059+ pop_redo_label
2605126060 @indent = @indent - 1
2605226061 emit(" }")
2605326062 else
@@ -26057,10 +26066,15 @@
2605726066 tmp = new_temp
2605826067 pfx = array_c_prefix(ct)
2605926068 emit(" for (mrb_int " + tmp + " = 0; " + tmp + " < sp_" + pfx + "_length(" + rc + "); " + tmp + "++) {")
26069+ # Loop-var assignment stays OUTSIDE the redo label so `redo` re-runs
26070+ # the body with the same value (matches MRI: `redo` does not advance).
2606026071 emit(" lv_" + vname + " = sp_" + pfx + "_get(" + rc + ", " + tmp + ");")
2606126072
2606226073 @indent = @indent + 1
26074+ redo_label = push_redo_label
26075+ emit_redo_label(redo_label)
2606326076 compile_stmts_body(@nd_body[nid])
26077+ pop_redo_label
2606426078 @indent = @indent - 1
2606526079 emit(" }")
2606626080 end