Commit 4bd47c12

4bd47c1218ffMerged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

feat(codegen): Range#map { |i| ... }

compile_map_expr had no Range branch, so a (0..N).map { ... } expression fell through to the default "0" return. The result silently became an int placeholder and any follow-up .length, [i], etc. lowered to bit shifts on 0.

Add a Range branch that emits a plain C for loop and pushes each block result into a fresh IntArray / StrArray / FloatArray. ParenthesesNode is unwrapped first since (1..8).map { ... } parses as CallNode(recv=ParenthesesNode(stmt=RangeNode)).

Block returns that aren't int / string / float (e.g. an array literal — [i, i*10]) fall through to the original "0" placeholder, preserving the existing typing where a follow-up @x = [...] re-assignment to the same slot still type-checks.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
4bd47c1218ff
Parent
d920667f4cc7

spinel_codegen.rbmodifiedView file

@@ -25814,6 +25814,93 @@
2581425814 end
2581525815 tmp_arr = new_temp
2581625816 tmp_i = new_temp
25817+ # Range#map: iterate i from first to last (inclusive when ..,
25818+ # exclusive when ...). The receiver isn't a Range value-type
25819+ # we can hold in a temp the way arrays are — recompile the
25820+ # endpoints directly. ParenthesesNode (e.g. `(1..8).map {...}`)
25821+ # is unwrapped first, since it parses as
25822+ # CallNode(recv=ParenthesesNode(stmt=RangeNode), name=map).
25823+ if rt == "range"
25824+ block_ret_r = "int"
25825+ blk_r = @nd_block[nid]
25826+ if blk_r >= 0
25827+ body_r = @nd_body[blk_r]
25828+ if body_r >= 0
25829+ stmts_r = get_stmts(body_r)
25830+ if stmts_r.length > 0
25831+ block_ret_r = infer_type(stmts_r.last)
25832+ end
25833+ end
25834+ end
25835+ # Only handle scalar block returns inline; an array-returning
25836+ # block (`(0..n).map { |i| [i, ...] }`) would make this an
25837+ # array-of-arrays, which the int/str/float push helpers can't
25838+ # express. Fall through to the "0" placeholder for those — the
25839+ # generic int_array typing keeps any follow-up `@x = [...]`
25840+ # assignment to the same slot type-checking.
25841+ if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float"
25842+ return "0"
25843+ end
25844+ @needs_int_array = 1
25845+ @needs_gc = 1
25846+ if block_ret_r == "string"
25847+ @needs_str_array = 1
25848+ emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
25849+ elsif block_ret_r == "float"
25850+ @needs_float_array = 1
25851+ emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
25852+ else
25853+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
25854+ end
25855+ rng_id = @nd_receiver[nid]
25856+ while rng_id >= 0 && @nd_type[rng_id] == "ParenthesesNode"
25857+ body_pn = @nd_body[rng_id]
25858+ if body_pn >= 0
25859+ ps = get_stmts(body_pn)
25860+ if ps.length > 0
25861+ rng_id = ps.last
25862+ else
25863+ rng_id = -1
25864+ end
25865+ else
25866+ rng_id = -1
25867+ end
25868+ end
25869+ first_e = compile_expr(@nd_left[rng_id])
25870+ last_e = compile_expr(@nd_right[rng_id])
25871+ excl = range_excl_end(rng_id) == 1 ? "<" : "<="
25872+ emit(" for (mrb_int " + tmp_i + " = " + first_e + "; " + tmp_i + " " + excl + " " + last_e + "; " + tmp_i + "++) {")
25873+ have_bp_r = (get_block_param(nid, 0) != "")
25874+ if have_bp_r
25875+ declare_var(bp1, "int")
25876+ emit(" lv_" + bp1 + " = " + tmp_i + ";")
25877+ end
25878+ @indent = @indent + 1
25879+ if blk_r >= 0
25880+ body_r2 = @nd_body[blk_r]
25881+ if body_r2 >= 0
25882+ stmts_r2 = get_stmts(body_r2)
25883+ k_r = 0
25884+ while k_r < stmts_r2.length - 1
25885+ compile_stmt(stmts_r2[k_r])
25886+ k_r = k_r + 1
25887+ end
25888+ if stmts_r2.length > 0
25889+ lastv_r = compile_expr(stmts_r2.last)
25890+ if block_ret_r == "string"
25891+ emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")
25892+ elsif block_ret_r == "float"
25893+ emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")
25894+ else
25895+ emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_r + ");")
25896+ end
25897+ end
25898+ end
25899+ end
25900+ @indent = @indent - 1
25901+ emit(" }")
25902+ return tmp_arr
25903+ end
2581725904 if rt == "int_array" || rt == "sym_array"
2581825905 @needs_int_array = 1
2581925906 @needs_gc = 1