blob: 4ae38a3c818443d74e302e9876103c43b7866ee3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
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;
}
|