7 public class NaiveGenerator {
10 java.io.PrintWriter output = null;
12 public NaiveGenerator(State state) {
16 public void generate(java.io.OutputStream output) {
17 this.output = new java.io.PrintWriter(output, true);
19 generate_tokentable();
20 generate_hashtables();
22 generate_implicit_checks();
27 private void generate_tokentable() {
29 CodeWriter cr = new StandardCodeWriter(output);
30 Iterator tokens = TokenLiteralExpr.tokens.keySet().iterator();
33 cr.outputline("// Token values");
36 while (tokens.hasNext()) {
37 Object token = tokens.next();
38 cr.outputline("// " + token.toString() + " = " + TokenLiteralExpr.tokens.get(token).toString());
45 private void generate_hashtables() {
47 CodeWriter cr = new StandardCodeWriter(output);
48 cr.outputline("int __Success = 1;\n");
49 cr.outputline("// creating hashtables ");
51 /* build all the hashtables */
52 Hashtable hashtables = new Hashtable();
55 Iterator sets = state.stSets.descriptors();
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();");
63 /* second pass build relationships between hashtables */
64 sets = state.stSets.descriptors();
66 while (sets.hasNext()) {
67 SetDescriptor set = (SetDescriptor) sets.next();
68 Iterator subsets = set.subsets();
70 while (subsets.hasNext()) {
71 SetDescriptor subset = (SetDescriptor) subsets.next();
72 cr.outputline(subset.getSafeSymbol() + "_hash->addParent(" + set.getSafeSymbol() + "_hash);");
77 Iterator relations = state.stRelations.descriptors();
79 /* first pass create all the hash tables */
80 while (relations.hasNext()) {
81 RelationDescriptor relation = (RelationDescriptor) relations.next();
83 if (relation.testUsage(RelationDescriptor.IMAGE)) {
84 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hash = new SimpleHash();");
87 if (relation.testUsage(RelationDescriptor.INVIMAGE)) {
88 cr.outputline("SimpleHash* " + relation.getSafeSymbol() + "_hashinv = new SimpleHash();");
96 private void generate_rules() {
98 /* first we must sort the rules */
99 GraphNode.DFS.depthFirstSearch(state.rulenodes.values());
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();
110 topologicalsort.addAll(state.rulenodes.values());
112 /* build all the rules */
113 Iterator rules = topologicalsort.iterator();
115 while (rules.hasNext()) {
117 GraphNode rulenode = (GraphNode) rules.next();
118 Rule rule = (Rule) rulenode.getOwner();
120 if (!state.vRules.contains(rule)) {
121 // this is no longer a top-level rule
127 CodeWriter cr = new StandardCodeWriter(output);
128 cr.pushSymbolTable(rule.getSymbolTable());
130 cr.outputline("// build " + rule.getLabel());
133 ListIterator quantifiers = rule.quantifiers();
135 while (quantifiers.hasNext()) {
136 Quantifier quantifier = (Quantifier) quantifiers.next();
137 quantifier.generate_open(cr);
142 rule.getGuardExpr().prettyPrint(cr);
145 /* now we have to generate the guard test */
147 VarDescriptor guardval = VarDescriptor.makeNew();
148 rule.getGuardExpr().generate(cr, guardval);
150 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
153 /* now we have to generate the inclusion code */
154 rule.getInclusion().generate(cr);
157 // close startblocks generated by DotExpr memory checks
158 //DotExpr.generate_memory_endblocks(cr);
160 while (quantifiers.hasPrevious()) {
161 Quantifier quantifier = (Quantifier) quantifiers.previous();
172 private void generate_implicit_checks() {
176 CodeWriter cr = new StandardCodeWriter(output);
178 // #TBD#: these should be implicit checks added to the set of constraints
179 //output.println("check multiplicity");
182 private void generate_checks() {
184 /* do constraint checks */
185 Vector constraints = state.vConstraints;
187 for (int i = 0; i < constraints.size(); i++) {
189 Constraint constraint = (Constraint) constraints.elementAt(i);
193 CodeWriter cr = new StandardCodeWriter(output);
194 cr.pushSymbolTable(constraint.getSymbolTable());
196 cr.outputline("// checking " + constraint.getLabel());
199 ListIterator quantifiers = constraint.quantifiers();
201 while (quantifiers.hasNext()) {
202 Quantifier quantifier = (Quantifier) quantifiers.next();
203 quantifier.generate_open(cr);
206 cr.outputline("int maybe = 0;");
208 /* now we have to generate the guard test */
210 VarDescriptor constraintboolean = VarDescriptor.makeNew("constraintboolean");
211 constraint.getLogicStatement().generate(cr, constraintboolean);
213 cr.outputline("if (maybe)");
215 cr.outputline("__Success = 0;");
216 cr.outputline("printf(\"maybe fail " + constraint.getNum() + ". \");");
217 cr.outputline("exit(1);");
220 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
223 cr.outputline("__Success = 0;");
224 cr.outputline("printf(\"fail " + constraint.getNum() + ". \");");
225 cr.outputline("exit(1);");
228 while (quantifiers.hasPrevious()) {
229 Quantifier quantifier = (Quantifier) quantifiers.previous();
240 output.println("//if (__Success) { printf(\"all tests passed\"); }");