Files
sx/examples/comptime/0630-comptime-compiler-type-kind.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

47 lines
2.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// Comptime compiler API — type-kind + enum-value reflection readers (Phase 3).
//
// Completes the READ side the metatype needs to re-express `type_info(T)` as sx:
//
// type_kind(t) → a stable kind discriminant, to branch on the shape
// (0 other · 1 struct · 2 enum · 3 tagged_union ·
// 4 tuple · 5 union · 6 array · 7 vector · 8 error_set)
// type_field_value(t, i) → enum variant i's integer value (explicit or ordinal)
//
// Together with find_type / type_field_count / type_field_name / type_field_type
// / type_nominal_name (examples 06280629), a comptime sx function can now fully
// reflect a struct/enum/tuple into data — no `#builtin` needed. All folded at
// `#run`, all serviced natively by the flat-memory VM.
#import "modules/std.sx";
StringId :: u32;
TypeId :: u32;
intern :: (s: string) -> StringId abi(.compiler);
text_of :: (id: StringId) -> string abi(.compiler);
find_type :: (name: StringId) -> TypeId abi(.compiler);
type_kind :: (t: TypeId) -> i64 abi(.compiler);
type_field_name :: (t: TypeId, idx: i64) -> StringId abi(.compiler);
type_field_value :: (t: TypeId, idx: i64) -> i64 abi(.compiler);
Color :: enum { red; green; blue; }
WindowFlags :: enum flags u32 { vsync :: 64; resizable :: 4; hidden :: 128; }
Point :: struct { x: i64; y: i64; }
color_kind :: #run type_kind(find_type(intern("Color"))); // 2 = enum
point_kind :: #run type_kind(find_type(intern("Point"))); // 1 = struct
// Plain enum → ordinal values.
green_val :: #run type_field_value(find_type(intern("Color")), 1); // 1
// Flags enum → explicit values, read by name + value.
vsync_name :: #run text_of(type_field_name(find_type(intern("WindowFlags")), 0)); // "vsync"
vsync_val :: #run type_field_value(find_type(intern("WindowFlags")), 0); // 64
main :: () {
print("Color kind = {}, Point kind = {}\n", color_kind, point_kind);
print("Color.green = {}\n", green_val);
print("WindowFlags.{} = {}\n", vsync_name, vsync_val);
}