39 lines
698 B
C
39 lines
698 B
C
#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);
|
|
}
|