adding/chaning many files
[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             FieldDescriptor fd=parseFieldDecl(fieldnode.getChild("field_declaration"));
65             cn.addField(fd);
66             return;
67         }
68         ParseNode methodnode=pn.getChild("method");
69         if (methodnode!=null) {
70             parseMethodDecl(cn,methodnode);
71             return;
72         }
73         throw new Error();
74     }
75
76     private FieldDescriptor parseFieldDecl(ParseNode pn) {
77         ParseNode mn=pn.getChild("modifier");
78         Modifiers m=parseModifiersList(mn);
79         return new FieldDescriptor(m,null,null);
80     }
81
82     private void parseMethodDecl(ClassNode cn, ParseNode pn) {
83         
84     }
85
86     public Modifiers parseModifiersList(ParseNode pn) {
87         Modifiers m=new Modifiers();
88         ParseNode modlist=pn.getChild("modifier_list");
89         if (modlist!=null) {
90             ParseNodeVector pnv=modlist.getChildren();
91             for(int i=0;i<pnv.size();i++) {
92                 ParseNode modn=pnv.elementAt(i);
93                 if (isNode(modn,"public"))
94                     m.addModifier(Modifiers.PUBLIC);
95                 if (isNode(modn,"protected"))
96                     m.addModifier(Modifiers.PROTECTED);
97                 if (isNode(modn,"private"))
98                     m.addModifier(Modifiers.PRIVATE);
99                 if (isNode(modn,"static"))
100                     m.addModifier(Modifiers.STATIC);
101                 if (isNode(modn,"final"))
102                     m.addModifier(Modifiers.FINAL);
103                 if (isNode(modn,"native"))
104                     m.addModifier(Modifiers.NATIVE);
105             }
106         }
107         return m;
108     }
109
110     private boolean isNode(ParseNode pn, String label) {
111         if (pn.getLabel().equals(label))
112             return true;
113         else return false;
114     }
115
116     private static boolean isEmpty(ParseNode pn) {
117         if (pn.getLabel().equals("empty"))
118             return true;
119         else
120             return false;
121     }
122
123     private static boolean isEmpty(String s) {
124         if (s.equals("empty"))
125             return true;
126         else
127             return false;
128     }
129         
130
131     /** Throw an exception if something is unexpected */
132     private void check(ParseNode pn, String label) {
133         if (pn == null) {
134             throw new Error(pn+ "IE: Expected '" + label + "', got null");
135         }
136         if (! pn.getLabel().equals(label)) {
137             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
138         }
139     }
140
141 }