gc and cons pairs

This commit is contained in:
Lobo 2026-01-06 13:03:59 -03:00
parent d64b0f0a6f
commit fe9a8a7039
8 changed files with 217 additions and 10 deletions

39
object.c Normal file
View file

@ -0,0 +1,39 @@
#include <stdio.h>
#include <stdlib.h>
#include "inttypes.h"
#include "wscm.h"
// cons lists
O cons(O head, O tail) {
I mark = rootmark();
addroot(&head);
addroot(&tail);
const Z sz = sizeof(H) + sizeof(C);
H *h = alloc(sz);
h->size = sz;
h->type = OBJ_CONS;
C *c = (C *)(h + 1);
c->head = head;
c->tail = tail;
rootreset(mark);
return BOX(h);
}
C *uncons(O obj) {
if (obj == NIL)
return NULL;
if (IMM(obj)) {
fprintf(stderr, "unpair: expected pair, got integer\n");
abort();
}
H *h = UNBOX(obj);
if (h->type != OBJ_CONS) {
fprintf(stderr, "unpair: expected pair, got type %" PRIdPTR "\n", h->type);
abort();
}
return (C *)(h + 1);
}