Commit ba065596
ba065596d617Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · +1 added · ~2 modified
Description
feat: sp_RbValue Phase 1 — foundational polymorphism support
Add SPINEL_TYPE_POLY as a tagged-union type (sp_RbValue) for variables and function parameters that hold values of incompatible types at different points in execution.
Phase 1a: sp_RbValue type with boxing/unboxing helpers (sp_box_int, sp_box_float, sp_box_str, sp_box_bool, sp_box_nil). Only emitted when needs_poly is true — monomorphic programs are unaffected.
Phase 1b: Runtime operations on sp_RbValue — sp_poly_puts dispatches by tag for correct output formatting, sp_poly_nil_p for nil? checks.
Phase 1c: Type inference widening — var_declare and infer_type now produce POLY instead of VALUE when incompatible scalar types meet (Int+String, String+nil, etc.). Numeric promotion (Int+Float→Float) is preserved. Complex types (Array, Hash, Object) never widen to POLY.
Phase 1d: Codegen for POLY — variable declarations use sp_RbValue, assignments auto-box (lv_x = sp_box_int(42)), function call sites box arguments when target param is POLY, puts dispatches through sp_poly_puts, and nil? uses sp_poly_nil_p.
All 37 existing examples produce identical output (zero regressions). New bm_poly.rb exercises variable reassignment, polymorphic method params, nilable tracking, and matches Ruby output exactly.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- ba065596d617
- Parent
- ef6b705314f9
examples/bm_poly.rbaddedView file
# Test polymorphic variables (sp_RbValue)# Variable holding different typesx = 1puts x # 1x = "hello"puts x # hello# Polymorphic method parameterdef show(val) puts valendshow(42)show("world")show(true)show(3.14)# Nilabley = "found"puts y # foundy = nilputs y.nil? # true — but this needs nil trackingputs "done"src/codegen.cmodifiedView file
Loading diff…
src/codegen.hmodifiedView file
Loading diff…