summaryrefslogtreecommitdiff
path: root/src/zisp/value/pair.zig
blob: ec7e69b1f8ce39f820fda0cfc4124cffd3f00122 (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
const std = @import("std");

const value = @import("../value.zig");
const gc = @import("../gc.zig");

const ptr = @import("ptr.zig");

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 @ptrCast(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);
}

// Zisp API

pub fn pred(v: Value) Value {
    return value.boole.pack(check(v) != null);
}

pub fn cons(v1: Value, v2: Value) Value {
    const pair = gc.makePair(v1, v2);
    return ptr.pack(@ptrCast(pair), .pair);
}

pub fn car(v: Value) Value {
    return unpack(v).car;
}

pub fn cdr(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;
}