test
[repair.git] / Repair / RepairCompiler / MCC / IR / NaiveGenerator.java
1 package MCC.IR;
2
3 import java.io.*;
4 import java.util.*;
5 import MCC.State;
6
7 public class NaiveGenerator {
8
9     State state;
10     java.io.PrintWriter output = null;
11             
12     public NaiveGenerator(State state) {
13         this.state = state;
14     }
15
16     public void generate(java.io.OutputStream output) {
17         this.output = new java.io.PrintWriter(output, true); 
18         
19         generate_tokentable();
20         generate_hashtables();
21         generate_rules();
22         generate_implicit_checks();
23         generate_checks();
24
25     }
26
27     private void generate_tokentable() {
28
29         CodeWriter cr = new StandardCodeWriter(output);        
30         Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();        
31
32         cr.outputline("");
33         cr.outputline("// Token values");
34         cr.outputline("");
35
36         while (tokens.hasNext()) {
37             Object token = tokens.next();
38             cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());            
39         }
40
41         cr.outputline("");
42         cr.outputline("");
43     }
44
45     private void generate_hashtables() {
46
47         CodeWriter cr = new StandardCodeWriter(output);
48         cr.outputline("int __Success = 1;\n");       
49         cr.outputline("// creating hashtables ");
50         
51         /* build all the hashtables */
52         Hashtable hashtables = new Hashtable();
53
54         /* build sets */
55         Iterator sets = state.stSets.descriptors();
56         
57         /* first pass create all the hash tables */
58         while (sets.hasNext()) {
59             SetDescriptor set = (SetDescriptor) sets.next();
60             cr.outputline("SimpleHash* " + set.getSafeSymbol() + "_hash = new SimpleHash();");
61         } 
62         
63         /* second pass build relationships between hashtables */
64         sets = state.stSets.descriptors();
65         
66         while (sets.hasNext()) {
67             SetDescriptor set = (SetDescriptor) sets.next();
68             Iterator subsets = set.subsets();
69             
70             while (subsets.hasNext()) {
71                 SetDescriptor subset = (SetDescriptor) subsets.next();                
72                 cr.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
73             }
74         } 
75
76         /* build relations */
77         Iterator relations = state.stRelations.descriptors();
78         
79         /* first pass create all the hash tables */
80         while (relations.hasNext()) {
81             RelationDescriptor relation = (RelationDescriptor) relations.next();
82             
83             if (relation.testUsage(RelationDescriptor.IMAGE)) {
84                 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();");
85             }
86
87             if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
88                 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
89             } 
90         }
91
92         cr.outputline("");
93         cr.outputline("");
94     }
95
96     private void generate_rules() {
97
98         /* first we must sort the rules */
99         GraphNode.DFS.depthFirstSearch(state.rulenodes.values());
100
101         TreeSet topologicalsort = new TreeSet(new Comparator() {
102                 public boolean equals(Object obj) { return false; }
103                 public int compare(Object o1, Object o2) {
104                     GraphNode g1 = (GraphNode) o1;
105                     GraphNode g2 = (GraphNode) o2;
106                     return g2.getFinishingTime() - g1.getFinishingTime();
107                 }
108             });
109         
110         topologicalsort.addAll(state.rulenodes.values());        
111
112         /* build all the rules */
113         Iterator rules = topologicalsort.iterator();
114                 
115         while (rules.hasNext()) {
116
117             GraphNode rulenode = (GraphNode) rules.next();
118             Rule rule = (Rule) rulenode.getOwner();            
119
120             {
121
122                 final SymbolTable st = rule.getSymbolTable();                
123                 CodeWriter cr = new StandardCodeWriter(output) {
124                         public SymbolTable getSymbolTable() { return st; }
125                     };
126                 
127                 cr.outputline("// build " + rule.getLabel());
128                 cr.startblock();
129
130                 ListIterator quantifiers = rule.quantifiers();
131
132                 while (quantifiers.hasNext()) {
133                     Quantifier quantifier = (Quantifier) quantifiers.next();                   
134                     quantifier.generate_open(cr);
135                 }            
136                         
137                 /* pretty print! */
138                 cr.output("//");
139                 rule.getGuardExpr().prettyPrint(cr);
140                 cr.outputline("");
141
142                 /* now we have to generate the guard test */
143         
144                 VarDescriptor guardval = VarDescriptor.makeNew();
145                 rule.getGuardExpr().generate(cr, guardval);
146                 
147                 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
148                 cr.startblock();
149
150                 /* now we have to generate the inclusion code */
151                 rule.getInclusion().generate(cr);
152                 cr.endblock();
153
154                 while (quantifiers.hasPrevious()) {
155                     Quantifier quantifier = (Quantifier) quantifiers.previous();
156                     cr.endblock();
157                 }
158
159                 cr.endblock();
160                 cr.outputline("");
161                 cr.outputline("");
162             }
163         }
164     }
165
166     private void generate_implicit_checks() {
167
168         /* do post checks */
169          
170         CodeWriter cr = new StandardCodeWriter(output);
171            
172         // #TBD#: these should be implicit checks added to the set of constraints
173         //output.println("check multiplicity");
174     }
175
176     private void generate_checks() {
177
178         /* do constraint checks */
179         Vector constraints = state.vConstraints;
180
181         for (int i = 0; i < constraints.size(); i++) {
182
183             Constraint constraint = (Constraint) constraints.elementAt(i); 
184
185             {
186
187                 final SymbolTable st = constraint.getSymbolTable();
188                 
189                 CodeWriter cr = new StandardCodeWriter(output) {
190                         public SymbolTable getSymbolTable() { return st; }
191                     };
192                 
193                 cr.outputline("// checking " + constraint.getLabel());
194                 cr.startblock();
195
196                 ListIterator quantifiers = constraint.quantifiers();
197
198                 while (quantifiers.hasNext()) {
199                     Quantifier quantifier = (Quantifier) quantifiers.next();                   
200                     quantifier.generate_open(cr);
201                 }            
202
203                 cr.outputline("int maybe = 0;");
204                         
205                 /* now we have to generate the guard test */
206         
207                 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
208                 constraint.getLogicStatement().generate(cr, constraintboolean);
209                 
210                 cr.outputline("if (maybe)");
211                 cr.startblock();
212                 cr.outputline("__Success = 0;");
213                 cr.outputline("printf(\"maybe fail " + (i+1) + ". \");");
214                 cr.endblock();
215
216                 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
217                 cr.startblock();
218
219                 cr.outputline("__Success = 0;");
220                 cr.outputline("printf(\"fail " + (i+1) + ". \");");
221                 cr.endblock();
222
223                 while (quantifiers.hasPrevious()) {
224                     Quantifier quantifier = (Quantifier) quantifiers.previous();
225                     cr.endblock();
226                 }
227
228                 cr.endblock();
229                 cr.outputline("");
230                 cr.outputline("");
231             }
232             
233         }
234
235         output.println("if (__Success) { printf(\"all tests passed\"); }");
236     }    
237
238 }
239
240
241