Checking in updates
[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.getChild("method_declaration"));
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("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));
97         } else {
98             throw new Error();
99         }
100     }
101
102     private NameDescriptor parseName(ParseNode pn) {
103         ParseNode nn=pn.getChild("name");
104         ParseNode base=nn.getChild("base");
105         ParseNode id=nn.getChild("identifier");
106         
107         if (base==null)
108             return new NameDescriptor(id.getTerminal());
109
110         return new NameDescriptor(parseName(base),id.getTerminal());
111         
112     }
113
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");
125             
126             ExpressionNode en=null;
127             if (epn!=null)
128                 en=parseExpression(epn.getFirstChild());
129   
130             cn.addField(new FieldDescriptor(m,t,identifier, en));
131         }
132         
133     }
134
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);
166         }
167         throw new Error();
168     }
169
170     private ExpressionNode parseAssignmentExpression(ParseNode pn) {
171         return null;
172     }
173
174
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);
180         cn.addMethod(md);
181     }
182
183     public MethodBodyNode parseMethodBody(ParseNode pn) {
184         
185     }
186
187     public MethodDescriptor parseMethodHeader(ParseNode pn) {
188         ParseNode mn=pn.getChild("modifiers");
189         Modifiers m=parseModifiersList(mn);
190         
191         ParseNode tn=pn.getChild("returntype");
192         TypeDescriptor returntype;
193         if (tn!=null) 
194             returntype=parseTypeDescriptor(tn);
195         else
196             returntype=new TypeDescriptor(TypeDescriptor.VOID);
197
198         ParseNode pmd=pn.getChild("method_declarator");
199         String name=pmd.getChild("name").getTerminal();
200         MethodDescriptor md=new MethodDescriptor(m, returntype, name);
201        
202         ParseNode paramnode=pmd.getChild("parameters");
203         parseParameterList(md,paramnode);
204         return md;
205     }
206
207     public void parseParameterList(MethodDescriptor md, ParseNode pn) {
208         ParseNode paramlist=pn.getChild("formal_parameter_list");
209         if (paramlist==null)
210             return;
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);
217          }
218     }
219
220     public Modifiers parseModifiersList(ParseNode pn) {
221         Modifiers m=new Modifiers();
222         ParseNode modlist=pn.getChild("modifier_list");
223         if (modlist!=null) {
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);
239             }
240         }
241         return m;
242     }
243
244     private boolean isNode(ParseNode pn, String label) {
245         if (pn.getLabel().equals(label))
246             return true;
247         else return false;
248     }
249
250     private static boolean isEmpty(ParseNode pn) {
251         if (pn.getLabel().equals("empty"))
252             return true;
253         else
254             return false;
255     }
256
257     private static boolean isEmpty(String s) {
258         if (s.equals("empty"))
259             return true;
260         else
261             return false;
262     }
263
264     /** Throw an exception if something is unexpected */
265     private void check(ParseNode pn, String label) {
266         if (pn == null) {
267             throw new Error(pn+ "IE: Expected '" + label + "', got null");
268         }
269         if (! pn.getLabel().equals(label)) {
270             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
271         }
272     }
273 }