summaryrefslogtreecommitdiff
path: root/com.c
diff options
context:
space:
mode:
Diffstat (limited to 'com.c')
-rw-r--r--com.c25
1 files changed, 19 insertions, 6 deletions
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) {