prim.c 1.94 KB
Newer Older
1
2
3
4
#include <string.h>

#include "prim.h"
#include "desc.h"
5
#include "thunk.h"
6
7
#include "mem.h"

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
8
9
#define arg(idx) stack_a[stack_top_a - idx]

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
10
struct Thunk* __add(Thunk* target) {
11
    return updateI(target, readI(arg(2)) + readI(arg(1)));
12
13
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
14
struct Thunk* __sub(Thunk* target) {
15
    return updateI(target, readI(arg(2)) - readI(arg(1)));
16
17
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
18
struct Thunk* __gt(Thunk* target) {
19
    return updateB(target, readI(arg(2)) > readI(arg(1)));
20
21
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
22
struct Thunk* __lt(Thunk* target) {
23
    return updateB(target, readI(arg(2)) < readI(arg(1)));
24
25
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
26
struct Thunk* __eqI(Thunk* target) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
27
28
    Thunk* arg1 = arg(2);
    Thunk* arg2 = arg(1);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
29
30

    return updateB(target, readI(arg1) == readI(arg2));
31
32
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
33
struct Thunk* __eqB(Thunk* target) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
34
35
    Thunk* arg1 = arg(2);
    Thunk* arg2 = arg(1);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
36
37

    return updateB(target, readB(arg1) == readB(arg2));
38
39
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
40
struct Thunk* __not(Thunk* target) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
41
    Thunk* arg1 = arg(1);
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
42
43

    return updateB(target, !readB(arg1));
44
45
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
46
void add_prim(int arity, int strictness, char* name, Thunk* (*exec)(Thunk*)) {
Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
    int nameLength = strlen(name);

    // before the PrimEntry there are "arity" number of SliceEntries
    SliceEntry* entry_base = (SliceEntry*) alloc_desc(sizeof (SliceEntry) * arity + sizeof (PrimEntry) + nameLength + 1);

    PrimEntry* entry = (PrimEntry*) (entry_base + arity);
    entry->base.type = FT_PRIM;
    entry->base.arity = arity;
    entry->strictness = strictness;
    entry->exec = exec;

    // TODO: should it be copied at all?
    memcpy(entry->name, name, nameLength);
    entry->name[nameLength] = '\0';

    // generate slices. avoid function call if arity is zero
    if (arity > 0) gen_slices(entry_base, (Desc*) entry, arity);

    add_desc(entry->name, (Desc*) entry);
66
67
}

Laszlo Domoszlai's avatar
Laszlo Domoszlai committed
68
69
70
71
72
73
74
75
void init_prim() {
    add_prim(2, 3, "add", &__add);
    add_prim(2, 3, "sub", &__sub);
    add_prim(2, 3, "gt", &__gt);
    add_prim(2, 3, "lt", &__lt);
    add_prim(2, 3, "eqI", &__eqI);
    add_prim(2, 3, "eqB", &__eqB);
    add_prim(1, 1, "not", &__not);
76
}