f619316470
Lower match to if-else for literals, ranges, enum tags, and wildcards. Fix bootstrap literal arms that always matched; port real arm AST/parser and Lcx_LowerMatch to selfhost with last-expression return. Expand pattern_matching example and add parse/use-after-move/double-mut golden diagnostics. Selfhost-loop remains binary-identical.
66 lines
1.2 KiB
Plaintext
66 lines
1.2 KiB
Plaintext
// Pattern Matching — enum tags, literals, ranges, wildcard
|
|
import Std::Io::{PrintLine, PrintInt};
|
|
|
|
|
|
enum Option {
|
|
Some(int),
|
|
None
|
|
}
|
|
|
|
func GetValue(opt: Option) -> int {
|
|
match opt {
|
|
Option::Some(value) => opt.data.Some_0,
|
|
Option::None => 0
|
|
}
|
|
}
|
|
|
|
func Classify(n: int) -> int {
|
|
// literal arms + exclusive range + inclusive range + wildcard
|
|
match n {
|
|
0 => 100,
|
|
1 => 101,
|
|
2..5 => 200,
|
|
6..=10 => 300,
|
|
_ => -1
|
|
}
|
|
}
|
|
|
|
func ColorName(c: int) -> String {
|
|
match c {
|
|
1 => "red",
|
|
2 => "green",
|
|
3 => "blue",
|
|
_ => "unknown"
|
|
}
|
|
}
|
|
|
|
func Main() -> int {
|
|
let opt1: Option = Option { tag: Option_Some };
|
|
opt1.data.Some_0 = 42;
|
|
|
|
let opt2: Option = Option { tag: Option_None };
|
|
|
|
PrintLine("opt1 value: ");
|
|
PrintInt(GetValue(opt1));
|
|
PrintLine("");
|
|
|
|
PrintLine("opt2 value: ");
|
|
PrintInt(GetValue(opt2));
|
|
PrintLine("");
|
|
|
|
PrintLine("classify:");
|
|
PrintInt(Classify(0));
|
|
PrintLine("");
|
|
PrintInt(Classify(3));
|
|
PrintLine("");
|
|
PrintInt(Classify(8));
|
|
PrintLine("");
|
|
PrintInt(Classify(99));
|
|
PrintLine("");
|
|
|
|
PrintLine("color:");
|
|
PrintLine(ColorName(2));
|
|
|
|
return 0;
|
|
}
|