interp: deterministic interpreter, aji run, run tests (P0-11, P0-12)

- src/interp/machine.zig: format-0 interpreter on the air64 target;
  checked/wrapping arithmetic, shifts, casts, aggregates, calls with a
  1024-frame limit, traps as records with T0xxx codes
- aji run: prints one JSON run record; exit code 4 on trap
- conformance runner: `expect: run` with `result:`/`trap:`, every run
  executed twice and compared byte-for-byte; header keys must be single
  lowercase words
- Phase 0 corpus: 3 accept, 19 reject, 15 run tests (41 total)
This commit is contained in:
dev committed 2026-10-04 12:28:27 +03:00
1 parent 50bf990355
commit 1f5008bb37
45 files changed
+1463 -23

No files matched your search

+71 -2
View File
@@ -12,6 +12,15 @@ const Expect = union(enum) {
accept,
/// Exact multiset of error codes, sorted.
reject: []const []const u8,
/// Verify, run `@main` and compare the run record.
run: Run,
const Run = union(enum) {
/// Expected `value` of a `returned` record, as canonical JSON.
result: []const u8,
/// Expected trap code.
trap: []const u8,
};
};
const Outcome = enum { pass, fail, pending };
@@ -133,12 +142,51 @@ fn runOne(arena: std.mem.Allocator, path: []const u8, source: []const u8) !Resul
switch (header.expect) {
.accept => if (report.status == .accepted) return .{ .outcome = .pass },
.reject => |want| if (report.status == .rejected and eqlCodes(want, got.items)) return .{ .outcome = .pass },
.run => |want| if (report.status == .accepted) return runModule(arena, report.module.?, want),
}
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "expected {f}, got {t} {s}", .{
fmtExpect(header.expect), report.status, try std.mem.join(arena, " ", got.items),
}) };
}
/// Runs the module twice and requires byte-identical run records (spec 11)
/// before comparing against the expectation.
fn runModule(arena: std.mem.Allocator, module: aji.air.Module, want: Expect.Run) !Result {
var records: [2][]const u8 = undefined;
var outcome: aji.interp.Outcome = undefined;
for (&records) |*rec| {
outcome = aji.interp.run(arena, module, "main") catch |err| switch (err) {
error.EntryNotFound => return .{ .outcome = .fail, .detail = "module has no parameterless @main" },
else => |e| return e,
};
var w: std.Io.Writer.Allocating = .init(arena);
try aji.interp.writeOutcome(module, outcome, &w.writer);
rec.* = w.written();
}
if (!std.mem.eql(u8, records[0], records[1]))
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "nondeterministic: {s} vs {s}", .{ records[0], records[1] }) };
const ok = switch (want) {
.trap => |code| outcome == .trapped and std.mem.eql(u8, outcome.trapped.code.id, code),
.result => |json| outcome == .returned and std.mem.eql(u8, json, try valueJson(arena, module, outcome.returned)),
};
if (ok) return .{ .outcome = .pass };
return .{ .outcome = .fail, .detail = try std.fmt.allocPrint(arena, "got {s}", .{records[0]}) };
}
fn valueJson(arena: std.mem.Allocator, module: aji.air.Module, v: aji.interp.Value) ![]const u8 {
var w: std.Io.Writer.Allocating = .init(arena);
var s: std.json.Stringify = .{ .writer = &w.writer };
try aji.interp.writeValue(module, v, &s);
return w.written();
}
fn isKey(s: []const u8) bool {
if (s.len == 0) return false;
for (s) |ch| if (!std.ascii.isLower(ch) and ch != '_') return false;
return true;
}
fn eqlCodes(a: []const []const u8, b: []const []const u8) bool {
if (a.len != b.len) return false;
for (a, b) |x, y| if (!std.mem.eql(u8, x, y)) return false;
@@ -156,13 +204,18 @@ fn formatExpect(e: Expect, w: *std.Io.Writer) std.Io.Writer.Error!void {
try w.writeAll("reject");
for (codes) |c| try w.print(" {s}", .{c});
},
.run => |r| switch (r) {
.result => |j| try w.print("run returning {s}", .{j}),
.trap => |c| try w.print("run trapping {s}", .{c}),
},
}
}
const HeaderError = error{ MissingExpect, MissingPhase, MissingSpec, UnknownKey, UnknownCode, BadExpect, OutOfMemory, InvalidCharacter, Overflow };
const HeaderError = error{ MissingExpect, MissingPhase, MissingSpec, MissingRunResult, UnknownKey, UnknownCode, BadExpect, OutOfMemory, InvalidCharacter, Overflow };
fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header {
var expect: ?Expect = null;
var run_result: ?Expect.Run = null;
var phase: ?u8 = null;
var spec: ?[]const u8 = null;
@@ -172,11 +225,15 @@ fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header
const content = std.mem.trim(u8, line[1..], " \t\r");
const colon = std.mem.indexOfScalar(u8, content, ':') orelse continue; // free-form comment
const key = content[0..colon];
if (!isKey(key)) continue; // free-form comment containing a colon
const value = std.mem.trim(u8, content[colon + 1 ..], " \t");
if (std.mem.eql(u8, key, "expect")) {
var it = std.mem.tokenizeScalar(u8, value, ' ');
const verb = it.next() orelse return error.BadExpect;
if (std.mem.eql(u8, verb, "accept")) {
if (std.mem.eql(u8, verb, "run")) {
if (it.next() != null) return error.BadExpect;
expect = .{ .run = undefined };
} else if (std.mem.eql(u8, verb, "accept")) {
if (it.next() != null) return error.BadExpect;
expect = .accept;
} else if (std.mem.eql(u8, verb, "reject")) {
@@ -189,12 +246,24 @@ fn parseHeader(arena: std.mem.Allocator, source: []const u8) HeaderError!Header
std.mem.sort([]const u8, codes.items, {}, lessThan);
expect = .{ .reject = codes.items };
} else return error.BadExpect;
} else if (std.mem.eql(u8, key, "result")) {
if (run_result != null) return error.BadExpect;
run_result = .{ .result = value };
} else if (std.mem.eql(u8, key, "trap")) {
if (run_result != null) return error.BadExpect;
const code = aji.diag.lookup(value) orelse return error.UnknownCode;
if (code.id[0] != 'T') return error.UnknownCode;
run_result = .{ .trap = value };
} else if (std.mem.eql(u8, key, "phase")) {
phase = try std.fmt.parseInt(u8, value, 10);
} else if (std.mem.eql(u8, key, "spec")) {
spec = value;
} else return error.UnknownKey;
}
if (expect) |*e| switch (e.*) {
.run => e.* = .{ .run = run_result orelse return error.MissingRunResult },
else => if (run_result != null) return error.BadExpect,
};
return .{
.expect = expect orelse return error.MissingExpect,
.phase = phase orelse return error.MissingPhase,