add test case
authorbdemsky <bdemsky>
Sat, 11 Feb 2006 07:34:42 +0000 (07:34 +0000)
committerbdemsky <bdemsky>
Sat, 11 Feb 2006 07:34:42 +0000 (07:34 +0000)
Robust/src/IR/FieldDescriptor.java
Robust/src/IR/MethodDescriptor.java [new file with mode: 0644]
Robust/src/IR/Operation.java [new file with mode: 0644]
Robust/src/IR/Tree/BuildIR.java
Robust/src/IR/Tree/ClassNode.java
Robust/src/IR/Tree/LiteralNode.java [new file with mode: 0644]
Robust/src/IR/Tree/OpNode.java [new file with mode: 0644]
Robust/src/IR/Tree/ParseNode.java
Robust/src/IR/TypeDescriptor.java
Robust/src/Makefile
Robust/src/t.test [new file with mode: 0644]

index 367d44bdbdebe2ffe1ab9e357503aa943a4239cf..33fe6eaef944c220c9b83a8e3afdf09fcf0feb6d 100644 (file)
@@ -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 (file)
index 0000000..c97bc7b
--- /dev/null
@@ -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 (file)
index 0000000..3a4aebc
--- /dev/null
@@ -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();
+    }
+
+
+}
index 84ac252832abba21e3489d33cb05726c47361341..d4723f69a5c952fc9eaf3498e8280e725aae5d32 100644 (file)
@@ -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) {
index 1392fc05a351be4cd7502b9f870999f1690cb2f3..a38637edeac68439d9c6fb281aceb283193e47a9 100644 (file)
@@ -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<methods.size();i++) {
+           MethodDescriptor md=(MethodDescriptor)methods.get(i);
+           st+=md.toString()+"\n";
+       }
        st+="}\n";
        return st;
     }
@@ -31,6 +37,10 @@ class ClassNode extends TreeNode {
        fields.add(fd);
     }
 
+    public void addMethod(MethodDescriptor md) {
+       methods.add(md);
+    }
+
     public void setModifiers(Modifiers modifiers) {
        this.modifiers=modifiers;
     }
diff --git a/Robust/src/IR/Tree/LiteralNode.java b/Robust/src/IR/Tree/LiteralNode.java
new file mode 100644 (file)
index 0000000..9bb3e67
--- /dev/null
@@ -0,0 +1,58 @@
+package IR.Tree;
+
+public class LiteralNode extends ExpressionNode {
+    public final static int INTEGER=1;
+    public final static int FLOAT=2;
+    public final static int BOOLEAN=3;
+    public final static int CHAR=4;
+    public final static int STRING=5;
+    public final static int NULL=6;
+
+
+    Object value;
+    int type;
+    
+    public LiteralNode(String type, Object o) {
+       this.type=parseType(type);
+       value=o;
+    }
+
+    private static int parseType(String type) {
+       if (type.equals("integer"))
+           return INTEGER;
+       else if (type.equals("float"))
+           return FLOAT;
+       else if (type.equals("boolean"))
+           return BOOLEAN;
+       else if (type.equals("char"))
+           return CHAR;
+       else if (type.equals("string"))
+           return STRING;
+       else if (type.equals("null"))
+           return NULL;
+       else throw new Error();
+    }
+
+    private String getType() {
+       if (type==INTEGER)
+           return "integer";
+       else if (type==FLOAT)
+           return "float";     
+       else if (type==BOOLEAN)
+           return "boolean";
+       else if (type==CHAR)
+           return "char";
+       else if (type==STRING)
+           return "string";
+       else if (type==NULL)
+           return "null";
+       else throw new Error();
+
+    }
+
+    public String printNode() {
+       if (type==NULL)
+           return "null";
+       return "/*"+getType()+ "*/"+value.toString();
+    }
+}
diff --git a/Robust/src/IR/Tree/OpNode.java b/Robust/src/IR/Tree/OpNode.java
new file mode 100644 (file)
index 0000000..2600024
--- /dev/null
@@ -0,0 +1,27 @@
+package IR.Tree;
+import IR.Operation;
+
+public class OpNode extends ExpressionNode {
+    ExpressionNode left;
+    ExpressionNode right;
+    Operation op;
+
+    public OpNode(ExpressionNode l, ExpressionNode r, Operation o) {
+       left=l;
+       right=r;
+       op=o;
+    }
+
+    public OpNode(ExpressionNode l, Operation o) {
+       left=l;
+       right=null;
+       op=o;
+    }
+
+    public String printNode() {
+       if (right==null)
+           return op.toString()+"("+left.printNode()+")";
+       else
+           return left.printNode()+" "+op.toString()+" "+right.printNode();
+    }
+}
index 6d414bb34fec3febde1c7a96660d3a093a5dae19..00858a6ffa95776ba568a05153fe01202d5da77b 100644 (file)
@@ -48,7 +48,7 @@ public class ParseNode implements Walkable {
        literal=o;
     }
 
-    public Object getLiteral(Object o) {
+    public Object getLiteral() {
        return literal;
     }
 
@@ -165,6 +165,9 @@ public class ParseNode implements Walkable {
        }
     }
 
+    public ParseNode getFirstChild() {
+       return children.elementAt(0);
+    }
 
     public ParseNodeVector getChildren(String label) {
        int i;
index bd27c44d222dba6e1705becbeb1d2665ae2cb308..534067f364aae531b35f5329fcb8217d6093d30d 100644 (file)
@@ -14,7 +14,9 @@ public class TypeDescriptor extends Descriptor {
     public static final int CHAR=5;
     public static final int FLOAT=6;
     public static final int DOUBLE=7;
-    public static final int CLASS=8;
+    public static final int VOID=8;
+    public static final int CLASS=9;
+
 
     int type;
     NameDescriptor name_desc;
@@ -52,6 +54,8 @@ public class TypeDescriptor extends Descriptor {
            return "float";
        else if (type==DOUBLE)
            return "double";
+       else if (type==VOID)
+           return "void";
        else throw new Error();
     }
 }
index a32fec3b3616a2959a89871ccff0449dd29fef06..e0ae3e5207e9330f486e0db82eb0d4f93388eb22 100644 (file)
@@ -11,8 +11,10 @@ Lex/WhiteSpace.class IR/Tree/ParseNode.class                         \
 IR/Tree/ParseNodeDOTVisitor.class IR/Tree/ParseNodeVector.class                \
 IR/Tree/Walkable.class IR/State.class IR/SymbolTable.class             \
 IR/Descriptor.class IR/Tree/Modifiers.class IR/Tree/FileNode.class     \
-IR/Tree/ClassNode.java IR/Tree/TreeNode.class IR/Tree/BuildIR.class    \
-IR/TypeDescriptor.java IR/FieldDescriptor.java
+IR/Tree/ClassNode.class IR/Tree/TreeNode.class IR/Tree/BuildIR.class   \
+IR/TypeDescriptor.class IR/FieldDescriptor.class IR/Operation.class    \
+IR/Tree/OpNode.class IR/Tree/LiteralNode.class                         \
+IR/Tree/ExpressionNode.class
 
 all: Parse/Sym.class Parse/Parser.class $(CLASSFILES)
 
diff --git a/Robust/src/t.test b/Robust/src/t.test
new file mode 100644 (file)
index 0000000..9e40fa5
--- /dev/null
@@ -0,0 +1,218 @@
+public class ParseNode {
+
+    private String label;
+    private ParseNode parent;
+    private ParseNodeVector children;
+    private int line=-1;
+    private Object literal;
+
+    //private SymbolTable st;
+
+    public ParseNode(String label) {
+       this.label = label;
+       this.line = -1;
+       this.parent = null;
+       this.literal=null;
+       children = new ParseNodeVector();
+    }
+
+    public ParseNode ( String label, int line ) {
+       this.label = label;
+       this.line = line;
+       this.parent = null;
+       this.literal=null;
+       children = new ParseNodeVector();
+    }
+
+    public void setLabel( String label ) {
+       this.label = label;
+    }
+
+    public String getLabel() {
+       return label;
+    }
+
+    public void setLiteral(Object o) {
+       literal=o;
+    }
+
+    public Object getLiteral(Object o) {
+       return literal;
+    }
+
+    /*
+    public void setSymbolTable(SymbolTable st) {
+       if (st == null) {
+           throw new IRException("symboltable is null!");
+       }
+       this.st = st;
+    }
+
+    public SymbolTable getSymbolTable() {
+       if (st == null) {
+           if (parent != null) {
+               return parent.getSymbolTable();
+           } else {
+               return null;
+           }
+       } else {
+           return st;
+       }
+    }
+    */
+
+    public int getLine() {
+       if (line >= 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<indent;i++) output += " ";
+       return output;
+    }
+
+    public String PPrint(int indent, boolean recursive) {
+
+        String output = new String();
+
+       if (children.size()==0) {
+           output += doIndent(indent) + "<" + label + "/>\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) + "</" + label + ">\n";
+       }
+
+       return output;  
+    }
+
+}
+