add test case
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3
4 public class BuildIR {
5     State state;
6     public BuildIR(State state) {
7         this.state=state;
8     }
9     public void buildtree() {
10         ParseNode pn=state.parsetree;
11         FileNode fn=parseFile(pn);
12         System.out.println(fn.printNode());
13     }
14
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");
19         if (tpn!=null) {
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 */
24                     continue;
25                 ClassNode cn=parseTypeDecl(type_pn);
26                 fn.addClass(cn);
27             }
28         }
29         return fn;
30     }
31
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 */
38             }
39             cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
40             parseClassBody(cn, pn.getChild("classbody"));
41             return cn;
42         } else throw new Error();
43     }
44
45     private void parseClassBody(ClassNode cn, ParseNode pn) {
46         ParseNode decls=pn.getChild("class_body_declaration_list");
47         if (decls!=null) {
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();
56             }
57         }
58     }
59
60     private void parseClassMember(ClassNode cn, ParseNode pn) {
61         ParseNode fieldnode=pn.getChild("field");
62
63         if (fieldnode!=null) {
64             parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
65             return;
66         }
67         ParseNode methodnode=pn.getChild("method");
68         if (methodnode!=null) {
69             parseMethodDecl(cn,methodnode);
70             return;
71         }
72         throw new Error();
73     }
74
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));
95         } else
96             throw new Error();
97     }
98
99     private NameDescriptor parseName(ParseNode pn) {
100         ParseNode nn=pn.getChild("name");
101         ParseNode base=nn.getChild("base");
102         ParseNode id=nn.getChild("identifier");
103         
104         if (base==null)
105             return new NameDescriptor(id.getTerminal());
106
107         return new NameDescriptor(parseName(base),id.getTerminal());
108         
109     }
110
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");
122             
123             ExpressionNode en=null;
124             if (epn!=null)
125                 en=parseExpression(epn.getFirstChild());
126   
127             cn.addField(new FieldDescriptor(m,t,identifier, en));
128         }
129         
130     }
131
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);
163         }
164         throw new Error();
165     }
166
167     private ExpressionNode parseAssignmentExpression(ParseNode pn) {
168         return null;
169     }
170
171
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);
176     }
177
178     public MethodDescriptor parseMethodHeader(ParseNode pn) {
179         ParseNode mn=pn.getChild("modifier");
180         Modifiers m=parseModifiersList(mn);
181         
182         ParseNode tn=pn.getChild("returntype");
183         TypeDescriptor returntype=parseTypeDescriptor(tn);
184         MethodDescriptor md=new MethodDescriptor(m, returntype, null);
185         return md;
186     }
187
188     public Modifiers parseModifiersList(ParseNode pn) {
189         Modifiers m=new Modifiers();
190         ParseNode modlist=pn.getChild("modifier_list");
191         if (modlist!=null) {
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);
207             }
208         }
209         return m;
210     }
211
212     private boolean isNode(ParseNode pn, String label) {
213         if (pn.getLabel().equals(label))
214             return true;
215         else return false;
216     }
217
218     private static boolean isEmpty(ParseNode pn) {
219         if (pn.getLabel().equals("empty"))
220             return true;
221         else
222             return false;
223     }
224
225     private static boolean isEmpty(String s) {
226         if (s.equals("empty"))
227             return true;
228         else
229             return false;
230     }
231
232     /** Throw an exception if something is unexpected */
233     private void check(ParseNode pn, String label) {
234         if (pn == null) {
235             throw new Error(pn+ "IE: Expected '" + label + "', got null");
236         }
237         if (! pn.getLabel().equals(label)) {
238             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
239         }
240     }
241 }