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();
122 final SymbolTable st = rule.getSymbolTable();
123 CodeWriter cr = new StandardCodeWriter(output) {
124 public SymbolTable getSymbolTable() { return st; }
127 cr.outputline("// build " + rule.getLabel());
130 ListIterator quantifiers = rule.quantifiers();
132 while (quantifiers.hasNext()) {
133 Quantifier quantifier = (Quantifier) quantifiers.next();
134 quantifier.generate_open(cr);
139 rule.getGuardExpr().prettyPrint(cr);
142 /* now we have to generate the guard test */
144 VarDescriptor guardval = VarDescriptor.makeNew();
145 rule.getGuardExpr().generate(cr, guardval);
147 cr.outputline("if (" + guardval.getSafeSymbol() + ")");
150 /* now we have to generate the inclusion code */
151 rule.getInclusion().generate(cr);
154 // close startblocks generated by DotExpr memory checks
155 //DotExpr.generate_memory_endblocks(cr);
157 while (quantifiers.hasPrevious()) {
158 Quantifier quantifier = (Quantifier) quantifiers.previous();
169 private void generate_implicit_checks() {
173 CodeWriter cr = new StandardCodeWriter(output);
175 // #TBD#: these should be implicit checks added to the set of constraints
176 //output.println("check multiplicity");
179 private void generate_checks() {
181 /* do constraint checks */
182 Vector constraints = state.vConstraints;
184 for (int i = 0; i < constraints.size(); i++) {
186 Constraint constraint = (Constraint) constraints.elementAt(i);
190 final SymbolTable st = constraint.getSymbolTable();
192 CodeWriter cr = new StandardCodeWriter(output) {
193 public SymbolTable getSymbolTable() { return st; }
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 " + (i+1) + ". \");");
219 cr.outputline("else if (!" + constraintboolean.getSafeSymbol() + ")");
222 cr.outputline("__Success = 0;");
223 cr.outputline("printf(\"fail " + (i+1) + ". \");");
226 while (quantifiers.hasPrevious()) {
227 Quantifier quantifier = (Quantifier) quantifiers.previous();
238 output.println("if (__Success) { printf(\"all tests passed\"); }");