Commit f87de047
f87de0478ee6Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 5 files · +2 added · ~3 modified
Description
parse,analyze,codegen: pattern-match as + guard variable bind
case x in pat => var (CapturePatternNode) and
case x in n if guard (IfNode-wrapped pattern) used to leave
the matched LV undeclared in the generated C.
- parse: PM_CAPTURE_PATTERN_NODE -> CapturePatternNode with value (inner pattern) + target (LV target)
- analyze: scan_locals registers the as-var with the scrutinee's type via a new @scan_case_match_pred_type side channel; IfNode-wrapped LocalVariableTargetNode is also unwrapped so the guard form registers its LV
- codegen: compile_in_pattern handles LocalVariableTargetNode (always-match + bind via comma-expr), IfNode (recurse for inner pattern, AND-fold the guard predicate), CapturePatternNode (recurse + bind). The InNode dispatch peels CapturePatternNode off ArrayPattern/HashPattern arms and injects the binding at body entry.
Refs #884 #905
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- f87de0478ee6
- Parent
- f6640684b3f3
spinel_analyze.rbmodifiedView file
@@ -110,6 +110,11 @@
110110
# type each bound LV with the enclosing CaseMatchNode's scrutinee111111
# element type. Issue #669.112112
@scan_case_match_pred_elem = ""113+
# Sibling channel for CapturePatternNode (`pat => var`) and114+
# bare LocalVariableTargetNode in InNode (`case x in n`). Holds115+
# the scrutinee's full type so the target LV gets the right slot.116+
# Issues #884 #905.117+
@scan_case_match_pred_type = ""113118
# Sibling side-channel for HashPatternNode: the hash's value type.114119
# Issue #805.115120
@scan_case_match_pred_val_t = ""@@ -26313,9 +26318,15 @@
2631326318
if @nd_type[nid] == "CaseMatchNode"2631426319
saved_pred_elem = @scan_case_match_pred_elem2631526320
saved_pred_val_t = @scan_case_match_pred_val_t26321+
saved_pred_type_full = @scan_case_match_pred_type2631626322
pred_nid = @nd_predicate[nid]2631726323
if pred_nid >= 02631826324
cmt = infer_type(pred_nid)26325+
# Issues #884 #905: stash the scrutinee's full type so a bare26326+
# `LocalVariableTargetNode` pattern (`case x in n`) or a26327+
# `CapturePatternNode` (`case x in [...] => arr`) can bind26328+
# its LV with the right slot type.26329+
@scan_case_match_pred_type = cmt2631926330
if cmt == "int_array" || cmt == "sym_array"2632026331
@scan_case_match_pred_elem = "int"2632126332
elsif cmt == "str_array"@@ -26344,8 +26355,55 @@
2634426355
scan_locals_children(nid, names, types, params)2634526356
@scan_case_match_pred_elem = saved_pred_elem2634626357
@scan_case_match_pred_val_t = saved_pred_val_t26358+
@scan_case_match_pred_type = saved_pred_type_full26359+
return26360+
end26361+
# `case x in pat => var` (CapturePatternNode) -- register the26362+
# target LV with the scrutinee's type. Issue #884.26363+
if @nd_type[nid] == "CapturePatternNode"26364+
cp_target = @nd_target[nid]26365+
if cp_target >= 0 && @nd_type[cp_target] == "LocalVariableTargetNode"26366+
cp_lname = @nd_name[cp_target]26367+
if not_in(cp_lname, names) == 1 && not_in(cp_lname, params) == 126368+
names.push(cp_lname)26369+
types.push(@scan_case_match_pred_type != "" ? @scan_case_match_pred_type : "int")26370+
@scan_literal_flags.push("")26371+
@scan_empty_flags.push("")26372+
@scan_empty_hash_flags.push("")26373+
end26374+
end26375+
scan_locals_children(nid, names, types, params)2634726376
return2634826377
end26378+
# `case x in n` (bare LocalVariableTargetNode pattern at the26379+
# InNode root) -- register n with the scrutinee's type. Also26380+
# `in n if guard` (IfNode wrapping a LocalVariableTargetNode26381+
# in its statements). Issues #905 #884.26382+
if @nd_type[nid] == "InNode"26383+
pat_in = @nd_pattern[nid]26384+
in_target_pat = pat_in26385+
# Unwrap IfNode (guard clause) to find the underlying pattern.26386+
# "statements" parser field maps onto @nd_body.26387+
if in_target_pat >= 0 && @nd_type[in_target_pat] == "IfNode"26388+
in_if_stmts = @nd_body[in_target_pat]26389+
if in_if_stmts >= 026390+
in_if_body = get_stmts(in_if_stmts)26391+
if in_if_body.length > 026392+
in_target_pat = in_if_body[0]26393+
end26394+
end26395+
end26396+
if in_target_pat >= 0 && @nd_type[in_target_pat] == "LocalVariableTargetNode"26397+
in_lname = @nd_name[in_target_pat]26398+
if not_in(in_lname, names) == 1 && not_in(in_lname, params) == 126399+
names.push(in_lname)26400+
types.push(@scan_case_match_pred_type != "" ? @scan_case_match_pred_type : "int")26401+
@scan_literal_flags.push("")26402+
@scan_empty_flags.push("")26403+
@scan_empty_hash_flags.push("")26404+
end26405+
end26406+
end2634926407
if @nd_type[nid] == "MultiWriteNode"2635026408
targets = parse_id_list(@nd_targets[nid])2635126409
val_id2 = @nd_expression[nid]spinel_codegen.rbmodifiedView file
Loading diff…
spinel_parse.cmodifiedView file
Loading diff…
test/pattern_match_as_and_guard.rbaddedView file
Loading diff…
test/pattern_match_as_and_guard.rb.expectedaddedView file
Loading diff…