aboutsummaryrefslogtreecommitdiffstats
path: root/support.c
blob: b400a056e34ebb16d1e5e4caa40f5d54cd715529 (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
72
73
74
75
76
77
78
#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 alloc()
{
    int32_t *st = &stack;
    st[sp - 1] = (int32_t)malloc(st[sp - 1] * sizeof(int32_t));
}

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

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

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

void dumpstack()
{
    int32_t *st = &stack;
    printf("<stack> ");
    for (int i = 0; i < sp; ++i)
        printf("%d ", st[i]);
    putchar('\n');
}