Skip to content

Commit 8fe8247

Browse files
authored
feat(compiler): Allow newline in or-patterns (#1323)
1 parent 11c2030 commit 8fe8247

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

compiler/src/parsing/wrapped_lexer.re

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ let restore_lexer_positions = state => {
351351
};
352352

353353
let token = state => {
354-
// if-else hack
354+
// if-else and or-patterns hack
355355
let (tok, _, _) as triple = token(state);
356356
if (tok == EOL) {
357357
let fst = ((a, _, _)) => a;
@@ -363,7 +363,8 @@ let token = state => {
363363
};
364364

365365
switch (fst(next_triple^)) {
366-
| ELSE => next_triple^
366+
| ELSE
367+
| PIPE => next_triple^
367368
| _ =>
368369
state.queued_tokens =
369370
List.tl(
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
pattern matching › or_match_4
2+
(module
3+
(type $none_=>_i32 (func (result i32)))
4+
(type $none_=>_none (func))
5+
(import \"_grainEnv\" \"mem\" (memory $0 0))
6+
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
7+
(import \"_grainEnv\" \"relocBase\" (global $relocBase_0 i32))
8+
(import \"_grainEnv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
9+
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
10+
(elem $elem (global.get $relocBase_0))
11+
(export \"memory\" (memory $0))
12+
(export \"_gmain\" (func $_gmain))
13+
(export \"_start\" (func $_start))
14+
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
15+
(func $_gmain (result i32)
16+
(local $0 i32)
17+
(local $1 i32)
18+
(local $2 i32)
19+
(local $3 i64)
20+
(local $4 f32)
21+
(local $5 f64)
22+
(local $6 i32)
23+
(local $7 i32)
24+
(return
25+
(block $cleanup_locals.15 (result i32)
26+
(local.set $0
27+
(block $compile_block.14 (result i32)
28+
(block $compile_store.2
29+
(local.set $6
30+
(i32.or
31+
(i32.shl
32+
(i32.eq
33+
(i32.const -2)
34+
(i32.const 2147483646)
35+
)
36+
(i32.const 31)
37+
)
38+
(i32.const 2147483646)
39+
)
40+
)
41+
(block $do_backpatches.1
42+
)
43+
)
44+
(block $compile_store.10
45+
(local.set $7
46+
(if (result i32)
47+
(i32.shr_u
48+
(local.get $6)
49+
(i32.const 31)
50+
)
51+
(block $compile_block.3 (result i32)
52+
(i32.const 1)
53+
)
54+
(block $compile_block.8 (result i32)
55+
(block $compile_store.5
56+
(local.set $7
57+
(i32.or
58+
(i32.shl
59+
(i32.eq
60+
(i32.const -2)
61+
(i32.const -2)
62+
)
63+
(i32.const 31)
64+
)
65+
(i32.const 2147483646)
66+
)
67+
)
68+
(block $do_backpatches.4
69+
)
70+
)
71+
(if (result i32)
72+
(i32.shr_u
73+
(local.get $7)
74+
(i32.const 31)
75+
)
76+
(block $compile_block.6 (result i32)
77+
(i32.const 1)
78+
)
79+
(block $compile_block.7
80+
(unreachable)
81+
)
82+
)
83+
)
84+
)
85+
)
86+
(block $do_backpatches.9
87+
)
88+
)
89+
(block $switch.11_outer (result i32)
90+
(block $switch.11_branch_0 (result i32)
91+
(drop
92+
(block $switch.11_branch_1 (result i32)
93+
(drop
94+
(block $switch.11_default (result i32)
95+
(br_table $switch.11_branch_1 $switch.11_default $switch.11_default
96+
(i32.const 0)
97+
(i32.shr_s
98+
(local.get $7)
99+
(i32.const 1)
100+
)
101+
)
102+
)
103+
)
104+
(br $switch.11_outer
105+
(block $compile_block.13 (result i32)
106+
(unreachable)
107+
)
108+
)
109+
)
110+
)
111+
(br $switch.11_outer
112+
(block $compile_block.12 (result i32)
113+
(i32.const 7)
114+
)
115+
)
116+
)
117+
)
118+
)
119+
)
120+
(local.get $0)
121+
)
122+
)
123+
)
124+
(func $_start
125+
(drop
126+
(call $_gmain)
127+
)
128+
)
129+
;; custom section \"cmi\", size 242
130+
)

compiler/test/suites/pattern_matching.re

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ describe("pattern matching", ({test, testSkip}) => {
225225
"or_match_3",
226226
"match ([5]) { [a, _] | [_, a, _] | [a] => true, _ => false }",
227227
);
228+
assertSnapshot("or_match_4", {|match (true) { true
229+
| false => 3 }|});
228230
// Aliases
229231
assertSnapshot("alias_match_1", "match (true) { _ as p => p }");
230232
assertSnapshot("alias_match_2", "match (true) { a as b => a && b }");

0 commit comments

Comments
 (0)