summaryrefslogtreecommitdiff
path: root/pit/src
diff options
context:
space:
mode:
authorLLLL Colonq <llll@colonq>2026-09-25 17:36:15 -0400
committerLLLL Colonq <llll@colonq>2026-09-25 17:36:15 -0400
commit17df4b1ff7f4cbcf3b3218fea68f65d3cb60c984 (patch)
tree5e0b2b81a3ac9c7e506303e8570c16c742072d5d /pit/src
parentc1a6dfb43efca52cacae738ce3d9648c4981906e (diff)
pit: Add fuel economy
Diffstat (limited to 'pit/src')
-rw-r--r--pit/src/main.c1
-rw-r--r--pit/src/runtime.c5
-rw-r--r--pit/src/runtime/eval.c2
3 files changed, 8 insertions, 0 deletions
diff --git a/pit/src/main.c b/pit/src/main.c
index 157ad87..2b741c1 100644
--- a/pit/src/main.c
+++ b/pit/src/main.c
@@ -11,6 +11,7 @@ int main(int argc, char **argv) {
i64 sz = 256 * 1024 * 1024;
u8 *buf = malloc((size_t) sz);
pit_runtime *rt = pit_runtime_new(buf, sz);
+ rt->fuel = 10000000;
pit_install_library_essential(rt);
pit_install_library_io(rt);
pit_install_library_plist(rt);
diff --git a/pit/src/runtime.c b/pit/src/runtime.c
index d4a0a62..ab65d71 100644
--- a/pit/src/runtime.c
+++ b/pit/src/runtime.c
@@ -45,6 +45,7 @@ pit_runtime *pit_runtime_new(u8 *buf, i64 len) {
ret->traversal = pit_vec_new(pit_traversal_entry)(pit_arena_alloc_back(a, stack_size), stack_size);
ret->callstack = pit_vec_new(pit_callstack_entry)(pit_arena_alloc_back(a, stack_size), stack_size);
ret->saved_bindings = pit_vec_new(pit_value)(pit_arena_alloc_back(a, stack_size), stack_size);
+ ret->fuel = -1;
ret->frozen_values = 0;
ret->frozen_symtab = 0;
ret->error = PIT_NIL;
@@ -60,6 +61,10 @@ pit_runtime *pit_runtime_new(u8 *buf, i64 len) {
return ret;
}
+void pit_runtime_set_fuel(pit_runtime *rt, i64 fuel) {
+ rt->fuel = fuel;
+}
+
void pit_runtime_freeze(pit_runtime *rt) {
rt->frozen_values = rt->heap->next;
rt->frozen_symtab = rt->symtab->next;
diff --git a/pit/src/runtime/eval.c b/pit/src/runtime/eval.c
index eba9715..a2ed166 100644
--- a/pit/src/runtime/eval.c
+++ b/pit/src/runtime/eval.c
@@ -155,6 +155,8 @@ bool pit_vm_run_one(pit_runtime *rt) {
if (rt->callstack->next < 1 || rt->error != PIT_NIL) return false;
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; }
+ 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; }
ent->code = pit_value_cons_cdr(rt, ent->code);