From 976aeda6465d0700843acbacdf79e18a652fd57e Mon Sep 17 00:00:00 2001 From: ubq323 Date: Sat, 17 Aug 2024 21:28:12 +0100 Subject: add when form --- com.c | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'com.c') diff --git a/com.c b/com.c index 862d26f..e14bbc6 100644 --- a/com.c +++ b/com.c @@ -270,6 +270,24 @@ static void if_form(Compiler *C, ObjArr *a, Op _, int flags) { CHECK(stack_cur_a == orig_stack_cur + 1, "this should never happen"); } +static void when_form(Compiler *C, ObjArr *a, Op _, int flags) { + // (when cond body...) + // cond + // 0branch -> A + // body... + // drop + // A: + // nil + cpl_expr(C, a->d[1], 0); + cpl_op(C, OP_0BRANCH); + size_t ph = placeholder(C); + cpl_body(C, a, 2, 0); + cpl_op(C, OP_DROP); + size_t dest = BYTECODE(C).len; + patch(C, ph, dest - ph - 2); + cpl_op(C, OP_NIL); +} + static void while_form(Compiler *C, ObjArr *a, Op _, int flags) { // (while cond body ...) // A: @@ -576,6 +594,7 @@ static BuiltinForm builtin_forms[] = { { "set!", 2, false, set_form, 0 }, { "do", 1, true, do_form, 0 }, { "if", 3, false, if_form, 0 }, + { "when", 2, true, when_form, 0 }, { "while", 2, true, while_form, 0 }, { "for", 2, true, for_form, 0 }, { "each", 2, true, each_form, 0 }, @@ -670,12 +689,6 @@ static void cpl_expr(Compiler *C, Val v, int flags) { "local declared not at top level (compiler bug)"); } - - - - - - static char buf[8193]; int main(int argc, char **argv) { -- cgit v1.2.3