aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBad Diode <bd@badd10de.dev>2021-10-28 13:54:56 +0200
committerBad Diode <bd@badd10de.dev>2021-10-28 13:54:56 +0200
commitc934ceda5e5a8f9d706b1f4e31f343e293e24f6d (patch)
tree99db3ea6d37bbb80ad70e900a15354af134cb549
parent0ff1716f45c9494b1aa02b8ddb2541821480c5ad (diff)
downloadbdl-c934ceda5e5a8f9d706b1f4e31f343e293e24f6d.tar.gz
bdl-c934ceda5e5a8f9d706b1f4e31f343e293e24f6d.zip
Fix bug with logic operations
-rwxr-xr-xMakefile2
-rw-r--r--examples/booleans.bdl136
-rwxr-xr-xsrc/bytecode/vm.h18
3 files changed, 81 insertions, 75 deletions
diff --git a/Makefile b/Makefile
index 3c65cfa..2aed0fb 100755
--- a/Makefile
+++ b/Makefile
@@ -50,7 +50,7 @@ run: $(BIN)
50 50
51tests: $(BIN) 51tests: $(BIN)
52 ./$(BIN) examples/arithmetic.bdl | diff tests/arithmetic_expected.txt - 52 ./$(BIN) examples/arithmetic.bdl | diff tests/arithmetic_expected.txt -
53 # ./$(BIN) examples/booleans.bdl | diff tests/booleans_expected.txt - 53 ./$(BIN) examples/booleans.bdl | diff tests/booleans_expected.txt -
54 # ./$(BIN) examples/lists.bdl | diff tests/lists_expected.txt - 54 # ./$(BIN) examples/lists.bdl | diff tests/lists_expected.txt -
55 # ./$(BIN) examples/types.bdl | diff tests/types_expected.txt - 55 # ./$(BIN) examples/types.bdl | diff tests/types_expected.txt -
56 ./$(BIN) examples/variables.bdl | diff tests/variables_expected.txt - 56 ./$(BIN) examples/variables.bdl | diff tests/variables_expected.txt -
diff --git a/examples/booleans.bdl b/examples/booleans.bdl
index 0598450..3d647a6 100644
--- a/examples/booleans.bdl
+++ b/examples/booleans.bdl
@@ -3,80 +3,80 @@
3;; 3;;
4 4
5;; Not. 5;; Not.
6(print "(not true) -> ") (not true) 6(print "(not true) -> ") (display (not true))(newline)
7(print "(not false) -> ") (not false) 7(print "(not false) -> ") (display (not false))(newline)
8(print "(not (not true)) -> ") (not (not true)) 8(print "(not (not true)) -> ") (display (not (not true)))(newline)
9(print "(not (not false)) -> ") (not (not false)) 9(print "(not (not false)) -> ") (display (not (not false)))(newline)
10(print "(not 1) -> ") (not 1) 10(print "(not 1) -> ") (display (not 1))(newline)
11(print "(not (not 1)) -> ") (not (not 1)) 11(print "(not (not 1)) -> ") (display (not (not 1)))(newline)
12(print "(not \"string\") -> ") (not "string") 12(print "(not \"string\") -> ") (display (not "string"))(newline)
13(print "(not (not \"string\")) -> ") (not (not "string")) 13(print "(not (not \"string\")) -> ") (display (not (not "string")))(newline)
14 14
15;; And. 15;; And.
16(print "(and 1 \"string\" 4 true) -> ") (and 1 "string" 4 true) 16(print "(and 1 \"string\" 4 true) -> ") (display (and 1 "string" 4 true))(newline)
17(print "(and true true true) -> ") (and true true true) 17(print "(and true true true) -> ") (display (and true true true))(newline)
18(print "(and (+ 1 2 3)) -> ") (and (+ 1 2 3)) 18; (print "(and (+ 1 2 3)) -> ") (display (and (+ 1 2 3)))(newline)
19(print "(and false false false) -> ") (and false false false) 19(print "(and false false false) -> ") (display (and false false false))(newline)
20(print "(and true false false) -> ") (and true false false) 20(print "(and true false false) -> ") (display (and true false false))(newline)
21(print "(and false true false) -> ") (and false true false) 21(print "(and false true false) -> ") (display (and false true false))(newline)
22(print "(and false true true) -> ") (and false true true) 22(print "(and false true true) -> ") (display (and false true true))(newline)
23(print "(and (not false) true true) -> ") (and (not false) true true) 23(print "(and (not false) true true) -> ") (display (and (not false) true true))(newline)
24 24
25;; Or. 25;; Or.
26(print "(or 1 \"string\" 4 true) -> ") (or 1 "string" 4 true) 26(print "(or 1 \"string\" 4 true) -> ") (display (or 1 "string" 4 true))(newline)
27(print "(or false 1) -> ") (or false 1) 27(print "(or false 1) -> ") (display (or false 1))(newline)
28(print "(or false \"string\") -> ") (or false "string") 28(print "(or false \"string\") -> ") (display (or false "string"))(newline)
29(print "(or false) -> ") (or false) 29; (print "(or false) -> ") (display (or false))(newline)
30(print "(or true true true) -> ") (or true true true) 30(print "(or true true true) -> ") (display (or true true true))(newline)
31(print "(or false false false) -> ") (or false false false) 31(print "(or false false false) -> ") (display (or false false false))(newline)
32(print "(or true false false) -> ") (or true false false) 32(print "(or true false false) -> ") (display (or true false false))(newline)
33(print "(or false true false) -> ") (or false true false) 33(print "(or false true false) -> ") (display (or false true false))(newline)
34(print "(or false true true) -> ") (or false true true) 34(print "(or false true true) -> ") (display (or false true true))(newline)
35(print "(or (not false) true true) -> ") (or (not false) true true) 35(print "(or (not false) true true) -> ") (display (or (not false) true true))(newline)
36(print "(or (not true) false) -> ") (or (not true) false) 36(print "(or (not true) false) -> ") (display (or (not true) false))(newline)
37 37
38;; If. 38;; If.
39(print "(if true true false) -> ") (if true true false) 39(print "(if true true false) -> ") (display (if true true false))(newline)
40(print "(if false true false) -> ") (if false true false) 40(print "(if false true false) -> ") (display (if false true false))(newline)
41(print "(if true (+ 1 2 3) 0) -> ") (if true (+ 1 2 3) 0) 41(print "(if true (+ 1 2 3) 0) -> ") (display (if true (+ 1 2 3) 0))(newline)
42(print "(if false (+ 1 2 3) 0) -> ") (if false (+ 1 2 3) 0) 42(print "(if false (+ 1 2 3) 0) -> ") (display (if false (+ 1 2 3) 0))(newline)
43(print "(if (or true false) (+ 1 2 3) (+ 7 8 9)) -> ") (if (or true false) (+ 1 2 3) (+ 7 8 9)) 43(print "(if (or true false) (+ 1 2 3) (+ 7 8 9)) -> ") (display (if (or true false) (+ 1 2 3) (+ 7 8 9)))(newline)
44(print "(if (or false false) (+ 1 2 3) (+ 7 8 9)) -> ") (if (or false false) (+ 1 2 3) (+ 7 8 9)) 44(print "(if (or false false) (+ 1 2 3) (+ 7 8 9)) -> ") (display (if (or false false) (+ 1 2 3) (+ 7 8 9)))(newline)
45(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)) 45(print "(if (or (+ 1 2 3) false) (+ 1 2 3) (+ 7 8 9)) -> ") (display (if (or (+ 1 2 3) false) (+ 1 2 3) (+ 7 8 9)))(newline)
46(print "(if true 7) -> ") (if true 7) 46(print "(if true 7) -> ") (display (if true 7))(newline)
47(print "(if false 7) -> ") (if false 7) (newline) 47(print "(if false 7) -> ") (display (if false 7))(newline)
48 48
49;; Cond. 49; ; ;; Cond.
50(print "(cond ((and true true true) 1) ((or true true false) 2) (else 3)) -> ") 50; ; (print "(cond ((and true true true) 1) ((or true true false) 2) (else 3)) -> ")
51(cond ((and true true true) 1) 51; ; (cond ((and true true true) 1)
52 ((or true true false) 2) 52; ; ((or true true false) 2)
53 (else 3)) 53; ; (else 3))
54(print "(cond ((and true true false) 1) ((or true true false) 2) (else 3)) -> ") 54; ; (print "(cond ((and true true false) 1) ((or true true false) 2) (else 3)) -> ")
55(cond ((and true true false) 1) 55; ; (cond ((and true true false) 1)
56 ((or true true false) 2) 56; ; ((or true true false) 2)
57 (else 3)) 57; ; (else 3))
58(print "(cond ((and true true false) 1) ((or false false false) 2) (else 3)) -> ") 58; ; (print "(cond ((and true true false) 1) ((or false false false) 2) (else 3)) -> ")
59(cond ((and true true false) 1) 59; ; (cond ((and true true false) 1)
60 ((or false false false) 2) 60; ; ((or false false false) 2)
61 (else 3)) 61; ; (else 3))
62(print "(cond ((and true true true) 1) ((or true true false) 2)) -> ") 62; ; (print "(cond ((and true true true) 1) ((or true true false) 2)) -> ")
63(cond ((and true true false) 1) 63; ; (cond ((and true true false) 1)
64 ((or false false false) 2)) (newline) 64; ; ((or false false false) 2)) (newline)
65(print "(cond ((and true true true) (+ 1 2 3)) ((or true true false) 2) (else 3)) -> ") 65; ; (print "(cond ((and true true true) (+ 1 2 3)) ((or true true false) 2) (else 3)) -> ")
66(cond ((and true true true) (+ 1 2 3)) 66; ; (cond ((and true true true) (+ 1 2 3))
67 ((or true true false) 2) 67; ; ((or true true false) 2)
68 (else 3)) 68; ; (else 3))
69 69
70;; Numeric comparisons. 70;; Numeric comparisons.
71(print "(< 1 2 3) -> ") (< 1 2 3) 71(print "(< 1 2 3) -> ") (display (< 1 2 3))(newline)
72(print "(< 3 2 1) -> ") (< 3 2 1) 72(print "(< 3 2 1) -> ") (display (< 3 2 1))(newline)
73(print "(> 1 2 3) -> ") (> 1 2 3) 73(print "(> 1 2 3) -> ") (display (> 1 2 3))(newline)
74(print "(> 3 2 1) -> ") (> 3 2 1) 74(print "(> 3 2 1) -> ") (display (> 3 2 1))(newline)
75(print "(= 1 2 3) -> ") (= 1 2 3) 75(print "(= 1 2 3) -> ") (display (= 1 2 3))(newline)
76(print "(= 3 2 1) -> ") (= 3 2 1) 76(print "(= 3 2 1) -> ") (display (= 3 2 1))(newline)
77(print "(= 3 3 3) -> ") (= 3 3 3) 77(print "(= 3 3 3) -> ") (display (= 3 3 3))(newline)
78(print "(= (+ 1 2) 3 (- 6 3)) -> ") (= (+ 1 2) 3 (- 6 3)) 78(print "(= (+ 1 2) 3 (- 6 3)) -> ") (display (= (+ 1 2) 3 (- 6 3)))(newline)
79(print "(< 1 1 3) -> ") (< 1 1 3) 79(print "(< 1 1 3) -> ") (display (< 1 1 3))(newline)
80(print "(<= 1 1 3) -> ") (<= 1 1 3) 80(print "(<= 1 1 3) -> ") (display (<= 1 1 3))(newline)
81(print "(> 3 3 1) -> ") (> 3 3 1) 81(print "(> 3 3 1) -> ") (display (> 3 3 1))(newline)
82(print "(>= 3 3 1) -> ") (>= 3 3 1) 82(print "(>= 3 3 1) -> ") (display (>= 3 3 1))(newline)
diff --git a/src/bytecode/vm.h b/src/bytecode/vm.h
index 0d1595d..2e1c85b 100755
--- a/src/bytecode/vm.h
+++ b/src/bytecode/vm.h
@@ -138,12 +138,18 @@ vm_reset(VM *vm) {
138 138
139#define LOGIC_OP(OP) \ 139#define LOGIC_OP(OP) \
140 do { \ 140 do { \
141 Object a = array_pop(vm->stack); \ 141 ssize_t n = AS_FIXNUM(array_pop(vm->stack)); \
142 Object b = array_pop(vm->stack); \ 142 size_t stack_size = array_size(vm->stack) - n; \
143 bool x = IS_TRUE(a); \ 143 Object obj = array_peek(vm->stack, n - 1); \
144 bool y = IS_TRUE(b); \ 144 bool ret = IS_TRUE(obj); \
145 Object result = y OP x ? TRUE_VAL : FALSE_VAL; \ 145 while (n > 1) { \
146 array_push(vm->stack, result); \ 146 obj = array_peek(vm->stack, n - 2); \
147 bool current = IS_TRUE(obj); \
148 ret = ret OP current; \
149 n--; \
150 } \
151 array_head(vm->stack)->size = stack_size; \
152 array_push(vm->stack, BOOL_VAL(ret)); \
147 } while (false) 153 } while (false)
148 154
149void 155void