Commit af9d40dd

af9d40dd3174Merged

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

Description

feat(parse,codegen): support SourceFileNode (FILE)

Adds parser case in spinel_parse.c that emits the toplevel script path (passed via argv) as the SourceFileNode's content. Codegen arm in compile_expr returns it as a c_string_literal, plus an infer_type arm so the type system knows FILE produces a string (without it, puts(FILE) emitted printf("%lld\n", ...) casting the pointer to long).

Limitation: spinel inlines require/require_relative at parse time, so FILE at every call site sees the toplevel script path. This matches CRuby for top-level uses but diverges inside required files. Documented in the test fixture.

Test: test/source_file.rb. Bootstrap: gen2.c == gen3.c.

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

Details

Commit
af9d40dd3174
Parent
311d03089966

spinel_codegen.rbmodifiedView file

@@ -1665,6 +1665,9 @@
16651665 if t == "StringNode"
16661666 return "string"
16671667 end
1668+ if t == "SourceFileNode"
1669+ return "string"
1670+ end
16681671 if t == "SymbolNode"
16691672 return "symbol"
16701673 end
@@ -15634,6 +15637,12 @@
1563415637 if t == "SourceLineNode"
1563515638 return @nd_value[nid].to_s
1563615639 end
15640+ if t == "SourceFileNode"
15641+ # __FILE__ — parser populated @nd_content with the toplevel
15642+ # script path. (Inlined-require call sites all see the same
15643+ # path; documented limitation in test/source_file.rb.)
15644+ return c_string_literal(@nd_content[nid])
15645+ end
1563715646 if t == "ArgumentsNode"
1563815647 arg_ids = parse_id_list(@nd_args[nid])
1563915648 if arg_ids.length > 0