const std = @import("std"); const gc = @import("../gc.zig"); const value = @import("../value.zig"); const ptr = @import("ptr.zig"); const PairPool = gc.PairPool; const Value = value.Value; pub const PairPtr = *align(8) Pair; pub const Pair = struct { car: Value, cdr: Value, }; // Zig API pub fn check(v: Value) ?PairPtr { return v.getPtr(.pair); } pub fn assert(v: Value) PairPtr { return check(v) orelse { v.dump(); @panic("not pair"); }; } pub fn unpack(v: Value) PairPtr { return assert(v); } pub fn consInPool(pool: *PairPool, car: Value, cdr: Value) !Value { const pair = try pool.cons(car, cdr); return ptr.pack(.pair, pair); } // Zisp API pub fn pred(v: Value) Value { return value.boole.pack(check(v) != null); } pub fn cons(car: Value, cdr: Value) Value { const pool = gc.mainPairPool(); return consInPool(pool, car, cdr) catch @panic("OOM"); // TODO } pub fn getCar(v: Value) Value { return unpack(v).car; } pub fn getCdr(v: Value) Value { return unpack(v).cdr; } pub fn setCar(v: Value, new: Value) void { unpack(v).car = new; } pub fn setCdr(v: Value, new: Value) void { unpack(v).cdr = new; }