Commit 3e608bc1

3e608bc1cf76Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

Merge pull request #193 from mame/pr/range-map

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

Details

Commit
3e608bc1cf76
Parent
801014e60f8e
Merge parent
4bd47c1218ff

spinel_codegen.rbmodifiedView file

@@ -26830,6 +26830,93 @@
2683026830 end
2683126831 tmp_arr = new_temp
2683226832 tmp_i = new_temp
26833+ # Range#map: iterate i from first to last (inclusive when ..,
26834+ # exclusive when ...). The receiver isn't a Range value-type
26835+ # we can hold in a temp the way arrays are — recompile the
26836+ # endpoints directly. ParenthesesNode (e.g. `(1..8).map {...}`)
26837+ # is unwrapped first, since it parses as
26838+ # CallNode(recv=ParenthesesNode(stmt=RangeNode), name=map).
26839+ if rt == "range"
26840+ block_ret_r = "int"
26841+ blk_r = @nd_block[nid]
26842+ if blk_r >= 0
26843+ body_r = @nd_body[blk_r]
26844+ if body_r >= 0
26845+ stmts_r = get_stmts(body_r)
26846+ if stmts_r.length > 0
26847+ block_ret_r = infer_type(stmts_r.last)
26848+ end
26849+ end
26850+ end
26851+ # Only handle scalar block returns inline; an array-returning
26852+ # block (`(0..n).map { |i| [i, ...] }`) would make this an
26853+ # array-of-arrays, which the int/str/float push helpers can't
26854+ # express. Fall through to the "0" placeholder for those — the
26855+ # generic int_array typing keeps any follow-up `@x = [...]`
26856+ # assignment to the same slot type-checking.
26857+ if block_ret_r != "int" && block_ret_r != "string" && block_ret_r != "float"
26858+ return "0"
26859+ end
26860+ @needs_int_array = 1
26861+ @needs_gc = 1
26862+ if block_ret_r == "string"
26863+ @needs_str_array = 1
26864+ emit(" sp_StrArray *" + tmp_arr + " = sp_StrArray_new();")
26865+ elsif block_ret_r == "float"
26866+ @needs_float_array = 1
26867+ emit(" sp_FloatArray *" + tmp_arr + " = sp_FloatArray_new();")
26868+ else
26869+ emit(" sp_IntArray *" + tmp_arr + " = sp_IntArray_new();")
26870+ end
26871+ rng_id = @nd_receiver[nid]
26872+ while rng_id >= 0 && @nd_type[rng_id] == "ParenthesesNode"
26873+ body_pn = @nd_body[rng_id]
26874+ if body_pn >= 0
26875+ ps = get_stmts(body_pn)
26876+ if ps.length > 0
26877+ rng_id = ps.last
26878+ else
26879+ rng_id = -1
26880+ end
26881+ else
26882+ rng_id = -1
26883+ end
26884+ end
26885+ first_e = compile_expr(@nd_left[rng_id])
26886+ last_e = compile_expr(@nd_right[rng_id])
26887+ excl = range_excl_end(rng_id) == 1 ? "<" : "<="
26888+ emit(" for (mrb_int " + tmp_i + " = " + first_e + "; " + tmp_i + " " + excl + " " + last_e + "; " + tmp_i + "++) {")
26889+ have_bp_r = (get_block_param(nid, 0) != "")
26890+ if have_bp_r
26891+ declare_var(bp1, "int")
26892+ emit(" lv_" + bp1 + " = " + tmp_i + ";")
26893+ end
26894+ @indent = @indent + 1
26895+ if blk_r >= 0
26896+ body_r2 = @nd_body[blk_r]
26897+ if body_r2 >= 0
26898+ stmts_r2 = get_stmts(body_r2)
26899+ k_r = 0
26900+ while k_r < stmts_r2.length - 1
26901+ compile_stmt(stmts_r2[k_r])
26902+ k_r = k_r + 1
26903+ end
26904+ if stmts_r2.length > 0
26905+ lastv_r = compile_expr(stmts_r2.last)
26906+ if block_ret_r == "string"
26907+ emit(" sp_StrArray_push(" + tmp_arr + ", " + lastv_r + ");")
26908+ elsif block_ret_r == "float"
26909+ emit(" sp_FloatArray_push(" + tmp_arr + ", " + lastv_r + ");")
26910+ else
26911+ emit(" sp_IntArray_push(" + tmp_arr + ", " + lastv_r + ");")
26912+ end
26913+ end
26914+ end
26915+ end
26916+ @indent = @indent - 1
26917+ emit(" }")
26918+ return tmp_arr
26919+ end
2683326920 if rt == "int_array" || rt == "sym_array"
2683426921 @needs_int_array = 1
2683526922 @needs_gc = 1