move list impl to forsp

main
Clyne 6 months ago
parent d832f781db
commit c546646cd4
Signed by: clyne
GPG Key ID: 1B74EE6C49C96795

@ -40,32 +40,39 @@ void eq()
st[sp - 1] = st[sp - 1] == st[sp]; st[sp - 1] = st[sp - 1] == st[sp];
} }
void cons() void alloc()
{
int32_t *st = &stack;
st[sp - 1] = (int32_t)malloc(st[sp - 1] * sizeof(int32_t));
}
void dealloc()
{ {
int32_t *st = &stack; int32_t *st = &stack;
int32_t *pair = (int32_t *)malloc(2 * sizeof(int32_t));
--sp; --sp;
pair[0] = st[sp]; free((int32_t *)st[sp]);
pair[1] = st[sp - 1];
st[sp - 1] = (int32_t)pair;
} }
void car() void peek()
{ {
int32_t *st = &stack; int32_t *st = &stack;
st[sp - 1] = ((int32_t *)st[sp - 1])[0]; st[sp - 1] = *(int32_t *)st[sp - 1];
} }
void cdr() void poke()
{ {
int32_t *st = &stack; int32_t *st = &stack;
st[sp - 1] = ((int32_t *)st[sp - 1])[1]; sp -= 2;
int32_t *ptr = (int32_t *)st[sp + 1];
*ptr = st[sp];
} }
void decons() void dumpstack()
{ {
int32_t *st = &stack; int32_t *st = &stack;
--sp; printf("<stack> ");
free((int32_t *)st[sp]); for (int i = 0; i < sp; ++i)
printf("%d ", st[i]);
putchar('\n');
} }

@ -11,6 +11,14 @@
($x x) $force ($x x) $force
(10 emit) $cr (10 emit) $cr
(print) $print (print) $print
(4 +) $cell+
($a $d 2 alloc $p
^a ^p poke
^d ^p cell+ poke
^p
) $cons
(peek) $car
(cell+ peek) $cdr
; if-stmt ; if-stmt
($c $t $f c ^f ^t rot cswap $_ force) $if ($c $t $f c ^f ^t rot cswap $_ force) $if

Loading…
Cancel
Save