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

1+# Test polymorphic variables (sp_RbValue)
2+
3+# Variable holding different types
4+x = 1
5+puts x # 1
6+x = "hello"
7+puts x # hello
8+
9+# Polymorphic method parameter
10+def show(val)
11+ puts val
12+end
13+show(42)
14+show("world")
15+show(true)
16+show(3.14)
17+
18+# Nilable
19+y = "found"
20+puts y # found
21+y = nil
22+puts y.nil? # true — but this needs nil tracking
23+
24+puts "done"