aboutsummaryrefslogtreecommitdiffstats
path: root/support.c
blob: 44b45838521a5d03296ffc04d9829e3c82841fce (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
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>

extern int32_t sp;
extern int32_t stack;

void emit()
{
    putchar(*(&stack + --sp));
}

void print()
{
    printf("%d\n", *(&stack + --sp));
}

void sub()
{
    int32_t *st = &stack;
    st[sp - 2] -= st[sp - 1];
    --sp;
}

void cswap()
{
    int32_t *st = &stack;
    --sp;
    if (st[sp]) {
        int32_t tmp = st[sp - 1];
        st[sp - 1] = st[sp - 2];
        st[sp - 2] = tmp;
    }
}

void eq()
{
    int32_t *st = &stack;
    --sp;
    st[sp - 1] = st[sp - 1] == st[sp];
}

void cons()
{
    int32_t *st = &stack;
    int32_t *pair = (int32_t *)malloc(2 * sizeof(int32_t));
    --sp;
    pair[0] = st[sp];
    pair[1] = st[sp - 1];
    st[sp - 1] = (int32_t)pair;
}

void car()
{
    int32_t *st = &stack;
    st[sp - 1] = ((int32_t *)st[sp - 1])[0];
}

void cdr()
{
    int32_t *st = &stack;
    st[sp - 1] = ((int32_t *)st[sp - 1])[1];
}

void decons()
{
    int32_t *st = &stack;
    --sp;
    free((int32_t *)st[sp]);
}