Commit 04020482
040204822b6aMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 5 files · +1 added · ~4 modified
Description
Add Hash#merge, Hash.new(default), String#dup, Comparable mixin
- A9: Hash#merge for sp_StrIntHash and sp_RbHash (creates new hash with entries from both, second hash overrides first)
- A19: sp_String#dup for mutable strings (IntArray#dup and const String#dup already worked)
- A20: Hash.new(val) with default_value field in sp_StrIntHash; get returns default when key not found
- B2: attr_writer already handled (verified)
- B3: Comparable mixin auto-generates <, >, <=, >=, == methods from <=> when class includes Comparable; handles both value and reference type classes
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 040204822b6a
- Parent
- f5e8a06c89dd
examples/bm_lrama_features2.rbaddedView file
1+
# Test lrama-required features (part 2)2+
3+
# A9: Hash#merge (sp_StrIntHash)4+
h1 = {"a" => 1, "b" => 2}5+
h2 = {"b" => 3, "c" => 4}6+
h3 = h1.merge(h2)7+
puts h3["a"] # 18+
puts h3["b"] # 3 (h2 overrides h1)9+
puts h3["c"] # 410+
11+
# A19: Array#dup (already works)12+
arr = [10, 20, 30]13+
arr2 = arr.dup14+
puts arr2[0] # 1015+
puts arr2.length # 316+
17+
# A19: String#dup (const string)18+
s1 = "hello"19+
s2 = s1.dup20+
puts s2 # hello21+
22+
# A20: Hash.new(0) with default value23+
counter = Hash.new(0)24+
counter["x"] = counter["x"] + 125+
counter["x"] = counter["x"] + 126+
counter["y"] = counter["y"] + 127+
puts counter["x"] # 228+
puts counter["y"] # 129+
puts counter["z"] # 0 (default)30+
31+
# B2: attr_writer32+
class Writer33+
attr_reader :name34+
attr_writer :name35+
def initialize(n)36+
@name = n37+
end38+
end39+
w = Writer.new("before")40+
puts w.name # before41+
w.name = "after"42+
puts w.name # after43+
44+
# B3: Comparable (include Comparable + def <=>)45+
class Weight46+
include Comparable47+
attr_reader :grams48+
def initialize(g)49+
@grams = g50+
end51+
def <=>(other)52+
@grams - other.grams53+
end54+
end55+
56+
w1 = Weight.new(100)57+
w2 = Weight.new(200)58+
w3 = Weight.new(100)59+
puts w1 < w2 # true60+
puts w2 > w1 # true61+
puts w1 == w3 # true62+
puts w1 > w2 # false63+
puts w1 <= w3 # true64+
puts w2 >= w1 # true65+
66+
puts "done"src/codegen.cmodifiedView file
Loading diff…
src/emit.cmodifiedView file
Loading diff…
src/expr.cmodifiedView file
Loading diff…
src/type.cmodifiedView file
Loading diff…