diff options
Diffstat (limited to 'pit/src/runtime')
| -rw-r--r-- | pit/src/runtime/dump.c | 7 | ||||
| -rw-r--r-- | pit/src/runtime/macroexpand.c | 41 | ||||
| -rw-r--r-- | pit/src/runtime/value.c | 11 |
3 files changed, 48 insertions, 11 deletions
diff --git a/pit/src/runtime/dump.c b/pit/src/runtime/dump.c index 4d67bb6..5c113fa 100644 --- a/pit/src/runtime/dump.c +++ b/pit/src/runtime/dump.c @@ -1,6 +1,8 @@ #include <lcq/pit/utils.h> #include <lcq/pit/runtime/dump.h> +#include <stdio.h> + pit_dump_condition pit_dump_with_callback( pit_runtime *rt, pit_value top, bool readable, pit_dump_callback write, void *data @@ -111,7 +113,10 @@ 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;) { - if (buf[i - 1] != '\\' && (h->in.bytes.data[j] == '\\' || h->in.bytes.data[j] == '"')) { + bool needs_backslash = + (i == 0 || buf[i - 1] != '\\') + && (h->in.bytes.data[j] == '\\' || h->in.bytes.data[j] == '"'); + if (needs_backslash) { buf[i++] = '\\'; } else { diff --git a/pit/src/runtime/macroexpand.c b/pit/src/runtime/macroexpand.c index 042a972..fa49968 100644 --- a/pit/src/runtime/macroexpand.c +++ b/pit/src/runtime/macroexpand.c @@ -5,13 +5,17 @@ pit_value pit_macroexpand_1(pit_runtime *rt, pit_value top) { i64 expr_stack_reset = rt->expr_stack->next; i64 result_stack_reset = rt->result_stack->next; i64 traversal_reset = rt->traversal->next; - if (pit_vec_push(pit_value)(rt->expr_stack, top) < 0) + if (pit_vec_push(pit_value)(rt->expr_stack, top) < 0) { pit_error(rt, "macro expansion stack overflow"); + goto end; + } while (rt->expr_stack->next > expr_stack_reset) { pit_value cur = PIT_NIL; if (rt->error != PIT_NIL) goto end; - if (pit_vec_pop(pit_value)(rt->expr_stack, &cur) < 0) + if (pit_vec_pop(pit_value)(rt->expr_stack, &cur) < 0) { pit_error(rt, "macro expansion stack underflow"); + goto end; + } if (pit_value_is_cons(rt, cur)) { pit_value fsym = pit_value_cons_car(rt, cur); bool is_symbol = pit_value_is_symbol(rt, fsym); @@ -26,21 +30,27 @@ pit_value pit_macroexpand_1(pit_runtime *rt, pit_value top) { pit_value f = pit_symtab_fget(rt, fsym); pit_value args = pit_value_cons_cdr(rt, cur); pit_value res = pit_vm_apply(rt, f, args); - if (pit_vec_push(pit_value)(rt->expr_stack, res) < 0) + if (pit_vec_push(pit_value)(rt->expr_stack, res) < 0) { pit_error(rt, "macro expansion stack overflow"); + goto end; + } } else { pit_value args = pit_value_cons_cdr(rt, cur); i64 argcount = 0; while (args != PIT_NIL) { pit_value a = pit_value_cons_car(rt, args); - if (pit_vec_push(pit_value)(rt->expr_stack, a) < 0) + if (pit_vec_push(pit_value)(rt->expr_stack, a) < 0) { pit_error(rt, "macro expansion stack overflow"); + goto end; + } args = pit_value_cons_cdr(rt, args); argcount += 1; } if (!is_symbol) { - if (pit_vec_push(pit_value)(rt->expr_stack, fsym) < 0) + if (pit_vec_push(pit_value)(rt->expr_stack, fsym) < 0) { pit_error(rt, "macro expansion stack overflow"); + goto end; + } } pit_traversal_push_application(rt, rt->traversal, argcount, ann); if (is_symbol) { @@ -53,31 +63,42 @@ pit_value pit_macroexpand_1(pit_runtime *rt, pit_value top) { } for (i64 idx = rt->traversal->next - 1; idx >= traversal_reset; --idx) { pit_traversal_entry *ent = pit_vec_get(pit_traversal_entry)(rt->traversal, idx); - if (ent == NULL) pit_error(rt, "macro expansion traversal invalid"); + if (ent == NULL) { + pit_error(rt, "macro expansion traversal invalid"); + goto end; + } if (rt->error != PIT_NIL) goto end; switch (ent->sort) { case PIT_TRAVERSAL_ENTRY_VALUE: - if (pit_vec_push(pit_value)(rt->result_stack, ent->in.value) < 0) + if (pit_vec_push(pit_value)(rt->result_stack, ent->in.value) < 0) { pit_error(rt, "macro expansion stack overflow"); + goto end; + } break; case PIT_TRAVERSAL_ENTRY_APPLICATION: { pit_value f = PIT_NIL; pit_value args = PIT_NIL; pit_value app = PIT_NIL; - if (pit_vec_pop(pit_value)(rt->result_stack, &f) < 0) + if (pit_vec_pop(pit_value)(rt->result_stack, &f) < 0) { pit_error(rt, "macro expansion stack underflow"); + goto end; + } for (i64 i = 0; i < ent->in.application.arity; ++i) { pit_value a = PIT_NIL; - if (pit_vec_pop(pit_value)(rt->result_stack, &a) < 0) + if (pit_vec_pop(pit_value)(rt->result_stack, &a) < 0) { pit_error(rt, "macro expansion stack underflow"); + goto end; + } args = pit_value_cons(rt, a, args); } app = pit_value_cons(rt, f, args); if (ent->in.application.annotation != NULL) { pit_annotation_set(rt, pit_value_as_ref(rt, app), *ent->in.application.annotation); } - if (pit_vec_push(pit_value)(rt->result_stack, app) < 0) + if (pit_vec_push(pit_value)(rt->result_stack, app) < 0) { pit_error(rt, "macroexpansion stack overflow"); + goto end; + } break; } default: diff --git a/pit/src/runtime/value.c b/pit/src/runtime/value.c index 6354f60..154e6a1 100644 --- a/pit/src/runtime/value.c +++ b/pit/src/runtime/value.c @@ -1,5 +1,16 @@ #include <lcq/pit/runtime/value.h> +const i64 PIT__SORT_DOUBLE = PIT_VALUE_SORT_DOUBLE; +const i64 PIT__SORT_INTEGER = PIT_VALUE_SORT_INTEGER; +const i64 PIT__SORT_SYMBOL = PIT_VALUE_SORT_SYMBOL; +const i64 PIT__SORT_CELL = PIT_VALUE_SORT_REF + PIT_VALUE_HEAVY_SORT_CELL; +const i64 PIT__SORT_CONS = PIT_VALUE_SORT_REF + PIT_VALUE_HEAVY_SORT_CONS; +const i64 PIT__SORT_ARRAY = PIT_VALUE_SORT_REF + PIT_VALUE_HEAVY_SORT_ARRAY; +const i64 PIT__SORT_BYTES = PIT_VALUE_SORT_REF + PIT_VALUE_HEAVY_SORT_BYTES; +const i64 PIT__SORT_FUNC = PIT_VALUE_SORT_REF + PIT_VALUE_HEAVY_SORT_FUNC; +const i64 PIT__SORT_NATIVEFUNC = PIT_VALUE_SORT_REF + PIT_VALUE_HEAVY_SORT_NATIVEFUNC; +const i64 PIT__SORT_NATIVEDATA = PIT_VALUE_SORT_REF + PIT_VALUE_HEAVY_SORT_NATIVEDATA; + pit_value pit_value_new(pit_runtime *rt, enum pit_value_sort s, u64 data) { if (s == PIT_VALUE_SORT_DOUBLE) { /* if (((data >> 52) & 0b011111111111) == 0b011111111111 && ((data >> 51) & 0b1) == 0) { */ |
