62 lines
1.1 KiB
C
62 lines
1.1 KiB
C
#include "inttypes.h"
|
|
#include "wscm.h"
|
|
#include <stdio.h>
|
|
|
|
void print(O obj);
|
|
|
|
void printcons(O obj) {
|
|
O c = obj;
|
|
I f = 1;
|
|
|
|
printf("(");
|
|
while (c != NIL && !IMM(c)) {
|
|
H *h = UNBOX(c);
|
|
if (h->type != OBJ_CONS) {
|
|
printf(" . ");
|
|
print(c);
|
|
printf(")");
|
|
return;
|
|
}
|
|
C *p = (C *)(h + 1);
|
|
if (!f)
|
|
printf(" ");
|
|
f = 0;
|
|
print(p->head);
|
|
c = p->tail;
|
|
}
|
|
if (c != NIL && !IMM(c)) {
|
|
printf(" . ");
|
|
print(c);
|
|
}
|
|
printf(")");
|
|
}
|
|
|
|
void print(O obj) {
|
|
if (obj == NIL) {
|
|
printf("()");
|
|
} else if (IMM(obj)) {
|
|
printf("%" PRIdPTR, ORD(obj));
|
|
} else {
|
|
void *x = (void *)UNBOX(obj);
|
|
if (((const U8 *)x) >= heap.from.start && ((const U8 *)x) < heap.from.end) {
|
|
H *h = (H *)x;
|
|
switch (h->type) {
|
|
case OBJ_CONS:
|
|
printcons(obj);
|
|
break;
|
|
default:
|
|
printf("<obj type=%" PRIdPTR " @ %p>", h->type, (void *)h);
|
|
break;
|
|
}
|
|
} else {
|
|
// If pointer is outside the heap, it's a symbol.
|
|
S *s = (S *)x;
|
|
printf("%.*s", (int)s->len, s->data);
|
|
}
|
|
}
|
|
}
|
|
|
|
void println(O obj) {
|
|
print(obj);
|
|
putchar('\n');
|
|
}
|