stdlib_test.nx source
↩ module page · 47 lines · 1370 B
1// stdlib_test.nx -- exercise Option + Result via import.
2//
3// Proves that `import "stdlib.nx"` exposes the prelude enums to the
4// importer with full constructor + match-binding semantics.
5
6import "syscalls.nx"
7import "types.nx"
8import "lex_kinds.nx"
9import "ir.nx"
10import "stdlib.nx"
11
12// Return the payload if Some, else 0. Exercises the .Some(v) binding
13// where v is loaded from offset 8 of the shadow struct.
14func unwrap_or_zero(o: *Option) -> i64 {
15 match o {
16 Option::None => { return 0 },
17 Option::Some(v) => { return v },
18 }
19 return 0
20}
21
22// Parse success or failure; branches on tag, binds payload on Err.
23func check(r: *Result) -> i64 {
24 match r {
25 Result::Ok(x) => { return x },
26 Result::Err(e) => { return 0 - e },
27 }
28 return 0
29}
30
31func main() -> i64 {
32 let a: *Option = Option::Some(42)
33 let b: *Option = Option::None
34 let n1: i64 = unwrap_or_zero(a)
35 let n2: i64 = unwrap_or_zero(b)
36 // Expected: n1 = 42, n2 = 0. No execution harness yet -- we
37 // check structural IR properties by letting the pipeline verify
38 // it compiles cleanly.
39
40 let ok: *Result = Result::Ok(10)
41 let err: *Result = Result::Err(7)
42 let s1: i64 = check(ok)
43 let s2: i64 = check(err)
44 // Expected: s1 = 10, s2 = -7.
45
46 return n1 + n2 + s1 + s2
47}