package IR;
import IR.Tree.Modifiers;
+import IR.Tree.ExpressionNode;
/**
* Descriptor
protected Modifiers modifier;
protected TypeDescriptor td;
+ protected String identifier;
+ protected ExpressionNode en;
- public FieldDescriptor(Modifiers m, TypeDescriptor t, String name) {
- super(name);
+ public FieldDescriptor(Modifiers m, TypeDescriptor t, String identifier, ExpressionNode e) {
+ super(identifier);
this.modifier=m;
this.td=t;
+ this.identifier=identifier;
+ this.en=e;
this.safename = "__" + name + "__";
this.uniqueid=count++;
}
public String toString() {
- return modifier.toString()+";";
+ if (en==null)
+ return modifier.toString()+td.toString()+" "+identifier+";";
+ else
+ return modifier.toString()+td.toString()+" "+identifier+"="+en.toString()+";";
}
}
--- /dev/null
+package IR;
+
+public class NameDescriptor extends Descriptor {
+ String identifier;
+ NameDescriptor nd;
+ public NameDescriptor(NameDescriptor nd, String id) {
+ super(nd.toString()+"."+id);
+ identifier=id;
+ this.nd=nd;
+ }
+
+ public NameDescriptor(String id) {
+ super(id);
+ identifier=id;
+ nd=null;
+ }
+
+ public String toString() {
+ if (nd==null)
+ return identifier;
+ else
+ return nd+"."+identifier;
+ }
+
+}
public SymbolTable globals;
public ParseNode parsetree;
+ public static TypeDescriptor getTypeDescriptor(int t) {
+ TypeDescriptor td=new TypeDescriptor(t);
+ return td;
+ }
+ public static TypeDescriptor getTypeDescriptor(NameDescriptor n) {
+ TypeDescriptor td=new TypeDescriptor(n);
+ return td;
+ }
}
ParseNode fieldnode=pn.getChild("field");
if (fieldnode!=null) {
- FieldDescriptor fd=parseFieldDecl(fieldnode.getChild("field_declaration"));
- cn.addField(fd);
+ parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
return;
}
ParseNode methodnode=pn.getChild("method");
throw new Error();
}
- private FieldDescriptor parseFieldDecl(ParseNode pn) {
+ private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
+ ParseNode tn=pn.getChild("type");
+ String type_st=tn.getTerminal();
+ if(type_st.equals("byte")) {
+ return state.getTypeDescriptor(TypeDescriptor.BYTE);
+ } else if(type_st.equals("short")) {
+ return state.getTypeDescriptor(TypeDescriptor.SHORT);
+ } else if(type_st.equals("int")) {
+ return state.getTypeDescriptor(TypeDescriptor.INT);
+ } else if(type_st.equals("long")) {
+ return state.getTypeDescriptor(TypeDescriptor.LONG);
+ } else if(type_st.equals("char")) {
+ return state.getTypeDescriptor(TypeDescriptor.CHAR);
+ } else if(type_st.equals("float")) {
+ return state.getTypeDescriptor(TypeDescriptor.FLOAT);
+ } else if(type_st.equals("double")) {
+ return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
+ } else if(type_st.equals("class")) {
+ ParseNode nn=tn.getChild("class");
+ return state.getTypeDescriptor(parseName(nn));
+ } else
+ throw new Error();
+ }
+
+ private NameDescriptor parseName(ParseNode pn) {
+ ParseNode nn=pn.getChild("name");
+ ParseNode base=nn.getChild("base");
+ ParseNode id=nn.getChild("identifier");
+
+ if (base==null)
+ return new NameDescriptor(id.getTerminal());
+
+ return new NameDescriptor(parseName(base),id.getTerminal());
+
+ }
+
+ private void parseFieldDecl(ClassNode cn,ParseNode pn) {
ParseNode mn=pn.getChild("modifier");
Modifiers m=parseModifiersList(mn);
- return new FieldDescriptor(m,null,null);
+ ParseNode tn=pn.getChild("type");
+ TypeDescriptor t=parseTypeDescriptor(tn);
+ ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
+ ParseNodeVector pnv=vn.getChildren();
+ for(int i=0;i<pnv.size();i++) {
+ ParseNode vardecl=pnv.elementAt(i);
+ String identifier=vardecl.getChild("identifier").getChild("single").getTerminal();
+ ParseNode epn=vardecl.getChild("initializer");
+
+ ExpressionNode en=null;
+ if (epn!=null)
+ en=parseExpression(epn);
+
+ cn.addField(new FieldDescriptor(m,t,identifier, en));
+ }
+
+ }
+
+ private ExpressionNode parseExpression(ParseNode pn) {
+ return null;
}
+
private void parseMethodDecl(ClassNode cn, ParseNode pn) {
}
else
return false;
}
-
/** Throw an exception if something is unexpected */
private void check(ParseNode pn, String label) {
throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
}
}
-
}
package IR.Tree;
import java.util.Vector;
import IR.FieldDescriptor;
+import IR.NameDescriptor;
class ClassNode extends TreeNode {
ClassNode() {
fields=new Vector();
}
String classname;
- Name superclass;
+ NameDescriptor superclass;
Modifiers modifiers;
Vector fields;
void setName(String name) {
classname=name;
}
- void setSuper(Name superclass) {
+ void setSuper(NameDescriptor superclass) {
this.superclass=superclass;
}
}
--- /dev/null
+package IR.Tree;
+
+public class ExpressionNode extends TreeNode {
+
+ public String printNode() {
+ return null;
+ }
+}
*/
public class TypeDescriptor extends Descriptor {
+ public static final int BYTE=1;
+ public static final int SHORT=2;
+ public static final int INT=3;
+ public static final int LONG=4;
+ public static final int CHAR=5;
+ public static final int FLOAT=6;
+ public static final int DOUBLE=7;
+ public static final int CLASS=8;
+
+ int type;
+ NameDescriptor name_desc;
- public TypeDescriptor(String name) {
- super(name);
+ public TypeDescriptor(NameDescriptor name) {
+ super(name.toString());
+ this.type=CLASS;
+ this.name_desc=name;
}
-
+ public TypeDescriptor(int t) {
+ super(decodeInt(t));
+ this.type=t;
+ }
+
+ public String toString() {
+ if (type==CLASS)
+ return name_desc.toString();
+ else
+ return decodeInt(type);
+ }
+
+ private static String decodeInt(int type) {
+ if (type==BYTE)
+ return "byte";
+ else if (type==SHORT)
+ return "short";
+ else if (type==INT)
+ return "int";
+ else if (type==LONG)
+ return "long";
+ else if (type==CHAR)
+ return "char";
+ else if (type==FLOAT)
+ return "float";
+ else if (type==DOUBLE)
+ return "double";
+ else throw new Error();
+ }
}
;
variable_declarator_id ::=
IDENTIFIER:id {:
- RESULT=(new ParseNode("variable_declarator_id")).addChild("name").addChild(id);:}
+ RESULT=(new ParseNode("single")).addChild(id).getRoot();:}
// | variable_declarator_id:id LBRACK RBRACK {:
-// RESULT=(new ParseNode("variable_declarator_id")).addChild("array").addChild(id);:}
+// RESULT=(new ParseNode("array")).addChild(id).getRoot();:}
;
variable_initializer ::=
expression:exp {: RESULT=exp; :}