changes`
[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);
126   
127             cn.addField(new FieldDescriptor(m,t,identifier, en));
128         }
129         
130     }
131
132     private ExpressionNode parseExpression(ParseNode pn) {
133         return null;
134     }
135
136
137     private void parseMethodDecl(ClassNode cn, ParseNode pn) {
138         
139     }
140
141     public Modifiers parseModifiersList(ParseNode pn) {
142         Modifiers m=new Modifiers();
143         ParseNode modlist=pn.getChild("modifier_list");
144         if (modlist!=null) {
145             ParseNodeVector pnv=modlist.getChildren();
146             for(int i=0;i<pnv.size();i++) {
147                 ParseNode modn=pnv.elementAt(i);
148                 if (isNode(modn,"public"))
149                     m.addModifier(Modifiers.PUBLIC);
150                 if (isNode(modn,"protected"))
151                     m.addModifier(Modifiers.PROTECTED);
152                 if (isNode(modn,"private"))
153                     m.addModifier(Modifiers.PRIVATE);
154                 if (isNode(modn,"static"))
155                     m.addModifier(Modifiers.STATIC);
156                 if (isNode(modn,"final"))
157                     m.addModifier(Modifiers.FINAL);
158                 if (isNode(modn,"native"))
159                     m.addModifier(Modifiers.NATIVE);
160             }
161         }
162         return m;
163     }
164
165     private boolean isNode(ParseNode pn, String label) {
166         if (pn.getLabel().equals(label))
167             return true;
168         else return false;
169     }
170
171     private static boolean isEmpty(ParseNode pn) {
172         if (pn.getLabel().equals("empty"))
173             return true;
174         else
175             return false;
176     }
177
178     private static boolean isEmpty(String s) {
179         if (s.equals("empty"))
180             return true;
181         else
182             return false;
183     }
184
185     /** Throw an exception if something is unexpected */
186     private void check(ParseNode pn, String label) {
187         if (pn == null) {
188             throw new Error(pn+ "IE: Expected '" + label + "', got null");
189         }
190         if (! pn.getLabel().equals(label)) {
191             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
192         }
193     }
194 }