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);
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("int")) {
83 return state.getTypeDescriptor(TypeDescriptor.INT);
84 } else if(type_st.equals("long")) {
85 return state.getTypeDescriptor(TypeDescriptor.LONG);
86 } else if(type_st.equals("char")) {
87 return state.getTypeDescriptor(TypeDescriptor.CHAR);
88 } else if(type_st.equals("float")) {
89 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
90 } else if(type_st.equals("double")) {
91 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
92 } else if(type_st.equals("class")) {
93 ParseNode nn=tn.getChild("class");
94 return state.getTypeDescriptor(parseName(nn));
99 private NameDescriptor parseName(ParseNode pn) {
100 ParseNode nn=pn.getChild("name");
101 ParseNode base=nn.getChild("base");
102 ParseNode id=nn.getChild("identifier");
105 return new NameDescriptor(id.getTerminal());
107 return new NameDescriptor(parseName(base),id.getTerminal());
111 private void parseFieldDecl(ClassNode cn,ParseNode pn) {
112 ParseNode mn=pn.getChild("modifier");
113 Modifiers m=parseModifiersList(mn);
114 ParseNode tn=pn.getChild("type");
115 TypeDescriptor t=parseTypeDescriptor(tn);
116 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
117 ParseNodeVector pnv=vn.getChildren();
118 for(int i=0;i<pnv.size();i++) {
119 ParseNode vardecl=pnv.elementAt(i);
120 String identifier=vardecl.getChild("identifier").getChild("single").getTerminal();
121 ParseNode epn=vardecl.getChild("initializer");
123 ExpressionNode en=null;
125 en=parseExpression(epn.getFirstChild());
127 cn.addField(new FieldDescriptor(m,t,identifier, en));
132 private ExpressionNode parseExpression(ParseNode pn) {
133 if (isNode(pn,"assignment"))
134 return parseAssignmentExpression(pn);
135 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
136 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
137 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
138 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
139 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
140 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
141 isNode(pn,"rightshift")||isNode(pn,"sub")||
142 isNode(pn,"add")||isNode(pn,"mult")||
143 isNode(pn,"div")||isNode(pn,"mod")) {
144 ParseNodeVector pnv=pn.getChildren();
145 ParseNode left=pnv.elementAt(0);
146 ParseNode right=pnv.elementAt(1);
147 Operation op=new Operation(pn.getLabel());
148 return new OpNode(parseExpression(left),parseExpression(right),op);
149 } else if (isNode(pn,"unaryplus")||
150 isNode(pn,"unaryminus")||
151 isNode(pn,"postinc")||
152 isNode(pn,"postdec")||
153 isNode(pn,"preinc")||
154 isNode(pn,"predec")) {
155 ParseNode left=pn.getFirstChild();
156 Operation op=new Operation(pn.getLabel());
157 return new OpNode(parseExpression(left),op);
158 } else if (isNode(pn,"literal")) {
159 String literaltype=pn.getTerminal();
160 ParseNode literalnode=pn.getChild(literaltype);
161 Object literal_obj=literalnode.getLiteral();
162 return new LiteralNode(literaltype, literal_obj);
167 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
172 private void parseMethodDecl(ClassNode cn, ParseNode pn) {
173 ParseNode headern=pn.getChild("header");
174 ParseNode bodyn=pn.getChild("body");
175 MethodDescriptor md=parseMethodHeader(headern);
178 public MethodDescriptor parseMethodHeader(ParseNode pn) {
179 ParseNode mn=pn.getChild("modifier");
180 Modifiers m=parseModifiersList(mn);
182 ParseNode tn=pn.getChild("returntype");
183 TypeDescriptor returntype=parseTypeDescriptor(tn);
184 MethodDescriptor md=new MethodDescriptor(m, returntype, null);
188 public Modifiers parseModifiersList(ParseNode pn) {
189 Modifiers m=new Modifiers();
190 ParseNode modlist=pn.getChild("modifier_list");
192 ParseNodeVector pnv=modlist.getChildren();
193 for(int i=0;i<pnv.size();i++) {
194 ParseNode modn=pnv.elementAt(i);
195 if (isNode(modn,"public"))
196 m.addModifier(Modifiers.PUBLIC);
197 if (isNode(modn,"protected"))
198 m.addModifier(Modifiers.PROTECTED);
199 if (isNode(modn,"private"))
200 m.addModifier(Modifiers.PRIVATE);
201 if (isNode(modn,"static"))
202 m.addModifier(Modifiers.STATIC);
203 if (isNode(modn,"final"))
204 m.addModifier(Modifiers.FINAL);
205 if (isNode(modn,"native"))
206 m.addModifier(Modifiers.NATIVE);
212 private boolean isNode(ParseNode pn, String label) {
213 if (pn.getLabel().equals(label))
218 private static boolean isEmpty(ParseNode pn) {
219 if (pn.getLabel().equals("empty"))
225 private static boolean isEmpty(String s) {
226 if (s.equals("empty"))
232 /** Throw an exception if something is unexpected */
233 private void check(ParseNode pn, String label) {
235 throw new Error(pn+ "IE: Expected '" + label + "', got null");
237 if (! pn.getLabel().equals(label)) {
238 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");