bfcf62a760e92896199b3c829a967e75a16b5028
[IRC.git] / Robust / src / IR / Tree / BuildIR.java
1 package IR.Tree;
2 import IR.*;
3 import java.util.*;
4
5
6 public class BuildIR {
7     State state;
8     public BuildIR(State state) {
9         this.state=state;
10     }
11     public void buildtree() {
12         for(Iterator it=state.parsetrees.iterator();it.hasNext();) {
13             ParseNode pn=(ParseNode)it.next();
14             parseFile(pn);
15         }
16     }
17
18     /** Parse the classes in this file */
19     public void parseFile(ParseNode pn) {
20         NameDescriptor packages;
21         Vector singleimports=new Vector();
22         Vector multiimports=new Vector();
23
24         ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
25         if (ipn!=null) {
26             ParseNodeVector pnv=ipn.getChildren();
27             for(int i=0;i<pnv.size();i++) {
28                 ParseNode pnimport=pnv.elementAt(i);
29                 NameDescriptor nd=parseName(pnimport.getChild("name"));
30                 if (isNode(pnimport,"import_single"))
31                     singleimports.add(nd);
32                 else
33                     multiimports.add(nd);
34             }
35         }
36         ParseNode ppn=pn.getChild("packages").getChild("package");
37         if (ppn!=null) {
38             packages=parseName(pn.getChild("name"));
39         }
40         ParseNode tpn=pn.getChild("type_declaration_list");
41         if (tpn!=null) {
42             ParseNodeVector pnv=tpn.getChildren();
43             for(int i=0;i<pnv.size();i++) {
44                 ParseNode type_pn=pnv.elementAt(i);
45                 if (isEmpty(type_pn)) /* Skip the semicolon */
46                     continue;
47                 if (isNode(type_pn,"class_declaration")) {
48                     ClassDescriptor cn=parseTypeDecl(type_pn);
49                     state.addClass(cn);
50                 } else if (isNode(type_pn,"task_declaration")) {
51                     TaskDescriptor td=parseTaskDecl(type_pn);
52                     state.addTask(td);
53                 } else {
54                     throw new Error(type_pn.getLabel());
55                 }
56             }
57         }
58     }
59
60     public TaskDescriptor parseTaskDecl(ParseNode pn) {
61         TaskDescriptor td=new TaskDescriptor(pn.getChild("name").getTerminal());
62         ParseNode bodyn=pn.getChild("body");
63         BlockNode bn=parseBlock(bodyn);
64         parseParameterList(td, pn);
65         state.addTreeCode(td,bn);
66         if (pn.getChild("flag_effects_list")!=null)
67             td.addFlagEffects(parseFlags(pn.getChild("flag_effects_list")));
68         return td;
69     }
70
71     public Vector parseFlags(ParseNode pn) {
72         Vector vfe=new Vector();
73         ParseNodeVector pnv=pn.getChildren();
74         for(int i=0;i<pnv.size();i++) {
75             ParseNode fn=pnv.elementAt(i);
76             FlagEffects fe=parseFlagEffects(fn);
77             vfe.add(fe);
78         }
79         return vfe;
80     }
81
82     public FlagEffects parseFlagEffects(ParseNode pn) {
83         if (isNode(pn,"flag_effect")) {
84             String flagname=pn.getChild("name").getTerminal();
85             FlagEffects fe=new FlagEffects(flagname);
86             if (pn.getChild("flag_list")!=null)
87                 parseFlagEffect(fe, pn.getChild("flag_list"));
88             if (pn.getChild("tag_list")!=null)
89                 parseTagEffect(fe, pn.getChild("tag_list"));
90             return fe;
91         } else throw new Error();
92     }
93     
94     public void parseTagEffect(FlagEffects fes, ParseNode pn) {
95         ParseNodeVector pnv=pn.getChildren();
96         for(int i=0;i<pnv.size();i++) {
97             ParseNode pn2=pnv.elementAt(i);
98             boolean status=true;
99             if (isNode(pn2,"not")) {
100                 status=false;
101                 pn2=pn2.getChild("name");
102             }
103             String name=pn2.getTerminal();
104             fes.addTagEffect(new TagEffect(name,status));
105         }
106     }
107
108     public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
109         ParseNodeVector pnv=pn.getChildren();
110         for(int i=0;i<pnv.size();i++) {
111             ParseNode pn2=pnv.elementAt(i);
112             boolean status=true;
113             if (isNode(pn2,"not")) {
114                 status=false;
115                 pn2=pn2.getChild("name");
116             }
117             String name=pn2.getTerminal();
118             fes.addEffect(new FlagEffect(name,status));
119         }
120     }
121
122     public FlagExpressionNode parseFlagExpression(ParseNode pn) {
123         if (isNode(pn,"or")) {
124             ParseNodeVector pnv=pn.getChildren();
125             ParseNode left=pnv.elementAt(0);
126             ParseNode right=pnv.elementAt(1);
127             return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
128         } else if (isNode(pn,"and")) {
129             ParseNodeVector pnv=pn.getChildren();
130             ParseNode left=pnv.elementAt(0);
131             ParseNode right=pnv.elementAt(1);
132             return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
133         } else if (isNode(pn, "not")) {
134             ParseNodeVector pnv=pn.getChildren();
135             ParseNode left=pnv.elementAt(0);
136             return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));           
137
138         } else if (isNode(pn,"name")) {
139             return new FlagNode(pn.getTerminal());
140         } else {
141             throw new Error();
142         }
143     }
144
145     public Vector parseChecks(ParseNode pn) {
146         Vector ccs=new Vector();
147         ParseNodeVector pnv=pn.getChildren();
148         for(int i=0;i<pnv.size();i++) {
149             ParseNode fn=pnv.elementAt(i);
150             ConstraintCheck cc=parseConstraintCheck(fn);
151             ccs.add(cc);
152         }
153         return ccs;
154     }
155
156     public ConstraintCheck parseConstraintCheck(ParseNode pn) {
157         if (isNode(pn,"cons_check")) {
158             String specname=pn.getChild("name").getChild("identifier").getTerminal();
159             Vector[] args=parseConsArgumentList(pn);
160             ConstraintCheck cc=new ConstraintCheck(specname);
161             for(int i=0;i<args[0].size();i++) {
162                 cc.addVariable((String)args[0].get(i));
163                 cc.addArgument((ExpressionNode)args[1].get(i));
164             }
165             return cc;
166         } else throw new Error();
167     }
168
169     public void parseParameterList(TaskDescriptor td, ParseNode pn) {
170         ParseNode paramlist=pn.getChild("task_parameter_list");
171         if (paramlist==null)
172             return;
173          ParseNodeVector pnv=paramlist.getChildren();
174          for(int i=0;i<pnv.size();i++) {
175              ParseNode paramn=pnv.elementAt(i);
176              TypeDescriptor type=parseTypeDescriptor(paramn);
177
178              String paramname=paramn.getChild("single").getTerminal();
179              FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
180
181              ParseNode tagnode=paramn.getChild("tag");
182
183              TagExpressionList tel=null;
184              if (tagnode!=null) {
185                  tel=parseTagExpressionList(tagnode);
186              }
187              
188              td.addParameter(type,paramname,fen, tel);
189          }
190     }
191
192     public TagExpressionList parseTagExpressionList(ParseNode pn) {
193         ParseNodeVector pnv=pn.getChildren();
194         TagExpressionList tel=new TagExpressionList();
195         for(int i=0;i<pnv.size();i++) {
196             ParseNode tn=pnv.elementAt(i);
197             String type=tn.getChild("type").getTerminal();
198             String name=tn.getChild("single").getTerminal();
199             tel.addTag(type, name);
200         }
201         return tel;
202     }
203
204     public ClassDescriptor parseTypeDecl(ParseNode pn) {
205         ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
206         if (!isEmpty(pn.getChild("super").getTerminal())) {
207             /* parse superclass name */
208             ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
209             NameDescriptor nd=parseName(snn);
210             cn.setSuper(nd.toString());
211         } else {
212             if (!cn.getSymbol().equals(TypeUtil.ObjectClass))
213                 cn.setSuper(TypeUtil.ObjectClass);
214         }
215         cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
216         parseClassBody(cn, pn.getChild("classbody"));
217         return cn;
218     }
219
220     private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
221         ParseNode decls=pn.getChild("class_body_declaration_list");
222         if (decls!=null) {
223             ParseNodeVector pnv=decls.getChildren();
224             for(int i=0;i<pnv.size();i++) {
225                 ParseNode decl=pnv.elementAt(i);
226                 if (isNode(decl,"member")) {
227                     parseClassMember(cn,decl);
228                 } else if (isNode(decl,"constructor")) {
229                     parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
230                 } else if (isNode(decl,"block")) {
231                 } else throw new Error();
232             }
233         }
234     }
235
236     private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
237         ParseNode fieldnode=pn.getChild("field");
238
239         if (fieldnode!=null) {
240             parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
241             return;
242         }
243         ParseNode methodnode=pn.getChild("method");
244         if (methodnode!=null) {
245             parseMethodDecl(cn,methodnode.getChild("method_declaration"));
246             return;
247         }
248         ParseNode flagnode=pn.getChild("flag");
249         if (flagnode!=null) {
250             parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
251             return;
252         }
253         throw new Error();
254     }
255
256     private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
257         ParseNode tn=pn.getChild("type");
258
259         String type_st=tn.getTerminal();
260         if(type_st.equals("byte")) {
261             return state.getTypeDescriptor(TypeDescriptor.BYTE);
262         } else if(type_st.equals("short")) {
263             return state.getTypeDescriptor(TypeDescriptor.SHORT);
264         } else if(type_st.equals("boolean")) {
265             return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
266         } else if(type_st.equals("int")) {
267             return state.getTypeDescriptor(TypeDescriptor.INT);
268         } else if(type_st.equals("long")) {
269             return state.getTypeDescriptor(TypeDescriptor.LONG);
270         } else if(type_st.equals("char")) {
271             return state.getTypeDescriptor(TypeDescriptor.CHAR);
272         } else if(type_st.equals("float")) {
273             return state.getTypeDescriptor(TypeDescriptor.FLOAT);
274         } else if(type_st.equals("double")) {
275             return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
276         } else if(type_st.equals("class")) {
277             ParseNode nn=tn.getChild("class");
278             return state.getTypeDescriptor(parseName(nn.getChild("name")));
279         } else if(type_st.equals("array")) {
280             ParseNode nn=tn.getChild("array");
281             TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
282             Integer numdims=(Integer)nn.getChild("dims").getLiteral();
283             for(int i=0;i<numdims.intValue();i++)
284                 td=td.makeArray(state);
285             return td;
286         } else {
287             throw new Error();
288         }
289     }
290
291     private NameDescriptor parseName(ParseNode nn) {
292         ParseNode base=nn.getChild("base");
293         ParseNode id=nn.getChild("identifier");
294         if (base==null)
295             return new NameDescriptor(id.getTerminal());
296         return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
297         
298     }
299
300     private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
301         String name=pn.getChild("name").getTerminal();
302         FlagDescriptor flag=new FlagDescriptor(name);
303         if (pn.getChild("external")!=null)
304             flag.makeExternal();
305         cn.addFlag(flag);
306     }
307
308     private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
309         ParseNode mn=pn.getChild("modifier");
310         Modifiers m=parseModifiersList(mn);
311
312         ParseNode tn=pn.getChild("type");
313         TypeDescriptor t=parseTypeDescriptor(tn);
314         ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
315         ParseNodeVector pnv=vn.getChildren();
316         for(int i=0;i<pnv.size();i++) {
317             ParseNode vardecl=pnv.elementAt(i);
318
319             
320             ParseNode tmp=vardecl;
321             TypeDescriptor arrayt=t;
322             while (tmp.getChild("single")==null) {
323                 arrayt=arrayt.makeArray(state);
324                 tmp=tmp.getChild("array");
325             }
326             String identifier=tmp.getChild("single").getTerminal();
327
328             ParseNode epn=vardecl.getChild("initializer");
329             
330             ExpressionNode en=null;
331             if (epn!=null)
332                 en=parseExpression(epn.getFirstChild());
333   
334             cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
335         }
336         
337     }
338
339     private ExpressionNode parseExpression(ParseNode pn) {
340         if (isNode(pn,"assignment"))
341             return parseAssignmentExpression(pn);
342         else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
343                  isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
344                  isNode(pn,"bitwise_and")||isNode(pn,"equal")||
345                  isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
346                  isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
347                  isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
348                  isNode(pn,"rightshift")||isNode(pn,"sub")||
349                  isNode(pn,"add")||isNode(pn,"mult")||
350                  isNode(pn,"div")||isNode(pn,"mod")) {
351             ParseNodeVector pnv=pn.getChildren();
352             ParseNode left=pnv.elementAt(0);
353             ParseNode right=pnv.elementAt(1);
354             Operation op=new Operation(pn.getLabel());
355             return new OpNode(parseExpression(left),parseExpression(right),op);
356         } else if (isNode(pn,"unaryplus")||
357                    isNode(pn,"unaryminus")||
358                    isNode(pn,"not")) {
359             ParseNode left=pn.getFirstChild();
360             Operation op=new Operation(pn.getLabel());
361             return new OpNode(parseExpression(left),op);
362         } else if (isNode(pn,"postinc")||
363                    isNode(pn,"postdec")) {
364             ParseNode left=pn.getFirstChild();
365             AssignOperation op=new AssignOperation(pn.getLabel());
366             return new AssignmentNode(parseExpression(left),null,op);
367
368         } else if (isNode(pn,"preinc")||
369                    isNode(pn,"predec")) {
370             ParseNode left=pn.getFirstChild();
371             AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
372             return new AssignmentNode(parseExpression(left),
373                                       new LiteralNode("integer",new Integer(1)),op);
374         } else if (isNode(pn,"literal")) {
375             String literaltype=pn.getTerminal();
376             ParseNode literalnode=pn.getChild(literaltype);
377             Object literal_obj=literalnode.getLiteral();
378             return new LiteralNode(literaltype, literal_obj);
379         } else if (isNode(pn,"createobject")) {
380             TypeDescriptor td=parseTypeDescriptor(pn);
381             Vector args=parseArgumentList(pn);
382             CreateObjectNode con=new CreateObjectNode(td);
383             for(int i=0;i<args.size();i++) {
384                 con.addArgument((ExpressionNode)args.get(i));
385             }
386             /* Could have flag set or tag added here */
387             if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
388                 FlagEffects fe=new FlagEffects(null);
389                 if (pn.getChild("flag_list")!=null)
390                     parseFlagEffect(fe, pn.getChild("flag_list"));
391
392                 if (pn.getChild("tag_list")!=null)
393                     parseTagEffect(fe, pn.getChild("tag_list"));
394                 con.addFlagEffects(fe);
395             }
396             
397             return con;
398         } else if (isNode(pn,"createarray")) {
399             //System.out.println(pn.PPrint(3,true));
400             TypeDescriptor td=parseTypeDescriptor(pn);
401             Vector args=parseDimExprs(pn);
402             int num=0;
403             if (pn.getChild("dims_opt").getLiteral()!=null)
404                 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
405             for(int i=0;i<(args.size()+num);i++)
406                 td=td.makeArray(state);
407             CreateObjectNode con=new CreateObjectNode(td);
408             for(int i=0;i<args.size();i++) {
409                 con.addArgument((ExpressionNode)args.get(i));
410             }
411             return con;
412         } else if (isNode(pn,"name")) {
413             NameDescriptor nd=parseName(pn);
414             return new NameNode(nd);
415         } else if (isNode(pn,"this")) {
416             NameDescriptor nd=new NameDescriptor("this");
417             return new NameNode(nd);
418         } else if (isNode(pn,"methodinvoke1")) {
419             NameDescriptor nd=parseName(pn.getChild("name"));
420             Vector args=parseArgumentList(pn);
421             MethodInvokeNode min=new MethodInvokeNode(nd);
422             for(int i=0;i<args.size();i++) {
423                 min.addArgument((ExpressionNode)args.get(i));
424             }
425             return min;
426         } else if (isNode(pn,"methodinvoke2")) {
427             String methodid=pn.getChild("id").getTerminal();
428             ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
429             Vector args=parseArgumentList(pn);
430             MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
431             for(int i=0;i<args.size();i++) {
432                 min.addArgument((ExpressionNode)args.get(i));
433             }
434             return min;
435         } else if (isNode(pn,"fieldaccess")) { 
436             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());         String fieldname=pn.getChild("field").getTerminal();
437             return new FieldAccessNode(en,fieldname);
438         } else if (isNode(pn,"arrayaccess")) { 
439             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
440             ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
441             return new ArrayAccessNode(en,index);
442         } else if (isNode(pn,"cast1")) { 
443             return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
444         } else if (isNode(pn,"cast2")) { 
445             return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
446         } else {
447             System.out.println("---------------------");
448             System.out.println(pn.PPrint(3,true));
449             throw new Error();
450         }
451     }
452
453     private Vector parseDimExprs(ParseNode pn) {
454         Vector arglist=new Vector();
455         ParseNode an=pn.getChild("dim_exprs");
456         if (an==null)   /* No argument list */
457             return arglist;
458         ParseNodeVector anv=an.getChildren();
459         for(int i=0;i<anv.size();i++) {
460             arglist.add(parseExpression(anv.elementAt(i)));
461         }
462         return arglist;
463     }
464
465     private Vector parseArgumentList(ParseNode pn) {
466         Vector arglist=new Vector();
467         ParseNode an=pn.getChild("argument_list");
468         if (an==null)   /* No argument list */
469             return arglist;
470         ParseNodeVector anv=an.getChildren();
471         for(int i=0;i<anv.size();i++) {
472             arglist.add(parseExpression(anv.elementAt(i)));
473         }
474         return arglist;
475     }
476
477     private Vector[] parseConsArgumentList(ParseNode pn) {
478         Vector arglist=new Vector();
479         Vector varlist=new Vector();
480         ParseNode an=pn.getChild("cons_argument_list");
481         if (an==null)   /* No argument list */
482             return new Vector[] {varlist, arglist};
483         ParseNodeVector anv=an.getChildren();
484         for(int i=0;i<anv.size();i++) {
485             ParseNode cpn=anv.elementAt(i);
486             ParseNode var=cpn.getChild("var");
487             ParseNode exp=cpn.getChild("exp").getFirstChild();
488             varlist.add(var.getTerminal());
489             arglist.add(parseExpression(exp));
490         }
491         return new Vector[] {varlist, arglist};
492     }
493
494     private ExpressionNode parseAssignmentExpression(ParseNode pn) {
495         AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
496         ParseNodeVector pnv=pn.getChild("args").getChildren();
497         
498         AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
499         return an;
500     }
501
502
503     private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
504         ParseNode headern=pn.getChild("method_header");
505         ParseNode bodyn=pn.getChild("body");
506         MethodDescriptor md=parseMethodHeader(headern);
507         BlockNode bn=parseBlock(bodyn);
508         cn.addMethod(md);
509         state.addTreeCode(md,bn);
510     }
511
512     private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
513         ParseNode mn=pn.getChild("modifiers");
514         Modifiers m=parseModifiersList(mn);
515         ParseNode cdecl=pn.getChild("constructor_declarator");
516         String name=cdecl.getChild("name").getChild("identifier").getTerminal();
517         MethodDescriptor md=new MethodDescriptor(m, name);
518         ParseNode paramnode=cdecl.getChild("parameters");
519         parseParameterList(md,paramnode);
520         ParseNode bodyn0=pn.getChild("body");
521         ParseNode bodyn=bodyn0.getChild("constructor_body");
522         cn.addMethod(md);
523         BlockNode bn=parseBlock(bodyn);
524         state.addTreeCode(md,bn);
525     }
526
527     public BlockNode parseBlock(ParseNode pn) {
528         if (pn==null||isEmpty(pn.getTerminal()))
529             return new BlockNode();
530         ParseNode bsn=pn.getChild("block_statement_list");
531         return parseBlockHelper(bsn);
532     }
533     
534     private BlockNode parseBlockHelper(ParseNode pn) {
535         ParseNodeVector pnv=pn.getChildren();
536         BlockNode bn=new BlockNode();
537         for(int i=0;i<pnv.size();i++) {
538             Vector bsv=parseBlockStatement(pnv.elementAt(i));
539             for(int j=0;j<bsv.size();j++) {
540                 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
541             }
542         }
543         return bn;
544     }
545
546     public BlockNode parseSingleBlock(ParseNode pn) {
547         BlockNode bn=new BlockNode();
548         Vector bsv=parseBlockStatement(pn);
549         for(int j=0;j<bsv.size();j++) {
550             bn.addBlockStatement((BlockStatementNode)bsv.get(j));
551         }
552         bn.setStyle(BlockNode.NOBRACES);
553         return bn;
554     }
555
556     public Vector parseBlockStatement(ParseNode pn) {
557         Vector blockstatements=new Vector();
558         if (isNode(pn,"tag_declaration")) {
559             String name=pn.getChild("single").getTerminal();
560             String type=pn.getChild("type").getTerminal();
561             
562             blockstatements.add(new TagDeclarationNode(name, type));
563         } else if (isNode(pn,"local_variable_declaration")) {
564             TypeDescriptor t=parseTypeDescriptor(pn);
565             ParseNode vn=pn.getChild("variable_declarators_list");
566             ParseNodeVector pnv=vn.getChildren();
567             for(int i=0;i<pnv.size();i++) {
568                 ParseNode vardecl=pnv.elementAt(i);
569
570             
571                 ParseNode tmp=vardecl;
572                 TypeDescriptor arrayt=t;
573                 while (tmp.getChild("single")==null) {
574                     arrayt=arrayt.makeArray(state);
575                     tmp=tmp.getChild("array");
576                 }
577                 String identifier=tmp.getChild("single").getTerminal();
578                 
579                 ParseNode epn=vardecl.getChild("initializer");
580             
581
582                 ExpressionNode en=null;
583                 if (epn!=null)
584                     en=parseExpression(epn.getFirstChild());
585                 
586                 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
587             }
588         } else if (isNode(pn,"nop")) {
589             /* Do Nothing */
590         } else if (isNode(pn,"expression")) {
591             blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
592         } else if (isNode(pn,"ifstatement")) {
593             blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
594                                        parseSingleBlock(pn.getChild("statement").getFirstChild()),
595                                        pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
596         } else if (isNode(pn,"taskexit")) {
597             Vector vfe=null;
598             if (pn.getChild("flag_effects_list")!=null)
599                 vfe=parseFlags(pn.getChild("flag_effects_list"));
600             Vector ccs=null;
601             if (pn.getChild("cons_checks")!=null)
602                 ccs=parseChecks(pn.getChild("cons_checks"));
603             
604             blockstatements.add(new TaskExitNode(vfe, ccs));
605         } else if (isNode(pn,"return")) {
606             if (isEmpty(pn.getTerminal()))
607                 blockstatements.add(new ReturnNode());
608             else {
609                 ExpressionNode en=parseExpression(pn.getFirstChild());
610                 blockstatements.add(new ReturnNode(en));
611             }
612         } else if (isNode(pn,"block_statement_list")) {
613             BlockNode bn=parseBlockHelper(pn);
614             blockstatements.add(new SubBlockNode(bn));
615         } else if (isNode(pn,"empty")) {
616             /* nop */
617         } else if (isNode(pn,"statement_expression_list")) {
618             ParseNodeVector pnv=pn.getChildren();
619             BlockNode bn=new BlockNode();
620             for(int i=0;i<pnv.size();i++) {
621                 ExpressionNode en=parseExpression(pnv.elementAt(i));
622                 blockstatements.add(new BlockExpressionNode(en));
623             }
624             bn.setStyle(BlockNode.EXPRLIST);
625         } else if (isNode(pn,"forstatement")) {
626             BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
627             BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
628             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
629             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
630             blockstatements.add(new LoopNode(init,condition,update,body));
631         } else if (isNode(pn,"whilestatement")) {
632             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
633             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
634             blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
635         } else if (isNode(pn,"dowhilestatement")) {
636             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
637             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
638             blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
639         } else {
640             System.out.println("---------------");
641             System.out.println(pn.PPrint(3,true));
642             throw new Error();
643         }
644         return blockstatements;
645     }
646
647     public MethodDescriptor parseMethodHeader(ParseNode pn) {
648         ParseNode mn=pn.getChild("modifiers");
649         Modifiers m=parseModifiersList(mn);
650         
651         ParseNode tn=pn.getChild("returntype");
652         TypeDescriptor returntype;
653         if (tn!=null) 
654             returntype=parseTypeDescriptor(tn);
655         else
656             returntype=new TypeDescriptor(TypeDescriptor.VOID);
657
658         ParseNode pmd=pn.getChild("method_declarator");
659         String name=pmd.getChild("name").getTerminal();
660         MethodDescriptor md=new MethodDescriptor(m, returntype, name);
661        
662         ParseNode paramnode=pmd.getChild("parameters");
663         parseParameterList(md,paramnode);
664         return md;
665     }
666
667     public void parseParameterList(MethodDescriptor md, ParseNode pn) {
668         ParseNode paramlist=pn.getChild("formal_parameter_list");
669         if (paramlist==null)
670             return;
671          ParseNodeVector pnv=paramlist.getChildren();
672          for(int i=0;i<pnv.size();i++) {
673              ParseNode paramn=pnv.elementAt(i);
674              TypeDescriptor type=parseTypeDescriptor(paramn);
675
676              ParseNode tmp=paramn;
677              while (tmp.getChild("single")==null) {
678                  type=type.makeArray(state);
679                  tmp=tmp.getChild("array");
680              }
681              String paramname=tmp.getChild("single").getTerminal();
682             
683              md.addParameter(type,paramname);
684          }
685     }
686
687     public Modifiers parseModifiersList(ParseNode pn) {
688         Modifiers m=new Modifiers();
689         ParseNode modlist=pn.getChild("modifier_list");
690         if (modlist!=null) {
691             ParseNodeVector pnv=modlist.getChildren();
692             for(int i=0;i<pnv.size();i++) {
693                 ParseNode modn=pnv.elementAt(i);
694                 if (isNode(modn,"public"))
695                     m.addModifier(Modifiers.PUBLIC);
696                 else if (isNode(modn,"protected"))
697                     m.addModifier(Modifiers.PROTECTED);
698                 else if (isNode(modn,"private"))
699                     m.addModifier(Modifiers.PRIVATE);
700                 else if (isNode(modn,"static"))
701                     m.addModifier(Modifiers.STATIC);
702                 else if (isNode(modn,"final"))
703                     m.addModifier(Modifiers.FINAL);
704                 else if (isNode(modn,"native"))
705                     m.addModifier(Modifiers.NATIVE);
706                 else if (isNode(modn,"synchronized"))
707                     m.addModifier(Modifiers.SYNCHRONIZED);
708                 else throw new Error("Unrecognized Modifier");
709             }
710         }
711         return m;
712     }
713
714     private boolean isNode(ParseNode pn, String label) {
715         if (pn.getLabel().equals(label))
716             return true;
717         else return false;
718     }
719
720     private static boolean isEmpty(ParseNode pn) {
721         if (pn.getLabel().equals("empty"))
722             return true;
723         else
724             return false;
725     }
726
727     private static boolean isEmpty(String s) {
728         if (s.equals("empty"))
729             return true;
730         else
731             return false;
732     }
733
734     /** Throw an exception if something is unexpected */
735     private void check(ParseNode pn, String label) {
736         if (pn == null) {
737             throw new Error(pn+ "IE: Expected '" + label + "', got null");
738         }
739         if (! pn.getLabel().equals(label)) {
740             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
741         }
742     }
743 }