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()+";";
}
}
--- /dev/null
+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+"()";
+ }
+}
--- /dev/null
+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();
+ }
+
+
+}
ExpressionNode en=null;
if (epn!=null)
- en=parseExpression(epn);
+ en=parseExpression(epn.getFirstChild());
cn.addField(new FieldDescriptor(m,t,identifier, en));
}
}
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) {
NameDescriptor superclass;
Modifiers modifiers;
Vector fields;
+ Vector methods;
public String printNode() {
String st=modifiers.toString()+classname;
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;
}
fields.add(fd);
}
+ public void addMethod(MethodDescriptor md) {
+ methods.add(md);
+ }
+
public void setModifiers(Modifiers modifiers) {
this.modifiers=modifiers;
}
--- /dev/null
+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();
+ }
+}
--- /dev/null
+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();
+ }
+}
literal=o;
}
- public Object getLiteral(Object o) {
+ public Object getLiteral() {
return literal;
}
}
}
+ public ParseNode getFirstChild() {
+ return children.elementAt(0);
+ }
public ParseNodeVector getChildren(String label) {
int i;
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;
return "float";
else if (type==DOUBLE)
return "double";
+ else if (type==VOID)
+ return "void";
else throw new Error();
}
}
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)
--- /dev/null
+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;
+ }
+
+}
+