summaryrefslogtreecommitdiff
path: root/pit/src/runtime
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2026-10-21 02:43:23 -0400
committerLLLL Colonq <llll@colonq>2026-10-21 02:43:23 -0400
commit62ab8e0b42a57bd794941f030b4c3140d6f7c744 (patch)
treed4d26aca670f67a290399e83ca5e7253a5b9c395 /pit/src/runtime
parentfe4c9bd487cb9ae59d86dc7680855c5797ae04e3 (diff)
Bake Lisp into builds, fix bug in error propagation
Diffstat (limited to 'pit/src/runtime')
-rw-r--r--pit/src/runtime/dump.c14
-rw-r--r--pit/src/runtime/eval.c15
-rw-r--r--pit/src/runtime/value/small.c2
3 files changed, 21 insertions, 10 deletions
diff --git a/pit/src/runtime/dump.c b/pit/src/runtime/dump.c
index 734c379..52441a8 100644
--- a/pit/src/runtime/dump.c
+++ b/pit/src/runtime/dump.c
@@ -111,8 +111,8 @@ pit_dump_condition pit_dump_with_callback(
if (readable) { buf[i++] = '"'; }
i64 maxlen = len - i;
for (i64 j = 0; i < maxlen && j < h->in.bytes.len;) {
- bool needs_backslash =
- (i == 0 || buf[i - 1] != '\\')
+ bool needs_backslash = readable
+ && (i == 0 || buf[i - 1] != '\\')
&& (h->in.bytes.data[j] == '\\' || h->in.bytes.data[j] == '"');
if (needs_backslash) {
buf[i++] = '\\';
@@ -192,11 +192,17 @@ i64 pit_dump_printf_callback(va_list ap, char *buf, size_t len, void *data) {
pit_value arg = va_arg(ap, pit_value);
return pit_dump(rt, buf, (i64) len, arg, false);
}
+i64 pit_dump_readable_printf_callback(va_list ap, char *buf, size_t len, void *data) {
+ pit_runtime *rt = (pit_runtime *) data;
+ pit_value arg = va_arg(ap, pit_value);
+ return pit_dump(rt, buf, (i64) len, arg, true);
+}
i64 pit_vsnprintf(pit_runtime *rt, char *buf, i64 len, char *format, va_list ap) {
if (len == 0) return 0;
- pit_printf_handler h[1];
+ pit_printf_handler h[2];
h[0].specifier = 'v'; h[0].callback = pit_dump_printf_callback;
- i64 ret = pit_vsnprintf_helper(buf, (size_t) (len - 1), format, ap, h, 1, rt);
+ h[1].specifier = 'V'; h[1].callback = pit_dump_readable_printf_callback;
+ i64 ret = pit_vsnprintf_helper(buf, (size_t) (len - 1), format, ap, h, 2, rt);
buf[ret] = 0;
return ret;
}
diff --git a/pit/src/runtime/eval.c b/pit/src/runtime/eval.c
index a2ed166..3cd1c1f 100644
--- a/pit/src/runtime/eval.c
+++ b/pit/src/runtime/eval.c
@@ -152,13 +152,14 @@ static pit_value vm_erase_empty(pit_runtime *rt, bool unbind) {
/* run one instruction of the VM */
bool pit_vm_run_one(pit_runtime *rt) {
vm_erase_empty(rt, true);
- if (rt->callstack->next < 1 || rt->error != PIT_NIL) return false;
+ if (rt->callstack->next < 1) return false;
+ if (rt->error != PIT_NIL) goto err;
pit_callstack_entry *ent = pit_vec_get(pit_callstack_entry)(rt->callstack, rt->callstack->next - 1);
- if (ent == NULL) { pit_error(rt, "malformed call stack"); return false; }
- if (rt->fuel == 0) { pit_error(rt, "out of fuel"); return false; }
+ if (ent == NULL) { pit_error(rt, "malformed call stack"); goto err; }
+ if (rt->fuel == 0) { pit_error(rt, "out of fuel"); goto err; }
rt->fuel -= 1;
pit_value ins = pit_value_cons_car(rt, ent->code);
- if (ins == PIT_NIL) { pit_error(rt, "malformed vm instruction: %v %v", ins); return false; }
+ if (ins == PIT_NIL) { pit_error(rt, "malformed vm instruction: %v %v", ins); goto err; }
ent->code = pit_value_cons_cdr(rt, ent->code);
pit_value op = pit_value_cons_car(rt, ins);
if (pit_symtab_symbol_name_match_cstr(rt, op, "literal")) {
@@ -196,13 +197,17 @@ bool pit_vm_run_one(pit_runtime *rt) {
pit_value args = PIT_NIL;
while (arity-- > 0) args = pit_value_cons(rt, vm_pop(rt), args);
pit_value bindings = vm_erase_empty(rt, false);
- if (!vm_call(rt, f, args, bindings)) return false;
+ if (!vm_call(rt, f, args, bindings)) goto err;
} else {
pit_error(rt, "unrecognized vm instruction: %v", ins);
return false;
}
vm_erase_empty(rt, true);
return true;
+err:
+ rt->result_stack->next = 0;
+ rt->callstack->next = 0;
+ return false;
}
/* run the VM until evaluation of this code finishes, returning the result */
diff --git a/pit/src/runtime/value/small.c b/pit/src/runtime/value/small.c
index e0bdf0a..7b9057e 100644
--- a/pit/src/runtime/value/small.c
+++ b/pit/src/runtime/value/small.c
@@ -23,7 +23,7 @@ pit_value pit_value_double_new(pit_runtime *rt, double d) {
i64 pit_value_as_integer(pit_runtime *rt, pit_value v) {
if (pit_value_sort(v) != PIT_VALUE_SORT_INTEGER) {
- pit_error(rt, "invalid use of value %v as integer", v);
+ pit_error(rt, "invalid use of value %V as integer", v);
return -1;
}
u64 lo = pit_value_data(v);