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 alias
2-
3-class Greeter
4- def initialize(name)
5- @name = name
1+class Temperature
2+ attr_reader :degrees
3+ def initialize(d)
4+ @degrees = d
65 end
7-
8- def name
9- @name
10- end
11-
12- def hello
13- @name
6+ def <=>(other)
7+ @degrees - other.degrees
148 end
15-
16- alias greet hello
9+ def <(other); (self <=> other) < 0; end
10+ def >(other); (self <=> other) > 0; end
11+ def ==(other); (self <=> other) == 0; end
1712 end
1813
19-g = Greeter.new("world")
20-puts g.hello # world
21-puts g.greet # world (alias of hello)
22-puts g.name # world
23-
14+t1 = Temperature.new(100)
15+t2 = Temperature.new(200)
16+t3 = Temperature.new(100)
17+puts t1 < t2 # true
18+puts t2 > t1 # true
19+puts t1 == t3 # true
20+puts t1 > t2 # false
2421 puts "done"