Compiler: $EXTERN C-symbol aliasing + shared string-expr dispatch - #4
Conversation
Follow-ups to the Level 2 FFI pragma (closes 8329647). Add an optional "= c_symbol" alias to '$EXTERN so a BASIC-legal call name can map to an arbitrary C symbol (e.g. one with underscores like sqlite3_open), removing the need for a hand-written wrapper. A new c_name field carries the emitted symbol; matching stays on the BASIC name. Factor the string-vs-numeric identifier detection out of PRINT into a shared peek_is_string_expr() helper and reuse it in INSTR's start-vs-haystack heuristic and WRITE's string quoting. This makes a string-returning extern classify correctly in all three, and also fixes a pre-existing case where INSTR and WRITE treated FF/FE string functions (CHR$, LEFT$, ENVIRON$, ...) as numeric. The pragma now rejects BASIC names that do not start with a letter, and a malformed alias (e.g. digit-first) falls back to the BASIC name rather than emitting an invalid C symbol. Tests extended (alias, string-extern in INSTR, string comparison); 63/63 compiler tests and the FFI harness pass.
|
Caution Review failedPull request was closed or merged during review 📝 WalkthroughWalkthroughThis PR adds ChangesFFI extern alias and string-expression flow
Sequence Diagram(s)sequenceDiagram
participant BASIC as BASIC program
participant Analysis as parse_extern_pragma
participant Codegen as code generator
participant CLib as c_rev
BASIC->>Analysis: declare $EXTERN Rev = c_rev AS STRING
Analysis-->>Codegen: extern_func_t{name="Rev", c_name="c_rev"}
BASIC->>Codegen: compile Rev("abXc") in INSTR / PRINT / comparison
Codegen->>CLib: emit call to c_rev(...)
CLib-->>Codegen: reversed string
Codegen-->>BASIC: use string result in generated expression
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Follow-ups to the Level 2 FFI pragma (closes
8329647).C-symbol aliasing
'$EXTERN NAME(ARGS) AS RET = c_symbolmaps a BASIC-legal call name to an arbitrary C symbol (e.g. underscores likesqlite3_open), so no hand-written wrapper is needed. A newc_namefield carries the emitted symbol; matching stays on the BASICname. A malformed alias (e.g. digit-first) falls back to the BASIC name rather than emitting invalid C.Shared string-expr dispatch
Factors the string-vs-numeric identifier detection out of
PRINTinto a non-consumingpeek_is_string_expr()helper, reused inINSTR's start-vs-haystack heuristic andWRITE's quoting. A string-returning extern now classifies correctly in all three, and this also fixes a pre-existing case whereINSTR/WRITEtreated FF/FE string functions (CHR$,LEFT$,ENVIRON$, ...) as numeric.Review-driven
A codex + agy review caught that routing a parenthesized string to the string path would loop (the string-atom emitter does not parse a leading
(). That handling was dropped and the limitation documented in Known Limitations;(falls through to numeric as elsewhere. Pragma also rejects BASIC names not starting with a letter.Tests
tests/ffiextended with the alias, a string-extern as anINSTRhaystack, and a string comparison. 63/63 compiler tests and the FFI harness pass. (Local interpreter run shows 3 unrelated sound-test failures from a missing audio sink in this session, not touched by this compiler-only change.)Summary by CodeRabbit
New Features
PRINT,WRITE,INSTR, and string comparisons.Bug Fixes
Documentation
Tests