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) == 1
64986498 return tuple_elem_type_at(rt, ti)
64996499 end
6500+ # `A, B = arr.map { block }` — each target is one element of the
6501+ # mapped array, so its type is the block's return type. Spinel
6502+ # collapses array-of-array to a placeholder (int_array) at the
6503+ # outer infer_type level, so we have to peek through the call
6504+ # 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 >= 0
6508+ bbody = @nd_body[blk]
6509+ if bbody >= 0
6510+ bbs = get_stmts(bbody)
6511+ if bbs.length > 0
6512+ bret = infer_type(bbs.last)
6513+ if bret != "int" && bret != "" && bret != "void"
6514+ return bret
6515+ end
6516+ end
6517+ end
6518+ end
6519+ end
6520+ # Array-typed RHS (e.g. `A, B = [1, 6].map { ... }`): each target
6521+ # 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+ end
6525+ if is_ptr_array_type(rt) == 1
6526+ return ptr_array_elem_type(rt)
6527+ end
6528+ if rt == "str_array"
6529+ return "string"
6530+ end
6531+ if rt == "float_array"
6532+ return "float"
6533+ end
65006534 "int"
65016535 end
65026536
@@ -16137,6 +16171,26 @@
1613716171 ti3 = ti3 + 1
1613816172 end
1613916173 end
16174+ # ptr_array RHS (e.g., `A, B = [1,6].map { (0..n).map { ... } }`
16175+ # whose outer map returns int_array_ptr_array). Each target
16176+ # is a class-typed (here IntArray*) element of the PtrArray.
16177+ if is_ptr_array_type(rhs_t) == 1
16178+ elem_pt = ptr_array_elem_type(rhs_t)
16179+ tmp_n = new_temp
16180+ emit(" sp_PtrArray *" + tmp_n + " = " + compile_expr(val_id) + ";")
16181+ ti3 = 0
16182+ while ti3 < targets.length
16183+ tid = targets[ti3]
16184+ if @nd_type[tid] == "ConstantTargetNode"
16185+ cn = @nd_name[tid]
16186+ if scope_n != ""
16187+ cn = scope_n + "_" + cn
16188+ end
16189+ emit(" cst_" + cn + " = (" + c_type(elem_pt) + ")sp_PtrArray_get(" + tmp_n + ", " + ti3.to_s + ");")
16190+ end
16191+ ti3 = ti3 + 1
16192+ end
16193+ end
1614016194 end
1614116195 @current_lexical_scope = old_scope
1614216196 mci = mci + 1