Commit 18c3f982

18c3f982b07dMerged

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

Description

feat(parse,codegen): support InterpolatedSymbolNode (:"foo_#{x}")

Parser emits the node with parts carrying the interpolation fragments (StringNode + EmbeddedStatementsNode + EmbeddedVariableNode), matching InterpolatedStringNode's shape. compile_expr arm in turn routes through compile_interpolated -- the assembled string IS the runtime value of the symbol.

Spinel-specific divergence from CRuby: dynamic symbols don't intern. sp_sym ids are reserved for compile-time literal symbols (registered in @symbol_names with stable indices); a runtime-built symbol can't allocate one without breaking the IntArray-backed sym_int_hash and sym_array storage that depend on the index being known at compile time. We therefore expose the symbol as its string form. Use sites that accept either:

  • puts / print / string interpolation -- pass through unchanged.
  • s.to_s -- already a string.
  • == against a string-typed RHS -- agrees.
  • == against another InterpolatedSymbolNode -- agrees.

Out of scope (use sites that need genuine symbol identity): sym_int_hash keys, sym_array elements, case x; when :literal against an interpolated subject.

Test exercises (test/interp_symbol.rb):

  • Two interpolated symbols with string and integer subjects.
  • Pass-through to a method that calls .to_s.

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

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

Details

Commit
18c3f982b07d
Parent
fea5369dddb0

spinel_codegen.rbmodifiedView file

@@ -1725,6 +1725,13 @@
17251725 if t == "InterpolatedStringNode"
17261726 return "string"
17271727 end
1728+ if t == "InterpolatedSymbolNode"
1729+ # Spinel doesn't intern dynamic symbols; the runtime value is
1730+ # the assembled string. Use sites that need symbol-typed
1731+ # behaviour (sym_int_hash keys, ===) won't work, but puts/==/
1732+ # string interpolation/regex match all do.
1733+ return "string"
1734+ end
17281735 if t == "TrueNode"
17291736 return "bool"
17301737 end
@@ -17057,6 +17064,15 @@
1705717064 if t == "InterpolatedStringNode"
1705817065 return compile_interpolated(nid)
1705917066 end
17067+ if t == "InterpolatedSymbolNode"
17068+ # `:"foo_#{x}"` -- assemble via the same parts loop as
17069+ # InterpolatedStringNode. CRuby returns a Symbol; Spinel returns
17070+ # the string form because dynamic symbol interning isn't
17071+ # supported (sp_sym ids are reserved for compile-time literals).
17072+ # Use sites that accept either string or symbol -- puts, ==,
17073+ # string interpolation -- behave identically.
17074+ return compile_interpolated(nid)
17075+ end
1706017076 if t == "NumberedReferenceReadNode"
1706117077 num = @nd_value[nid]
1706217078 if num >= 1 && num <= 9