Files
sakurac/tests/programs/enums_and_match_values_1.sakura
T
2026-10-28 22:13:49 -05:00

40 lines
1.7 KiB
Plaintext

import std::io;
enum color: u8 { red, green = 4, blue }
enum level { low = -3, mid, high = 10 }
enum wide: u64 { top = 18446744073709551615, bottom = 0 }
struct nothing {}
union value { var none: nothing; var number: i64; var text: span<u8>; }
struct paint { var shade: color; var levels: span<level>; }
def color::next(this: &color): color {
return match this { red => color::green, green => color::blue, blue => color::red };
}
def name(v: value): string {
return match v { number(n) => $"int {n}", text(s) => s, none => "none" };
}
def most<T>(a: T, b: T): T where T: ordered { return a > b ? a : b; }
def main(): void {
var c := color::red;
val seen: color[3] = [c, c.next(), c.next().next()];
var total: u32 = 0;
for s in seen { total += s; }
var ls: level[2] = [level::low, level::high];
val p := paint{ shade = color::blue, levels = span::<level>::over((&ls) as *level, 2) };
println($"{seen} {total} {p} {most(color::green, color::red)} {wide::top} {wide::bottom}");
for n in [0, 4, 5] {
val shade := n as color;
val word := match shade { red => "r", green => "g", blue => "b" };
val lv := match n { 0 => level::low, 4, 5 => level::mid, else => level::high };
print($"{shade}:{word}:{lv}:{lv as i32} ");
}
println("");
var values: value[3] = [value{}, value{ number = 7 }, value{ text = "text" }];
for v in values { print($"{name(v)}; "); }
println("");
val bytes := [color::green, color::blue];
var copy := bytes;
copy[0] = color::red;
println($"{bytes} {copy} {bytes == copy} {level::mid < level::high} {c == 0}");
val pick := def(x: color): int { return match x { red => 1, else => 2 } * 10; };
println($"{pick(color::red)} {pick(color::blue)}");
}