Reader
This commit is contained in:
77
src/lisp.c
Normal file
77
src/lisp.c
Normal file
@ -0,0 +1,77 @@
|
||||
#include "lisp.h"
|
||||
|
||||
void lisp_init() {
|
||||
Qnil = Fmake_symbol(LISP_LITSTR("nil"));
|
||||
Qt = Fmake_symbol(LISP_LITSTR("t"));
|
||||
Qunbound = Fmake_symbol(LISP_LITSTR("unbound"));
|
||||
}
|
||||
|
||||
void lisp_shutdown() {}
|
||||
|
||||
void debug_print(FILE *file, LispVal *obj) {
|
||||
switch (TYPE_OF(obj)) {
|
||||
case TYPE_FIXNUM:
|
||||
fprintf(file, "%jd", (intmax_t) XFIXNUM(obj));
|
||||
break;
|
||||
case TYPE_FLOAT:
|
||||
fprintf(file, "%f", (double) XLISP_FLOAT(obj));
|
||||
break;
|
||||
case TYPE_STRING: {
|
||||
LispString *s = obj;
|
||||
fputc('"', file);
|
||||
fwrite(s->data, 1, s->length, file);
|
||||
fputc('"', file);
|
||||
break;
|
||||
}
|
||||
case TYPE_SYMBOL: {
|
||||
LispString *name = ((LispSymbol *) obj)->name;
|
||||
fwrite(name->data, 1, name->length, file);
|
||||
break;
|
||||
}
|
||||
case TYPE_HASH_TABLE: {
|
||||
fprintf(file, "<hash-table count=%zu at 0x%jx>",
|
||||
((LispHashTable *) obj)->count, (uintmax_t) obj);
|
||||
break;
|
||||
}
|
||||
case TYPE_FUNCTION: {
|
||||
fprintf(file, "<function at 0x%jx>", (uintmax_t) obj);
|
||||
break;
|
||||
}
|
||||
case TYPE_CONS: {
|
||||
fputc('(', file);
|
||||
FOREACH_TAIL(obj, tail) {
|
||||
if (CONSP(tail)) {
|
||||
debug_print(file, XCAR(tail));
|
||||
if (!NILP(XCDR(tail))) {
|
||||
fputc(' ', file);
|
||||
}
|
||||
} else {
|
||||
fwrite(". ", 1, 2, file);
|
||||
debug_print(file, tail);
|
||||
}
|
||||
}
|
||||
fputc(')', file);
|
||||
break;
|
||||
}
|
||||
case TYPE_VECTOR: {
|
||||
LispVector *v = obj;
|
||||
fputc('[', file);
|
||||
for (size_t i = 0; i < v->length; ++i) {
|
||||
debug_print(file, v->data[i]);
|
||||
if (i < v->length - 1) {
|
||||
fputc(' ', file);
|
||||
}
|
||||
}
|
||||
fputc(']', file);
|
||||
break;
|
||||
}
|
||||
default:
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void debug_obj_info(FILE *file, LispVal *obj) {
|
||||
fprintf(file, "%s -> ", LISP_TYPE_NAMES[TYPE_OF(obj)]);
|
||||
debug_print(file, obj);
|
||||
fputc('\n', file);
|
||||
}
|
||||
Reference in New Issue
Block a user