Files
sx/examples/comptime/0608-comptime-comptime.sx
agra 66bdc70bf1 test: group examples into per-category folders
Move examples/*.sx and their expected/ snapshots into per-category
subfolders (examples/<category>/...). Folder = leading filename token,
with ffi-objc/ffi-jni kept whole; filenames are unchanged. The corpus
runner and LSP sweep now discover each category's expected/ dir, while
issues/ stays flat. Example 1058's repo-root-relative companion import
is made file-relative. Path strings embedded in 164 snapshots were
regenerated (path-only changes). Test-layout docs in CLAUDE.md updated.
2026-06-21 14:41:34 +03:00

84 lines
1.8 KiB
Plaintext

#import "modules/std.sx";
#import "modules/math";
#import "modules/build.sx";
#import "modules/std/test.sx";
pkg :: #import "tests/fixtures/testpkg";
add :: (a: i32, b: i32) -> i32 { a + b }
mul :: (a: i32, b: i32) -> i32 { a * b }
// #run compile-time constants
CT_VAL :: #run add(10, 15);
CT_MUL :: #run mul(6, 7);
CT_CHAIN :: #run add(CT_VAL, 5);
// #run compile-time optional tests
// #run compile-time optional tests
ct_opt_coalesce :: () -> i32 {
x: ?i32 = 42;
y: ?i32 = null;
return (x ?? 0) + (y ?? 99);
}
ct_opt_unwrap :: () -> i32 {
x: ?i32 = 77;
return x!;
}
ct_opt_guard :: () -> i32 {
x: ?i32 = 10;
if x == null { return -1; }
return x;
}
CT_OPT_COALESCE :: #run ct_opt_coalesce();
CT_OPT_UNWRAP :: #run ct_opt_unwrap();
CT_OPT_GUARD :: #run ct_opt_guard();
// #insert helpers
// #insert helpers
gen_code :: () -> string {
return "print(\"insert-ok\\n\");";
}
gen_val :: () -> string {
return "print(\"insert-gen: {}\\n\", 42);";
}
// --- Error handling (failable functions: sets, raise/try/catch/or/onfail) ---
main :: () {
// ========================================================
// 8. COMPILE-TIME
// ========================================================
print("=== 8. Comptime ===\n");
// #run constant
print("run-const: {}\n", CT_VAL);
// #run with expression
print("run-expr: {}\n", CT_MUL);
// #run chained dependency
print("run-chain: {}\n", CT_CHAIN);
// #run comptime optionals
print("ct-opt-coalesce: {}\n", CT_OPT_COALESCE); // ct-opt-coalesce: 141
print("ct-opt-unwrap: {}\n", CT_OPT_UNWRAP); // ct-opt-unwrap: 77
print("ct-opt-guard: {}\n", CT_OPT_GUARD); // ct-opt-guard: 10
// #insert with function
#insert gen_code();
// #insert additional
#insert gen_val();
}