You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

72 lines
989 B
C

#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 = 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]);
}