Files
simple-lisp/src/main.c
T

124 lines
3.7 KiB
C
Raw Normal View History

2025-06-28 16:47:23 +09:00
#include "lisp.h"
#include "read.h"
2025-06-30 23:29:02 +09:00
static int exit_status = 0;
LispVal *Ftoplevel_exit_handler(LispVal *except);
static LispFunction _Ftoplevel_exit_handler_function = {
.type = TYPE_FUNCTION,
2025-09-10 02:57:48 -07:00
.is_builtin = true,
.is_macro = false,
.builtin = (lisp_function_ptr_t) &Ftoplevel_exit_handler,
2025-07-03 01:36:25 +09:00
.args = Qnil,
.kwargs = Qnil,
2025-07-03 02:43:12 +09:00
.rargs = Qnil,
.oargs = Qnil,
.rest_arg = Qnil,
2025-06-30 23:29:02 +09:00
.lexenv = Qnil,
};
#define Ftoplevel_exit_handler_function \
LISPVAL(&_Ftoplevel_exit_handler_function)
LispVal *Ftoplevel_exit_handler(LispVal *except) {
2025-09-10 02:57:48 -07:00
LispVal *detail = TAIL(HEAD(except));
if (NILP(detail) || NILP(HEAD(detail))) {
2025-06-30 23:29:02 +09:00
exit_status = 0;
2025-09-10 02:57:48 -07:00
} else if (!INTEGERP(HEAD(detail))) {
2025-06-30 23:29:02 +09:00
exit_status = 1;
} else {
2025-09-10 02:57:48 -07:00
exit_status = ((LispInteger *) HEAD(detail))->value;
2025-06-30 23:29:02 +09:00
}
return Qnil;
}
LispVal *Ftoplevel_error_handler(LispVal *except);
static LispFunction _Ftoplevel_error_handler_function = {
.type = TYPE_FUNCTION,
2025-09-10 02:57:48 -07:00
.is_builtin = true,
.is_macro = false,
.builtin = (lisp_function_ptr_t) &Ftoplevel_error_handler,
2025-07-03 01:36:25 +09:00
.args = Qnil,
.kwargs = Qnil,
2025-06-30 23:29:02 +09:00
.lexenv = Qnil,
2025-07-03 02:43:12 +09:00
.rargs = Qnil,
.oargs = Qnil,
.rest_arg = Qnil,
2025-06-30 23:29:02 +09:00
};
#define Ftoplevel_error_handler_function \
LISPVAL(&_Ftoplevel_error_handler_function)
LispVal *Ftoplevel_error_handler(LispVal *except) {
2025-09-10 02:57:48 -07:00
LispVal *type = HEAD(HEAD(except));
LispVal *detail = TAIL(HEAD(except));
LispVal *backtrace = HEAD(TAIL(except));
2025-06-30 23:29:02 +09:00
fprintf(stderr, "Caught signal of type ");
debug_dump(stderr, type, true);
if (!NILP(detail)) {
fprintf(stderr, "Details: ");
debug_dump(stderr, detail, true);
}
fprintf(stderr, "\nBacktrace (toplevel comes last):\n");
FOREACH(frame, backtrace) {
fprintf(stderr, " ");
debug_dump(stderr, frame, true);
}
exit_status = 1;
return Qnil;
}
2025-06-28 16:47:23 +09:00
DEF_STATIC_SYMBOL(toplevel_read, "toplevel-read");
2025-06-28 16:47:23 +09:00
int main(int argc, const char **argv) {
2025-09-11 03:10:59 -07:00
if (argc < 2) {
fprintf(stderr, "No input file!\n");
return 1;
}
FILE *in = fopen(argv[1], "r");
if (!in) {
perror("fopen");
return 1;
}
fseek(in, 0, SEEK_END);
off_t file_len = ftello(in);
rewind(in);
char buffer[file_len];
fread(buffer, 1, file_len, in);
fclose(in);
2025-06-28 16:47:23 +09:00
lisp_init();
2025-09-11 03:10:59 -07:00
REGISTER_SYMBOL(toplevel_read);
REGISTER_STATIC_FUNCTION(Ftoplevel_error_handler_function, "(e)", "");
REGISTER_STATIC_FUNCTION(Ftoplevel_exit_handler_function, "(e)", "");
size_t pos = 0;
WITH_PUSH_FRAME(Qtoplevel, Qnil, false, {
2025-09-14 02:45:44 -07:00
LispVal *err_var = INTERN_STATIC("err-var");
puthash(the_stack->handlers, Qt,
// simply call the above function
const_list(true, 3, err_var, Ftoplevel_error_handler_function,
err_var));
puthash(the_stack->handlers, Qshutdown_signal,
// simply call the above function
const_list(true, 3, err_var, Ftoplevel_exit_handler_function,
err_var));
LispVal *nil_nil = Fpair(Qnil, Qnil);
puthash(the_stack->handlers, Qeof_error,
// ignore
nil_nil);
refcount_unref(nil_nil);
refcount_unref(err_var);
2025-09-11 03:10:59 -07:00
while (true) {
LispVal *tv;
WITH_PUSH_FRAME(Qtoplevel_read, Qnil, false, {
size_t res =
read_from_buffer(buffer + pos, file_len - pos, &tv);
if (res == LISP_EOF) {
break;
}
pos += res;
});
WITH_CLEANUP(tv, {
refcount_unref(Feval(tv)); //
});
}
});
2025-09-10 02:57:48 -07:00
lisp_shutdown();
return exit_status;
2025-06-28 16:47:23 +09:00
}