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

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