model and checks
[repair.git] / Repair / RepairCompiler / MCC / IR / LogicStatement.java
1 package MCC.IR;
2
3 import java.util.*;
4
5 public class LogicStatement {
6
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");
10     
11     public static class Operation {
12         private final String name;
13         private Operation(String opname) { name = opname; }
14         public String toString() { return name; }
15     }
16
17     Operation op;
18     LogicStatement left;
19     LogicStatement right;
20
21     public LogicStatement(Operation op, LogicStatement left, LogicStatement right) {
22         if (op == NOT) {
23             throw new IllegalArgumentException("Must be a AND or OR expression.");
24         }
25
26         this.op = op;
27         this.left = left;
28         this.right = right;
29     }
30
31     public LogicStatement(Operation op, LogicStatement left) {
32         if (op != NOT) {
33             throw new IllegalArgumentException("Must be a NOT expression.");
34         }
35
36         this.op = op;
37         this.left = left;
38         this.right = null;
39     }
40
41     protected LogicStatement() {
42         this.op = null;
43         this.left = null;
44         this.right = null;
45     }
46
47     public Set getRequiredDescriptors() {
48         Set v = left.getRequiredDescriptors();
49         if (right != null) {
50             v.addAll(right.getRequiredDescriptors());
51         }
52         return v;
53     }
54
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");
59
60         if (right != null) {
61             right.generate(writer, rightd);
62         }
63
64         if (op == NOT) {
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);
81         } else {
82             throw new IRException();
83         }        
84     }
85    
86 }
87
88
89
90
91
92
93
94
95
96
97
98
99