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 matching
4+def describe(val)
5+ case val
6+ in Integer
7+ "integer: #{val}"
8+ in String
9+ "string: #{val}"
10+ in Float
11+ "float"
12+ in true | false
13+ "boolean"
14+ in nil
15+ "nil"
16+ end
17+end
18+
19+puts describe(42) # integer: 42
20+puts describe("hello") # string: hello
21+puts describe(3.14) # float
22+puts describe(true) # boolean
23+puts describe(nil) # nil
24+
25+# Value matching
26+def check(x)
27+ case x
28+ in 0
29+ "zero"
30+ in 1
31+ "one"
32+ in Integer
33+ "other int"
34+ end
35+end
36+
37+puts check(0) # zero
38+puts check(1) # one
39+puts check(99) # other int
40+
41+puts "done"