Commit 9422716d
9422716d73a8Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 8 months ago · 2 files · +1 added · ~1 modified
Description
feat: case/in pattern matching with sp_RbValue tag dispatch
Implement PM_CASE_MATCH_NODE (case/in) for pattern matching on polymorphic sp_RbValue values. Patterns generate tag/value checks: Integer/String/Float class patterns, literal value patterns (0, 1), nil/true/false, and alternation (true | false). Also fixes PM_INTERPOLATED_STRING_NODE codegen_expr to use AOT-compatible sp_str_concat/sp_int_to_s/sp_poly_to_s instead of mrb_value fallback.
Co-Authored-By: Claude Opus 4.6 (1M context) [email protected]
Details
- Commit
- 9422716d73a8
- Parent
- 4d7973ddec3c
examples/bm_pattern.rbaddedView file
1+
# Test pattern matching (case/in)2+
3+
# Type-based matching4+
def describe(val)5+
case val6+
in Integer7+
"integer: #{val}"8+
in String9+
"string: #{val}"10+
in Float11+
"float"12+
in true | false13+
"boolean"14+
in nil15+
"nil"16+
end17+
end18+
19+
puts describe(42) # integer: 4220+
puts describe("hello") # string: hello21+
puts describe(3.14) # float22+
puts describe(true) # boolean23+
puts describe(nil) # nil24+
25+
# Value matching26+
def check(x)27+
case x28+
in 029+
"zero"30+
in 131+
"one"32+
in Integer33+
"other int"34+
end35+
end36+
37+
puts check(0) # zero38+
puts check(1) # one39+
puts check(99) # other int40+
41+
puts "done"src/codegen.cmodifiedView file
Loading diff…