5 public class LogicStatement {
7 public static final Operation AND = new Operation("AND");
8 public static final Operation OR = new Operation("OR");
9 public static final Operation NOT = new Operation("NOT");
11 public static class Operation {
12 private final String name;
13 private Operation(String opname) { name = opname; }
14 public String toString() { return name; }
21 public LogicStatement(Operation op, LogicStatement left, LogicStatement right) {
23 throw new IllegalArgumentException("Must be a AND or OR expression.");
31 public LogicStatement(Operation op, LogicStatement left) {
33 throw new IllegalArgumentException("Must be a NOT expression.");
41 protected LogicStatement() {
47 public Set getRequiredDescriptors() {
48 Set v = left.getRequiredDescriptors();
50 v.addAll(right.getRequiredDescriptors());
55 public void generate(CodeWriter writer, VarDescriptor dest) {
56 VarDescriptor leftd = VarDescriptor.makeNew("leftboolean");
57 left.generate(writer, leftd);
58 VarDescriptor rightd = VarDescriptor.makeNew("rightboolean");
61 right.generate(writer, rightd);
65 writer.outputline("// 3-valued NOT");
66 writer.outputline("int " + dest.getSafeSymbol() + " = " + leftd.getSafeSymbol() + " == -1 ? -1 : !" + leftd.getSafeSymbol() + ";");
67 } else if (op == AND) {
68 writer.outputline("// 3-valued AND");
69 // !a || !b ? 0 : either maybe ? maybe : AND;
70 String a = leftd.getSafeSymbol();
71 String b = rightd.getSafeSymbol();
72 String expr = a + " == 0 || " + b + " == 0 ? 0 : " + a + " == -1 || " + b + " == -1 ? -1 : 1;";
73 writer.outputline("int " + dest.getSafeSymbol() + " = " + expr);
74 } else if (op == OR) {
75 writer.outputline("// 3-valued OR");
76 // a == 1 || b == 1 ? 1 : a == -1 || b == -1 ? -1 : 0;
77 String a = leftd.getSafeSymbol();
78 String b = rightd.getSafeSymbol();
79 String expr = a + " == 1 || " + b + " == 1 ? 1 : " + a + " == -1 || " + b + " == -1 ? -1 : 0;";
80 writer.outputline("int " + dest.getSafeSymbol() + " = " + expr);
82 throw new IRException();