kit/*.stag, paired with worked input/output examples.Stag Standard Kit API Reference
The Stag Standard Kit comprises 18 modules embedded directly inside the stagc binary at compile time. Every module below documents its exact exported types, traits, constants, parameter inputs, return outputs, and a worked Stag example with expected output. Every module is verified by a native Stag test suite (kit/tests/<module>_tests.stag) at both -O0 and -O2.
Module Index
| Module | Import Path | Native Test Suite | Summary |
|---|---|---|---|
kit.alloc | use kit.alloc | kit/tests/alloc_tests.stag | portable heap allocation backed by ISO C libc. |
kit.args | use kit.args | kit/tests/args_tests.stag | command-line argument inspection (kit::args). Populated automatically by the Stag startup entry point from C (argc, argv). |
kit.check | use kit.check | kit/tests/check_tests.stag | Test assertions and runtime invariant checks. |
kit.hash | use kit.hash | kit/tests/hash_tests.stag | fast 64-bit hashing (Hasher, Hash) and primitive Eq impls. |
kit.io | use kit.io | kit/tests/io_tests.stag | cross-platform console and RAII file I/O backed by ISO C stdio. |
kit.json | use kit.json | kit/tests/json_tests.stag | RFC 8259 JSON parser, validator, DOM, RFC 6901 JSON Pointer, and compact / pretty serializer. |
kit.list | use kit.list | kit/tests/list_tests.stag | generic heap-allocated growable array (List<T>). Full RAII Drop support for elements, Index/IndexMut, in-place sorting, and for-compatible ListIter<T>. |
kit.math | use kit.math | kit/tests/math_tests.stag | integer and floating-point math utilities backed by native hardware LLVM math intrinsics and pure IEEE-754 operations. |
kit.maybe | use kit.maybe | kit/tests/maybe_tests.stag | optional value container (Maybe<T>). Directly compatible with ? propagation and for iterator protocols. |
kit.mem | use kit.mem | kit/tests/mem_tests.stag | low-level memory manipulation utilities. |
kit.outcome | use kit.outcome | kit/tests/outcome_tests.stag | fallible result container (Outcome<T, E>). Directly compatible with postfix ? error propagation. |
kit.path | use kit.path | kit/tests/path_tests.stag | cross-platform file path manipulation (kit::path). Handles both POSIX / and Windows \ separators cleanly. |
kit.rand | use kit.rand | kit/tests/rand_tests.stag | Deterministic pseudo-random number generation (Xoshiro256** + SplitMix64). All 64-bit mixing operations are overflow-free under checked Stag arithmetic. |
kit.scribe | use kit.scribe | kit/tests/scribe_tests.stag | zero-overhead formatting traits (Scribe, Show) and helpers. |
kit.sys | use kit.sys | kit/tests/sys_tests.stag | portable process and environment utilities backed by ISO C stdlib. |
kit.table | use kit.table | kit/tests/table_tests.stag | generic open-addressing hash table (Table<K: Eq + Hash, V>). Uses linear probing with tombstones, power-of-2 capacities, automatic rehashing, for-compatible TableIter<K, V>, and full RAII Drop safety for both keys and values. |
kit.text | use kit.text | kit/tests/text_tests.stag | owned, growable, NUL-terminated UTF-8 string (Text). Maintains a trailing NUL byte so as_cstr() is O(1) with zero allocation for C FFI. |
kit.time | use kit.time | kit/tests/time_tests.stag | wall-clock timestamps, monotonic high-resolution clocks, duration helpers, stopwatch timer, and sleep utilities. |
Detailed Module Specifications
kit.alloc
Stag Standard Kit: portable heap allocation backed by ISO C libc.
- Import:
use kit.alloc(or selectiveuse kit.alloc (Symbol, ...)) - Source:
kit/alloc.stag(80 lines) · Native Tests:kit/tests/alloc_tests.stag
Free Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
alloc_bytes | size: i64 | *mut u8 |
alloc_zeroed | size: i64 | *mut u8 |
realloc_bytes | ptr: *mut u8, new_size: i64 | *mut u8 |
dealloc_bytes | ptr: *mut u8 | unit |
alloc_array[T] | count: i64 | *mut T |
realloc_array[T] | ptr: *mut T, new_count: i64 | *mut T |
dealloc_array[T] | ptr: *mut T | unit |
Worked Example (kit.alloc)
use kit.alloc
use kit.io (Console)
func main() -> i32:
buf := alloc.alloc_zeroed(4)
*ptr_offset(buf, 0) = 65u8
*ptr_offset(buf, 1) = 66u8
*ptr_offset(buf, 2) = 67u8
s := str_from_raw(buf, 3)
Console.println(s)
alloc.dealloc_bytes(buf)
0i32Expected Output:
ABCkit.args
Stag Standard Kit: command-line argument inspection (kit::args). Populated automatically by the Stag startup entry point from C (argc, argv).
- Import:
use kit.args(or selectiveuse kit.args (Symbol, ...)) - Source:
kit/args.stag(81 lines) · Native Tests:kit/tests/args_tests.stag
Free Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
count | *(none)* | i64 |
get | idx: i64 | Maybe[str] |
program_name | *(none)* | str |
all | *(none)* | List[str] |
has_flag | flag: str | bool |
flag_value | flag: str | Maybe[Text] |
Worked Example (kit.args)
use kit.args
use kit.io (Console)
func main() -> i32:
n := args.count()
prog := args.at(0).unwrap_or("")
msg := f"argc={n} argv[0]={prog}"
Console.println(msg.as_str())
0i32Expected Output:
argc=1 argv[0]=./appkit.check
Stag Standard Kit: Test assertions and runtime invariant checks.
- Import:
use kit.check(or selectiveuse kit.check (Symbol, ...)) - Source:
kit/check.stag(131 lines) · Native Tests:kit/tests/check_tests.stag
Free Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
fail | msg: str | never |
assert | cond: bool, msg: str | unit |
ok | cond: bool | unit |
eq_i32 | actual: i32, expected: i32, label: str | unit |
eq_i64 | actual: i64, expected: i64, label: str | unit |
ne_i64 | actual: i64, unexpected: i64, label: str | unit |
lt_i64 | actual: i64, bound: i64, label: str | unit |
le_i64 | actual: i64, bound: i64, label: str | unit |
gt_i64 | actual: i64, bound: i64, label: str | unit |
ge_i64 | actual: i64, bound: i64, label: str | unit |
eq_u64 | actual: u64, expected: u64, label: str | unit |
eq_bool | actual: bool, expected: bool, label: str | unit |
eq_str | actual: str, expected: str, label: str | unit |
ne_str | actual: str, unexpected: str, label: str | unit |
contains_str | haystack: str, needle: str, label: str | unit |
eq_text | actual: *Text, expected: str, label: str | unit |
near_f64 | actual: f64, expected: f64, eps: f64, label: str | unit |
pub func eq[T: Eq](actual: *T, expected: *T, label: str) | — | unit |
pub func ne[T: Eq](actual: *T, unexpected: *T, label: str) | — | unit |
is_some[T] | m: *Maybe[T], label: str | unit |
is_none[T] | m: *Maybe[T], label: str | unit |
pub func is_ok[T, E](o: *Outcome[T, E], label: str) | — | unit |
pub func is_err[T, E](o: *Outcome[T, E], label: str) | — | unit |
Worked Example (kit.check)
use kit.check
test "arithmetic invariants":
check.eq_i64(6 * 7, 42, "product")
check.gt_i64(42, 10, "lower bound")
check.near_f64(0.1 + 0.2, 0.3, 1e-12, "ieee754 tolerance")
check.contains_str("stag-0.1.0", "0.1", "version substring")Expected Output:
[PASS] arithmetic invariants
Summary: 1 passed, 0 failed (total 1)kit.hash
Stag Standard Kit: fast 64-bit hashing (Hasher, Hash) and primitive Eq impls.
- Import:
use kit.hash(or selectiveuse kit.hash (Symbol, ...)) - Source:
kit/hash.stag(139 lines) · Native Tests:kit/tests/hash_tests.stag
Types, Traits & Constants
pub record Hasher
pub trait HashFree Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
pub func hash_of[T: Hash](v: *T) -> u64 | — | unit |
Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
Hasher | new | *(none)* | Hasher |
Hasher | write_u8 | mut self, b: u8 | unit |
Hasher | write_u64 | mut self, v: u64 | unit |
Hasher | write_i64 | mut self, v: i64 | unit |
Hasher | write_bytes | mut self, ptr: *u8, len: i64 | unit |
Hasher | write_str | mut self, s: str | unit |
Hasher | finish | in self | u64 |
i8 : Eq | eq | in self, o: *i8 | bool |
i16 : Eq | eq | in self, o: *i16 | bool |
i32 : Eq | eq | in self, o: *i32 | bool |
i64 : Eq | eq | in self, o: *i64 | bool |
u8 : Eq | eq | in self, o: *u8 | bool |
u16 : Eq | eq | in self, o: *u16 | bool |
u32 : Eq | eq | in self, o: *u32 | bool |
u64 : Eq | eq | in self, o: *u64 | bool |
bool : Eq | eq | in self, o: *bool | bool |
str : Eq | eq | in self, o: *str | bool |
i8 : Hash | hash | in self, h: *mut Hasher | unit |
i16 : Hash | hash | in self, h: *mut Hasher | unit |
i32 : Hash | hash | in self, h: *mut Hasher | unit |
i64 : Hash | hash | in self, h: *mut Hasher | unit |
u8 : Hash | hash | in self, h: *mut Hasher | unit |
u16 : Hash | hash | in self, h: *mut Hasher | unit |
u32 : Hash | hash | in self, h: *mut Hasher | unit |
u64 : Hash | hash | in self, h: *mut Hasher | unit |
bool : Hash | hash | in self, h: *mut Hasher | unit |
str : Hash | hash | in self, h: *mut Hasher | unit |
Worked Example (kit.hash)
use kit.hash (Hash, Hasher, hash_of)
use kit.io (Console)
record Key:
id: i64
tag: u8
attach Key : Hash:
func hash(in self, h: *mut Hasher):
h.write_i64(self.id)
h.write_u8(self.tag)
func main() -> i32:
k1 := Key { id: 7, tag: 1u8 }
k2 := Key { id: 7, tag: 1u8 }
same := hash_of(&k1) == hash_of(&k2)
msg := f"equal_hashes={same}"
Console.println(msg.as_str())
0i32Expected Output:
equal_hashes=truekit.io
Stag Standard Kit: cross-platform console and RAII file I/O backed by ISO C stdio.
- Import:
use kit.io(or selectiveuse kit.io (Symbol, ...)) - Source:
kit/io.stag(191 lines) · Native Tests:kit/tests/io_tests.stag
Types, Traits & Constants
pub record Console
pub record FileFree Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
read_file | path: str | Outcome[Text, i32] |
read_lines | path: str | Outcome[List[Text], i32] |
write_file | path: str, content: str | Outcome[unit, i32] |
append_file | path: str, content: str | Outcome[unit, i32] |
remove_file | path: str | bool |
file_exists | path: str | bool |
Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
Console | out | *(none)* | Console |
Console | print | s: str | unit |
Console | println | s: str | unit |
Console | print_i64 | n: i64 | unit |
Console | println_i64 | n: i64 | unit |
Console | flush | *(none)* | unit |
Console | read_line | *(none)* | Outcome[Text, i32] |
Console : Scribe | write_str | mut self, s: str | unit |
Console : Scribe | write_byte | mut self, b: u8 | unit |
File | open_read | path: str | Outcome[File, i32] |
File | open_write | path: str | Outcome[File, i32] |
File | open_append | path: str | Outcome[File, i32] |
File | is_open | in self | bool |
File | write_bytes | mut self, data: []u8 | Outcome[i64, i32] |
File | write_str | mut self, s: str | Outcome[i64, i32] |
File | read_all | mut self | Outcome[Text, i32] |
File | flush | mut self | Outcome[unit, i32] |
File | close | mut self | unit |
File : Scribe | write_str | mut self, s: str | unit |
File : Scribe | write_byte | mut self, b: u8 | unit |
File : Drop | drop | mut self | unit |
Worked Example (kit.io)
use kit.io
use kit.io (Console)
func main() -> i32:
p := "demo_io.txt"
_ := io.write_file(p, "alpha\nbeta\n")
content := io.read_file(p).unwrap()
Console.print(content.as_str())
_ := io.remove_file(p)
0i32Expected Output:
alpha
betakit.json
Stag Standard Kit: RFC 8259 JSON parser, validator, DOM, RFC 6901 JSON Pointer, and compact / pretty serializer.
- Import:
use kit.json(or selectiveuse kit.json (Symbol, ...)) - Source:
kit/json.stag(1283 lines) · Native Tests:kit/tests/json_tests.stag
Types, Traits & Constants
pub const DEFAULT_MAX_DEPTH: i64 = 128
pub choice JsonFree Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
parse_with_max_depth | input: str, max_depth: i64 | Outcome[Json, Text] |
parse | input: str | Outcome[Json, Text] |
validate_with_max_depth | input: str, max_depth: i64 | Outcome[unit, Text] |
validate | input: str | Outcome[unit, Text] |
Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
Json | null_val | *(none)* | Json |
Json | bool_val | v: bool | Json |
Json | int_val | v: i64 | Json |
Json | float_val | v: f64 | Json |
Json | str_val | s: str | Json |
Json | text_val | t: Text | Json |
Json | array | *(none)* | Json |
Json | array_with_cap | cap: i64 | Json |
Json | object | *(none)* | Json |
Json | object_with_cap | cap: i64 | Json |
Json | is_null | in self | bool |
Json | is_bool | in self | bool |
Json | is_int | in self | bool |
Json | is_float | in self | bool |
Json | is_number | in self | bool |
Json | is_str | in self | bool |
Json | is_array | in self | bool |
Json | is_object | in self | bool |
Json | as_bool | in self | Maybe[bool] |
Json | as_int | in self | Maybe[i64] |
Json | as_i64 | in self | Maybe[i64] |
Json | as_f64 | in self | Maybe[f64] |
Json | as_num | in self | Maybe[f64] |
Json | as_str | in self | Maybe[str] |
Json | as_arr | in self | Maybe[*List[Json]] |
Json | as_arr_mut | mut self | Maybe[*mut List[Json]] |
Json | as_obj | in self | Maybe[*Table[Text, Json]] |
Json | as_obj_mut | mut self | Maybe[*mut Table[Text, Json]] |
Json | get_index | in self, idx: i64 | Maybe[*Json] |
Json | get_index_mut | mut self, idx: i64 | Maybe[*mut Json] |
Json | get_key | in self, key: str | Maybe[*Json] |
Json | get_key_mut | mut self, key: str | Maybe[*mut Json] |
Json | has_key | in self, key: str | bool |
Json | len | in self | i64 |
Json | is_empty | in self | bool |
Json | push | mut self, val: Json | bool |
Json | set | mut self, key: str, val: Json | bool |
Json | remove_key | mut self, key: str | Maybe[Json] |
Json | pointer | in self, path: str | Maybe[*Json] |
Json | pointer_mut | mut self, path: str | Maybe[*mut Json] |
Json | clone | in self | Json |
Json | equals | in self, other: *Json | bool |
Json | write_to | in self, out: *mut Text | unit |
Json | write_pretty_to | in self, out: *mut Text, indent: i64 | unit |
Json | stringify | in self | Text |
Json | stringify_with_cap | in self, cap: i64 | Text |
Json | stringify_pretty | in self, indent: i64 | Text |
Json | stringify_pretty_with_cap | in self, indent: i64, cap: i64 | Text |
Json : Eq | eq | in self, other: *Json | bool |
Worked Example (kit.json)
use kit.io (Console)
use kit.json
func main() -> i32:
doc := json.parse("{\"lang\":\"stag\",\"ver\":[0,1,0]}").unwrap()
lang := doc.get("lang").unwrap().as_str().unwrap()
minor := doc.get("ver").unwrap().at(1).unwrap().as_i64().unwrap()
out := json.stringify(&doc)
msg := f"lang={lang} minor={minor} raw={out}"
Console.println(msg.as_str())
0i32Expected Output:
lang=stag minor=1 raw={"lang":"stag","ver":[0,1,0]}kit.list
Stag Standard Kit: generic heap-allocated growable array (List<T>). Full RAII Drop support for elements, Index/IndexMut, in-place sorting, and for-compatible ListIter<T>.
- Import:
use kit.list(or selectiveuse kit.list (Symbol, ...)) - Source:
kit/list.stag(206 lines) · Native Tests:kit/tests/list_tests.stag
Types, Traits & Constants
pub record ListIter[T]
pub record List[T]Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
attach[T] ListIter[T] | next | mut self | Maybe[*T] |
attach[T] List[T] | new | *(none)* | List[T] |
attach[T] List[T] | with_cap | cap: i64 | List[T] |
attach[T] List[T] | len | in self | i64 |
attach[T] List[T] | cap | in self | i64 |
attach[T] List[T] | is_empty | in self | bool |
attach[T] List[T] | reserve | mut self, additional: i64 | unit |
attach[T] List[T] | push | mut self, value: T | unit |
attach[T] List[T] | pop | mut self | Maybe[T] |
attach[T] List[T] | get | in self, idx: i64 | Maybe[*T] |
attach[T] List[T] | get_mut | mut self, idx: i64 | Maybe[*mut T] |
attach[T] List[T] | first | in self | Maybe[*T] |
attach[T] List[T] | last | in self | Maybe[*T] |
attach[T] List[T] | insert | mut self, idx: i64, value: T | unit |
attach[T] List[T] | remove | mut self, idx: i64 | T |
attach[T] List[T] | swap_remove | mut self, idx: i64 | T |
attach[T] List[T] | reverse | mut self | unit |
attach[T] List[T] | sort_by | mut self, cmp: func(*T, *T) -> i32 | unit |
attach[T] List[T] | find_index | in self, pred: func(*T) -> bool | Maybe[i64] |
attach[T] List[T] | contains_by | in self, pred: func(*T) -> bool | bool |
attach[T] List[T] | clear | mut self | unit |
attach[T] List[T] | as_slice | in self | []T |
attach[T] List[T] | as_mut_slice | mut self | []mut T |
attach[T] List[T] | iter | in self | ListIter[T] |
attach[T] List[T] : Index | index | in self, i: i64 | *Self.Output |
attach[T] List[T] : IndexMut | index_mut | mut self, i: i64 | *mut Self.Output |
attach[T] List[T] : Drop | drop | mut self | unit |
Worked Example (kit.list)
use kit.io (Console)
use kit.list (List)
func cmp_i64(a: *i64, b: *i64) -> i32:
if *a < *b then -1i32 elif *a > *b then 1i32 else 0i32
func main() -> i32:
var xs = List[i64].new()
xs.push(30)
xs.push(10)
xs.push(20)
xs.sort_by(cmp_i64)
msg := f"len={xs.len()} sorted=[{*xs.get(0).unwrap()}, {*xs.get(1).unwrap()}, {*xs.get(2).unwrap()}]"
Console.println(msg.as_str())
0i32Expected Output:
len=3 sorted=[10, 20, 30]kit.math
Stag Standard Kit: integer and floating-point math utilities backed by native hardware LLVM math intrinsics and pure IEEE-754 operations.
- Import:
use kit.math(or selectiveuse kit.math (Symbol, ...)) - Source:
kit/math.stag(133 lines) · Native Tests:kit/tests/math_tests.stag
Types, Traits & Constants
pub const PI: f64 = 3.1415926535897931
pub const TAU: f64 = 6.2831853071795862
pub const E: f64 = 2.7182818284590451
pub const SQRT2: f64 = 1.4142135623730951
pub const func abs_i64(x: i64) -> i64 = if x < 0 then -x else x
pub const func min_i64(a: i64, b: i64) -> i64 = if a < b then a else b
pub const func max_i64(a: i64, b: i64) -> i64 = if a > b then a else b
pub const func clamp_i64(x: i64, lo: i64, hi: i64) -> i64:
pub const func gcd_i64(a: i64, b: i64) -> i64:
pub const func lcm_i64(a: i64, b: i64) -> i64:
pub const func pow_i64(base: i64, exp_val: i64) -> i64:
pub const func min_f64(a: f64, b: f64) -> f64 = if a < b then a else b
pub const func max_f64(a: f64, b: f64) -> f64 = if a > b then a else b
pub const func clamp_f64(x: f64, lo: f64, hi: f64) -> f64:
pub const func lerp_f64(a: f64, b: f64, t: f64) -> f64 = a + (b - a) * tFree Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
sqrt | x: f64 | f64 |
sqrt_f64 | x: f64 | f64 |
sqrt_f32 | x: f32 | f32 |
fma_f64 | a: f64, b: f64, c: f64 | f64 |
fma_f32 | a: f32, b: f32, c: f32 | f32 |
sin_f64 | x: f64 | f64 |
cos_f64 | x: f64 | f64 |
tan_f64 | x: f64 | f64 |
asin_f64 | x: f64 | f64 |
acos_f64 | x: f64 | f64 |
atan_f64 | x: f64 | f64 |
atan2_f64 | y: f64, x: f64 | f64 |
exp_f64 | x: f64 | f64 |
ln_f64 | x: f64 | f64 |
log10_f64 | x: f64 | f64 |
pow_f64 | base: f64, exponent: f64 | f64 |
floor_f64 | x: f64 | f64 |
ceil_f64 | x: f64 | f64 |
round_f64 | x: f64 | f64 |
trunc_f64 | x: f64 | f64 |
abs_f64 | x: f64 | f64 |
abs_f32 | x: f32 | f32 |
fmod_f64 | x: f64, y: f64 | f64 |
hypot_f64 | x: f64, y: f64 | f64 |
Worked Example (kit.math)
use kit.io (Console)
use kit.math
func main() -> i32:
h := math.hypot_f64(3.0, 4.0)
g := math.gcd_i64(84, 30)
c := math.clamp_i64(150, 0, 100)
msg := f"hypot={h} gcd={g} clamp={c}"
Console.println(msg.as_str())
0i32Expected Output:
hypot=5 gcd=6 clamp=100kit.maybe
Stag Standard Kit: optional value container (Maybe<T>). Directly compatible with ? propagation and for iterator protocols.
- Import:
use kit.maybe(or selectiveuse kit.maybe (Symbol, ...)) - Source:
kit/maybe.stag(56 lines) · Native Tests:kit/tests/maybe_tests.stag
Types, Traits & Constants
pub choice Maybe[T]Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
attach[T] Maybe[T] | unwrap | self | T |
attach[T] Maybe[T] | unwrap_or | self, fallback: T | T |
attach[T] Maybe[T] | map[U] | self, f: func(T) -> U | Maybe[U] |
attach[T] Maybe[T] | and_then[U] | self, f: func(T) -> Maybe[U] | Maybe[U] |
attach[T] Maybe[T] | take | mut self | Maybe[T] |
Worked Example (kit.maybe)
use kit.io (Console)
use kit.maybe (Maybe)
func double_i64(x: i64) -> i64 = x * 2
func main() -> i32:
a := Maybe[i64].Some(21).map(double_i64).unwrap_or(0)
b: Maybe[i64] := Maybe.None
msg := f"a={a} b={b.unwrap_or(-1)}"
Console.println(msg.as_str())
0i32Expected Output:
a=42 b=-1kit.mem
Stag Standard Kit: low-level memory manipulation utilities.
- Import:
use kit.mem(or selectiveuse kit.mem (Symbol, ...)) - Source:
kit/mem.stag(47 lines) · Native Tests:kit/tests/mem_tests.stag
Types, Traits & Constants
pub const func size_of[T]() -> i64 = sizeof(T)
pub const func align_of[T]() -> i64 = alignof(T)Free Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
swap[T] | a: *mut T, b: *mut T | unit |
replace[T] | dst: *mut T, value: T | T |
zero_bytes | dst: *mut u8, len: i64 | unit |
bytes_eq | a: *u8, b: *u8, len: i64 | bool |
bytes_cmp | a: *u8, b: *u8, len: i64 | i32 |
Worked Example (kit.mem)
use kit.io (Console)
use kit.mem
func main() -> i32:
var a: i64 = 10
var b: i64 = 99
mem.swap(&mut a, &mut b)
prev := mem.replace(&mut a, 50)
msg := f"a={a} b={b} prev={prev}"
Console.println(msg.as_str())
0i32Expected Output:
a=50 b=10 prev=99kit.outcome
Stag Standard Kit: fallible result container (Outcome<T, E>). Directly compatible with postfix ? error propagation.
- Import:
use kit.outcome(or selectiveuse kit.outcome (Symbol, ...)) - Source:
kit/outcome.stag(68 lines) · Native Tests:kit/tests/outcome_tests.stag
Types, Traits & Constants
pub choice Outcome[T, E]Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
attach[T, E] Outcome[T, E] | unwrap | self | T |
attach[T, E] Outcome[T, E] | unwrap_or | self, fallback: T | T |
attach[T, E] Outcome[T, E] | unwrap_err | self | E |
attach[T, E] Outcome[T, E] | map[U] | self, f: func(T) -> U | Outcome[U, E] |
attach[T, E] Outcome[T, E] | map_err[F] | self, f: func(E) -> F | Outcome[T, F] |
attach[T, E] Outcome[T, E] | ok | self | Maybe[T] |
attach[T, E] Outcome[T, E] | err | self | Maybe[E] |
Worked Example (kit.outcome)
use kit.io (Console)
use kit.outcome (Outcome)
func safe_div(a: i64, b: i64) -> Outcome[i64, str]:
if b == 0:
return Outcome.Err("divide by zero")
Outcome.Ok(a / b)
func main() -> i32:
ok_v := safe_div(84, 2).unwrap_or(0)
err_flag := safe_div(10, 0).is_err()
msg := f"ok={ok_v} err={err_flag}"
Console.println(msg.as_str())
0i32Expected Output:
ok=42 err=truekit.path
Stag Standard Kit: cross-platform file path manipulation (kit::path). Handles both POSIX / and Windows \ separators cleanly.
- Import:
use kit.path(or selectiveuse kit.path (Symbol, ...)) - Source:
kit/path.stag(105 lines) · Native Tests:kit/tests/path_tests.stag
Free Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
join | a: str, b: str | Text |
file_name | p: str | Text |
parent | p: str | Text |
extension | p: str | Text |
stem | p: str | Text |
has_extension | p: str, ext: str | bool |
normalize_slashes | p: str | Text |
Worked Example (kit.path)
use kit.io (Console)
use kit.path
func main() -> i32:
joined := path.join("src/compiler", "sema.stag")
st := path.stem(joined.as_str())
ex := path.extension(joined.as_str())
msg := f"joined={joined} stem={st} ext={ex}"
Console.println(msg.as_str())
0i32Expected Output:
joined=src/compiler/sema.stag stem=sema ext=stagkit.rand
Stag Standard Kit: Deterministic pseudo-random number generation (Xoshiro256** + SplitMix64). All 64-bit mixing operations are overflow-free under checked Stag arithmetic.
- Import:
use kit.rand(or selectiveuse kit.rand (Symbol, ...)) - Source:
kit/rand.stag(128 lines) · Native Tests:kit/tests/rand_tests.stag
Types, Traits & Constants
pub record RngAttached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
Rng | from_seed | seed: u64 | Rng |
Rng | from_time | *(none)* | Rng |
Rng | next_u64 | mut self | u64 |
Rng | next_i64 | mut self | i64 |
Rng | range_u64 | mut self, bound: u64 | u64 |
Rng | range_i64 | mut self, lo: i64, hi: i64 | i64 |
Rng | next_f64 | mut self | f64 |
Rng | chance | mut self, probability: f64 | bool |
Rng | shuffle[T] | mut self, xs: *mut List[T] | unit |
Worked Example (kit.rand)
use kit.io (Console)
use kit.rand (Rng)
func main() -> i32:
var rng = Rng.from_seed(42u64)
roll := rng.range_i64(1, 7)
unit := rng.next_f64()
msg := f"roll={roll} in_unit={unit >= 0.0 && unit < 1.0}"
Console.println(msg.as_str())
0i32Expected Output:
roll=4 in_unit=truekit.scribe
Stag Standard Kit: zero-overhead formatting traits (Scribe, Show) and helpers.
- Import:
use kit.scribe(or selectiveuse kit.scribe (Symbol, ...)) - Source:
kit/scribe.stag(160 lines) · Native Tests:kit/tests/scribe_tests.stag
Types, Traits & Constants
pub trait Scribe
pub trait ShowFree Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
pub func to_text[T: Show](val: *T) -> Text | — | unit |
format_i64 | val: i64 | Text |
format_f64 | val: f64 | Text |
format_hex | val: u64 | Text |
Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
Text : Scribe | write_str | mut self, s: str | unit |
Text : Scribe | write_byte | mut self, b: u8 | unit |
i8 : Show | show | in self, out: *mut Text | unit |
i16 : Show | show | in self, out: *mut Text | unit |
i32 : Show | show | in self, out: *mut Text | unit |
i64 : Show | show | in self, out: *mut Text | unit |
u8 : Show | show | in self, out: *mut Text | unit |
u16 : Show | show | in self, out: *mut Text | unit |
u32 : Show | show | in self, out: *mut Text | unit |
u64 : Show | show | in self, out: *mut Text | unit |
f32 : Show | show | in self, out: *mut Text | unit |
f64 : Show | show | in self, out: *mut Text | unit |
bool : Show | show | in self, out: *mut Text | unit |
str : Show | show | in self, out: *mut Text | unit |
Text : Show | show | in self, out: *mut Text | unit |
Worked Example (kit.scribe)
use kit.io (Console)
use kit.scribe (Scribe, Show, format_hex, to_text)
use kit.text (Text)
func main() -> i32:
var buf = Text.new()
buf.write_str("mask=")
buf.write_hex(255u64)
buf.write_str(" ok=")
buf.write_bool(true)
n := 2026
msg := f"{buf} year={to_text(&n)} hex={format_hex(16u64)}"
Console.println(msg.as_str())
0i32Expected Output:
mask=0xff ok=true year=2026 hex=0x10kit.sys
Stag Standard Kit: portable process and environment utilities backed by ISO C stdlib.
- Import:
use kit.sys(or selectiveuse kit.sys (Symbol, ...)) - Source:
kit/sys.stag(37 lines) · Native Tests:kit/tests/sys_tests.stag
Free Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
exit_process | code: i32 | never |
abort_process | *(none)* | never |
env_get | name: str | Maybe[Text] |
env_has | name: str | bool |
Worked Example (kit.sys)
use kit.io (Console)
use kit.sys
func main() -> i32:
has_path := sys.env_has("PATH")
missing := sys.env_get("STAG_UNSET_ENV").is_none()
msg := f"has_path={has_path} missing={missing}"
Console.println(msg.as_str())
0i32Expected Output:
has_path=true missing=truekit.table
Stag Standard Kit: generic open-addressing hash table (Table<K: Eq + Hash, V>). Uses linear probing with tombstones, power-of-2 capacities, automatic rehashing, for-compatible TableIter<K, V>, and full RAII Drop safety for both keys and values.
- Import:
use kit.table(or selectiveuse kit.table (Symbol, ...)) - Source:
kit/table.stag(252 lines) · Native Tests:kit/tests/table_tests.stag
Types, Traits & Constants
pub record TableEntry[K, V]
pub record TableIter[K, V]
pub record Table[K: Eq + Hash, V]Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
attach[K, V] TableIter[K, V] | next | mut self | Maybe[TableEntry[K, V]] |
attach[K: Eq + Hash, V] Table[K, V] | new | *(none)* | Table[K, V] |
attach[K: Eq + Hash, V] Table[K, V] | with_cap | min_cap: i64 | Table[K, V] |
attach[K: Eq + Hash, V] Table[K, V] | len | in self | i64 |
attach[K: Eq + Hash, V] Table[K, V] | cap | in self | i64 |
attach[K: Eq + Hash, V] Table[K, V] | is_empty | in self | bool |
attach[K: Eq + Hash, V] Table[K, V] | reserve | mut self, additional: i64 | unit |
attach[K: Eq + Hash, V] Table[K, V] | set | mut self, key: K, value: V | Maybe[V] |
attach[K: Eq + Hash, V] Table[K, V] | find_by_hash[Q] | in self, h: u64, query: *Q, eq_fn: func(*K, *Q) -> bool | i64 |
attach[K: Eq + Hash, V] Table[K, V] | get_by_hash[Q] | in self, h: u64, query: *Q, eq_fn: func(*K, *Q) -> bool | Maybe[*V] |
attach[K: Eq + Hash, V] Table[K, V] | get_mut_by_hash[Q] | mut self, h: u64, query: *Q, eq_fn: func(*K, *Q) -> bool | Maybe[*mut V] |
attach[K: Eq + Hash, V] Table[K, V] | del_by_hash[Q] | mut self, h: u64, query: *Q, eq_fn: func(*K, *Q) -> bool | Maybe[V] |
attach[K: Eq + Hash, V] Table[K, V] | get | in self, key: *K | Maybe[*V] |
attach[K: Eq + Hash, V] Table[K, V] | get_mut | mut self, key: *K | Maybe[*mut V] |
attach[K: Eq + Hash, V] Table[K, V] | has | in self, key: *K | bool |
attach[K: Eq + Hash, V] Table[K, V] | del | mut self, key: *K | Maybe[V] |
attach[K: Eq + Hash, V] Table[K, V] | clear | mut self | unit |
attach[K: Eq + Hash, V] Table[K, V] | iter | in self | TableIter[K, V] |
attach[K: Eq + Hash, V] Table[K, V] : Drop | drop | mut self | unit |
Worked Example (kit.table)
use kit.io (Console)
use kit.table (Table)
use kit.text (Text)
func main() -> i32:
var scores = Table[Text, i64].new()
_ := scores.set(Text.from_str("lexer"), 10)
_ := scores.set(Text.from_str("sema"), 32)
k := Text.from_str("sema")
msg := f"len={scores.len()} sema={*scores.get(&k).unwrap()}"
Console.println(msg.as_str())
0i32Expected Output:
len=2 sema=32kit.text
Stag Standard Kit: owned, growable, NUL-terminated UTF-8 string (Text). Maintains a trailing NUL byte so as_cstr() is O(1) with zero allocation for C FFI.
- Import:
use kit.text(or selectiveuse kit.text (Symbol, ...)) - Source:
kit/text.stag(361 lines) · Native Tests:kit/tests/text_tests.stag
Types, Traits & Constants
pub record TextAttached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
Text | new | *(none)* | Text |
Text | with_cap | cap: i64 | Text |
Text | from_str | s: str | Text |
Text | from_cstr | cstr: *u8 | Text |
Text | clone | in self | Text |
Text | len | in self | i64 |
Text | cap | in self | i64 |
Text | is_empty | in self | bool |
Text | reserve | mut self, additional: i64 | unit |
Text | push_byte | mut self, b: u8 | unit |
Text | push_raw | mut self, ptr: *u8, len: i64 | unit |
Text | push_repeat | mut self, b: u8, count: i64 | unit |
Text | push_str | mut self, s: str | unit |
Text | push_u64 | mut self, val: u64 | unit |
Text | push_i64 | mut self, val: i64 | unit |
Text | push_f64 | mut self, val: f64 | unit |
Text | clear | mut self | unit |
Text | as_str | in self | str |
Text | as_cstr | in self | *u8 |
Text | as_bytes | in self | []u8 |
Text | byte_at | in self, idx: i64 | Maybe[u8] |
Text | substr | in self, start: i64, end: i64 | Text |
Text | eq_str | in self, s: str | bool |
Text | starts_with | in self, prefix: str | bool |
Text | ends_with | in self, suffix: str | bool |
Text | find | in self, needle: str | Maybe[i64] |
Text | contains | in self, needle: str | bool |
Text | trim_start | in self | Text |
Text | trim_end | in self | Text |
Text | trim | in self | Text |
Text | to_upper | in self | Text |
Text | to_lower | in self | Text |
Text | split | in self, delim: u8 | List[Text] |
Text | replace_str | in self, from: str, to: str | Text |
Text | parse_u64 | in self | Outcome[u64, i32] |
Text | parse_i64 | in self | Outcome[i64, i32] |
Text : Drop | drop | mut self | unit |
Text : Eq | eq | in self, other: *Text | bool |
Text : Ord | cmp | in self, other: *Text | i32 |
Text : Add | add | in self, rhs: Text | Self.Output |
Text : Hash | hash | in self, h: *mut Hasher | unit |
Worked Example (kit.text)
use kit.io (Console)
use kit.text (Text)
func main() -> i32:
raw := Text.from_str(" stag,llvm,kit ").trim()
parts := raw.split(44u8)
n := Text.from_str("-42").parse_i64().unwrap()
msg := f"parts={parts.len()} first={parts.get(0).unwrap().to_upper()} parsed={n}"
Console.println(msg.as_str())
0i32Expected Output:
parts=3 first=STAG parsed=-42kit.time
Stag Standard Kit: wall-clock timestamps, monotonic high-resolution clocks, duration helpers, stopwatch timer, and sleep utilities.
- Import:
use kit.time(or selectiveuse kit.time (Symbol, ...)) - Source:
kit/time.stag(202 lines) · Native Tests:kit/tests/time_tests.stag
Types, Traits & Constants
pub const NANOS_PER_MICRO: i64 = 1000
pub const NANOS_PER_MILLI: i64 = 1000000
pub const NANOS_PER_SECOND: i64 = 1000000000
pub const MICROS_PER_MILLI: i64 = 1000
pub const MICROS_PER_SECOND: i64 = 1000000
pub const MILLIS_PER_SECOND: i64 = 1000
pub record Duration
pub record TimerFree Functions (Inputs & Outputs)
| Function | Input Parameters | Return Type |
|---|---|---|
unix_seconds | *(none)* | i64 |
unix_millis | *(none)* | i64 |
unix_micros | *(none)* | i64 |
unix_nanos | *(none)* | i64 |
now_ms | *(none)* | i64 |
now_us | *(none)* | i64 |
now_ns | *(none)* | i64 |
diff_seconds | end_ts: i64, start_ts: i64 | f64 |
monotonic_ns | *(none)* | i64 |
monotonic_us | *(none)* | i64 |
monotonic_ms | *(none)* | i64 |
monotonic_seconds | *(none)* | f64 |
sleep_ns | ns: i64 | unit |
sleep_us | us: i64 | unit |
sleep_ms | ms: i64 | unit |
sleep_seconds | sec: i64 | unit |
Attached Methods & Trait Implementations (Inputs & Outputs)
| Receiver / Trait Block | Method | Input Parameters | Return Type |
|---|---|---|---|
Duration | zero | *(none)* | Duration |
Duration | from_nanos | ns: i64 | Duration |
Duration | from_micros | us: i64 | Duration |
Duration | from_millis | ms: i64 | Duration |
Duration | from_seconds | sec: i64 | Duration |
Duration | as_nanos | in self | i64 |
Duration | as_micros | in self | i64 |
Duration | as_millis | in self | i64 |
Duration | as_millis_f64 | in self | f64 |
Duration | as_seconds | in self | f64 |
Duration | is_zero | in self | bool |
Duration | add | in self, other: Duration | Duration |
Duration | sub | in self, other: Duration | Duration |
Timer | start | *(none)* | Timer |
Timer | elapsed_ns | in self | i64 |
Timer | elapsed_us | in self | i64 |
Timer | elapsed_ms | in self | i64 |
Timer | elapsed_ms_f64 | in self | f64 |
Timer | elapsed_seconds | in self | f64 |
Timer | elapsed | in self | Duration |
Timer | lap_ns | mut self | i64 |
Timer | lap_ms | mut self | i64 |
Timer | lap | mut self | Duration |
Timer | reset | mut self | unit |
Worked Example (kit.time)
use kit.io (Console)
use kit.time
func main() -> i32:
t := time.Timer.start()
time.sleep_ms(5)
elapsed := t.elapsed_ms()
epoch := time.unix_seconds()
msg := f"elapsed_ge_4ms={elapsed >= 4} epoch_valid={epoch > 1700000000}"
Console.println(msg.as_str())
0i32Expected Output:
elapsed_ge_4ms=true epoch_valid=true