Skip to content

Commit bab6c5f

Browse files
committed
Add boolean expression evaluator
1 parent fcd430a commit bab6c5f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

interp.d

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/+ A boolean expression interpreter.
2+
+ Used to evaluate boolean bitfields for the query.
3+
+/
4+
module interp;
5+
6+
import std.variant;
7+
import std.typecons;
8+
// TCO
9+
alias BitType = uint;
10+
enum Op { AND, OR, NOT, ATOM };
11+
bool eval_form(BitType bitstring, Form form) {
12+
if (form.op == Op.AND) {
13+
return eval_and(bitstring, form.operands);
14+
} else if (form.op == Op.OR) {
15+
return eval_or(bitstring, form.operands);
16+
} else if (form.op == Op.NOT) {
17+
return cast(bool)(!(bitstring & (1 << form.operands[0].bitshift)));
18+
//return cast(bool)(!(bitstring & (1 << form.bitshift)));
19+
} else {
20+
return cast(bool)(bitstring & (1 << form.bitshift));
21+
}
22+
}
23+
struct Form {
24+
Op op;
25+
union {
26+
Form[] operands;
27+
// What is faster, a ubyte or a byte?
28+
ubyte bitshift;
29+
}
30+
}
31+
bool eval_and(BitType bitstring, Form[] terms) {
32+
if (terms.length == 0) {
33+
return true;
34+
}
35+
if (eval_form(bitstring, terms[0])) {
36+
return eval_and(bitstring, terms[1..$]);
37+
} else {
38+
// short-circuit evaluation
39+
return false;
40+
}
41+
}
42+
bool eval_or(BitType bitstring, Form[] terms) {
43+
if (terms.length == 0) {
44+
return false;
45+
}
46+
if (eval_form(bitstring, terms[0])) {
47+
// short-circuit evaluation
48+
return true;
49+
} else {
50+
return eval_or(bitstring, terms[1..$]);
51+
}
52+
}
53+
private Form Atom(ubyte bitshift) {
54+
Form form = { Op.ATOM, bitshift: bitshift };
55+
return form;
56+
}
57+
unittest {
58+
assert(eval_form(0b1, Form(Op.OR, [Atom(0)])) == true);
59+
assert(eval_form(0b11, Form(Op.AND, [Atom(0), Atom(1)])) == true);
60+
assert(eval_form(0b10, Form(Op.AND, [Atom(0), Atom(1)])) == false);
61+
assert(eval_form(0b10, Form(Op.OR , [Atom(0), Atom(1)])) == true);
62+
assert(eval_form(0b111,Form(Op.AND, [Form(Op.AND, [Atom(0), Atom(2)]), Atom(1)])) == true);
63+
}

0 commit comments

Comments
 (0)