From 6be79eda61b3c64b38528c023845991a0cb4a274 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Sat, 15 Jun 2024 21:06:27 -0400 Subject: recursion; list via support.c --- support.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) (limited to 'support.c') diff --git a/support.c b/support.c index cc0304f..44a39e4 100644 --- a/support.c +++ b/support.c @@ -1,5 +1,6 @@ #include #include +#include extern int32_t sp; extern int32_t stack; @@ -9,6 +10,11 @@ void emit() putchar(*(&stack + --sp)); } +void print() +{ + printf("%d\n", *(&stack + --sp)); +} + void sub() { int32_t *st = &stack; @@ -33,3 +39,33 @@ void eq() --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]); +} + -- cgit v1.2.3