From: bdemsky Date: Sat, 11 Feb 2006 07:34:42 +0000 (+0000) Subject: add test case X-Git-Tag: preEdgeChange~989 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=62b9f72d7dd259e9ef65f5579f4ad7c590d1262a;p=IRC.git add test case --- diff --git a/Robust/src/IR/FieldDescriptor.java b/Robust/src/IR/FieldDescriptor.java index 367d44bd..33fe6eae 100644 --- a/Robust/src/IR/FieldDescriptor.java +++ b/Robust/src/IR/FieldDescriptor.java @@ -29,6 +29,6 @@ public class FieldDescriptor extends Descriptor { if (en==null) return modifier.toString()+td.toString()+" "+identifier+";"; else - return modifier.toString()+td.toString()+" "+identifier+"="+en.toString()+";"; + return modifier.toString()+td.toString()+" "+identifier+"="+en.printNode()+";"; } } diff --git a/Robust/src/IR/MethodDescriptor.java b/Robust/src/IR/MethodDescriptor.java new file mode 100644 index 00000000..c97bc7ba --- /dev/null +++ b/Robust/src/IR/MethodDescriptor.java @@ -0,0 +1,29 @@ +package IR; +import IR.Tree.Modifiers; +import IR.Tree.ExpressionNode; + +/** + * Descriptor + * + * represents a symbol in the language (var name, function name, etc). + */ + +public class MethodDescriptor extends Descriptor { + + protected Modifiers modifier; + protected TypeDescriptor returntype; + protected String identifier; + + public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) { + super(identifier); + this.modifier=m; + this.returntype=rt; + this.identifier=identifier; + this.safename = "__" + name + "__"; + this.uniqueid=count++; + } + + public String toString() { + return modifier.toString()+td.toString()+" "+identifier+"()"; + } +} diff --git a/Robust/src/IR/Operation.java b/Robust/src/IR/Operation.java new file mode 100644 index 00000000..3a4aebc6 --- /dev/null +++ b/Robust/src/IR/Operation.java @@ -0,0 +1,144 @@ +package IR; + +public class Operation { + public static final int LOGIC_OR=1; + public static final int LOGIC_AND=2; + public static final int BIT_OR=3; + public static final int BIT_XOR=4; + public static final int BIT_AND=5; + public static final int EQUAL=6; + public static final int NOTEQUAL=7; + public static final int LT=8; + public static final int GT=9; + public static final int LTE=10; + public static final int GTE=11; + public static final int LEFTSHIFT=12; + public static final int RIGHTSHIFT=13; + public static final int SUB=14; + public static final int ADD=15; + public static final int MULT=16; + public static final int DIV=17; + public static final int MOD=18; + public static final int UNARYPLUS=19; + public static final int UNARYMINUS=20; + public static final int POSTINC=21; + public static final int POSTDEC=22; + public static final int PREINC=21; + public static final int PREDEC=22; + + private int operation; + public Operation(int op) { + this.operation=op; + } + + public Operation(String op) { + this.operation=parseOp(op); + } + + public static int parseOp(String st) { + if (st.equals("logical_or")) + return LOGIC_OR; + else if (st.equals("logical_and")) + return LOGIC_AND; + else if (st.equals("bitwise_or")) + return BIT_OR; + else if (st.equals("bitwise_xor")) + return BIT_XOR; + else if (st.equals("bitwise_and")) + return BIT_AND; + else if (st.equals("equal")) + return EQUAL; + else if (st.equals("not_equal")) + return NOTEQUAL; + else if (st.equals("comp_lt")) + return LT; + else if (st.equals("comp_gt")) + return GT; + else if (st.equals("comp_lte")) + return LTE; + else if (st.equals("comp_gte")) + return GTE; + else if (st.equals("leftshift")) + return LEFTSHIFT; + else if (st.equals("rightshift")) + return RIGHTSHIFT; + else if (st.equals("sub")) + return SUB; + else if (st.equals("add")) + return ADD; + else if (st.equals("mult")) + return MULT; + else if (st.equals("div")) + return DIV; + else if (st.equals("mod")) + return MOD; + else if (st.equals("unaryplus")) + return UNARYPLUS; + else if (st.equals("unaryminus")) + return UNARYMINUS; + else if (st.equals("postinc")) + return POSTINC; + else if (st.equals("postdec")) + return POSTDEC; + else if (st.equals("preinc")) + return PREINC; + else if (st.equals("predec")) + return PREDEC; + else + throw new Error(); + } + + public String toString() { + if (operation==LOGIC_OR) + return "||"; + else if (operation==LOGIC_AND) + return "&&"; + else if (operation==BIT_OR) + return "|"; + else if (operation==BIT_XOR) + return "^"; + else if (operation==BIT_AND) + return "&"; + else if (operation==EQUAL) + return "=="; + else if (operation==NOTEQUAL) + return "!="; + else if (operation==LT) + return "<"; + else if (operation==GT) + return ">"; + else if (operation==LTE) + return "<="; + else if (operation==GTE) + return ">="; + else if (operation==LEFTSHIFT) + return "<<"; + else if (operation==RIGHTSHIFT) + return ">>"; + else if (operation==SUB) + return "-"; + else if (operation==ADD) + return "+"; + else if (operation==MULT) + return "*"; + else if (operation==DIV) + return "/"; + else if (operation==MOD) + return "%"; + else if (operation==UNARYPLUS) + return "unaryplus"; + else if (operation==UNARYMINUS) + return "unaryminus"; + else if (operation==POSTINC) + return "postinc"; + else if (operation==POSTDEC) + return "postdec"; + else if (operation==PREINC) + return "preinc"; + else if (operation==PREDEC) + return "predec"; + else throw new Error(); + } + + +} diff --git a/Robust/src/IR/Tree/BuildIR.java b/Robust/src/IR/Tree/BuildIR.java index 84ac2528..d4723f69 100644 --- a/Robust/src/IR/Tree/BuildIR.java +++ b/Robust/src/IR/Tree/BuildIR.java @@ -122,7 +122,7 @@ public class BuildIR { ExpressionNode en=null; if (epn!=null) - en=parseExpression(epn); + en=parseExpression(epn.getFirstChild()); cn.addField(new FieldDescriptor(m,t,identifier, en)); } @@ -130,12 +130,59 @@ public class BuildIR { } private ExpressionNode parseExpression(ParseNode pn) { + if (isNode(pn,"assignment")) + return parseAssignmentExpression(pn); + else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")|| + isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")|| + isNode(pn,"bitwise_and")||isNode(pn,"equal")|| + isNode(pn,"not_equal")||isNode(pn,"comp_lt")|| + isNode(pn,"comp_lte")||isNode(pn,"comp_gt")|| + isNode(pn,"comp_gte")||isNode(pn,"leftshift")|| + isNode(pn,"rightshift")||isNode(pn,"sub")|| + isNode(pn,"add")||isNode(pn,"mult")|| + isNode(pn,"div")||isNode(pn,"mod")) { + ParseNodeVector pnv=pn.getChildren(); + ParseNode left=pnv.elementAt(0); + ParseNode right=pnv.elementAt(1); + Operation op=new Operation(pn.getLabel()); + return new OpNode(parseExpression(left),parseExpression(right),op); + } else if (isNode(pn,"unaryplus")|| + isNode(pn,"unaryminus")|| + isNode(pn,"postinc")|| + isNode(pn,"postdec")|| + isNode(pn,"preinc")|| + isNode(pn,"predec")) { + ParseNode left=pn.getFirstChild(); + Operation op=new Operation(pn.getLabel()); + return new OpNode(parseExpression(left),op); + } else if (isNode(pn,"literal")) { + String literaltype=pn.getTerminal(); + ParseNode literalnode=pn.getChild(literaltype); + Object literal_obj=literalnode.getLiteral(); + return new LiteralNode(literaltype, literal_obj); + } + throw new Error(); + } + + private ExpressionNode parseAssignmentExpression(ParseNode pn) { return null; } private void parseMethodDecl(ClassNode cn, ParseNode pn) { + ParseNode headern=pn.getChild("header"); + ParseNode bodyn=pn.getChild("body"); + MethodDescriptor md=parseMethodHeader(headern); + } + + public MethodDescriptor parseMethodHeader(ParseNode pn) { + ParseNode mn=pn.getChild("modifier"); + Modifiers m=parseModifiersList(mn); + ParseNode tn=pn.getChild("returntype"); + TypeDescriptor returntype=parseTypeDescriptor(tn); + MethodDescriptor md=new MethodDescriptor(m, returntype, null); + return md; } public Modifiers parseModifiersList(ParseNode pn) { diff --git a/Robust/src/IR/Tree/ClassNode.java b/Robust/src/IR/Tree/ClassNode.java index 1392fc05..a38637ed 100644 --- a/Robust/src/IR/Tree/ClassNode.java +++ b/Robust/src/IR/Tree/ClassNode.java @@ -13,6 +13,7 @@ class ClassNode extends TreeNode { NameDescriptor superclass; Modifiers modifiers; Vector fields; + Vector methods; public String printNode() { String st=modifiers.toString()+classname; @@ -23,6 +24,11 @@ class ClassNode extends TreeNode { FieldDescriptor fd=(FieldDescriptor)fields.get(i); st+=fd.toString()+"\n"; } + + for(int i=0;i= 0) { + return line; + } else { + if (parent != null) { + return parent.getLine(); + } else { + return 0; + } + } + } + + public void setParent( ParseNode parent ) { + this.parent = parent; + } + + public ParseNode getParent() { + return parent; + } + + public ParseNode insertChild(ParseNode child) { + if (child == null) { + } + + children.insertElementAt(child, 0); + child.setParent(this); + return child; + } + + public ParseNode insertChild(String newlabel) { + ParseNode child = new ParseNode(newlabel, -1); + return insertChild(child); + } + + public ParseNode addChild( ParseNode child ) { + + if (child == null) { + } + + children.addElement (child); + child.setParent(this); + return child; + } + + public ParseNode addChild( String newlabel ) { + + ParseNode child = new ParseNode(newlabel, -1); + children.addElement(child); + child.setParent(this); + return child; + } + + public ParseNode addChild (String newlabel, int line) { + ParseNode child = new ParseNode(newlabel, line); + children.addElement(child); + child.setParent(this); + return child; + } + + public ParseNodeVector getChildren() { + return children; + } + + public ParseNode getChild (String label) { + int i; + ParseNode p; + + for (i = 0; i < children.size(); i++) { + p = children.elementAt(i); + if (p.getLabel().equals(label)) { + return p; + } + } + + return null; + } + + public ParseNode getRoot() { + if (parent==null) + return this; else return parent.getRoot(); + } + + public String getTerminal () { + ParseNode pn = children.elementAt(0); + if (pn == null) { + return null; + } else { + return pn.getLabel(); + } + } + + + public ParseNodeVector getChildren(String label) { + int i; + ParseNodeVector v = new ParseNodeVector(); + + for (i = 0; i < children.size(); i++) { + ParseNode pn = children.elementAt(i); + if (pn.getLabel().equals(label)) + v.addElement(pn); + } + + return v; + } + + public String getNodeName() { + return label + " - " + getLine(); + } + + public int getNeighborCount() { + return children.size(); + } + + public Object getNeighbor(int index) { + return children.elementAt(index); + } + + public String doIndent(int indent) { + + String output = new String(); + for(int i=0;i\n"; + } else { + output += doIndent(indent) + "<" + label + ">\n"; + indent += 2; + + if (recursive) { + for (int i = 0; i < children.size(); i++) { + Walkable w = (Walkable)children.elementAt(i); + output += w.PPrint(indent, true); + } + } else { + for (int i = 0; i < children.size(); i++) { + Walkable w = (Walkable)children.elementAt(i); + output += doIndent(indent) + "<" + w.getNodeName() + "/>\n"; + } + } + + indent -= 2; + output += doIndent(indent) + "\n"; + } + + return output; + } + +} +