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

@@ -743,6 +743,26 @@
743743 R("call", n->call);
744744 break;
745745 }
746+ case PM_MATCH_REQUIRED_NODE: {
747+ /* Rightward assignment: `expr => var` (Ruby 3.0+). When the
748+ pattern is a single LocalVariableTargetNode, this is just
749+ `var = expr` and we lower it to a LocalVariableWriteNode so
750+ the codegen reuses the regular assignment path. Full pattern
751+ matching (array / hash patterns, pinned vars) is out of scope
752+ and falls through to the unknown-node passthrough. */
753+ pm_match_required_node_t *n = (pm_match_required_node_t *)node;
754+ if (n->pattern && PM_NODE_TYPE_P(n->pattern, PM_LOCAL_VARIABLE_TARGET_NODE)) {
755+ pm_local_variable_target_node_t *t = (pm_local_variable_target_node_t *)n->pattern;
756+ N("LocalVariableWriteNode");
757+ NAME("name", t->name);
758+ R("value", n->value);
759+ } else {
760+ N("MatchRequiredNode");
761+ R("value", n->value);
762+ R("pattern", n->pattern);
763+ }
764+ break;
765+ }
746766 case PM_ALTERNATION_PATTERN_NODE: {
747767 pm_alternation_pattern_node_t *n = (pm_alternation_pattern_node_t *)node;
748768 N("AlternationPatternNode");