doc: sample: add match-statement syntax and ir samples

This commit is contained in:
2025-11-04 10:38:34 +00:00
parent 02b21126b5
commit 8bb734d882
2 changed files with 65 additions and 0 deletions

18
doc/sample/Match.im Normal file
View File

@@ -0,0 +1,18 @@
age = 65.
yz = 32.
p3 = 3.
q = match yz in
1 => do
wq = 32.
wq * 2
end,
2 => age / 12,
p3 => age / 365,
5 | 6 => age = 32,
10..=15 => age = 64,
_ => 0
end

47
doc/sample/Match.mie Normal file
View File

@@ -0,0 +1,47 @@
define void @init() static {
entry:
%age = alloca id
store i32 #65, ptr %age
%yz = alloca id
store i32 #32, ptr %yz
%p3 = alloca id
store i32 #3, ptr %p3
%1 = load id, ptr %yz
%2 = load id, ptr %p3
switch id %1, label %match.d,
[ i32 #1, label %match.0 ],
[ i32 #2, label %match.1 ],
[ i32 %2, label %match.2 ]
match.0:
%wq = alloca id
store i32 #32, ptr %wq
%3 = load id, ptr %wq
%multmp = mul id %3, i32 #2
br label %match.end
match.1:
%4 = load id, ptr %age
%divtmp = mul id %4, i32 #12
br label %match.end
match.2:
%5 = load id, ptr %age
%divtmp.0 = mul id %5, i32 #365
br label %match.end
match.d:
br label %match.end
match.end:
%match.result = phi id
[ %match.0, %multmp],
[ %match.1, %divtmp ],
[ %match.2, %divtmp.0 ],
[ %match.d, #0 ]
%q = alloca id
store id %match.result, ptr %q
ret void
}