Commit 5e927e86
5e927e868e36Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 3 files · +1 added · ~2 modified
Description
Add attr_accessor, for..in, loop, class methods
- attr_accessor/attr_reader/attr_writer: auto-generate getter/setter methods from symbol arguments in class bodies
- for..in with Range: PM_FOR_NODE → C for loop (inclusive/exclusive)
- loop do...end: emit while(1) { body }
- Class methods (def self.foo): emit without self parameter, callable via ClassName.method() syntax
Test: examples/bm_attr.rb — all patterns, output matches Ruby. All regressions pass.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 5e927e868e36
- Parent
- 7cfabb8887a7
examples/bm_attr.rbaddedView file
1+
# Test attr_accessor, attr_reader, attr_writer, class methods, for..in, loop2+
3+
class Point4+
attr_accessor :x, :y5+
6+
def initialize(x, y)7+
@x = x8+
@y = y9+
end10+
11+
def self.origin12+
Point.new(0, 0)13+
end14+
15+
def distance_to(other)16+
dx = @x - other.x17+
dy = @y - other.y18+
Math.sqrt(dx * dx + dy * dy)19+
end20+
21+
def to_s22+
"(" + @x.to_s + ", " + @y.to_s + ")"23+
end24+
end25+
26+
p1 = Point.new(3, 4)27+
puts p1.x # 328+
puts p1.y # 429+
p1.x = 1030+
puts p1.x # 1031+
32+
p2 = Point.origin33+
puts p2.x # 034+
35+
# for..in with range36+
total = 037+
for i in 1..1038+
total += i39+
end40+
puts total # 5541+
42+
# loop with break43+
count = 044+
loop do45+
count += 146+
break if count >= 547+
end48+
puts count # 549+
50+
# Array#each with index (using each)51+
arr = (1..5).to_a52+
sum = 053+
arr.each do |x|54+
sum += x55+
end56+
puts sum # 1557+
58+
# String#to_s on object59+
puts p1.to_s # (10, 4)src/codegen.cmodifiedView file
Loading diff…
src/codegen.hmodifiedView file
Loading diff…