From 8f0c21094bc69a9adbebc42a4fc4744ed0501428 Mon Sep 17 00:00:00 2001 From: Bad Diode Date: Sun, 10 Oct 2021 13:43:05 +0200 Subject: Add if primitive procedure --- examples/booleans.bdl | 75 ++++++++++++++++++++++++++------------------- src/bootstrap/main.c | 1 + src/bootstrap/primitives.c | 23 +++++++++++++- tests/booleans_expected.txt | 9 ++++++ 4 files changed, 75 insertions(+), 33 deletions(-) diff --git a/examples/booleans.bdl b/examples/booleans.bdl index d248bf8..d526f25 100644 --- a/examples/booleans.bdl +++ b/examples/booleans.bdl @@ -3,43 +3,54 @@ ;; ;; Boolean test. -(print "(boolean? true) -> ") (boolean? true) -(print "(boolean? false) -> ") (boolean? false) -(print "(boolean? 1) -> ") (boolean? 1) -(print "(boolean? 5) -> ") (boolean? 5) +(print "(boolean? true) -> ") (boolean? true) +(print "(boolean? false) -> ") (boolean? false) +(print "(boolean? 1) -> ") (boolean? 1) +(print "(boolean? 5) -> ") (boolean? 5) (print "(boolean? \"string\") -> ") (boolean? "string") -(print "(boolean? (+ 1 2 3)) -> ") (boolean? (+ 1 2 3)) -(print "(boolean? (not 1)) -> ") (boolean? (not 1)) +(print "(boolean? (+ 1 2 3)) -> ") (boolean? (+ 1 2 3)) +(print "(boolean? (not 1)) -> ") (boolean? (not 1)) ;; Not. -(print "(not true) -> ") (not true) -(print "(not false) -> ") (not false) -(print "(not (not true)) -> ") (not (not true)) -(print "(not (not false)) -> ") (not (not false)) -(print "(not 1) -> ") (not 1) -(print "(not (not 1)) -> ") (not (not 1)) -(print "(not \"string\") -> ") (not "string") +(print "(not true) -> ") (not true) +(print "(not false) -> ") (not false) +(print "(not (not true)) -> ") (not (not true)) +(print "(not (not false)) -> ") (not (not false)) +(print "(not 1) -> ") (not 1) +(print "(not (not 1)) -> ") (not (not 1)) +(print "(not \"string\") -> ") (not "string") (print "(not (not \"string\")) -> ") (not (not "string")) ;; And. -(print "(and 1 \"string\" 4 true) -> ") (and 1 "string" 4 true) -(print "(and true true true) -> ") (and true true true) -(print "(and (+ 1 2 3)) -> ") (and (+ 1 2 3)) -(print "(and false false false) -> ") (and false false false) -(print "(and true false false) -> ") (and true false false) -(print "(and false true false) -> ") (and false true false) -(print "(and false true true) -> ") (and false true true) -(print "(and (not false) true true) -> ") (and (not false) true true) +(print "(and 1 \"string\" 4 true) -> ") (and 1 "string" 4 true) +(print "(and true true true) -> ") (and true true true) +(print "(and (+ 1 2 3)) -> ") (and (+ 1 2 3)) +(print "(and false false false) -> ") (and false false false) +(print "(and true false false) -> ") (and true false false) +(print "(and false true false) -> ") (and false true false) +(print "(and false true true) -> ") (and false true true) +(print "(and (not false) true true) -> ") (and (not false) true true) ;; Or. -(print "(or 1 \"string\" 4 true) -> ") (or 1 "string" 4 true) -(print "(or false 1) -> ") (or false 1) -(print "(or false \"string\") -> ") (or false "string") -(print "(or false) -> ") (or false) -(print "(or true true true) -> ") (or true true true) -(print "(or false false false) -> ") (or false false false) -(print "(or true false false) -> ") (or true false false) -(print "(or false true false) -> ") (or false true false) -(print "(or false true true) -> ") (or false true true) -(print "(or (not false) true true) -> ") (or (not false) true true) -(print "(or (not true) false) -> ") (or (not true) false) +(print "(or 1 \"string\" 4 true) -> ") (or 1 "string" 4 true) +(print "(or false 1) -> ") (or false 1) +(print "(or false \"string\") -> ") (or false "string") +(print "(or false) -> ") (or false) +(print "(or true true true) -> ") (or true true true) +(print "(or false false false) -> ") (or false false false) +(print "(or true false false) -> ") (or true false false) +(print "(or false true false) -> ") (or false true false) +(print "(or false true true) -> ") (or false true true) +(print "(or (not false) true true) -> ") (or (not false) true true) +(print "(or (not true) false) -> ") (or (not true) false) + +;; If. +(print "(if true true false) -> ") (if true true false) +(print "(if false true false) -> ") (if false true false) +(print "(if true (+ 1 2 3) 0) -> ") (if true (+ 1 2 3) 0) +(print "(if false (+ 1 2 3) 0) -> ") (if false (+ 1 2 3) 0) +(print "(if (or true false) (+ 1 2 3) (+ 7 8 9)) -> ") (if (or true false) (+ 1 2 3) (+ 7 8 9)) +(print "(if (or false false) (+ 1 2 3) (+ 7 8 9)) -> ") (if (or false false) (+ 1 2 3) (+ 7 8 9)) +(print "(if (or (+ 1 2 3) false) (+ 1 2 3) (+ 7 8 9)) -> ") (if (or (+ 1 2 3) false) (+ 1 2 3) (+ 7 8 9)) +(print "(if true 7) -> ") (if true 7) +(print "(if false 7) -> ") (if false 7) diff --git a/src/bootstrap/main.c b/src/bootstrap/main.c index a8ba7bc..8ec1231 100755 --- a/src/bootstrap/main.c +++ b/src/bootstrap/main.c @@ -40,6 +40,7 @@ init(void) { environment[env_n++] = (EnvSymbol){MAKE_SYM("not"), make_procedure(proc_not)}; environment[env_n++] = (EnvSymbol){MAKE_SYM("and"), make_procedure(proc_and)}; environment[env_n++] = (EnvSymbol){MAKE_SYM("or"), make_procedure(proc_or)}; + environment[env_n++] = (EnvSymbol){MAKE_SYM("if"), make_procedure(proc_if)}; environment[env_n++] = (EnvSymbol){MAKE_SYM("display"), make_procedure(proc_display)}; environment[env_n++] = (EnvSymbol){MAKE_SYM("print"), make_procedure(proc_print)}; } diff --git a/src/bootstrap/primitives.c b/src/bootstrap/primitives.c index 97058f9..61ce360 100644 --- a/src/bootstrap/primitives.c +++ b/src/bootstrap/primitives.c @@ -309,5 +309,26 @@ proc_or(Object *args) { return obj_false; } -// TODO: if/cond +Object * +proc_if(Object *args) { + if (args->type != OBJ_TYPE_PAIR || args->cdr->type != OBJ_TYPE_PAIR) { + fprintf(stderr, "error: wrong number of arguments.\n"); + return NULL; + } + + Object *condition = eval(args->car); + if (condition == obj_true) { + Object *ret = eval(args->cdr->car); + return ret; + } + if (args->cdr->cdr != obj_nil) { + Object *ret = eval(args->cdr->cdr->car); + return ret; + } + + return obj_nil; +} + +// TODO: cond +// TODO: equality greater than smaller than... // TODO: fixnum left/right shift, mask, invert diff --git a/tests/booleans_expected.txt b/tests/booleans_expected.txt index 81f0313..d05d314 100644 --- a/tests/booleans_expected.txt +++ b/tests/booleans_expected.txt @@ -32,3 +32,12 @@ (or false true true) -> true (or (not false) true true) -> true (or (not true) false) -> false +(if true true false) -> true +(if false true false) -> false +(if true (+ 1 2 3) 0) -> 6 +(if false (+ 1 2 3) 0) -> 0 +(if (or true false) (+ 1 2 3) (+ 7 8 9)) -> 6 +(if (or false false) (+ 1 2 3) (+ 7 8 9)) -> 24 +(if (or (+ 1 2 3) false) (+ 1 2 3) (+ 7 8 9)) -> 6 +(if true 7) -> 7 +(if false 7) -> () -- cgit v1.2.1