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) {
57 writer.outputline("int " + dest.getSafeSymbol() + ";");
61 VarDescriptor leftd = VarDescriptor.makeNew("leftboolean");
62 left.generate(writer, leftd);
64 writer.outputline("// 3-valued NOT");
65 writer.outputline("if (!maybe)");
67 writer.outputline(dest.getSafeSymbol() + " = !" + leftd.getSafeSymbol() + ";");
70 } else { // two operands
72 VarDescriptor leftd = VarDescriptor.makeNew("leftboolean");
73 String lm = (VarDescriptor.makeNew("leftmaybe")).getSafeSymbol();
74 left.generate(writer, leftd);
75 writer.outputline("int " + lm + " = maybe;");
77 VarDescriptor rightd = VarDescriptor.makeNew("rightboolean");
78 String rm = (VarDescriptor.makeNew("rightmaybe")).getSafeSymbol();
80 right.generate(writer, rightd);
81 writer.outputline("int " + rm + " = maybe;");
83 String l = leftd.getSafeSymbol();
84 String r = rightd.getSafeSymbol();
111 * M = (L*RM) + (R*LM) + (LM*RM)
115 // maybe = (l && rm) || (r && lm) || (lm && rm)
116 writer.outputline("maybe = (" + l + " && " + rm + ") || (" + r + " && " + lm + ") || (" + lm + " && " + rm + ");");
117 writer.outputline(dest.getSafeSymbol() + " = " + l + " && " + r + ";");
119 } else if (op == OR) {
144 * M = (!L*RM) + (!R*LM) + (LM*RM)
148 // maybe = (!l && rm) || (!r && lm) || (lm && rm)
149 writer.outputline("maybe = (!" + l + " && " + rm + ") || (!" + r + " && " + lm + ") || (" + lm + " && " + rm + ");");
150 writer.outputline(dest.getSafeSymbol() + " = " + l + " || " + r + ";");
152 throw new IRException();