Created
September 14, 2010 07:01
-
-
Save stesla/578655 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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