fix: C backend, parser, sema, HIR lowering for all examples
- C backend: strip c8/c16/c32 string prefixes in emitted C - C backend: resolve imports to fully-qualified names (Std_Io_PrintLine) - Parser: fix infinite loop on < in comparisons vs generic calls - Parser: fix enum patterns without parens (Option::None) - Parser: fix match expressions inside blocks - Sema: extract pattern bindings for match arms - HIR: lower match expressions to if-else chains with enum tag checks - HIR/C backend: support block expressions as function return values - Makefile: add integration tests for all 9 examples - Add pattern_matching.bux example
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
// Pattern Matching - Match expressions with algebraic enums
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
|
||||
enum Option {
|
||||
Some(int),
|
||||
None
|
||||
}
|
||||
|
||||
func GetValue(opt: Option) -> int {
|
||||
match opt {
|
||||
Option::Some(value) => opt.data.Some_0,
|
||||
Option::None => 0
|
||||
}
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let opt1: Option = Option { tag: Option_Some };
|
||||
opt1.data.Some_0 = 42;
|
||||
|
||||
let opt2: Option = Option { tag: Option_None };
|
||||
|
||||
Std_Io_PrintLine("opt1 value: ");
|
||||
Std_Io_PrintInt(GetValue(opt1));
|
||||
Std_Io_PrintLine("");
|
||||
|
||||
Std_Io_PrintLine("opt2 value: ");
|
||||
Std_Io_PrintInt(GetValue(opt2));
|
||||
Std_Io_PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user