diff options
author | ubq323 <ubq323@ubq323.website> | 2024-08-17 21:28:12 +0100 |
---|---|---|
committer | ubq323 <ubq323@ubq323.website> | 2024-08-17 21:28:12 +0100 |
commit | 976aeda6465d0700843acbacdf79e18a652fd57e (patch) | |
tree | 259686d58b6b1b8dab248f3b9bf03aa5157735ac /com.c | |
parent | 41989a2ad51b15bf512b81f2c147070ba8a28ef8 (diff) |
add when form
Diffstat (limited to 'com.c')
-rw-r--r-- | com.c | 25 |
1 files changed, 19 insertions, 6 deletions
@@ -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) { |