lock(reify): meta.sx surface + bodyless #builtin decls + loud bails

REIFY Phase 0.0. Add the comptime type-metaprogramming surface as the
on-demand module modules/std/meta.sx (NOT the prelude — declaring its
data types in always-loaded core.sx interns them into every module's
type table and shifts every .ir snapshot):

  - EnumVariant / EnumInfo / TypeInfo data types. TypeInfo's variant uses
    the backtick raw escape `enum so it reads as the keyword.
  - reify / type_info / field_type as bodyless #builtin decls.

Each builtin bails LOUDLY when reached unimplemented (no silent default):
  - reify(...) in a :: type-alias position -> decl.zig .call branch
    (also the Phase 0.2 construction hook); poisons the alias .unresolved.
  - reify / field_type in any other type position ->
    generic.zig resolveTypeCallWithBindings.
  - type_info(...) in expression position -> call.zig tryLowerReflectionCall.

Unit test src/parser.test.zig (registered in root.zig) locks that the
decls parse. zig build test green (447 unit, 669 examples).
This commit is contained in:
agra
2026-06-16 17:44:19 +03:00
parent ded106333b
commit 81669c72b7
8 changed files with 196 additions and 13 deletions

79
src/parser.test.zig Normal file
View File

@@ -0,0 +1,79 @@
// Parser tests — pin parse-level shapes the example corpus can't isolate
// (the corpus runs the full `sx run` pipeline, never the parser alone).
const std = @import("std");
const ast = @import("ast.zig");
const Node = ast.Node;
const Parser = @import("parser.zig").Parser;
// REIFY Phase 0.0 (lock): the comptime type-metaprogramming surface added to
// `library/modules/std/meta.sx` must PARSE — the data types as struct/enum
// decls, and `reify`/`type_info`/`field_type` as bodyless `#builtin` consts.
// This locks the declared shape before the interpreter-side construction lands
// (Phase 0.2). Mirrors the exact spellings in meta.sx.
test "parser: reify TypeInfo data types + #builtin decls parse" {
var arena = std.heap.ArenaAllocator.init(std.testing.allocator);
defer arena.deinit();
const alloc = arena.allocator();
const src =
\\EnumVariant :: struct {
\\ name: string;
\\ payload: Type;
\\}
\\EnumInfo :: struct {
\\ variants: []EnumVariant;
\\}
\\TypeInfo :: enum {
\\ `enum: EnumInfo;
\\}
\\reify :: (info: TypeInfo) -> Type #builtin;
\\type_info :: ($T: Type) -> TypeInfo #builtin;
\\field_type :: ($T: Type, idx: i64) -> Type #builtin;
\\
;
var parser = Parser.init(alloc, src);
const root = try parser.parse();
try std.testing.expect(root.data == .root);
const decls = root.data.root.decls;
try std.testing.expectEqual(@as(usize, 6), decls.len);
const Found = struct {
// A top-level `Name :: struct/enum {…}` parses to a `.struct_decl` /
// `.enum_decl` node DIRECTLY (not wrapped in a const_decl); only the
// `#builtin` forms are `.const_decl`. Match on the shared `declName`.
fn byName(ds: []const *Node, name: []const u8) ?*const Node {
for (ds) |d| {
if (d.data.declName()) |n| {
if (std.mem.eql(u8, n, name)) return d;
}
}
return null;
}
};
// Data types: struct / struct / enum, parsed as their decl nodes directly.
const ev = Found.byName(decls, "EnumVariant") orelse return error.MissingDecl;
try std.testing.expect(ev.data == .struct_decl);
const ei = Found.byName(decls, "EnumInfo") orelse return error.MissingDecl;
try std.testing.expect(ei.data == .struct_decl);
const ti = Found.byName(decls, "TypeInfo") orelse return error.MissingDecl;
try std.testing.expect(ti.data == .enum_decl);
// The single `` `enum `` variant of TypeInfo. The backtick raw escape
// stores the bare keyword as the variant name.
const ed = ti.data.enum_decl;
try std.testing.expectEqual(@as(usize, 1), ed.variant_names.len);
try std.testing.expectEqualStrings("enum", ed.variant_names[0]);
// Builtins: the `(params) -> Ret #builtin;` form parses as a `.fn_decl`
// (the `->` triggers the function-def path) whose body is a `#builtin`
// marker — same shape as the existing reflection builtins in core.sx.
for ([_][]const u8{ "reify", "type_info", "field_type" }) |bn| {
const d = Found.byName(decls, bn) orelse return error.MissingDecl;
try std.testing.expect(d.data == .fn_decl);
try std.testing.expect(d.data.fn_decl.body.data == .builtin_expr);
try std.testing.expect(d.data.fn_decl.return_type != null);
}
}