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
if t == "InterpolatedStringNode" return "string" end if t == "InterpolatedSymbolNode" # Spinel doesn't intern dynamic symbols; the runtime value is # the assembled string. Use sites that need symbol-typed # behaviour (sym_int_hash keys, ===) won't work, but puts/==/ # string interpolation/regex match all do. return "string" end if t == "TrueNode" return "bool" end if t == "InterpolatedStringNode" return compile_interpolated(nid) end if t == "InterpolatedSymbolNode" # `:"foo_#{x}"` -- assemble via the same parts loop as # InterpolatedStringNode. CRuby returns a Symbol; Spinel returns # the string form because dynamic symbol interning isn't # supported (sp_sym ids are reserved for compile-time literals). # Use sites that accept either string or symbol -- puts, ==, # string interpolation -- behave identically. return compile_interpolated(nid) end if t == "NumberedReferenceReadNode" num = @nd_value[nid] if num >= 1 && num <= 9spinel_parse.cmodifiedView file
Loading diff…
test/interp_symbol.rbaddedView file
Loading diff…
test/interp_symbol.rb.expectedaddedView file
Loading diff…