first evaluator draft

This commit is contained in:
Lobo 2026-01-07 09:40:21 -03:00
parent 9fc5f10fc6
commit 2532dd9f4a
8 changed files with 399 additions and 43 deletions

29
print.c
View file

@ -1,6 +1,7 @@
#include "inttypes.h"
#include "wscm.h"
#include <stdio.h>
#include <inttypes.h>
void print(O obj);
@ -21,10 +22,10 @@ void printcons(O obj) {
if (!f)
printf(" ");
f = 0;
print(p->head);
c = p->tail;
print(p->car);
c = p->cdr;
}
if (c != NIL && !IMM(c)) {
if (c != NIL) {
printf(" . ");
print(c);
}
@ -38,20 +39,28 @@ void print(O obj) {
printf("%" PRIdPTR, ORD(obj));
} else {
void *x = (void *)UNBOX(obj);
if (((const U8 *)x) >= heap.from.start && ((const U8 *)x) < heap.from.end) {
switch (TYPE(obj)) {
case TAG_SYM: {
S *s = (S *)((U)x & ~7);
printf("%.*s", (int)s->len, s->data);
break;
}
case TAG_PRIM: {
P *p = (P *)((U)x & ~7);
printf("<#primitive %s>", p->name);
break;
}
default: {
H *h = (H *)x;
switch (h->type) {
case OBJ_CONS:
printcons(obj);
break;
default:
printf("<obj type=%" PRIdPTR " @ %p>", h->type, (void *)h);
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);
}
}
}
}