Commit d2efbd64
d2efbd643422Merged
Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
feat(parse,codegen): support MultiTargetNode (nested LHS)
a, (b, c), d = 1, [2, 3], 4 -- the inner parenthesized group on
the LHS becomes a MultiTargetNode that recursively unpacks its
slot of the RHS into the inner targets. Three pieces:
-
Parser case in spinel_parse.c -- emits the node with
lefts,rest,rightslike the outer MultiWriteNode. -
scan_locals MultiTargetNode arm -- declares the inner targets' local-variable names in the surrounding scope, with element types derived from the matching outer-RHS slot. Without this,
bandcreference undeclaredlv_b/lv_cin the C output. -
emit_multi_write_target arm -- when the outer multi-write lands at this MultiTarget slot, dispatch to compile_nested_multi_target which reads each inner-target's value via sp__get(arr, k).
Helpers added:
-
nested_target_elem_type(slot_type) -- "int_array" -> "int", "float_array" -> "float", "str_array" -> "string", "sym_array" -> "sym". Mirrors the dispatch in compile_nested_multi_target.
-
compile_nested_multi_target(tid, value_expr, value_type) -- emits a fresh temp for the inner array and per-target index reads. GC-rooted via @needs_gc.
Restriction: the outer-RHS slot at the MultiTarget position must be
typed as a homogeneous array (int_array / str_array / float_array /
sym_array). Heterogeneous (poly_array) slots, e.g.
x, (y, [...]) = 10, [20, [30, 40]] where the inner array contains
mixed types, exit with a clear error -- documented in the test
fixture as out of scope.
Test exercises (test/multi_target_nested.rb):
a, (b, c), d = 1, [2, 3], 4-- int_array inner slot.x, (s, t), y = 10, ["hello", "world"], 20-- str_array inner.
Tests: 274 pass. Bootstrap: gen2.c == gen3.c.
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- d2efbd643422
- Parent
- c89434454a6b
spinel_codegen.rbmodifiedView file
end end end if @nd_type[tid] == "MultiTargetNode" # Nested LHS: `a, (b, c) = 1, [2, 3]`. The inner targets' # local-variable names need to be declared in this scope # too, with the per-element type derived from the matching # outer-RHS slot's array type. inner_targets = parse_id_list(@nd_targets[tid]) inner_slot_type = multi_write_target_type(val_id2, ti2) inner_elem_type = nested_target_elem_type(inner_slot_type) inner_targets.each { |inner_tid| if @nd_type[inner_tid] == "LocalVariableTargetNode" ilname = @nd_name[inner_tid] if not_in(ilname, names) == 1 if not_in(ilname, params) == 1 names.push(ilname) types.push(inner_elem_type) @scan_literal_flags.push("") @scan_empty_flags.push("") @scan_empty_hash_flags.push("") end end end } end ti2 = ti2 + 1 } rest_id2 = @nd_rest[nid] # the value is supplied by the caller. emit(" " + sanitize_gvar(@nd_name[tid]) + " = " + value_expr + ";") end if @nd_type[tid] == "MultiTargetNode" # Nested LHS: `a, (b, c), d = 1, [2, 3], 4`. The slot of the # outer multi-write that this node occupies is an array-typed # value; recursively unpack it into the inner targets via # per-element index reads. compile_nested_multi_target(tid, value_expr, value_type) end end # Handle `a, *b = rhs` / `*a, b = rhs` / `a, *b, c = rhs`. end end # The element type of an array-typed MultiTarget slot. For # `a, (b, c) = 1, [2, 3]` the slot value_type is "int_array" and # this returns "int" so b/c are declared as mrb_int. Mirrors the # array-prefix dispatch in compile_nested_multi_target. def nested_target_elem_type(slot_type) if slot_type == "int_array" return "int" end if slot_type == "float_array" return "float" end if slot_type == "str_array" return "string" end if slot_type == "sym_array" return "sym" end "int" end # Recursively unpack a `MultiTargetNode` slot in a multi-assign. # `value_expr` holds the value of this slot (always an array-typed # expression, since the source is `a, (b, c), d = ..., [2, 3], ...`). # We dispatch on the array's element type and emit per-target # index reads. def compile_nested_multi_target(tid, value_expr, value_type) lefts = parse_id_list(@nd_targets[tid]) prefix = "" elem_t = "int" if value_type == "int_array" prefix = "IntArray" elem_t = "int" end if value_type == "float_array" prefix = "FloatArray" elem_t = "float" end if value_type == "str_array" prefix = "StrArray" elem_t = "string" end if value_type == "sym_array" prefix = "IntArray" elem_t = "sym" end if prefix == "" $stderr.puts "Spinel: MultiTargetNode requires a typed-array RHS slot; got value_type \"" + value_type + "\"" exit(1) end @needs_gc = 1 tmp_arr = new_temp emit(" sp_" + prefix + " *" + tmp_arr + " = " + value_expr + ";") k = 0 while k < lefts.length inner_val = "sp_" + prefix + "_get(" + tmp_arr + ", " + k.to_s + ")" emit_multi_write_target(lefts[k], inner_val, elem_t) k = k + 1 end end def compile_multi_write(nid) targets = parse_id_list(@nd_targets[nid]) val_id = @nd_expression[nid]spinel_parse.cmodifiedView file
Loading diff…
test/multi_target_nested.rbaddedView file
Loading diff…
test/multi_target_nested.rb.expectedaddedView file
Loading diff…