Skip to content

Instantly share code, notes, and snippets.

@stesla
Created September 14, 2010 07:01
Show Gist options
  • Save stesla/578655 to your computer and use it in GitHub Desktop.
Save stesla/578655 to your computer and use it in GitHub Desktop.
/*
* Lists
*/
struct cons {
ref_t car, cdr;
};
ref_t make_cons(ref_t car, ref_t cdr) {
struct cons *ptr = safe_malloc(sizeof(struct cons));
ptr->car = car, ptr->cdr = cdr;
return ((ref_t) ptr) | LIST_MASK;
}
ref_t car(ref_t obj) {
assert(islist(obj));
return ((struct cons *) (obj ^ LIST_MASK))->car;
}
ref_t cdr(ref_t obj) {
assert(islist(obj));
return ((struct cons *) (obj ^ LIST_MASK))->cdr;
}
static void printlist(ref_t obj) {
ref_t thecar = car(obj);
ref_t thecdr = cdr(obj);
print(thecar);
if (isnil(thecdr))
;
else if (islist(thecdr)) {
putchar(' ');
printlist(thecdr);
} else {
printf(" . ");
print(thecdr);
}
}
void print(ref_t obj) {
if (isnil(obj))
printf("nil");
else if (istrue(obj))
printf("true");
else if (isfixnum(obj))
printf("%i", fixnum_to_int(obj));
else if (islist(obj)) {
putchar('(');
printlist(obj);
putchar(')');
}
else
printf("0x%x", obj);
}
void println(ref_t obj) {
print(obj);
puts("");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment