Files
sakurac/tests/programs/const_declarations_1.sakura
2026-11-19 04:54:17 -06:00

54 lines
1.7 KiB
Plaintext

import std::io;
enum color: u8 { red, green = 4, blue }
union shape { var circle: f32; var count: i32; }
struct entry { var weight: i16; var hue: color; var form: shape; }
def make(): entry[2] {
var out: entry[2];
for i in 0..2 {
out[i] = entry{ weight = -(i as i16) * 100, hue = color::green,
form = i % 2 == 0 ? shape{ circle = 1.5 } : shape{ count = i as i32 } };
}
return out;
}
const entries: entry[2] = make();
def crc_table(): u32[256] {
var table: u32[256];
for i in 0..256 {
var c := i as u32;
for _ in 0..8 { c = (c & 1) != 0 ? 0xEDB88320 ^ (c >> 1) : c >> 1; }
table[i] = c;
}
return table;
}
const crc: u32[256] = crc_table();
def doubled(): int { return base * 2; }
const twice := doubled();
const base := 21;
struct counter { var n: int; }
def counter::bump(&mut this): void { this.n++; }
def bumped(): int { var c := counter{ 0 }; c.bump(); c.bump(); return c.n; }
trait sized { const bytes: u64; }
const i32::bytes: u64 = 4;
const i64::bytes: u64 = 8;
def show<T>(x: T): string where T: sized {
const bits := T::bytes * 8;
if const bits > 32 { return $"wide {x}"; } else { return $"narrow {x}"; }
}
def kind<T>(x: T): string {
if const T is integer { return $"{x + 1} is {size_of::<T>() * 8} bits"; }
else if const T is ordered { return "ordered"; }
else { return "other"; }
}
const small: f32 = 0.1;
const neg: i8 = -5;
val copied := crc;
def main(): void {
for e in entries { println($"{e.weight} {e.hue} {e.form}"); }
const local := bumped() + twice;
println($"{crc[1]} {crc.length} {copied[255]} {local} {small} {neg}");
println($"{show(3 as i32)} {show(4)} {kind(7 as u8)} {kind(2.5)} {kind(entries)}");
var mine := crc;
mine[0] = 1;
println($"{crc[0]} {mine[0]}");
}