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
A("rights", &n->rights); break; } case PM_EMBEDDED_VARIABLE_NODE: { /* `"foo #@bar"` shorthand for `"foo #{@bar}"`. The cleanest implementation is parser-side lowering: synthesize an EmbeddedStatementsNode wrapping a StatementsNode whose single body element is the variable read. The existing interpolation path then handles it without any codegen change. Same lowering trick as PM_IT_LOCAL_VARIABLE_READ_NODE -- emit a different node type at the same `id` slot so parent walks (parts list of the surrounding InterpolatedString) keep the slot unchanged. The wrapped StatementsNode gets a fresh id allocated via node_counter++. */ pm_embedded_variable_node_t *n = (pm_embedded_variable_node_t *)node; N("EmbeddedStatementsNode"); int var_id = flatten(n->variable); int stmts_id = node_counter++; out_add("N %d StatementsNode", stmts_id); out_add("A %d body %d", stmts_id, var_id); out_add("R %d statements %d", id, stmts_id); break; } default: { /* Fallback: emit unknown node type */ char buf[64];test/embedded_var.rbaddedView file
Loading diff…
test/embedded_var.rb.expectedaddedView file
Loading diff…