Commit 4e5a78a8

4e5a78a89432Merged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

feat(parse): support EmbeddedVariableNode ("foo #@bar")

Parser-side lowering with no codegen change. EmbeddedVariableNode covers the three sigils for shorthand variable interpolation: "foo #$gv", "foo #@iv", "foo #@@cv". The cleanest implementation is to lower at parse time to the equivalent EmbeddedStatementsNode shape -- a StatementsNode with a single body element that's the variable read -- so the existing compile_interpolated path handles it without modification.

Same lowering trick as PM_IT_LOCAL_VARIABLE_READ_NODE at line 1011 (and PM_SHAREABLE_CONSTANT_NODE / PM_IMPLICIT_NODE earlier): emit a different node type at the same id slot so the parent's parts list keeps the same reference. The wrapped StatementsNode gets a fresh id via node_counter++, and we emit the array-record manually since out_add takes printf-style varargs.

Test exercises (test/embedded_var.rb):

  • "got: #$global" -- top-level GlobalVariableReadNode embedded.
  • "got: #@ivar" -- InstanceVariableReadNode embedded inside a method.

Tests: 272 pass. Bootstrap: gen2.c == gen3.c.

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
4e5a78a89432
Parent
59ff8d7a4f30

spinel_parse.cmodifiedView file

@@ -1033,6 +1033,27 @@
10331033 A("rights", &n->rights);
10341034 break;
10351035 }
1036+ case PM_EMBEDDED_VARIABLE_NODE: {
1037+ /* `"foo #@bar"` shorthand for `"foo #{@bar}"`. The cleanest
1038+ implementation is parser-side lowering: synthesize an
1039+ EmbeddedStatementsNode wrapping a StatementsNode whose single
1040+ body element is the variable read. The existing interpolation
1041+ path then handles it without any codegen change.
1042+
1043+ Same lowering trick as PM_IT_LOCAL_VARIABLE_READ_NODE -- emit
1044+ a different node type at the same `id` slot so parent walks
1045+ (parts list of the surrounding InterpolatedString) keep the
1046+ slot unchanged. The wrapped StatementsNode gets a fresh id
1047+ allocated via node_counter++. */
1048+ pm_embedded_variable_node_t *n = (pm_embedded_variable_node_t *)node;
1049+ N("EmbeddedStatementsNode");
1050+ int var_id = flatten(n->variable);
1051+ int stmts_id = node_counter++;
1052+ out_add("N %d StatementsNode", stmts_id);
1053+ out_add("A %d body %d", stmts_id, var_id);
1054+ out_add("R %d statements %d", id, stmts_id);
1055+ break;
1056+ }
10361057 default: {
10371058 /* Fallback: emit unknown node type */
10381059 char buf[64];