comptime VM: Phase 3 — field-level reflection readers
Three more read-only compiler-API readers on the TypeId-handle shape, each backed
by a new TypeTable query that both the legacy handler and the VM call (no drift):
type_nominal_name(t: TypeId) -> StringId (nominalName; loud-bail for unnamed types)
type_field_name(t: TypeId, idx: i64) -> StringId (memberName)
type_field_type(t: TypeId, idx: i64) -> TypeId (memberType)
All loud-bail on out-of-range idx / no-member — no silent default. First multi-arg
compiler fns (callCompilerFn now reads arg 1 = idx); added Vm.argHandle/argTypeId
range-checked arg readers and moved find_type/type_field_count onto them. Names use
the type_* family to avoid colliding with the std metatype builtins (field_name /
type_name in core.sx); the new TypeTable.nominalName is distinct from the existing
typeName(id) display-string renderer.
Example 0629 reflects Pair { lo: Point; hi: Point } — each field name + the nominal
name of a field's type, #run-folded, VM-HANDLED natively. VM unit test added.
Parity 690/690 (gate OFF and -Dcomptime-flat).
This commit is contained in:
45
examples/0629-comptime-compiler-field-reflect.sx
Normal file
45
examples/0629-comptime-compiler-field-reflect.sx
Normal file
@@ -0,0 +1,45 @@
|
||||
// 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";
|
||||
|
||||
compiler :: #library "compiler";
|
||||
|
||||
StringId :: u32;
|
||||
TypeId :: u32;
|
||||
|
||||
intern :: (s: string) -> StringId abi(.zig) extern compiler;
|
||||
text_of :: (id: StringId) -> string abi(.zig) extern compiler;
|
||||
find_type :: (name: StringId) -> TypeId abi(.zig) extern compiler;
|
||||
type_field_count :: (t: TypeId) -> i64 abi(.zig) extern compiler;
|
||||
type_nominal_name :: (t: TypeId) -> StringId abi(.zig) extern compiler;
|
||||
type_field_name :: (t: TypeId, idx: i64) -> StringId abi(.zig) extern compiler;
|
||||
type_field_type :: (t: TypeId, idx: i64) -> TypeId abi(.zig) extern 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);
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
0
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
Pair has 2 fields
|
||||
field 0 = lo : Point
|
||||
field 1 = hi : Point
|
||||
Reference in New Issue
Block a user