diff options
Diffstat (limited to 'pit/src/runtime/macroexpand.c')
| -rw-r--r-- | pit/src/runtime/macroexpand.c | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/pit/src/runtime/macroexpand.c b/pit/src/runtime/macroexpand.c index ffa796d..fdc1f14 100644 --- a/pit/src/runtime/macroexpand.c +++ b/pit/src/runtime/macroexpand.c @@ -1,9 +1,6 @@ #include <lcq/pit/runtime/macroexpand.h> -#include <stdio.h> - -pit_value pit_macroexpand(pit_runtime *rt, pit_value top) { - fprintf(stderr, "macroexpand: "); pit_dump_to_file(rt, stderr, top, false); fprintf(stderr, "\n"); +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; @@ -17,10 +14,14 @@ pit_value pit_macroexpand(pit_runtime *rt, pit_value top) { 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); + bool is_special_form = is_symbol && pit_symtab_is_symbol_special_form(rt, fsym); + bool is_macro = is_symbol && pit_symtab_is_symbol_macro(rt, fsym); pit_annotation *ann = pit_annotation_get(rt, pit_value_as_ref(rt, cur)); - if (is_symbol && pit_symtab_is_symbol_special_form(rt, fsym)) { + if (is_special_form && pit_symtab_symbol_name_match_cstr(rt, fsym, "quote")) { + /* don't macroexpand inside quote! + NOTICE if we add other special forms, make sure to consider them here if necessary! */ pit_traversal_push_value(rt, rt->traversal, cur); - } else if (is_symbol && pit_symtab_is_symbol_macro(rt, fsym)) { + } else if (is_macro) { 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); @@ -90,7 +91,15 @@ end: { rt->expr_stack->next = expr_stack_reset; rt->result_stack->next = result_stack_reset; rt->traversal->next = traversal_reset; - fprintf(stderr, "macroexpand result: "); pit_dump_to_file(rt, stderr, ret, false); fprintf(stderr, "\n"); return ret; } } + +pit_value pit_macroexpand(pit_runtime *rt, pit_value prev) { + pit_value next = prev; + do { + prev = next; + next = pit_macroexpand_1(rt, prev); + } while (!pit_value_equal(rt, next, prev)); + return next; +} |
