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:
1 parent
50bf990355
commit
1f5008bb37
45 files changed
+1463
-23
No files matched your search
@@ -130,7 +130,7 @@ zig fmt --check build.zig build.zig.zon src tools
|
||||
|
||||
Toolchain: Zig 0.17.0. Workflow, layout and definition of done: `CONTRIBUTING.md`. Implementation plan and current phase: `docs/plan/README.md`. Documentation layout and where new documents go: `docs/README.md`. Unresolved spec gaps: `docs/design/open-questions.md` — log new ones there instead of guessing.
|
||||
|
||||
`aji` CLI output contract: diagnostics are JSON Lines on stdout, human notes on stderr; exit codes 0 accepted, 1 rejected, 2 usage/I/O error, 3 not yet implemented.
|
||||
`aji` CLI output contract: diagnostics are JSON Lines on stdout, human notes on stderr; exit codes 0 accepted/returned, 1 rejected, 2 usage/I/O error, 3 not yet implemented, 4 trapped. `aji run` prints one JSON run record.
|
||||
|
||||
## Testing policy
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ Reason: Zig's standard library changes between minor versions; an unpinned toolc
|
||||
|
||||
## DD-009 — CLI diagnostics are JSON Lines on stdout
|
||||
|
||||
`aji` writes one JSON diagnostic per line on stdout, human notes on stderr, and uses fixed exit codes (0 accepted, 1 rejected, 2 usage/I/O, 3 not yet implemented).
|
||||
`aji` writes one JSON diagnostic per line on stdout, human notes on stderr, and uses fixed exit codes (0 accepted or returned, 1 rejected, 2 usage/I/O, 3 not yet implemented, 4 trapped).
|
||||
|
||||
Reason: agents consume diagnostics programmatically (spec 10); mixing prose into the machine channel breaks repair loops.
|
||||
|
||||
|
||||
+3
-4
@@ -13,7 +13,7 @@ Non-normative. How the reference implementation is tested. Policy (what must be
|
||||
| Schedule stress | via runner flags (from P3) | `zig build conformance -- --schedules=N` | randomized and systematic interleavings |
|
||||
| Native parity | (from P7) | `zig build parity` | interpreter and native backend agree on the whole corpus |
|
||||
|
||||
Only unit and conformance exist today.
|
||||
Unit and conformance exist today; conformance covers accept, reject and run tests.
|
||||
|
||||
## Conformance tests
|
||||
|
||||
@@ -30,9 +30,8 @@ Planned header extensions (added by the phase that needs them):
|
||||
|
||||
| Key | Phase | Meaning |
|
||||
|---|---|---|
|
||||
| `expect: run` | P0 | module must verify and run |
|
||||
| `stdout:` | P0 | expected output, `\n`-escaped |
|
||||
| `trap:` | P0 | expected trap kind (Q-004) |
|
||||
| `expect: run` + `result:` / `trap:` | P0 (implemented) | module must verify; run record must match |
|
||||
| `stdout:` | P2 | expected output, `\n`-escaped (needs `io.write`) |
|
||||
| `seed:` / `schedules:` | P3 | deterministic scheduler seed / number of randomized schedules |
|
||||
| `profile:` | P2 | runtime profile (`hosted`, `deterministic`, …) |
|
||||
|
||||
|
||||
@@ -20,6 +20,11 @@ pub const ValueId = module.ValueId;
|
||||
pub const Span = module.Span;
|
||||
pub const Param = module.Param;
|
||||
pub const Target = module.Target;
|
||||
pub const ArithOp = module.ArithOp;
|
||||
pub const ArithMode = module.ArithMode;
|
||||
pub const BitOp = module.BitOp;
|
||||
pub const CmpOp = module.CmpOp;
|
||||
pub const CastMode = module.CastMode;
|
||||
pub const forEachUse = module.forEachUse;
|
||||
pub const successors = module.successors;
|
||||
|
||||
|
||||
+15
-6
@@ -1,6 +1,15 @@
|
||||
//! AIR interpreter (spec 07, spec 11). Defines observable semantics.
|
||||
//!
|
||||
//! Requirements:
|
||||
//! - executes only verified AIR;
|
||||
//! - deterministic: identical input and seed give identical output and trace;
|
||||
//! - traps are explicit values, never host panics or unwinding.
|
||||
//! AIR interpreter (spec 07a, spec 11). Defines observable semantics.
|
||||
|
||||
const machine = @import("interp/machine.zig");
|
||||
|
||||
pub const Value = machine.Value;
|
||||
pub const Trap = machine.Trap;
|
||||
pub const Outcome = machine.Outcome;
|
||||
pub const run = machine.run;
|
||||
pub const writeOutcome = machine.writeOutcome;
|
||||
pub const writeValue = machine.writeValue;
|
||||
pub const call_depth_limit = machine.call_depth_limit;
|
||||
|
||||
test {
|
||||
_ = machine;
|
||||
}
|
||||
@@ -0,0 +1,529 @@
|
||||
//! Reference interpreter for format version 0 (spec 07a, spec 11 §1).
|
||||
//!
|
||||
//! Requirements:
|
||||
//! - executes only verified AIR;
|
||||
//! - deterministic: no host time, randomness or address-dependent behavior;
|
||||
//! - traps are values (`Outcome.trapped`), never host panics or unwinding.
|
||||
//!
|
||||
//! Target description: `air64` (spec 09 §9). All memory comes from the arena
|
||||
//! passed to `run` and is released by the caller as a whole.
|
||||
|
||||
const std = @import("std");
|
||||
const air = @import("../air.zig");
|
||||
const diag = @import("../diag.zig");
|
||||
|
||||
pub const call_depth_limit = 1024;
|
||||
|
||||
pub const Value = union(enum) {
|
||||
/// Bit pattern of the integer, zero-extended to 128 bits.
|
||||
int: struct { ty: air.IntType, bits: u128 },
|
||||
boolean: bool,
|
||||
void,
|
||||
aggregate: Aggregate,
|
||||
|
||||
pub const Aggregate = struct {
|
||||
decl: u32,
|
||||
/// Active variant for enums; 0 for structs.
|
||||
variant: u32,
|
||||
fields: []const Value,
|
||||
};
|
||||
};
|
||||
|
||||
pub const Trap = struct {
|
||||
code: *const diag.Code,
|
||||
function: []const u8,
|
||||
block: []const u8,
|
||||
inst: u32,
|
||||
};
|
||||
|
||||
pub const Outcome = union(enum) {
|
||||
returned: Value,
|
||||
trapped: Trap,
|
||||
};
|
||||
|
||||
pub const Error = std.mem.Allocator.Error || error{EntryNotFound};
|
||||
|
||||
/// Runs `entry`, which must take no parameters. `module` must have passed
|
||||
/// verification.
|
||||
pub fn run(arena: std.mem.Allocator, module: air.Module, entry: []const u8) Error!Outcome {
|
||||
const idx = module.functionIndex(entry) orelse return error.EntryNotFound;
|
||||
if (module.functions[idx].params.len != 0) return error.EntryNotFound;
|
||||
var m: Machine = .{ .arena = arena, .module = module };
|
||||
return switch (try m.call(idx, &.{})) {
|
||||
.value => |v| .{ .returned = v },
|
||||
.trap => |t| .{ .trapped = t },
|
||||
};
|
||||
}
|
||||
|
||||
const TrapKind = enum {
|
||||
explicit,
|
||||
assert,
|
||||
overflow,
|
||||
division_by_zero,
|
||||
shift_out_of_range,
|
||||
cast_out_of_range,
|
||||
call_depth,
|
||||
|
||||
fn code(k: TrapKind) *const diag.Code {
|
||||
return switch (k) {
|
||||
.explicit => diag.lookup("T0001").?,
|
||||
.assert => diag.lookup("T0002").?,
|
||||
.overflow => diag.lookup("T0003").?,
|
||||
.division_by_zero => diag.lookup("T0004").?,
|
||||
.shift_out_of_range => diag.lookup("T0005").?,
|
||||
.cast_out_of_range => diag.lookup("T0006").?,
|
||||
.call_depth => diag.lookup("T0007").?,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
/// Traps raised by integer operations.
|
||||
const TrapError = error{ overflow, division_by_zero, shift_out_of_range, cast_out_of_range };
|
||||
|
||||
fn kindOf(e: TrapError) TrapKind {
|
||||
return switch (e) {
|
||||
error.overflow => .overflow,
|
||||
error.division_by_zero => .division_by_zero,
|
||||
error.shift_out_of_range => .shift_out_of_range,
|
||||
error.cast_out_of_range => .cast_out_of_range,
|
||||
};
|
||||
}
|
||||
|
||||
const CallResult = union(enum) { value: Value, trap: Trap };
|
||||
|
||||
const Machine = struct {
|
||||
arena: std.mem.Allocator,
|
||||
module: air.Module,
|
||||
depth: u32 = 0,
|
||||
|
||||
fn call(m: *Machine, fn_index: u32, args: []const Value) std.mem.Allocator.Error!CallResult {
|
||||
const f = m.module.functions[fn_index];
|
||||
m.depth += 1;
|
||||
defer m.depth -= 1;
|
||||
|
||||
const vals = try m.arena.alloc(Value, f.values.len);
|
||||
for (f.params, args) |p, a| vals[p.value] = a;
|
||||
|
||||
var block: u32 = 0;
|
||||
while (true) {
|
||||
const b = f.blocks[block];
|
||||
for (b.insts, 0..) |inst, ii| {
|
||||
const step = try m.exec(f, vals, inst);
|
||||
switch (step) {
|
||||
.next => {},
|
||||
.jump => |t| {
|
||||
block = t;
|
||||
break;
|
||||
},
|
||||
.ret => |v| return .{ .value = v },
|
||||
.trap => |k| return .{ .trap = .{
|
||||
.code = k.code(),
|
||||
.function = f.name,
|
||||
.block = b.name,
|
||||
.inst = @intCast(ii),
|
||||
} },
|
||||
.nested_trap => |t| return .{ .trap = t },
|
||||
}
|
||||
} else unreachable; // verified blocks end with a terminator
|
||||
}
|
||||
}
|
||||
|
||||
const Step = union(enum) {
|
||||
next,
|
||||
jump: u32,
|
||||
ret: Value,
|
||||
trap: TrapKind,
|
||||
/// A trap raised inside a callee, already located.
|
||||
nested_trap: Trap,
|
||||
};
|
||||
|
||||
fn exec(m: *Machine, f: air.Function, vals: []Value, inst: air.Inst) std.mem.Allocator.Error!Step {
|
||||
switch (inst.op) {
|
||||
.@"const" => |c| return bind(vals, inst.result, switch (c.ty) {
|
||||
.int => |it| .{ .int = .{ .ty = it, .bits = c.bits } },
|
||||
.bool => .{ .boolean = c.bits != 0 },
|
||||
else => unreachable,
|
||||
}),
|
||||
.copy => |v| return bind(vals, inst.result, vals[v]),
|
||||
.arith => |x| {
|
||||
const bits = arith(x.op, x.mode, x.ty, vals[x.a].int.bits, vals[x.b].int.bits) catch |e| return .{ .trap = kindOf(e) };
|
||||
return bind(vals, inst.result, .{ .int = .{ .ty = x.ty, .bits = bits } });
|
||||
},
|
||||
.bit => |x| switch (x.ty) {
|
||||
.bool => {
|
||||
const a = vals[x.a].boolean;
|
||||
const b = vals[x.b].boolean;
|
||||
return bind(vals, inst.result, .{ .boolean = switch (x.op) {
|
||||
.@"and" => a and b,
|
||||
.@"or" => a or b,
|
||||
.xor => a != b,
|
||||
else => unreachable,
|
||||
} });
|
||||
},
|
||||
.int => |it| {
|
||||
const bits = bitop(x.op, it, vals[x.a].int.bits, vals[x.b].int.bits) catch |e| return .{ .trap = kindOf(e) };
|
||||
return bind(vals, inst.result, .{ .int = .{ .ty = it, .bits = bits } });
|
||||
},
|
||||
else => unreachable,
|
||||
},
|
||||
.cmp => |x| return bind(vals, inst.result, .{ .boolean = switch (x.ty) {
|
||||
.bool => switch (x.op) {
|
||||
.eq => vals[x.a].boolean == vals[x.b].boolean,
|
||||
.ne => vals[x.a].boolean != vals[x.b].boolean,
|
||||
else => unreachable,
|
||||
},
|
||||
.int => |it| compare(x.op, it, vals[x.a].int.bits, vals[x.b].int.bits),
|
||||
else => unreachable,
|
||||
} }),
|
||||
.cast => |x| {
|
||||
const from = vals[x.a].int;
|
||||
const bits = cast(x.mode, from.ty, x.to, from.bits) catch |e| return .{ .trap = kindOf(e) };
|
||||
return bind(vals, inst.result, .{ .int = .{ .ty = x.to, .bits = bits } });
|
||||
},
|
||||
.assert => |v| return if (vals[v].boolean) .next else .{ .trap = .assert },
|
||||
.struct_make => |x| return bind(vals, inst.result, .{ .aggregate = .{
|
||||
.decl = x.decl,
|
||||
.variant = 0,
|
||||
.fields = try m.gather(vals, x.args),
|
||||
} }),
|
||||
.struct_get => |x| return bind(vals, inst.result, vals[x.a].aggregate.fields[x.field]),
|
||||
.enum_make => |x| return bind(vals, inst.result, .{ .aggregate = .{
|
||||
.decl = x.decl,
|
||||
.variant = x.variant,
|
||||
.fields = try m.gather(vals, x.args),
|
||||
} }),
|
||||
.call => |x| {
|
||||
if (m.depth >= call_depth_limit) return .{ .trap = .call_depth };
|
||||
const args = try m.gather(vals, x.args);
|
||||
switch (try m.call(x.callee, args)) {
|
||||
.value => |v| {
|
||||
if (inst.result) |r| vals[r] = v;
|
||||
return .next;
|
||||
},
|
||||
.trap => |t| return .{ .nested_trap = t },
|
||||
}
|
||||
},
|
||||
.br => |t| return m.jump(f, vals, t),
|
||||
.cbr => |x| return m.jump(f, vals, if (vals[x.cond].boolean) x.then else x.@"else"),
|
||||
.@"switch" => |x| {
|
||||
const v = vals[x.value].int.bits;
|
||||
for (x.cases) |c| if (c.bits == v) return m.jump(f, vals, c.target);
|
||||
return m.jump(f, vals, x.default);
|
||||
},
|
||||
.enum_switch => |x| {
|
||||
const agg = vals[x.value].aggregate;
|
||||
for (x.arms) |arm| if (arm.variant == agg.variant) {
|
||||
for (f.blocks[arm.block].params, agg.fields) |p, fv| vals[p.value] = fv;
|
||||
return .{ .jump = arm.block };
|
||||
};
|
||||
return m.jump(f, vals, x.default.?);
|
||||
},
|
||||
.ret => |v| return .{ .ret = if (v) |x| vals[x] else .void },
|
||||
.trap => return .{ .trap = .explicit },
|
||||
}
|
||||
}
|
||||
|
||||
fn bind(vals: []Value, result: ?air.ValueId, v: Value) Step {
|
||||
vals[result.?] = v;
|
||||
return .next;
|
||||
}
|
||||
|
||||
fn gather(m: *Machine, vals: []const Value, ids: []const air.ValueId) ![]const Value {
|
||||
const out = try m.arena.alloc(Value, ids.len);
|
||||
for (ids, out) |id, *o| o.* = vals[id];
|
||||
return out;
|
||||
}
|
||||
|
||||
/// Block arguments are bound simultaneously (spec 07 §8.2).
|
||||
fn jump(m: *Machine, f: air.Function, vals: []Value, t: air.Target) !Step {
|
||||
const tmp = try m.gather(vals, t.args);
|
||||
for (f.blocks[t.block].params, tmp) |p, v| vals[p.value] = v;
|
||||
return .{ .jump = t.block };
|
||||
}
|
||||
};
|
||||
|
||||
// ---------------------------------------------------------------- integer semantics (spec 02 §2.1)
|
||||
|
||||
fn Unsigned(comptime T: type) type {
|
||||
return @Int(.unsigned, @bitSizeOf(T));
|
||||
}
|
||||
|
||||
fn decode(comptime T: type, bits: u128) T {
|
||||
return @bitCast(@as(Unsigned(T), @truncate(bits)));
|
||||
}
|
||||
|
||||
fn encode(comptime T: type, v: T) u128 {
|
||||
return @as(Unsigned(T), @bitCast(v));
|
||||
}
|
||||
|
||||
fn arith(op: air.ArithOp, mode: air.ArithMode, ty: air.IntType, a: u128, b: u128) TrapError!u128 {
|
||||
switch (ty) {
|
||||
inline else => |t| {
|
||||
const T = air.IntType.Zig(t);
|
||||
const x = decode(T, a);
|
||||
const y = decode(T, b);
|
||||
const r: T = switch (mode) {
|
||||
.wrap => switch (op) {
|
||||
.add => x +% y,
|
||||
.sub => x -% y,
|
||||
.mul => x *% y,
|
||||
.div, .rem => unreachable, // not in the grammar
|
||||
},
|
||||
.checked => switch (op) {
|
||||
.add => try noOverflow(T, @addWithOverflow(x, y)),
|
||||
.sub => try noOverflow(T, @subWithOverflow(x, y)),
|
||||
.mul => try noOverflow(T, @mulWithOverflow(x, y)),
|
||||
.div, .rem => r: {
|
||||
if (y == 0) return error.division_by_zero;
|
||||
if (comptime t.signed()) {
|
||||
if (x == std.math.minInt(T) and y == -1) return error.overflow;
|
||||
}
|
||||
break :r if (op == .div) @divTrunc(x, y) else @rem(x, y);
|
||||
},
|
||||
},
|
||||
};
|
||||
return encode(T, r);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn noOverflow(comptime T: type, r: struct { T, u1 }) TrapError!T {
|
||||
if (r[1] != 0) return error.overflow;
|
||||
return r[0];
|
||||
}
|
||||
|
||||
fn bitop(op: air.BitOp, ty: air.IntType, a: u128, b: u128) TrapError!u128 {
|
||||
switch (ty) {
|
||||
inline else => |t| {
|
||||
const T = air.IntType.Zig(t);
|
||||
const x = decode(T, a);
|
||||
const y = decode(T, b);
|
||||
const r: T = switch (op) {
|
||||
.@"and" => x & y,
|
||||
.@"or" => x | y,
|
||||
.xor => x ^ y,
|
||||
.shl, .shr => r: {
|
||||
if (y < 0 or y >= @bitSizeOf(T)) return error.shift_out_of_range;
|
||||
const n: std.math.Log2Int(T) = @intCast(y);
|
||||
break :r if (op == .shl) x << n else x >> n;
|
||||
},
|
||||
};
|
||||
return encode(T, r);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn compare(op: air.CmpOp, ty: air.IntType, a: u128, b: u128) bool {
|
||||
switch (ty) {
|
||||
inline else => |t| {
|
||||
const T = air.IntType.Zig(t);
|
||||
const x = decode(T, a);
|
||||
const y = decode(T, b);
|
||||
return switch (op) {
|
||||
.eq => x == y,
|
||||
.ne => x != y,
|
||||
.lt => x < y,
|
||||
.le => x <= y,
|
||||
.gt => x > y,
|
||||
.ge => x >= y,
|
||||
};
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
fn cast(mode: air.CastMode, from: air.IntType, to: air.IntType, bits: u128) TrapError!u128 {
|
||||
// Every integer type fits in i129, so the numeric value is exact here.
|
||||
const v: i129 = switch (from) {
|
||||
inline else => |t| decode(air.IntType.Zig(t), bits),
|
||||
};
|
||||
switch (to) {
|
||||
inline else => |t| {
|
||||
const T = air.IntType.Zig(t);
|
||||
const r: T = switch (mode) {
|
||||
.checked, .widen => std.math.cast(T, v) orelse return error.cast_out_of_range,
|
||||
.wrap => @bitCast(@as(Unsigned(T), @truncate(@as(u129, @bitCast(v))))),
|
||||
};
|
||||
return encode(T, r);
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- run records (spec 07a "Run result")
|
||||
|
||||
pub fn writeValue(module: air.Module, v: Value, s: *std.json.Stringify) std.Io.Writer.Error!void {
|
||||
switch (v) {
|
||||
.int => |x| switch (x.ty) {
|
||||
inline else => |t| try s.print("{d}", .{decode(air.IntType.Zig(t), x.bits)}),
|
||||
},
|
||||
.boolean => |b| try s.write(b),
|
||||
.void => try s.write(null),
|
||||
.aggregate => |agg| {
|
||||
const d = module.types[agg.decl];
|
||||
switch (d.kind) {
|
||||
.@"struct" => |fields| {
|
||||
try s.beginObject();
|
||||
for (fields, agg.fields) |fd, fv| {
|
||||
try s.objectField(fd.name);
|
||||
try writeValue(module, fv, s);
|
||||
}
|
||||
try s.endObject();
|
||||
},
|
||||
.@"enum" => |variants| {
|
||||
try s.beginObject();
|
||||
try s.objectField("variant");
|
||||
try s.write(variants[agg.variant].name);
|
||||
try s.objectField("payload");
|
||||
try s.beginArray();
|
||||
for (agg.fields) |fv| try writeValue(module, fv, s);
|
||||
try s.endArray();
|
||||
try s.endObject();
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the run record for `outcome` as one JSON object (no newline).
|
||||
pub fn writeOutcome(module: air.Module, outcome: Outcome, w: *std.Io.Writer) std.Io.Writer.Error!void {
|
||||
var s: std.json.Stringify = .{ .writer = w };
|
||||
try s.beginObject();
|
||||
try s.objectField("status");
|
||||
switch (outcome) {
|
||||
.returned => |v| {
|
||||
try s.write("returned");
|
||||
try s.objectField("value");
|
||||
try writeValue(module, v, &s);
|
||||
},
|
||||
.trapped => |t| {
|
||||
try s.write("trapped");
|
||||
try s.objectField("code");
|
||||
try s.write(t.code.id);
|
||||
try s.objectField("kind");
|
||||
try s.write(t.code.kind);
|
||||
try s.objectField("function");
|
||||
try s.write(t.function);
|
||||
try s.objectField("block");
|
||||
try s.write(t.block);
|
||||
try s.objectField("inst");
|
||||
try s.write(t.inst);
|
||||
},
|
||||
}
|
||||
try s.endObject();
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------- tests
|
||||
|
||||
const check = @import("../check.zig");
|
||||
|
||||
fn expectRun(src: []const u8, expected: []const u8) !void {
|
||||
var arena_state: std.heap.ArenaAllocator = .init(std.testing.allocator);
|
||||
defer arena_state.deinit();
|
||||
const arena = arena_state.allocator();
|
||||
const report = try check.checkAir(arena, "t.air", src);
|
||||
if (report.status != .accepted) {
|
||||
for (report.diagnostics) |d| std.debug.print("{s} {s}\n", .{ d.code.id, d.message });
|
||||
return error.NotAccepted;
|
||||
}
|
||||
const outcome = try run(arena, report.module.?, "main");
|
||||
var out: std.Io.Writer.Allocating = .init(arena);
|
||||
try writeOutcome(report.module.?, outcome, &out.writer);
|
||||
try std.testing.expectEqualStrings(expected, out.written());
|
||||
}
|
||||
|
||||
const h = "module t\nair 0\nlang 0.1\nfeatures []\n";
|
||||
|
||||
fn main1(comptime ret: []const u8, comptime insts: []const u8) []const u8 {
|
||||
return h ++ "fn @main() -> " ++ ret ++ "\neffects []\nentry:\n" ++ insts;
|
||||
}
|
||||
|
||||
test "checked arithmetic and traps" {
|
||||
try expectRun(comptime main1("u8", " %a = const.u8 200\n %b = const.u8 55\n %c = add.checked.u8 %a, %b\n ret %c\n"), "{\"status\":\"returned\",\"value\":255}");
|
||||
try expectRun(comptime main1("u8", " %a = const.u8 200\n %b = const.u8 56\n %c = add.checked.u8 %a, %b\n ret %c\n"), "{\"status\":\"trapped\",\"code\":\"T0003\",\"kind\":\"trap.overflow\",\"function\":\"main\",\"block\":\"entry\",\"inst\":2}");
|
||||
try expectRun(comptime main1("u8", " %a = const.u8 200\n %b = const.u8 56\n %c = add.wrap.u8 %a, %b\n ret %c\n"), "{\"status\":\"returned\",\"value\":0}");
|
||||
try expectRun(comptime main1("i32", " %a = const.i32 -7\n %b = const.i32 2\n %c = rem.checked.i32 %a, %b\n ret %c\n"), "{\"status\":\"returned\",\"value\":-1}");
|
||||
try expectRun(comptime main1("i32", " %a = const.i32 -7\n %b = const.i32 2\n %c = div.checked.i32 %a, %b\n ret %c\n"), "{\"status\":\"returned\",\"value\":-3}");
|
||||
try expectRun(comptime main1("i8", " %a = const.i8 -128\n %b = const.i8 -1\n %c = div.checked.i8 %a, %b\n ret %c\n"), "{\"status\":\"trapped\",\"code\":\"T0003\",\"kind\":\"trap.overflow\",\"function\":\"main\",\"block\":\"entry\",\"inst\":2}");
|
||||
try expectRun(comptime main1("u32", " %a = const.u32 1\n %b = const.u32 0\n %c = div.checked.u32 %a, %b\n ret %c\n"), "{\"status\":\"trapped\",\"code\":\"T0004\",\"kind\":\"trap.division_by_zero\",\"function\":\"main\",\"block\":\"entry\",\"inst\":2}");
|
||||
try expectRun(comptime main1("u128", " %a = const.u128 0xffff_ffff_ffff_ffff_ffff_ffff_ffff_ffff\n ret %a\n"), "{\"status\":\"returned\",\"value\":340282366920938463463374607431768211455}");
|
||||
}
|
||||
|
||||
test "shifts and casts" {
|
||||
try expectRun(comptime main1("u8", " %a = const.u8 0x81\n %b = const.u8 1\n %c = shl.u8 %a, %b\n ret %c\n"), "{\"status\":\"returned\",\"value\":2}");
|
||||
try expectRun(comptime main1("i8", " %a = const.i8 -128\n %b = const.i8 7\n %c = shr.i8 %a, %b\n ret %c\n"), "{\"status\":\"returned\",\"value\":-1}");
|
||||
try expectRun(comptime main1("u8", " %a = const.u8 1\n %b = const.u8 8\n %c = shl.u8 %a, %b\n ret %c\n"), "{\"status\":\"trapped\",\"code\":\"T0005\",\"kind\":\"trap.shift_out_of_range\",\"function\":\"main\",\"block\":\"entry\",\"inst\":2}");
|
||||
try expectRun(comptime main1("u8", " %a = const.i32 -1\n %c = cast.wrap.u8 %a\n ret %c\n"), "{\"status\":\"returned\",\"value\":255}");
|
||||
try expectRun(comptime main1("i64", " %a = const.i8 -3\n %c = cast.widen.i64 %a\n ret %c\n"), "{\"status\":\"returned\",\"value\":-3}");
|
||||
try expectRun(comptime main1("u8", " %a = const.i32 300\n %c = cast.checked.u8 %a\n ret %c\n"), "{\"status\":\"trapped\",\"code\":\"T0006\",\"kind\":\"trap.cast_out_of_range\",\"function\":\"main\",\"block\":\"entry\",\"inst\":1}");
|
||||
try expectRun(comptime main1("i16", " %a = const.u8 255\n %c = cast.wrap.i16 %a\n ret %c\n"), "{\"status\":\"returned\",\"value\":255}");
|
||||
}
|
||||
|
||||
test "control flow, aggregates, calls and call depth" {
|
||||
try expectRun(h ++
|
||||
\\type @P = struct { x: i32, ok: bool }
|
||||
\\type @O = enum { None, Some(@P) }
|
||||
\\fn @main() -> @O
|
||||
\\effects []
|
||||
\\entry:
|
||||
\\ %n = const.u64 10
|
||||
\\ %r = call @sum(%n)
|
||||
\\ %t = cast.checked.i32 %r
|
||||
\\ %b = const.bool true
|
||||
\\ %p = struct.make @P(%t, %b)
|
||||
\\ %o = enum.make @O.Some(%p)
|
||||
\\ ret %o
|
||||
\\fn @sum(%n: u64) -> u64
|
||||
\\effects []
|
||||
\\entry:
|
||||
\\ %z = const.u64 0
|
||||
\\ br @loop(%z, %z)
|
||||
\\loop(%i: u64, %acc: u64):
|
||||
\\ %d = gt.u64 %i, %n
|
||||
\\ cbr %d, @exit(), @body()
|
||||
\\body:
|
||||
\\ %acc2 = add.checked.u64 %acc, %i
|
||||
\\ %one = const.u64 1
|
||||
\\ %i2 = add.checked.u64 %i, %one
|
||||
\\ br @loop(%i2, %acc2)
|
||||
\\exit:
|
||||
\\ ret %acc
|
||||
\\
|
||||
, "{\"status\":\"returned\",\"value\":{\"variant\":\"Some\",\"payload\":[{\"x\":55,\"ok\":true}]}}");
|
||||
|
||||
try expectRun(h ++
|
||||
\\fn @main() -> void
|
||||
\\effects []
|
||||
\\entry:
|
||||
\\ call @rec()
|
||||
\\ ret
|
||||
\\fn @rec() -> void
|
||||
\\effects []
|
||||
\\entry:
|
||||
\\ call @rec()
|
||||
\\ ret
|
||||
\\
|
||||
, "{\"status\":\"trapped\",\"code\":\"T0007\",\"kind\":\"trap.call_depth\",\"function\":\"rec\",\"block\":\"entry\",\"inst\":0}");
|
||||
}
|
||||
|
||||
test "block arguments bind simultaneously" {
|
||||
// Swapping through block parameters must not observe a half-updated state.
|
||||
try expectRun(h ++
|
||||
\\type @Pair = struct { a: u8, b: u8 }
|
||||
\\fn @main() -> @Pair
|
||||
\\effects []
|
||||
\\entry:
|
||||
\\ %x = const.u8 1
|
||||
\\ %y = const.u8 2
|
||||
\\ %k = const.u8 0
|
||||
\\ br @swap(%x, %y, %k)
|
||||
\\swap(%a: u8, %b: u8, %n: u8):
|
||||
\\ %one = const.u8 1
|
||||
\\ %done = eq.u8 %n, %one
|
||||
\\ cbr %done, @out(), @again()
|
||||
\\again:
|
||||
\\ br @swap(%b, %a, %one)
|
||||
\\out:
|
||||
\\ %p = struct.make @Pair(%a, %b)
|
||||
\\ ret %p
|
||||
\\
|
||||
, "{\"status\":\"returned\",\"value\":{\"a\":2,\"b\":1}}");
|
||||
}
|
||||
+17
-5
@@ -3,8 +3,9 @@
|
||||
//! Output contract (stable, consumed by agents):
|
||||
//! - diagnostics go to stdout as JSON Lines, one object per line;
|
||||
//! - human-oriented notes go to stderr;
|
||||
//! - exit codes: 0 accepted, 1 rejected, 2 usage or I/O error,
|
||||
//! 3 input not decidable yet (pipeline stage unimplemented).
|
||||
//! - `aji run` writes one run record (spec 07a "Run result") to stdout;
|
||||
//! - exit codes: 0 accepted / returned, 1 rejected, 2 usage or I/O error,
|
||||
//! 3 input not decidable yet (pipeline stage unimplemented), 4 trapped.
|
||||
|
||||
const std = @import("std");
|
||||
const aji = @import("aji");
|
||||
@@ -14,13 +15,14 @@ const usage =
|
||||
\\
|
||||
\\commands:
|
||||
\\ check <file.air> parse and verify a text AIR module
|
||||
\\ run <file.air> verify, then run @main and print the run record
|
||||
\\ codes list diagnostic codes as JSON Lines
|
||||
\\ explain <code> show one diagnostic code
|
||||
\\ version print toolchain version
|
||||
\\
|
||||
;
|
||||
|
||||
const ExitCode = enum(u8) { accepted = 0, rejected = 1, usage = 2, unimplemented = 3 };
|
||||
const ExitCode = enum(u8) { accepted = 0, rejected = 1, usage = 2, unimplemented = 3, trapped = 4 };
|
||||
|
||||
pub fn main(init: std.process.Init) !void {
|
||||
const arena = init.arena.allocator();
|
||||
@@ -59,8 +61,8 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
|
||||
try writeCode(out, c.*);
|
||||
return .accepted;
|
||||
}
|
||||
if (std.mem.eql(u8, cmd, "check")) {
|
||||
if (args.len != 3) return usageError("check takes exactly one file");
|
||||
if (std.mem.eql(u8, cmd, "check") or std.mem.eql(u8, cmd, "run")) {
|
||||
if (args.len != 3) return usageError("check and run take exactly one file");
|
||||
const path = args[2];
|
||||
const source = std.Io.Dir.cwd().readFileAlloc(io, path, arena, .limited(64 << 20)) catch |err| {
|
||||
std.debug.print("aji: cannot read {s}: {t}\n", .{ path, err });
|
||||
@@ -71,6 +73,16 @@ fn run(io: std.Io, arena: std.mem.Allocator, args: []const [:0]const u8, out: *s
|
||||
try d.writeJson(out);
|
||||
try out.writeByte('\n');
|
||||
}
|
||||
if (report.status == .accepted and std.mem.eql(u8, cmd, "run")) {
|
||||
const module = report.module.?;
|
||||
const outcome = aji.interp.run(arena, module, "main") catch |err| switch (err) {
|
||||
error.EntryNotFound => return usageError("module has no parameterless @main"),
|
||||
else => |e| return e,
|
||||
};
|
||||
try aji.interp.writeOutcome(module, outcome, out);
|
||||
try out.writeByte('\n');
|
||||
return if (outcome == .trapped) .trapped else .accepted;
|
||||
}
|
||||
return switch (report.status) {
|
||||
.accepted => .accepted,
|
||||
.rejected => .rejected,
|
||||
|
||||
@@ -8,6 +8,7 @@ Run with `zig build conformance` (add `-- --json` for JSON Lines, `-- --filter <
|
||||
tests/conformance/
|
||||
phase<N>/accept/*.air modules a conforming verifier MUST accept
|
||||
phase<N>/reject/*.air modules a conforming verifier MUST reject
|
||||
phase<N>/run/*.air modules that MUST verify and produce a given run record
|
||||
```
|
||||
|
||||
`phase<N>` is the implementation phase (`docs/plan/README.md`) that first requires the test to pass.
|
||||
@@ -18,11 +19,15 @@ Every test starts with a block of `;` lines. Keys:
|
||||
|
||||
| Key | Required | Value |
|
||||
|---|---|---|
|
||||
| `expect` | yes | `accept`, or `reject` followed by one or more codes from `docs/reference/error-codes.md` |
|
||||
| `expect` | yes | `accept`; `reject` followed by one or more codes from `docs/reference/error-codes.md`; or `run` |
|
||||
| `result` | with `run` | expected `value` of the run record as canonical JSON, e.g. `42` or `{"x":1}` |
|
||||
| `trap` | with `run` | expected trap code, e.g. `T0003` |
|
||||
| `phase` | yes | implementation phase number |
|
||||
| `spec` | yes | spec section that justifies the expectation |
|
||||
|
||||
Header lines without a `key:` are free-form description. The runner blanks the header before handing the module to the pipeline, so line numbers in diagnostics are unchanged and the AIR grammar does not need to know about test metadata.
|
||||
A key is a single lowercase word; header lines without a `key:` prefix are free-form description. The runner blanks the header before handing the module to the pipeline, so line numbers in diagnostics are unchanged and the AIR grammar does not need to know about test metadata.
|
||||
|
||||
`run` executes `@main` twice and fails if the two run records differ (spec 11); otherwise it compares the record with `result` or `trap`. Exactly one of them is required.
|
||||
|
||||
`reject` compares the exact multiset of error codes. Listing a code twice means two diagnostics with that code are expected.
|
||||
|
||||
@@ -32,11 +37,11 @@ Header lines without a `key:` are free-form description. The runner blanks the h
|
||||
|---|---|
|
||||
| `PASS` | result matches `expect` |
|
||||
| `FAIL` | result differs, or the header is malformed; the run exits 1 |
|
||||
| `PENDING` | the pipeline stage needed for this input is not implemented yet |
|
||||
| `PENDING` | the test's `phase` is later than `aji.implemented_phase`, or the pipeline stage it needs is not implemented yet |
|
||||
|
||||
## Rules
|
||||
|
||||
- Every verifier rule gets at least one `accept` and one `reject` test (AGENTS.md, testing policy).
|
||||
- One defect per `reject` test. A test that triggers several unrelated errors is hard to repair against.
|
||||
- Keep tests minimal: the smallest module that exhibits the rule.
|
||||
- Text AIR syntax in the seed tests is provisional until the text AIR grammar is specified (Phase 0, task P0-1). Update the tests when the grammar lands.
|
||||
- Tests are written in text AIR format 0 (`docs/reference/air-text.ebnf`) and start with the four-line module header.
|
||||
@@ -0,0 +1,212 @@
|
||||
; Checked and wrapping arithmetic on every integer type.
|
||||
; expect: accept
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.all_integer_widths
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f_u8(%a: u8, %b: u8) -> u8
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.u8 %a, %b
|
||||
%d = sub.wrap.u8 %s, %b
|
||||
%m = mul.wrap.u8 %d, %a
|
||||
%q = div.checked.u8 %m, %b
|
||||
%r = rem.checked.u8 %q, %b
|
||||
%x = xor.u8 %r, %a
|
||||
%l = shl.u8 %x, %b
|
||||
%c = lt.u8 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_u16(%a: u16, %b: u16) -> u16
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.u16 %a, %b
|
||||
%d = sub.wrap.u16 %s, %b
|
||||
%m = mul.wrap.u16 %d, %a
|
||||
%q = div.checked.u16 %m, %b
|
||||
%r = rem.checked.u16 %q, %b
|
||||
%x = xor.u16 %r, %a
|
||||
%l = shl.u16 %x, %b
|
||||
%c = lt.u16 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_u32(%a: u32, %b: u32) -> u32
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.u32 %a, %b
|
||||
%d = sub.wrap.u32 %s, %b
|
||||
%m = mul.wrap.u32 %d, %a
|
||||
%q = div.checked.u32 %m, %b
|
||||
%r = rem.checked.u32 %q, %b
|
||||
%x = xor.u32 %r, %a
|
||||
%l = shl.u32 %x, %b
|
||||
%c = lt.u32 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_u64(%a: u64, %b: u64) -> u64
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.u64 %a, %b
|
||||
%d = sub.wrap.u64 %s, %b
|
||||
%m = mul.wrap.u64 %d, %a
|
||||
%q = div.checked.u64 %m, %b
|
||||
%r = rem.checked.u64 %q, %b
|
||||
%x = xor.u64 %r, %a
|
||||
%l = shl.u64 %x, %b
|
||||
%c = lt.u64 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_u128(%a: u128, %b: u128) -> u128
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.u128 %a, %b
|
||||
%d = sub.wrap.u128 %s, %b
|
||||
%m = mul.wrap.u128 %d, %a
|
||||
%q = div.checked.u128 %m, %b
|
||||
%r = rem.checked.u128 %q, %b
|
||||
%x = xor.u128 %r, %a
|
||||
%l = shl.u128 %x, %b
|
||||
%c = lt.u128 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_usize(%a: usize, %b: usize) -> usize
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.usize %a, %b
|
||||
%d = sub.wrap.usize %s, %b
|
||||
%m = mul.wrap.usize %d, %a
|
||||
%q = div.checked.usize %m, %b
|
||||
%r = rem.checked.usize %q, %b
|
||||
%x = xor.usize %r, %a
|
||||
%l = shl.usize %x, %b
|
||||
%c = lt.usize %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_i8(%a: i8, %b: i8) -> i8
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.i8 %a, %b
|
||||
%d = sub.wrap.i8 %s, %b
|
||||
%m = mul.wrap.i8 %d, %a
|
||||
%q = div.checked.i8 %m, %b
|
||||
%r = rem.checked.i8 %q, %b
|
||||
%x = xor.i8 %r, %a
|
||||
%l = shl.i8 %x, %b
|
||||
%c = lt.i8 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_i16(%a: i16, %b: i16) -> i16
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.i16 %a, %b
|
||||
%d = sub.wrap.i16 %s, %b
|
||||
%m = mul.wrap.i16 %d, %a
|
||||
%q = div.checked.i16 %m, %b
|
||||
%r = rem.checked.i16 %q, %b
|
||||
%x = xor.i16 %r, %a
|
||||
%l = shl.i16 %x, %b
|
||||
%c = lt.i16 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_i32(%a: i32, %b: i32) -> i32
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.i32 %a, %b
|
||||
%d = sub.wrap.i32 %s, %b
|
||||
%m = mul.wrap.i32 %d, %a
|
||||
%q = div.checked.i32 %m, %b
|
||||
%r = rem.checked.i32 %q, %b
|
||||
%x = xor.i32 %r, %a
|
||||
%l = shl.i32 %x, %b
|
||||
%c = lt.i32 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_i64(%a: i64, %b: i64) -> i64
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.i64 %a, %b
|
||||
%d = sub.wrap.i64 %s, %b
|
||||
%m = mul.wrap.i64 %d, %a
|
||||
%q = div.checked.i64 %m, %b
|
||||
%r = rem.checked.i64 %q, %b
|
||||
%x = xor.i64 %r, %a
|
||||
%l = shl.i64 %x, %b
|
||||
%c = lt.i64 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_i128(%a: i128, %b: i128) -> i128
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.i128 %a, %b
|
||||
%d = sub.wrap.i128 %s, %b
|
||||
%m = mul.wrap.i128 %d, %a
|
||||
%q = div.checked.i128 %m, %b
|
||||
%r = rem.checked.i128 %q, %b
|
||||
%x = xor.i128 %r, %a
|
||||
%l = shl.i128 %x, %b
|
||||
%c = lt.i128 %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
|
||||
fn @f_isize(%a: isize, %b: isize) -> isize
|
||||
effects []
|
||||
entry:
|
||||
%s = add.checked.isize %a, %b
|
||||
%d = sub.wrap.isize %s, %b
|
||||
%m = mul.wrap.isize %d, %a
|
||||
%q = div.checked.isize %m, %b
|
||||
%r = rem.checked.isize %q, %b
|
||||
%x = xor.isize %r, %a
|
||||
%l = shl.isize %x, %b
|
||||
%c = lt.isize %l, %a
|
||||
cbr %c, @yes(), @no()
|
||||
yes:
|
||||
ret %l
|
||||
no:
|
||||
ret %a
|
||||
@@ -0,0 +1,33 @@
|
||||
; Recursive calls, void callees and never-returning functions.
|
||||
; expect: accept
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.recursion_and_void_calls
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @fact(%n: u64) -> u64
|
||||
effects []
|
||||
entry:
|
||||
%one = const.u64 1
|
||||
%base = le.u64 %n, %one
|
||||
cbr %base, @done(), @step()
|
||||
done:
|
||||
ret %one
|
||||
step:
|
||||
%m = sub.checked.u64 %n, %one
|
||||
%r = call @fact(%m)
|
||||
%p = mul.checked.u64 %n, %r
|
||||
ret %p
|
||||
|
||||
fn @check(%c: bool) -> void
|
||||
effects []
|
||||
entry:
|
||||
assert %c
|
||||
ret
|
||||
|
||||
fn @fail() -> never
|
||||
effects []
|
||||
entry:
|
||||
trap
|
||||
@@ -0,0 +1,38 @@
|
||||
; switch over integers and exhaustive enum.switch with payload binding.
|
||||
; expect: accept
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.switch_and_enums
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
type @Shape = enum { Empty, Square(u32), Rect(u32, u32) }
|
||||
|
||||
fn @area(%s: @Shape) -> u32
|
||||
effects []
|
||||
entry:
|
||||
enum.switch @Shape %s, Empty -> @empty, Square -> @square, Rect -> @rect
|
||||
empty:
|
||||
%z = const.u32 0
|
||||
ret %z
|
||||
square(%side: u32):
|
||||
%a = mul.checked.u32 %side, %side
|
||||
ret %a
|
||||
rect(%w: u32, %h: u32):
|
||||
%b = mul.checked.u32 %w, %h
|
||||
ret %b
|
||||
|
||||
fn @classify(%n: i8) -> u8
|
||||
effects []
|
||||
entry:
|
||||
switch.i8 %n, -1 -> @neg(), 0 -> @zero(), default -> @other()
|
||||
neg:
|
||||
%a = const.u8 0
|
||||
ret %a
|
||||
zero:
|
||||
%b = const.u8 1
|
||||
ret %b
|
||||
other:
|
||||
%c = const.u8 2
|
||||
ret %c
|
||||
@@ -0,0 +1,15 @@
|
||||
; Passes u16 to a block parameter of type u8.
|
||||
; expect: reject E1001
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.block_argument_type
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%a: u16) -> void
|
||||
effects []
|
||||
entry:
|
||||
br @next(%a)
|
||||
next(%x: u8):
|
||||
ret
|
||||
@@ -0,0 +1,15 @@
|
||||
; Branch passes two arguments to a block with one parameter.
|
||||
; expect: reject E5002
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.branch_arity
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%a: u8) -> u8
|
||||
effects []
|
||||
entry:
|
||||
br @next(%a, %a)
|
||||
next(%x: u8):
|
||||
ret %x
|
||||
@@ -0,0 +1,19 @@
|
||||
; Calls @g with an i32 where u32 is expected.
|
||||
; expect: reject E1001
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.call_argument_type
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%a: i32) -> void
|
||||
effects []
|
||||
entry:
|
||||
call @g(%a)
|
||||
ret
|
||||
|
||||
fn @g(%x: u32) -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
@@ -0,0 +1,15 @@
|
||||
; %a is defined twice.
|
||||
; expect: reject E5001
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.double_definition
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%a = const.u8 1
|
||||
%a = const.u8 2
|
||||
ret %a
|
||||
@@ -0,0 +1,18 @@
|
||||
; Two functions named @f.
|
||||
; expect: reject E0003
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.duplicate_function
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f() -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
|
||||
fn @f() -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
@@ -0,0 +1,13 @@
|
||||
; f64 is reserved in format version 0.
|
||||
; expect: reject E0006
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.float_reserved
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%x: f64) -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
@@ -0,0 +1,14 @@
|
||||
; 256 does not fit in u8.
|
||||
; expect: reject E0002
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.literal_out_of_range
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%a = const.u8 256
|
||||
ret %a
|
||||
@@ -0,0 +1,14 @@
|
||||
; widen from i8 to u64 loses negative values.
|
||||
; expect: reject E1002
|
||||
; phase: 0
|
||||
; spec: docs/spec/02-types-and-values.md
|
||||
module test.lossy_widen
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%a: i8) -> u64
|
||||
effects []
|
||||
entry:
|
||||
%b = cast.widen.u64 %a
|
||||
ret %b
|
||||
@@ -0,0 +1,17 @@
|
||||
; enum.switch misses variant B and has no default.
|
||||
; expect: reject E5002
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.non_exhaustive_enum_switch
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
type @E = enum { A, B }
|
||||
|
||||
fn @f(%e: @E) -> void
|
||||
effects []
|
||||
entry:
|
||||
enum.switch @E %e, A -> @x
|
||||
x:
|
||||
ret
|
||||
@@ -0,0 +1,15 @@
|
||||
; A struct contains itself by value.
|
||||
; expect: reject E1003
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.recursive_type
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
type @Node = struct { value: u8, next: @Node }
|
||||
|
||||
fn @f() -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
@@ -0,0 +1,13 @@
|
||||
; Returns u64 from a function declared to return u32.
|
||||
; expect: reject E1001
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.return_type_mismatch
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%a: u64) -> u32
|
||||
effects []
|
||||
entry:
|
||||
ret %a
|
||||
@@ -0,0 +1,14 @@
|
||||
; Missing comma between operands.
|
||||
; expect: reject E0001
|
||||
; phase: 0
|
||||
; spec: docs/reference/air-text.ebnf
|
||||
module test.syntax_error
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%a: u8) -> u8
|
||||
effects []
|
||||
entry:
|
||||
%b = add.checked.u8 %a %a
|
||||
ret %b
|
||||
@@ -0,0 +1,14 @@
|
||||
; A terminator is followed by another instruction.
|
||||
; expect: reject E5002
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.terminator_mid_block
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f() -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
ret
|
||||
@@ -0,0 +1,13 @@
|
||||
; 65 parameters exceed the limit of 64.
|
||||
; expect: reject E5101
|
||||
; phase: 0
|
||||
; spec: docs/spec/08-verifier.md
|
||||
module test.too_many_params
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%p0: u8, %p1: u8, %p2: u8, %p3: u8, %p4: u8, %p5: u8, %p6: u8, %p7: u8, %p8: u8, %p9: u8, %p10: u8, %p11: u8, %p12: u8, %p13: u8, %p14: u8, %p15: u8, %p16: u8, %p17: u8, %p18: u8, %p19: u8, %p20: u8, %p21: u8, %p22: u8, %p23: u8, %p24: u8, %p25: u8, %p26: u8, %p27: u8, %p28: u8, %p29: u8, %p30: u8, %p31: u8, %p32: u8, %p33: u8, %p34: u8, %p35: u8, %p36: u8, %p37: u8, %p38: u8, %p39: u8, %p40: u8, %p41: u8, %p42: u8, %p43: u8, %p44: u8, %p45: u8, %p46: u8, %p47: u8, %p48: u8, %p49: u8, %p50: u8, %p51: u8, %p52: u8, %p53: u8, %p54: u8, %p55: u8, %p56: u8, %p57: u8, %p58: u8, %p59: u8, %p60: u8, %p61: u8, %p62: u8, %p63: u8, %p64: u8) -> void
|
||||
effects []
|
||||
entry:
|
||||
ret
|
||||
@@ -0,0 +1,13 @@
|
||||
; Branch to a label that does not exist.
|
||||
; expect: reject E5002
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.unknown_block
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f() -> void
|
||||
effects []
|
||||
entry:
|
||||
br @nowhere()
|
||||
@@ -0,0 +1,14 @@
|
||||
; frobnicate is not an AIR opcode; %a is then never defined.
|
||||
; expect: reject E0004 E5001
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.unknown_opcode
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%a = frobnicate.u8 1
|
||||
ret %a
|
||||
@@ -0,0 +1,8 @@
|
||||
; AIR format version 7 is not implemented.
|
||||
; expect: reject E0005
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.unsupported_version
|
||||
air 7
|
||||
lang 0.1
|
||||
features []
|
||||
@@ -0,0 +1,20 @@
|
||||
; %x is defined on one path only but used after the join.
|
||||
; expect: reject E5001
|
||||
; phase: 0
|
||||
; spec: docs/spec/07-air.md
|
||||
module test.use_not_dominated
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @f(%c: bool) -> u8
|
||||
effects []
|
||||
entry:
|
||||
cbr %c, @a(), @b()
|
||||
a:
|
||||
%x = const.u8 1
|
||||
br @join()
|
||||
b:
|
||||
br @join()
|
||||
join:
|
||||
ret %x
|
||||
@@ -0,0 +1,21 @@
|
||||
; Structs and enums are rendered in the run record.
|
||||
; expect: run
|
||||
; result: {"variant":"Err","payload":[{"code":404,"fatal":false}]}
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.aggregates
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
type @Error = struct { code: u16, fatal: bool }
|
||||
type @Result = enum { Ok(u32), Err(@Error) }
|
||||
|
||||
fn @main() -> @Result
|
||||
effects []
|
||||
entry:
|
||||
%code = const.u16 404
|
||||
%fatal = const.bool false
|
||||
%e = struct.make @Error(%code, %fatal)
|
||||
%r = enum.make @Result.Err(%e)
|
||||
ret %r
|
||||
@@ -0,0 +1,17 @@
|
||||
; shr on signed values is arithmetic.
|
||||
; expect: run
|
||||
; result: -1
|
||||
; phase: 0
|
||||
; spec: docs/spec/02-types-and-values.md
|
||||
module test.arithmetic_shift
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> i16
|
||||
effects []
|
||||
entry:
|
||||
%a = const.i16 -32768
|
||||
%b = const.i16 15
|
||||
%c = shr.i16 %a, %b
|
||||
ret %c
|
||||
@@ -0,0 +1,16 @@
|
||||
; A false assertion traps.
|
||||
; expect: run
|
||||
; trap: T0002
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.assert_failure
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> void
|
||||
effects []
|
||||
entry:
|
||||
%f = const.bool false
|
||||
assert %f
|
||||
ret
|
||||
@@ -0,0 +1,21 @@
|
||||
; Unbounded recursion hits the call-depth limit.
|
||||
; expect: run
|
||||
; trap: T0007
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.call_depth
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> void
|
||||
effects []
|
||||
entry:
|
||||
call @down()
|
||||
ret
|
||||
|
||||
fn @down() -> void
|
||||
effects []
|
||||
entry:
|
||||
call @down()
|
||||
ret
|
||||
@@ -0,0 +1,16 @@
|
||||
; Checked cast of 300 to u8 traps.
|
||||
; expect: run
|
||||
; trap: T0006
|
||||
; phase: 0
|
||||
; spec: docs/spec/02-types-and-values.md
|
||||
module test.checked_cast
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%a = const.u32 300
|
||||
%b = cast.checked.u8 %a
|
||||
ret %b
|
||||
@@ -0,0 +1,17 @@
|
||||
; u8 200 + 56 overflows.
|
||||
; expect: run
|
||||
; trap: T0003
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.checked_overflow
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%a = const.u8 200
|
||||
%b = const.u8 56
|
||||
%c = add.checked.u8 %a, %b
|
||||
ret %c
|
||||
@@ -0,0 +1,17 @@
|
||||
; Division by zero traps.
|
||||
; expect: run
|
||||
; trap: T0004
|
||||
; phase: 0
|
||||
; spec: docs/spec/02-types-and-values.md
|
||||
module test.division_by_zero
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> i32
|
||||
effects []
|
||||
entry:
|
||||
%a = const.i32 1
|
||||
%b = const.i32 0
|
||||
%c = div.checked.i32 %a, %b
|
||||
ret %c
|
||||
@@ -0,0 +1,14 @@
|
||||
; The trap instruction traps.
|
||||
; expect: run
|
||||
; trap: T0001
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.explicit_trap
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> void
|
||||
effects []
|
||||
entry:
|
||||
trap
|
||||
@@ -0,0 +1,27 @@
|
||||
; Iterative Fibonacci with loop-carried block parameters.
|
||||
; expect: run
|
||||
; result: 2880067194370816120
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.fibonacci
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> u64
|
||||
effects []
|
||||
entry:
|
||||
%n = const.u64 90
|
||||
%z = const.u64 0
|
||||
%o = const.u64 1
|
||||
br @loop(%z, %z, %o)
|
||||
loop(%i: u64, %a: u64, %b: u64):
|
||||
%d = ge.u64 %i, %n
|
||||
cbr %d, @done(), @step()
|
||||
step:
|
||||
%c = add.checked.u64 %a, %b
|
||||
%one = const.u64 1
|
||||
%j = add.checked.u64 %i, %one
|
||||
br @loop(%j, %b, %c)
|
||||
done:
|
||||
ret %a
|
||||
@@ -0,0 +1,15 @@
|
||||
; Phase 0 hello: @main returns a value that the run record reports (Q-008).
|
||||
; expect: run
|
||||
; result: 42
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.hello
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> u32
|
||||
effects []
|
||||
entry:
|
||||
%v = const.u32 42
|
||||
ret %v
|
||||
@@ -0,0 +1,17 @@
|
||||
; Shift by the bit width traps.
|
||||
; expect: run
|
||||
; trap: T0005
|
||||
; phase: 0
|
||||
; spec: docs/spec/02-types-and-values.md
|
||||
module test.shift_out_of_range
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> u32
|
||||
effects []
|
||||
entry:
|
||||
%a = const.u32 1
|
||||
%b = const.u32 32
|
||||
%c = shl.u32 %a, %b
|
||||
ret %c
|
||||
@@ -0,0 +1,17 @@
|
||||
; i64 MIN / -1 traps as overflow.
|
||||
; expect: run
|
||||
; trap: T0003
|
||||
; phase: 0
|
||||
; spec: docs/spec/02-types-and-values.md
|
||||
module test.signed_min_div
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> i64
|
||||
effects []
|
||||
entry:
|
||||
%a = const.i64 -9223372036854775808
|
||||
%b = const.i64 -1
|
||||
%c = div.checked.i64 %a, %b
|
||||
ret %c
|
||||
@@ -0,0 +1,17 @@
|
||||
; Remainder has the sign of the dividend.
|
||||
; expect: run
|
||||
; result: -1
|
||||
; phase: 0
|
||||
; spec: docs/spec/02-types-and-values.md
|
||||
module test.truncating_remainder
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> i32
|
||||
effects []
|
||||
entry:
|
||||
%a = const.i32 -7
|
||||
%b = const.i32 3
|
||||
%c = rem.checked.i32 %a, %b
|
||||
ret %c
|
||||
@@ -0,0 +1,17 @@
|
||||
; Wrapping add does not trap.
|
||||
; expect: run
|
||||
; result: 0
|
||||
; phase: 0
|
||||
; spec: docs/spec/07a-air-instructions.md
|
||||
module test.wrapping_add
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%a = const.u8 200
|
||||
%b = const.u8 56
|
||||
%c = add.wrap.u8 %a, %b
|
||||
ret %c
|
||||
@@ -0,0 +1,16 @@
|
||||
; Wrapping cast keeps the low bits of the two's-complement value.
|
||||
; expect: run
|
||||
; result: 254
|
||||
; phase: 0
|
||||
; spec: docs/spec/02-types-and-values.md
|
||||
module test.wrapping_cast
|
||||
air 0
|
||||
lang 0.1
|
||||
features []
|
||||
|
||||
fn @main() -> u8
|
||||
effects []
|
||||
entry:
|
||||
%a = const.i32 -2
|
||||
%b = cast.wrap.u8 %a
|
||||
ret %b
|
||||
+71
-2
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user