Files
sx/examples/0629-comptime-compiler-field-reflect.sx
agra 2060373c16 comptime VM arc: abi(.compiler) ABI, out as sx fn, VM-native diagnostics, BuildConfig threaded
Lands the full VM/compiler-API arc on branch reify (701/0 both gates):
- abi(.compiler) ABI replaces abi(.zig) extern compiler + the fake
  #library "compiler"; bodiless decl = compiler-API surface, bodied =
  user compiler-domain fn (lowered for VM eval, emit-skipped).
- out is a plain sx fn (libc write) — the out builtin deleted; the VM
  handles it via host-FFI. trace_resolve + interp_print_frames ported.
- 4B VM-native diagnostics: 1179/1180 render proper comptime type
  construction failed: under strict.
- S5a: build_options/set_post_link_callback on abi(.compiler) with
  BuildConfig threaded into the VM (green intermediate).
- 0522 fixed (describe(args: []Type)); regression 0638.

Strict deletion-gate down to 4 compiler_call bails (1609/1614/1615/1616)
+ 1654 (legitimate unresolvable-symbol diagnostic).
2026-06-19 07:04:10 +03:00

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);
}