Commit 5e9d5a95
5e9d5a9571d7Merged
Ryan Seys <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
feat(parse): support rightward assignment (expr => var)
Ruby 3.0+ rightward assignment expr => var is encoded by Prism as
a MatchRequiredNode whose pattern is a LocalVariableTargetNode and
whose value is the source expression. Pre-fix the codegen saw the
MatchRequiredNode as UnknownNode_101 and dropped the assignment.
Fix lowers the simple-target case (single LocalVariableTargetNode pattern, the only common shape in non-pattern-matching contexts) to a LocalVariableWriteNode at the AST serializer boundary:
expr => var → var = expr (LocalVariableWriteNode)
Full pattern matching (expr => [a, b], expr => {key:}, pinned
vars ^x) falls through to a passthrough MatchRequiredNode that the
codegen still doesn't handle — those remain out of scope (the
existing case .. in .. form already covers most uses).
Test exercises five shapes:
- integer literal
42 => x - string literal
"hello" => msg - method-call result
square(7) => sq - chained statements with two locals
- boolean expression
(5 > 3) => is_bigger
Bootstrap: gen2.c == gen3.c. Tests: 191 pass.
Details
- Commit
- 5e9d5a9571d7
- Parent
- 02005f26deba
spinel_parse.cmodifiedView file
R("call", n->call); break; } case PM_MATCH_REQUIRED_NODE: { /* Rightward assignment: `expr => var` (Ruby 3.0+). When the pattern is a single LocalVariableTargetNode, this is just `var = expr` and we lower it to a LocalVariableWriteNode so the codegen reuses the regular assignment path. Full pattern matching (array / hash patterns, pinned vars) is out of scope and falls through to the unknown-node passthrough. */ pm_match_required_node_t *n = (pm_match_required_node_t *)node; if (n->pattern && PM_NODE_TYPE_P(n->pattern, PM_LOCAL_VARIABLE_TARGET_NODE)) { pm_local_variable_target_node_t *t = (pm_local_variable_target_node_t *)n->pattern; N("LocalVariableWriteNode"); NAME("name", t->name); R("value", n->value); } else { N("MatchRequiredNode"); R("value", n->value); R("pattern", n->pattern); } break; } case PM_ALTERNATION_PATTERN_NODE: { pm_alternation_pattern_node_t *n = (pm_alternation_pattern_node_t *)node; N("AlternationPatternNode");test/rightward_assign.rbaddedView file
Loading diff…