// gensubgraph.beh -- generate code for SubgraphVisitor. // $Id: gensubgraphvis.beh,v 1.3 2000/10/23 19:32:00 dougo Exp $ Program { /** Add a "SubgraphVisitor" class to the set of class definitions. Don't call this until after buildClassDefTable()! */ void addSubgraphVisitor(String name) {{ addClassDef(ClassDef.parse("notparsed visitor " + name + " = " + " boolean init " + Text.begin + " true " + Text.end + ".")); }} /** Generate the behavior file for SubgraphVisitor. */ void generateSubgraphVisitor(String name, File file) = allParts { before Program {{ host.openOutputFile(file); Program.out.println( "// This file is automatically generated by DemeterJ.\n" +"\n" + name + " {\n" +" " + Text.begin + "\n" +" private java.util.Stack history = new java.util.Stack();\n" +" public " + name + "(Object obj) {\n" +" history.push(obj);\n" +" }\n" +" " + Text.end + "\n" ); }} {{ ClassName classname; String body; }} before ClassDef {{ classname = host.get_classname(); body = " Object obj = history.peek();\n" + " is_equal = is_equal && obj.getClass().equals(host.getClass());\n"+ " if(!is_equal) return;\n"; }} {{ int tempcount=0; }} before Part {{ // Don't compare final or static members. if (host.isFinal() || host.isStatic() /* || host.isDerived() */) return; PartName partname = host.get_partname(); ClassName dest = host.get_classname(); if (Program.prog.definesClass(dest)) { String part_body = " if (is_equal) {\n" + " " + classname + " obj = (" + classname + ") history.peek();\n" + " Object temp" + ++tempcount + " = obj.get_" + partname + "();\n" + " if(temp" + tempcount + "==null) { is_equal = false; return; }\n" + " history.push(temp" + tempcount + ");\n" + " }\n"; Program.out.println( host.makeBefore(classname, part_body) + "\n" + host.makeAfter(classname, " if (is_equal) history.pop();\n") + "\n"); } else { /* body += "\n && host.get_" + partname + "()"; String rest = "((" + classname + ") obj).get_" + partname + "()"; if (dest.isBuiltinType()) { body += " == " + rest; } else { body += ".equals(" + rest + ")"; } */ String rest = "((" + classname + ") obj).get_" + partname + "()"; if(dest.isBuiltinType()) { body += " is_equal = is_equal && host.get_" + partname + "() == " + rest + ";\n"; } else { body += " Object temp" + ++tempcount + " = host.get_" + partname + "();\n" + " Object temp" + ++tempcount + " = " + rest + ";\n" + " if(temp" + (tempcount-1) + " == null && temp" + tempcount + " == null) {}\n" + " else if(temp" + (tempcount-1) + " == null ||\n" + " temp" + tempcount + " == null ||\n" + " !temp" + (tempcount-1) + ".equals(temp" + tempcount + ")) { is_equal = false; return; }\n"; } } }} after ClassDef {{ Program.out.println(classname.makeBefore(body) + "\n"); }} after Program {{ Program.out.println( " return boolean " + Text.begin + " is_equal " + Text.end + "\n}"); host.closeOutputFile(); }} } }