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.
45 lines
1.9 KiB
Plaintext
45 lines
1.9 KiB
Plaintext
// Comptime compiler API — field-level reflection readers (Phase 3).
|
|
//
|
|
// Builds on the `find_type` / `type_field_count` readers (example 0628) with the
|
|
// per-member readers, all on the same plain-`u32`-handle shape (scalar in,
|
|
// handle out — no marshaling):
|
|
//
|
|
// type_field_name(t, i) → the i-th member's name handle (StringId)
|
|
// type_field_type(t, i) → the i-th member's type handle (TypeId)
|
|
// type_nominal_name(t) → a named type's own name handle (StringId)
|
|
//
|
|
// Reflecting `Pair { lo: Point; hi: Point; }`: read each field's name, and the
|
|
// nominal name of each field's type. Chains
|
|
// intern → find_type → type_field_{name,type} → (type_nominal_name) → text_of,
|
|
// all folded at comptime, 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_field_count :: (t: TypeId) -> i64 abi(.compiler);
|
|
type_nominal_name :: (t: TypeId) -> StringId abi(.compiler);
|
|
type_field_name :: (t: TypeId, idx: i64) -> StringId abi(.compiler);
|
|
type_field_type :: (t: TypeId, idx: i64) -> TypeId abi(.compiler);
|
|
|
|
Point :: struct { x: i64; y: i64; }
|
|
Pair :: struct { lo: Point; hi: Point; }
|
|
|
|
pair :: #run find_type(intern("Pair"));
|
|
n :: #run type_field_count(find_type(intern("Pair")));
|
|
f0_name :: #run text_of(type_field_name(find_type(intern("Pair")), 0));
|
|
f1_name :: #run text_of(type_field_name(find_type(intern("Pair")), 1));
|
|
// field 0's type is `Point` — read its nominal name through the type handle.
|
|
f0_type :: #run text_of(type_nominal_name(type_field_type(find_type(intern("Pair")), 0)));
|
|
|
|
main :: () {
|
|
print("Pair has {} fields\n", n);
|
|
print("field 0 = {} : {}\n", f0_name, f0_type);
|
|
print("field 1 = {} : {}\n", f1_name, f0_type);
|
|
}
|