aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-10 13:43:05 +0200
committerBad Diode <bd@badd10de.dev>2021-10-10 13:43:05 +0200
commit8f0c21094bc69a9adbebc42a4fc4744ed0501428 (patch)
tree58a3fd5c4b29649a46dc45066011be4b00c1f46e
parent4673fde605090320fbab227e56bb085eec97362a (diff)
downloadbdl-8f0c21094bc69a9adbebc42a4fc4744ed0501428.tar.gz
bdl-8f0c21094bc69a9adbebc42a4fc4744ed0501428.zip
Add if primitive procedure
-rw-r--r--examples/booleans.bdl75
-rwxr-xr-xsrc/bootstrap/main.c1
-rw-r--r--src/bootstrap/primitives.c23
-rw-r--r--tests/booleans_expected.txt9
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 @@
3;; 3;;
4 4
5;; Boolean test. 5;; Boolean test.
6(print "(boolean? true) -> ") (boolean? true) 6(print "(boolean? true) -> ") (boolean? true)
7(print "(boolean? false) -> ") (boolean? false) 7(print "(boolean? false) -> ") (boolean? false)
8(print "(boolean? 1) -> ") (boolean? 1) 8(print "(boolean? 1) -> ") (boolean? 1)
9(print "(boolean? 5) -> ") (boolean? 5) 9(print "(boolean? 5) -> ") (boolean? 5)
10(print "(boolean? \"string\") -> ") (boolean? "string") 10(print "(boolean? \"string\") -> ") (boolean? "string")
11(print "(boolean? (+ 1 2 3)) -> ") (boolean? (+ 1 2 3)) 11(print "(boolean? (+ 1 2 3)) -> ") (boolean? (+ 1 2 3))
12(print "(boolean? (not 1)) -> ") (boolean? (not 1)) 12(print "(boolean? (not 1)) -> ") (boolean? (not 1))
13 13
14;; Not. 14;; Not.
15(print "(not true) -> ") (not true) 15(print "(not true) -> ") (not true)
16(print "(not false) -> ") (not false) 16(print "(not false) -> ") (not false)
17(print "(not (not true)) -> ") (not (not true)) 17(print "(not (not true)) -> ") (not (not true))
18(print "(not (not false)) -> ") (not (not false)) 18(print "(not (not false)) -> ") (not (not false))
19(print "(not 1) -> ") (not 1) 19(print "(not 1) -> ") (not 1)
20(print "(not (not 1)) -> ") (not (not 1)) 20(print "(not (not 1)) -> ") (not (not 1))
21(print "(not \"string\") -> ") (not "string") 21(print "(not \"string\") -> ") (not "string")
22(print "(not (not \"string\")) -> ") (not (not "string")) 22(print "(not (not \"string\")) -> ") (not (not "string"))
23 23
24;; And. 24;; And.
25(print "(and 1 \"string\" 4 true) -> ") (and 1 "string" 4 true) 25(print "(and 1 \"string\" 4 true) -> ") (and 1 "string" 4 true)
26(print "(and true true true) -> ") (and true true true) 26(print "(and true true true) -> ") (and true true true)
27(print "(and (+ 1 2 3)) -> ") (and (+ 1 2 3)) 27(print "(and (+ 1 2 3)) -> ") (and (+ 1 2 3))
28(print "(and false false false) -> ") (and false false false) 28(print "(and false false false) -> ") (and false false false)
29(print "(and true false false) -> ") (and true false false) 29(print "(and true false false) -> ") (and true false false)
30(print "(and false true false) -> ") (and false true false) 30(print "(and false true false) -> ") (and false true false)
31(print "(and false true true) -> ") (and false true true) 31(print "(and false true true) -> ") (and false true true)
32(print "(and (not false) true true) -> ") (and (not false) true true) 32(print "(and (not false) true true) -> ") (and (not false) true true)
33 33
34;; Or. 34;; Or.
35(print "(or 1 \"string\" 4 true) -> ") (or 1 "string" 4 true) 35(print "(or 1 \"string\" 4 true) -> ") (or 1 "string" 4 true)
36(print "(or false 1) -> ") (or false 1) 36(print "(or false 1) -> ") (or false 1)
37(print "(or false \"string\") -> ") (or false "string") 37(print "(or false \"string\") -> ") (or false "string")
38(print "(or false) -> ") (or false) 38(print "(or false) -> ") (or false)
39(print "(or true true true) -> ") (or true true true) 39(print "(or true true true) -> ") (or true true true)
40(print "(or false false false) -> ") (or false false false) 40(print "(or false false false) -> ") (or false false false)
41(print "(or true false false) -> ") (or true false false) 41(print "(or true false false) -> ") (or true false false)
42(print "(or false true false) -> ") (or false true false) 42(print "(or false true false) -> ") (or false true false)
43(print "(or false true true) -> ") (or false true true) 43(print "(or false true true) -> ") (or false true true)
44(print "(or (not false) true true) -> ") (or (not false) true true) 44(print "(or (not false) true true) -> ") (or (not false) true true)
45(print "(or (not true) false) -> ") (or (not true) false) 45(print "(or (not true) false) -> ") (or (not true) false)
46
47;; If.
48(print "(if true true false) -> ") (if true true false)
49(print "(if false true false) -> ") (if false true false)
50(print "(if true (+ 1 2 3) 0) -> ") (if true (+ 1 2 3) 0)
51(print "(if false (+ 1 2 3) 0) -> ") (if false (+ 1 2 3) 0)
52(print "(if (or true false) (+ 1 2 3) (+ 7 8 9)) -> ") (if (or true false) (+ 1 2 3) (+ 7 8 9))
53(print "(if (or false false) (+ 1 2 3) (+ 7 8 9)) -> ") (if (or false false) (+ 1 2 3) (+ 7 8 9))
54(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))
55(print "(if true 7) -> ") (if true 7)
56(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) {
40 environment[env_n++] = (EnvSymbol){MAKE_SYM("not"), make_procedure(proc_not)}; 40 environment[env_n++] = (EnvSymbol){MAKE_SYM("not"), make_procedure(proc_not)};
41 environment[env_n++] = (EnvSymbol){MAKE_SYM("and"), make_procedure(proc_and)}; 41 environment[env_n++] = (EnvSymbol){MAKE_SYM("and"), make_procedure(proc_and)};
42 environment[env_n++] = (EnvSymbol){MAKE_SYM("or"), make_procedure(proc_or)}; 42 environment[env_n++] = (EnvSymbol){MAKE_SYM("or"), make_procedure(proc_or)};
43 environment[env_n++] = (EnvSymbol){MAKE_SYM("if"), make_procedure(proc_if)};
43 environment[env_n++] = (EnvSymbol){MAKE_SYM("display"), make_procedure(proc_display)}; 44 environment[env_n++] = (EnvSymbol){MAKE_SYM("display"), make_procedure(proc_display)};
44 environment[env_n++] = (EnvSymbol){MAKE_SYM("print"), make_procedure(proc_print)}; 45 environment[env_n++] = (EnvSymbol){MAKE_SYM("print"), make_procedure(proc_print)};
45} 46}
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) {
309 return obj_false; 309 return obj_false;
310} 310}
311 311
312// TODO: if/cond 312Object *
313proc_if(Object *args) {
314 if (args->type != OBJ_TYPE_PAIR || args->cdr->type != OBJ_TYPE_PAIR) {
315 fprintf(stderr, "error: wrong number of arguments.\n");
316 return NULL;
317 }
318
319 Object *condition = eval(args->car);
320 if (condition == obj_true) {
321 Object *ret = eval(args->cdr->car);
322 return ret;
323 }
324 if (args->cdr->cdr != obj_nil) {
325 Object *ret = eval(args->cdr->cdr->car);
326 return ret;
327 }
328
329 return obj_nil;
330}
331
332// TODO: cond
333// TODO: equality greater than smaller than...
313// TODO: fixnum left/right shift, mask, invert 334// 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 @@
32(or false true true) -> true 32(or false true true) -> true
33(or (not false) true true) -> true 33(or (not false) true true) -> true
34(or (not true) false) -> false 34(or (not true) false) -> false
35(if true true false) -> true
36(if false true false) -> false
37(if true (+ 1 2 3) 0) -> 6
38(if false (+ 1 2 3) 0) -> 0
39(if (or true false) (+ 1 2 3) (+ 7 8 9)) -> 6
40(if (or false false) (+ 1 2 3) (+ 7 8 9)) -> 24
41(if (or (+ 1 2 3) false) (+ 1 2 3) (+ 7 8 9)) -> 6
42(if true 7) -> 7
43(if false 7) -> ()