reimplementation (oupsi)

This commit is contained in:
Lobo 2026-01-10 10:03:31 -03:00
parent 0572264f76
commit 1aec6085d9
27 changed files with 1213 additions and 21 deletions

70
src/print.c Normal file
View file

@ -0,0 +1,70 @@
#include <inttypes.h>
#include <stdio.h>
#include <wolflisp.h>
void print(O obj);
void print_pair(O obj) {
O c = obj;
I f = 1;
printf("(");
while (c != NIL && !IMM(c)) {
Gh *h = UNBOX(c);
if (h->type != TYPE_PAIR) {
printf(" . ");
print(c);
printf(")");
return;
}
Pa *p = (Pa *)(h + 1);
if (!f)
printf(" ");
f = 0;
print(p->head);
c = p->tail;
}
if (c != NIL) {
printf(" . ");
print(c);
}
printf(")");
}
void print(O obj) {
if (obj == NIL) {
printf("()");
} else if (IMM(obj)) {
printf("%" PRIdPTR, ORD(obj));
} else {
switch (TAG_OF(obj)) {
case TAG_SYM: {
Sy *s = (Sy *)UNTAG(obj);
printf("%.*s", (int)s->len, s->data);
break;
}
case TAG_PRIM: {
Pr *p = (Pr *)UNTAG(obj);
printf("<#primitive %s>", p->name);
break;
}
default: {
Gh *h = UNBOX(obj);
switch (h->type) {
case TYPE_PAIR:
print_pair(obj);
break;
default:
printf("<#obj type=%" PRId32 " @ %p>", h->type, (void *)h);
break;
}
}
}
}
}
void println(O obj) {
print(obj);
putchar('\n');
}