Commit f10bb189
f10bb189f5e8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 2 files · ~2 modified
Description
feat: Comparable module with operator methods
Integrate sanitize_method() for C function names from Ruby operator methods (<=> → _cmp, < → _lt, etc.). Key changes:
- emit_method(): sanitize method names in C function signatures
- codegen_expr(): sanitize method names when dispatching calls on typed objects
- Forward declarations for class methods with sanitized names
- Fix infer_type for PM_SELF_NODE to return current class type
- Fix infer_type for constructors (ClassName.new) to check before binary ops
- Add PM_SELF_NODE handling in codegen_expr
- Recurse into call arguments in method param type inference walk
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- f10bb189f5e8
- Parent
- b699e005bf3a
examples/bm_comparable.rbmodifiedView file
@@ -1,24 +1,21 @@
1-
# Test alias2-
3-
class Greeter4-
def initialize(name)5-
@name = name1+
class Temperature2+
attr_reader :degrees3+
def initialize(d)4+
@degrees = d65
end7-
8-
def name9-
@name10-
end11-
12-
def hello13-
@name6+
def <=>(other)7+
@degrees - other.degrees148
end15-
16-
alias greet hello9+
def <(other); (self <=> other) < 0; end10+
def >(other); (self <=> other) > 0; end11+
def ==(other); (self <=> other) == 0; end1712
end1813
19-
g = Greeter.new("world")20-
puts g.hello # world21-
puts g.greet # world (alias of hello)22-
puts g.name # world23-
14+
t1 = Temperature.new(100)15+
t2 = Temperature.new(200)16+
t3 = Temperature.new(100)17+
puts t1 < t2 # true18+
puts t2 > t1 # true19+
puts t1 == t3 # true20+
puts t1 > t2 # false2421
puts "done"src/codegen.cmodifiedView file
Loading diff…