Commit 30f6a5ce
30f6a5cec650Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · +1 added · ~2 modified
Description
feat: sp_RbValue Phase 2 — heterogeneous arrays and bimorphic duck typing
Phase 2a: Array literals now generate sp_RbArray (heterogeneous), with sp_RbValue elements. arr.each iterates with POLY-typed block params, and arr.length returns the count. All array literals use sp_RbArray; sp_IntArray remains for Range#to_a results.
Phase 2b: When a function parameter receives objects of 2+ different classes (e.g., Dog.new and Cat.new), type inference widens to POLY and tracks the known class set. Method calls on such parameters generate bimorphic inline if/else dispatch using per-class tags in the sp_tag enum (SP_TAG_Dog = 64, SP_TAG_Cat = 65, etc.). Objects are boxed via sp_box_obj(tag, ptr) and unboxed with tag check + pointer cast.
Also generates default constructors for classes with no initialize method, so Dog.new / Cat.new work for simple data-less classes.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 30f6a5cec650
- Parent
- ba065596d617
examples/bm_poly2.rbaddedView file
# Test Phase 2: heterogeneous arrays, bimorphic dispatch# Heterogeneous arrayarr = [1, "two", 3.0, true, nil]arr.each do |x| puts xend# 1, two, 3.0, true, (blank)# Array with mixed typesnums = [10, 20, 30]puts nums.length # 3# Duck typing (bimorphic)class Dog def speak "Woof!" endendclass Cat def speak "Meow!" endenddef make_noise(animal) puts animal.speakendmake_noise(Dog.new) # Woof!make_noise(Cat.new) # Meow!puts "done"src/codegen.cmodifiedView file
Loading diff…
src/codegen.hmodifiedView file
Loading diff…