this.op=op;
}
- public String printNode() {
- return left.printNode()+" "+op.toString()+" "+right.printNode();
+ public String printNode(int indent) {
+ return left.printNode(indent)+" "+op.toString()+" "+right.printNode(indent);
}
}
this.en=e;
}
- public String printNode() {
- return en.printNode()+";";
+ public String printNode(int indent) {
+ return en.printNode(indent);
}
}
class BlockNode extends TreeNode {
Vector blockstatements;
+ int printStyle=0;
+ public final static int NORMAL=0;
+ public final static int NOBRACES=1;
+ public final static int EXPRLIST=2;
+
public BlockNode() {
blockstatements=new Vector();
}
public void addBlockStatement(BlockStatementNode bsn) {
blockstatements.add(bsn);
}
+ public void setStyle(int style) {
+ printStyle=style;
+ }
- public String printNode() {
- String st="{\n";
- for(int i=0;i<blockstatements.size();i++) {
- BlockStatementNode bsn=(BlockStatementNode)blockstatements.get(i);
- st+=bsn.printNode()+"\n";
- }
- st+="}\n";
- return st;
+ public String printNode(int indent) {
+ if (printStyle==NORMAL) {
+ String st="{\n";
+ for(int i=0;i<blockstatements.size();i++) {
+ BlockStatementNode bsn=(BlockStatementNode)blockstatements.get(i);
+ st+=printSpace(indent+INDENT)+bsn.printNode(indent+INDENT);
+ if (!((bsn instanceof SubBlockNode)||
+ (bsn instanceof LoopNode)||
+ (bsn instanceof IfStatementNode)))
+ st+=";\n";
+ if (bsn instanceof IfStatementNode)
+ st+="\n";
+ }
+ st+=printSpace(indent)+"}";
+ return st;
+ } else if (printStyle==NOBRACES) {
+ String st="";
+ for(int i=0;i<blockstatements.size();i++) {
+ BlockStatementNode bsn=(BlockStatementNode)blockstatements.get(i);
+ st+=printSpace(indent)+bsn.printNode(indent);
+ if (!((bsn instanceof SubBlockNode)||
+ (bsn instanceof LoopNode)||
+ (bsn instanceof IfStatementNode)))
+ st+=";";
+ }
+ return st;
+ } else if (printStyle==EXPRLIST) {
+ String st="";
+ for(int i=0;i<blockstatements.size();i++) {
+ BlockStatementNode bsn=(BlockStatementNode)blockstatements.get(i);
+ st+=bsn.printNode(0);
+ if ((i+1)!=blockstatements.size())
+ st+=", ";
+ }
+ return st;
+ } else throw new Error();
}
}
public BlockStatementNode() {
}
- public String printNode() {
+ public String printNode(int indent) {
return null;
}
public void buildtree() {
ParseNode pn=state.parsetree;
FileNode fn=parseFile(pn);
- System.out.println(fn.printNode());
+ System.out.println(fn.printNode(0));
}
/** Parse the classes in this file */
private NameDescriptor parseName(ParseNode nn) {
ParseNode base=nn.getChild("base");
ParseNode id=nn.getChild("identifier");
-
if (base==null)
return new NameDescriptor(id.getTerminal());
-
return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
}
ParseNode mn=pn.getChild("modifier");
Modifiers m=parseModifiersList(mn);
-
ParseNode tn=pn.getChild("type");
TypeDescriptor t=parseTypeDescriptor(tn);
ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
String fieldname=pn.getChild("field").getTerminal();
return new FieldAccessNode(en,fieldname);
+ } else if (isNode(pn,"cast1")) {
+ return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
+ } else if (isNode(pn,"cast2")) {
+ return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
} else {
System.out.println("---------------------");
System.out.println(pn.PPrint(3,true));
for(int j=0;j<bsv.size();j++) {
bn.addBlockStatement((BlockStatementNode)bsv.get(j));
}
+ bn.setStyle(BlockNode.NOBRACES);
return bn;
}
blockstatements.add(new SubBlockNode(bn));
} else if (isNode(pn,"empty")) {
/* nop */
- } /*else {
+ } else if (isNode(pn,"statement_expression_list")) {
+ ParseNodeVector pnv=pn.getChildren();
+ BlockNode bn=new BlockNode();
+ for(int i=0;i<pnv.size();i++) {
+ ExpressionNode en=parseExpression(pnv.elementAt(i));
+ blockstatements.add(new BlockExpressionNode(en));
+ }
+ bn.setStyle(BlockNode.EXPRLIST);
+ } else if (isNode(pn,"forstatement")) {
+ BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
+ BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
+ ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
+ BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
+ blockstatements.add(new LoopNode(init,condition,update,body));
+ } else {
System.out.println("---------------");
System.out.println(pn.PPrint(3,true));
throw new Error();
- }*/
+ }
return blockstatements;
}
--- /dev/null
+package IR.Tree;
+import IR.TypeDescriptor;
+
+public class CastNode extends ExpressionNode {
+ TypeDescriptor td;
+ ExpressionNode etd;
+ ExpressionNode exp;
+
+ public CastNode(TypeDescriptor type, ExpressionNode exp) {
+ this.td=type;
+ this.exp=exp;
+ this.etd=null;
+ }
+
+ public CastNode(ExpressionNode type, ExpressionNode exp) {
+ this.td=null;
+ this.exp=exp;
+ this.etd=type;
+ }
+
+ public String printNode(int indentlevel) {
+ if (etd==null)
+ return "("+td.toString()+")"+exp.printNode(indentlevel);
+ else
+ return "("+etd.printNode(indentlevel)+")"+exp.printNode(indentlevel);
+ }
+}
Vector methods;
Hashtable methodmap;
- public String printNode() {
+ public String printNode(int indent) {
String st=modifiers.toString()+"class "+classname;
if (superclass!=null)
st+="extends "+superclass.toString();
st+=" {\n";
+ indent+=INDENT;
for(int i=0;i<fields.size();i++) {
FieldDescriptor fd=(FieldDescriptor)fields.get(i);
- st+=fd.toString()+"\n";
+ st+=printSpace(indent)+fd.toString()+"\n";
}
+ if (fields.size()>0)
+ st+="\n";
for(int i=0;i<methods.size();i++) {
MethodDescriptor md=(MethodDescriptor)methods.get(i);
- st+=md.toString()+"\n";
+ st+=printSpace(indent)+md.toString()+" ";
BlockNode bn=(BlockNode)methodmap.get(md);
- st+=bn.printNode();
+ st+=bn.printNode(indent)+"\n\n";
}
st+="}\n";
return st;
argumentlist.add(en);
}
- public String printNode() {
+ public String printNode(int indent) {
String st="new "+td.toString()+"(";
for(int i=0;i<argumentlist.size();i++) {
ExpressionNode en=(ExpressionNode)argumentlist.get(i);
- st+=en.printNode();
+ st+=en.printNode(indent);
if ((i+1)!=argumentlist.size())
st+=", ";
}
vd=var;
}
- public String printNode() {
+ public String printNode(int indent) {
return vd.toString();
}
public class ExpressionNode extends TreeNode {
- public String printNode() {
+ public String printNode(int indentlevel) {
return null;
}
}
left=l;
}
- public String printNode() {
- return left.printNode()+"."+fieldname;
+ public String printNode(int indent) {
+ return left.printNode(indent)+"."+fieldname;
}
}
type_decls.add(tdn);
}
- public String printNode() {
+ public String printNode(int indent) {
String st="";
for(int i=0;i<type_decls.size();i++) {
ClassNode cn=(ClassNode) type_decls.get(i);
- st+=cn.printNode();
+ st+=cn.printNode(indent);
}
return st;
this.else_st=else_st;
}
- public String printNode() {
+ public String printNode(int indent) {
if (else_st==null)
- return "if("+cond.printNode()+") {\n"+true_st.printNode()+"\n}\n";
+ return "if("+cond.printNode(indent)+") "+true_st.printNode(indent);
else
- return "if("+cond.printNode()+") {\n"+true_st.printNode()+"\n} else {\n"+
- else_st.printNode()+"}\n";
+ return "if("+cond.printNode(indent)+") "+true_st.printNode(indent)+" else "+ else_st.printNode(indent);
}
}
}
- public String printNode() {
+ public String printNode(int indent) {
if (type==NULL)
return "null";
+ if (type==STRING) {
+ return '"'+escapeString(value.toString())+'"';
+ }
return "/*"+getType()+ "*/"+value.toString();
}
+ private static String escapeString(String st) {
+ String new_st="";
+ for(int i=0;i<st.length();i++) {
+ char x=st.charAt(i);
+ if (x=='\n')
+ new_st+="\\n";
+ else if (x=='"')
+ new_st+="'"+'"'+"'";
+ else new_st+=x;
+ }
+ return new_st;
+ }
}
package IR.Tree;
class LoopNode extends BlockStatementNode {
- public SubBlockNode() {
+ BlockNode initializer;
+ ExpressionNode condition;
+ BlockNode update;
+ BlockNode body;
+ int type=0;
+ public static int FORLOOP=1;
+ public static int WHILELOOP=2;
+ public static int DOWHILELOOP=3;
+
+ public LoopNode(BlockNode initializer,ExpressionNode condition, BlockNode update, BlockNode body) {
+ this.initializer=initializer;
+ this.condition=condition;
+ this.update=update;
+ this.body=body;
+ initializer.setStyle(BlockNode.EXPRLIST);
+ update.setStyle(BlockNode.EXPRLIST);
+ type=FORLOOP;
+ }
+
+ public LoopNode(ExpressionNode condition, BlockNode body, int type) {
+ this.condition=condition;
+ this.body=body;
+ this.type=type;
}
- public String printNode() {
- return null;
+ public String printNode(int indent) {
+ if (type==FORLOOP) {
+ return "for("+initializer.printNode(0)+";"+condition.printNode(0)+
+ ";"+update.printNode(0)+") "+body.printNode(indent)+"\n";
+ } else if (type==WHILELOOP) {
+ return "while("+condition.printNode(0)+") "+body.printNode(indent+INDENT)+"\n";
+ } else if (type==DOWHILELOOP) {
+ return "do "+ body.printNode(indent+INDENT)+
+ "while("+condition.printNode(0)+")\n";
+ } else throw new Error();
}
}
argumentlist.add(en);
}
- public String printNode() {
+ public String printNode(int indent) {
String st;
if (nd==null) {
- st=en.printNode()+"."+methodid+"(";
+ st=en.printNode(indent)+"."+methodid+"(";
} else {
st=nd.toString()+"(";
}
for(int i=0;i<argumentlist.size();i++) {
ExpressionNode en=(ExpressionNode)argumentlist.get(i);
- st+=en.printNode();
+ st+=en.printNode(indent);
if ((i+1)!=argumentlist.size())
st+=", ";
}
this.name=nd;
}
- public String printNode() {
+ public String printNode(int indent) {
return name.toString();
}
}
op=o;
}
- public String printNode() {
+ public String printNode(int indent) {
if (right==null)
- return op.toString()+"("+left.printNode()+")";
+ return op.toString()+"("+left.printNode(indent)+")";
else
- return left.printNode()+" "+op.toString()+" "+right.printNode();
+ return left.printNode(indent)+" "+op.toString()+" "+right.printNode(indent);
}
}
this.en=en;
}
- public String printNode() {
+ public String printNode(int indent) {
if (en==null)
return "return";
else
- return "return "+en.printNode();
+ return "return "+en.printNode(indent);
}
}
this.bn=bn;
}
- public String printNode() {
- return bn.printNode();
+ public String printNode(int indent) {
+ return bn.printNode(indent);
}
}
package IR.Tree;
class TreeNode {
+ public static final int INDENT=2;
- public String printNode() {
+ public String printNode(int indent) {
return null;
}
+ public static String printSpace(int x) {
+ String sp="";
+ for(int i=0;i<x;i++)
+ sp+=" ";
+ return sp;
+ }
}