Commit a9dd360a
a9dd360aed33Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
Merge pull request #242 from mame/pr/multi-write-target-from-map
fix(codegen): multi-write A, B = arr.map { block } from ptr_array RHS
Details
- Commit
- a9dd360aed33
- Parent
- f23a36e09ea5
- Merge parent
- 3cafbe024df9
spinel_codegen.rbmodifiedView file
@@ -6497,6 +6497,40 @@
64976497
if is_tuple_type(rt) == 164986498
return tuple_elem_type_at(rt, ti)64996499
end6500+
# `A, B = arr.map { block }` — each target is one element of the6501+
# mapped array, so its type is the block's return type. Spinel6502+
# collapses array-of-array to a placeholder (int_array) at the6503+
# outer infer_type level, so we have to peek through the call6504+
# node directly to recover the element type.6505+
if @nd_type[val_id] == "CallNode" && @nd_name[val_id] == "map"6506+
blk = @nd_block[val_id]6507+
if blk >= 06508+
bbody = @nd_body[blk]6509+
if bbody >= 06510+
bbs = get_stmts(bbody)6511+
if bbs.length > 06512+
bret = infer_type(bbs.last)6513+
if bret != "int" && bret != "" && bret != "void"6514+
return bret6515+
end6516+
end6517+
end6518+
end6519+
end6520+
# Array-typed RHS (e.g. `A, B = [1, 6].map { ... }`): each target6521+
# gets one element of the recv array. Use the recv's element type.6522+
if rt == "int_array" || rt == "sym_array"6523+
return "int"6524+
end6525+
if is_ptr_array_type(rt) == 16526+
return ptr_array_elem_type(rt)6527+
end6528+
if rt == "str_array"6529+
return "string"6530+
end6531+
if rt == "float_array"6532+
return "float"6533+
end65006534
"int"65016535
end65026536
@@ -16137,6 +16171,26 @@
1613716171
ti3 = ti3 + 11613816172
end1613916173
end16174+
# ptr_array RHS (e.g., `A, B = [1,6].map { (0..n).map { ... } }`16175+
# whose outer map returns int_array_ptr_array). Each target16176+
# is a class-typed (here IntArray*) element of the PtrArray.16177+
if is_ptr_array_type(rhs_t) == 116178+
elem_pt = ptr_array_elem_type(rhs_t)16179+
tmp_n = new_temp16180+
emit(" sp_PtrArray *" + tmp_n + " = " + compile_expr(val_id) + ";")16181+
ti3 = 016182+
while ti3 < targets.length16183+
tid = targets[ti3]16184+
if @nd_type[tid] == "ConstantTargetNode"16185+
cn = @nd_name[tid]16186+
if scope_n != ""16187+
cn = scope_n + "_" + cn16188+
end16189+
emit(" cst_" + cn + " = (" + c_type(elem_pt) + ")sp_PtrArray_get(" + tmp_n + ", " + ti3.to_s + ");")16190+
end16191+
ti3 = ti3 + 116192+
end16193+
end1614016194
end1614116195
@current_lexical_scope = old_scope1614216196
mci = mci + 1test/multi_write_map_block.rbaddedView file
Loading diff…
test/multi_write_map_block.rb.expectedaddedView file
Loading diff…