Files
sx/examples/0105-types-flags.sx
agra 4e942b5373 test: migrate examples to XXXX-category-name layout + split expected streams
Rename all example tests/companions to the XXXX-category-test-name scheme
(per-category 100-blocks: basic 0010, types 0100, ... errors 1000,
diagnostics 1100, ffi 1200, ffi-objc 1300, ffi-jni 1400, vectors 1500,
platform 1600). Companions and dir/C fixtures move in lockstep with their
parent test; #import/#source/#include paths rewritten to match.

Expected output now lives in examples/expected/ (a sibling dir of the
tests) split into three streams per the new convention:
  <name>.exit / <name>.stdout / <name>.stderr  (+ optional <name>.ir)

run_examples.sh rewritten: scans examples/ and issues/ for an
expected/<name>.exit marker, captures stdout and stderr separately (no
more 2>&1), compares each stream + exit + optional IR snapshot.

Behavior validated unchanged: every renamed test reproduces its prior
merged output + exit (diffs limited to file paths/basenames embedded in
diagnostics + traces, which correctly reflect the new names). Suite:
292 passed, 0 failed. 50-smoke.sx split + issue relocation + docs follow
in subsequent commits.
2026-06-01 19:05:15 +03:00

70 lines
1.6 KiB
Plaintext

#import "modules/std.sx";
// Auto power-of-2 values: read=1, write=2, execute=4
Perms :: enum flags {
read;
write;
execute;
}
// Explicit values with u32 backing type (e.g. for C interop)
WindowFlags :: enum flags u32 {
vsync :: 64;
resizable :: 4;
hidden :: 128;
}
// Backing type on plain enums too
Color :: enum u8 { red; green; blue; }
check_perms :: (p: Perms) {
print(" checking: {}\n", p);
if p & .read { print(" - can read\n"); }
if p & .write { print(" - can write\n"); }
if p & .execute { print(" - can execute\n"); }
}
main :: () {
// Combine flags with |
p :Perms = .read | .write;
print("perms: {}\n", p);
// Test individual flags with &
check_perms(p);
// All flags
all :Perms = .read | .write | .execute;
print("\nall: {}\n", all);
check_perms(all);
// Single flag
r :Perms = .read;
print("\nread only: {}\n", r);
check_perms(r);
// Pass flags to functions, match on them
print("\nmatch on flags:\n");
f :Perms = .execute;
if f == {
case .read: print(" read\n");
case .write: print(" write\n");
case .execute: print(" execute\n");
}
// Explicit values
w :WindowFlags = .vsync | .resizable;
print("\nwindow: {}\n", w);
print("raw value: {}\n", cast(s64) w);
// Backing type on plain enums
c :Color = .blue;
print("\ncolor: {}\n", c);
print("raw: {}\n", cast(s64) c);
// Bitwise ops work on plain integers too
x := 0xFF & 0x0F;
y := 1 | 2 | 4;
print("\n0xFF & 0x0F = {}\n", x);
print("1 | 2 | 4 = {}\n", y);
}