6 public BuildIR(State state) {
9 public void buildtree() {
10 ParseNode pn=state.parsetree;
11 FileNode fn=parseFile(pn);
12 System.out.println(fn.printNode());
15 /** Parse the classes in this file */
16 public FileNode parseFile(ParseNode pn) {
17 FileNode fn=new FileNode();
18 ParseNode tpn=pn.getChild("type_declaration_list");
20 ParseNodeVector pnv=tpn.getChildren();
21 for(int i=0;i<pnv.size();i++) {
22 ParseNode type_pn=pnv.elementAt(i);
23 if (isEmpty(type_pn)) /* Skip the semicolon */
25 ClassNode cn=parseTypeDecl(type_pn);
32 public ClassNode parseTypeDecl(ParseNode pn) {
33 if (isNode(pn, "class_declaration")) {
34 ClassNode cn=new ClassNode();
35 cn.setName(pn.getChild("name").getTerminal());
36 if (!isEmpty(pn.getChild("super").getTerminal())) {
37 /* parse superclass name */
39 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
40 parseClassBody(cn, pn.getChild("classbody"));
42 } else throw new Error();
45 private void parseClassBody(ClassNode cn, ParseNode pn) {
46 ParseNode decls=pn.getChild("class_body_declaration_list");
48 ParseNodeVector pnv=decls.getChildren();
49 for(int i=0;i<pnv.size();i++) {
50 ParseNode decl=pnv.elementAt(i);
51 if (isNode(decl,"member")) {
52 parseClassMember(cn,decl);
53 } else if (isNode(decl,"constructor")) {
54 } else if (isNode(decl,"block")) {
55 } else throw new Error();
60 private void parseClassMember(ClassNode cn, ParseNode pn) {
61 ParseNode fieldnode=pn.getChild("field");
63 if (fieldnode!=null) {
64 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
67 ParseNode methodnode=pn.getChild("method");
68 if (methodnode!=null) {
69 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
75 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
76 ParseNode tn=pn.getChild("type");
77 String type_st=tn.getTerminal();
78 if(type_st.equals("byte")) {
79 return state.getTypeDescriptor(TypeDescriptor.BYTE);
80 } else if(type_st.equals("short")) {
81 return state.getTypeDescriptor(TypeDescriptor.SHORT);
82 } else if(type_st.equals("boolean")) {
83 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
84 } else if(type_st.equals("int")) {
85 return state.getTypeDescriptor(TypeDescriptor.INT);
86 } else if(type_st.equals("long")) {
87 return state.getTypeDescriptor(TypeDescriptor.LONG);
88 } else if(type_st.equals("char")) {
89 return state.getTypeDescriptor(TypeDescriptor.CHAR);
90 } else if(type_st.equals("float")) {
91 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
92 } else if(type_st.equals("double")) {
93 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
94 } else if(type_st.equals("class")) {
95 ParseNode nn=tn.getChild("class");
96 return state.getTypeDescriptor(parseName(nn));
102 private NameDescriptor parseName(ParseNode pn) {
103 ParseNode nn=pn.getChild("name");
104 ParseNode base=nn.getChild("base");
105 ParseNode id=nn.getChild("identifier");
108 return new NameDescriptor(id.getTerminal());
110 return new NameDescriptor(parseName(base),id.getTerminal());
114 private void parseFieldDecl(ClassNode cn,ParseNode pn) {
115 ParseNode mn=pn.getChild("modifier");
116 Modifiers m=parseModifiersList(mn);
117 ParseNode tn=pn.getChild("type");
118 TypeDescriptor t=parseTypeDescriptor(tn);
119 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
120 ParseNodeVector pnv=vn.getChildren();
121 for(int i=0;i<pnv.size();i++) {
122 ParseNode vardecl=pnv.elementAt(i);
123 String identifier=vardecl.getChild("single").getTerminal();
124 ParseNode epn=vardecl.getChild("initializer");
126 ExpressionNode en=null;
128 en=parseExpression(epn.getFirstChild());
130 cn.addField(new FieldDescriptor(m,t,identifier, en));
135 private ExpressionNode parseExpression(ParseNode pn) {
136 if (isNode(pn,"assignment"))
137 return parseAssignmentExpression(pn);
138 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
139 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
140 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
141 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
142 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
143 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
144 isNode(pn,"rightshift")||isNode(pn,"sub")||
145 isNode(pn,"add")||isNode(pn,"mult")||
146 isNode(pn,"div")||isNode(pn,"mod")) {
147 ParseNodeVector pnv=pn.getChildren();
148 ParseNode left=pnv.elementAt(0);
149 ParseNode right=pnv.elementAt(1);
150 Operation op=new Operation(pn.getLabel());
151 return new OpNode(parseExpression(left),parseExpression(right),op);
152 } else if (isNode(pn,"unaryplus")||
153 isNode(pn,"unaryminus")||
154 isNode(pn,"postinc")||
155 isNode(pn,"postdec")||
156 isNode(pn,"preinc")||
157 isNode(pn,"predec")) {
158 ParseNode left=pn.getFirstChild();
159 Operation op=new Operation(pn.getLabel());
160 return new OpNode(parseExpression(left),op);
161 } else if (isNode(pn,"literal")) {
162 String literaltype=pn.getTerminal();
163 ParseNode literalnode=pn.getChild(literaltype);
164 Object literal_obj=literalnode.getLiteral();
165 return new LiteralNode(literaltype, literal_obj);
170 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
175 private void parseMethodDecl(ClassNode cn, ParseNode pn) {
176 ParseNode headern=pn.getChild("method_header");
177 ParseNode bodyn=pn.getChild("body");
178 MethodDescriptor md=parseMethodHeader(headern);
179 MethodBodyNode mbn=parseMethodBody(bodyn);
183 public MethodBodyNode parseMethodBody(ParseNode pn) {
187 public MethodDescriptor parseMethodHeader(ParseNode pn) {
188 ParseNode mn=pn.getChild("modifiers");
189 Modifiers m=parseModifiersList(mn);
191 ParseNode tn=pn.getChild("returntype");
192 TypeDescriptor returntype;
194 returntype=parseTypeDescriptor(tn);
196 returntype=new TypeDescriptor(TypeDescriptor.VOID);
198 ParseNode pmd=pn.getChild("method_declarator");
199 String name=pmd.getChild("name").getTerminal();
200 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
202 ParseNode paramnode=pmd.getChild("parameters");
203 parseParameterList(md,paramnode);
207 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
208 ParseNode paramlist=pn.getChild("formal_parameter_list");
211 ParseNodeVector pnv=paramlist.getChildren();
212 for(int i=0;i<pnv.size();i++) {
213 ParseNode paramn=pnv.elementAt(i);
214 TypeDescriptor type=parseTypeDescriptor(paramn);
215 String paramname=paramn.getChild("single").getTerminal();
216 md.addParameter(type,paramname);
220 public Modifiers parseModifiersList(ParseNode pn) {
221 Modifiers m=new Modifiers();
222 ParseNode modlist=pn.getChild("modifier_list");
224 ParseNodeVector pnv=modlist.getChildren();
225 for(int i=0;i<pnv.size();i++) {
226 ParseNode modn=pnv.elementAt(i);
227 if (isNode(modn,"public"))
228 m.addModifier(Modifiers.PUBLIC);
229 if (isNode(modn,"protected"))
230 m.addModifier(Modifiers.PROTECTED);
231 if (isNode(modn,"private"))
232 m.addModifier(Modifiers.PRIVATE);
233 if (isNode(modn,"static"))
234 m.addModifier(Modifiers.STATIC);
235 if (isNode(modn,"final"))
236 m.addModifier(Modifiers.FINAL);
237 if (isNode(modn,"native"))
238 m.addModifier(Modifiers.NATIVE);
244 private boolean isNode(ParseNode pn, String label) {
245 if (pn.getLabel().equals(label))
250 private static boolean isEmpty(ParseNode pn) {
251 if (pn.getLabel().equals("empty"))
257 private static boolean isEmpty(String s) {
258 if (s.equals("empty"))
264 /** Throw an exception if something is unexpected */
265 private void check(ParseNode pn, String label) {
267 throw new Error(pn+ "IE: Expected '" + label + "', got null");
269 if (! pn.getLabel().equals(label)) {
270 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");