package IR;
import IR.Tree.Modifiers;
import IR.Tree.ExpressionNode;
+import java.util.Vector;
/**
* Descriptor
protected Modifiers modifier;
protected TypeDescriptor returntype;
protected String identifier;
+ protected Vector param_name;
+ protected Vector param_type;
public MethodDescriptor(Modifiers m, TypeDescriptor rt, String identifier) {
super(identifier);
this.identifier=identifier;
this.safename = "__" + name + "__";
this.uniqueid=count++;
+ param_name=new Vector();
+ param_type=new Vector();
+ }
+ public void addParameter(TypeDescriptor type, String paramname) {
+ param_name.add(paramname);
+ param_type.add(type);
}
public String toString() {
- return modifier.toString()+td.toString()+" "+identifier+"()";
+ String st=modifier.toString()+returntype.toString()+" "+identifier+"(";
+ for(int i=0;i<param_type.size();i++) {
+ st+=param_type.get(i).toString()+" "+param_name.get(i).toString();
+ if ((i+1)!=param_type.size())
+ st+=", ";
+ }
+ st+=")";
+ return st;
}
}
}
ParseNode methodnode=pn.getChild("method");
if (methodnode!=null) {
- parseMethodDecl(cn,methodnode);
+ parseMethodDecl(cn,methodnode.getChild("method_declaration"));
return;
}
throw new Error();
return state.getTypeDescriptor(TypeDescriptor.BYTE);
} else if(type_st.equals("short")) {
return state.getTypeDescriptor(TypeDescriptor.SHORT);
+ } else if(type_st.equals("boolean")) {
+ return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
} else if(type_st.equals("int")) {
return state.getTypeDescriptor(TypeDescriptor.INT);
} else if(type_st.equals("long")) {
} else if(type_st.equals("class")) {
ParseNode nn=tn.getChild("class");
return state.getTypeDescriptor(parseName(nn));
- } else
+ } else {
throw new Error();
+ }
}
private NameDescriptor parseName(ParseNode pn) {
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();
+ String identifier=vardecl.getChild("single").getTerminal();
ParseNode epn=vardecl.getChild("initializer");
ExpressionNode en=null;
private void parseMethodDecl(ClassNode cn, ParseNode pn) {
- ParseNode headern=pn.getChild("header");
+ ParseNode headern=pn.getChild("method_header");
ParseNode bodyn=pn.getChild("body");
MethodDescriptor md=parseMethodHeader(headern);
+ MethodBodyNode mbn=parseMethodBody(bodyn);
+ cn.addMethod(md);
+ }
+
+ public MethodBodyNode parseMethodBody(ParseNode pn) {
+
}
public MethodDescriptor parseMethodHeader(ParseNode pn) {
- ParseNode mn=pn.getChild("modifier");
+ ParseNode mn=pn.getChild("modifiers");
Modifiers m=parseModifiersList(mn);
ParseNode tn=pn.getChild("returntype");
- TypeDescriptor returntype=parseTypeDescriptor(tn);
- MethodDescriptor md=new MethodDescriptor(m, returntype, null);
+ TypeDescriptor returntype;
+ if (tn!=null)
+ returntype=parseTypeDescriptor(tn);
+ else
+ returntype=new TypeDescriptor(TypeDescriptor.VOID);
+
+ ParseNode pmd=pn.getChild("method_declarator");
+ String name=pmd.getChild("name").getTerminal();
+ MethodDescriptor md=new MethodDescriptor(m, returntype, name);
+
+ ParseNode paramnode=pmd.getChild("parameters");
+ parseParameterList(md,paramnode);
return md;
}
+ public void parseParameterList(MethodDescriptor md, ParseNode pn) {
+ ParseNode paramlist=pn.getChild("formal_parameter_list");
+ if (paramlist==null)
+ return;
+ ParseNodeVector pnv=paramlist.getChildren();
+ for(int i=0;i<pnv.size();i++) {
+ ParseNode paramn=pnv.elementAt(i);
+ TypeDescriptor type=parseTypeDescriptor(paramn);
+ String paramname=paramn.getChild("single").getTerminal();
+ md.addParameter(type,paramname);
+ }
+ }
+
public Modifiers parseModifiersList(ParseNode pn) {
Modifiers m=new Modifiers();
ParseNode modlist=pn.getChild("modifier_list");
package IR.Tree;
import java.util.Vector;
import IR.FieldDescriptor;
+import IR.MethodDescriptor;
import IR.NameDescriptor;
class ClassNode extends TreeNode {
classname=null;
superclass=null;
fields=new Vector();
+ methods=new Vector();
}
String classname;
NameDescriptor superclass;
--- /dev/null
+package IR.Tree;
+
+class MethodBodyNode extends TreeNode {
+
+ public String printNode() {
+ return null;
+ }
+}
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 VOID=8;
- public static final int CLASS=9;
+ public static final int BOOLEAN=6;
+ public static final int FLOAT=7;
+ public static final int DOUBLE=8;
+ public static final int VOID=9;
+ public static final int CLASS=10;
int type;
private static String decodeInt(int type) {
if (type==BYTE)
return "byte";
+ else if (type==BOOLEAN)
+ return "boolean";
else if (type==SHORT)
return "short";
else if (type==INT)
variable_declarator ::=
variable_declarator_id:id {:
ParseNode pn=new ParseNode("variable_declarator");
- pn.addChild("identifier").addChild(id);
+ pn.addChild(id);
RESULT=pn;
:}
| variable_declarator_id:id EQ variable_initializer:init {:
ParseNode pn=new ParseNode("variable_declarator");
- pn.addChild("identifier").addChild(id);
+ pn.addChild(id);
pn.addChild("initializer").addChild(init);
RESULT=pn;
:}
method_declaration ::=
method_header:header method_body:body {:
ParseNode pn=new ParseNode("method_declaration");
- pn.addChild("header").addChild(header);
+ pn.addChild(header);
pn.addChild("body").addChild(body);
RESULT=pn;
:}
ParseNode pn=new ParseNode("method_header");
pn.addChild("modifiers").addChild(mo);
pn.addChild("returntype").addChild(type);
- pn.addChild("declarator").addChild(decl);
+ pn.addChild(decl);
RESULT=pn;
:}
| modifiers_opt:mo VOID method_declarator:decl //throws_opt
{:
ParseNode pn=new ParseNode("method_header");
pn.addChild("modifiers").addChild(mo);
- pn.addChild("declarator").addChild(decl);
+ pn.addChild(decl);
RESULT=pn;
:}
;
formal_parameter ::=
type:type variable_declarator_id:name {:
ParseNode pn=new ParseNode("formal_parameter");
- pn.addChild("type").addChild(type);
- pn.addChild("name").addChild(name);
+ pn.addChild(type);
+ pn.addChild(name);
RESULT=pn;
:}
// | FINAL type variable_declarator_id