Changes:
[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         //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
194         //To test, feed in any input program that uses tags
195         ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
196         TagExpressionList tel=new TagExpressionList();
197         for(int i=0;i<pnv.size();i++) {
198             ParseNode tn=pnv.elementAt(i);
199             String type=tn.getChild("type").getTerminal();
200             String name=tn.getChild("single").getTerminal();
201             tel.addTag(type, name);
202         }
203         return tel;
204     }
205
206     public ClassDescriptor parseTypeDecl(ParseNode pn) {
207         ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
208         if (!isEmpty(pn.getChild("super").getTerminal())) {
209             /* parse superclass name */
210             ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
211             NameDescriptor nd=parseName(snn);
212             cn.setSuper(nd.toString());
213         } else {
214             if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
215                   cn.getSymbol().equals(TypeUtil.TagClass)))
216                 cn.setSuper(TypeUtil.ObjectClass);
217         }
218         cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
219         parseClassBody(cn, pn.getChild("classbody"));
220         return cn;
221     }
222
223     private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
224         ParseNode decls=pn.getChild("class_body_declaration_list");
225         if (decls!=null) {
226             ParseNodeVector pnv=decls.getChildren();
227             for(int i=0;i<pnv.size();i++) {
228                 ParseNode decl=pnv.elementAt(i);
229                 if (isNode(decl,"member")) {
230                     parseClassMember(cn,decl);
231                 } else if (isNode(decl,"constructor")) {
232                     parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
233                 } else if (isNode(decl,"block")) {
234                 } else throw new Error();
235             }
236         }
237     }
238
239     private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
240         ParseNode fieldnode=pn.getChild("field");
241
242         if (fieldnode!=null) {
243             parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
244             return;
245         }
246         ParseNode methodnode=pn.getChild("method");
247         if (methodnode!=null) {
248             parseMethodDecl(cn,methodnode.getChild("method_declaration"));
249             return;
250         }
251         ParseNode flagnode=pn.getChild("flag");
252         if (flagnode!=null) {
253             parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
254             return;
255         }
256         throw new Error();
257     }
258
259     private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
260         ParseNode tn=pn.getChild("type");
261
262         String type_st=tn.getTerminal();
263         if(type_st.equals("byte")) {
264             return state.getTypeDescriptor(TypeDescriptor.BYTE);
265         } else if(type_st.equals("short")) {
266             return state.getTypeDescriptor(TypeDescriptor.SHORT);
267         } else if(type_st.equals("boolean")) {
268             return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
269         } else if(type_st.equals("int")) {
270             return state.getTypeDescriptor(TypeDescriptor.INT);
271         } else if(type_st.equals("long")) {
272             return state.getTypeDescriptor(TypeDescriptor.LONG);
273         } else if(type_st.equals("char")) {
274             return state.getTypeDescriptor(TypeDescriptor.CHAR);
275         } else if(type_st.equals("float")) {
276             return state.getTypeDescriptor(TypeDescriptor.FLOAT);
277         } else if(type_st.equals("double")) {
278             return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
279         } else if(type_st.equals("class")) {
280             ParseNode nn=tn.getChild("class");
281             return state.getTypeDescriptor(parseName(nn.getChild("name")));
282         } else if(type_st.equals("array")) {
283             ParseNode nn=tn.getChild("array");
284             TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
285             Integer numdims=(Integer)nn.getChild("dims").getLiteral();
286             for(int i=0;i<numdims.intValue();i++)
287                 td=td.makeArray(state);
288             return td;
289         } else {
290             throw new Error();
291         }
292     }
293
294     private NameDescriptor parseName(ParseNode nn) {
295         ParseNode base=nn.getChild("base");
296         ParseNode id=nn.getChild("identifier");
297         if (base==null)
298             return new NameDescriptor(id.getTerminal());
299         return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
300         
301     }
302
303     private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
304         String name=pn.getChild("name").getTerminal();
305         FlagDescriptor flag=new FlagDescriptor(name);
306         if (pn.getChild("external")!=null)
307             flag.makeExternal();
308         cn.addFlag(flag);
309     }
310
311     private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
312         ParseNode mn=pn.getChild("modifier");
313         Modifiers m=parseModifiersList(mn);
314
315         ParseNode tn=pn.getChild("type");
316         TypeDescriptor t=parseTypeDescriptor(tn);
317         ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
318         ParseNodeVector pnv=vn.getChildren();
319         for(int i=0;i<pnv.size();i++) {
320             ParseNode vardecl=pnv.elementAt(i);
321
322             
323             ParseNode tmp=vardecl;
324             TypeDescriptor arrayt=t;
325             while (tmp.getChild("single")==null) {
326                 arrayt=arrayt.makeArray(state);
327                 tmp=tmp.getChild("array");
328             }
329             String identifier=tmp.getChild("single").getTerminal();
330
331             ParseNode epn=vardecl.getChild("initializer");
332             
333             ExpressionNode en=null;
334             if (epn!=null)
335                 en=parseExpression(epn.getFirstChild());
336   
337             cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
338         }
339         
340     }
341
342     private ExpressionNode parseExpression(ParseNode pn) {
343         if (isNode(pn,"assignment"))
344             return parseAssignmentExpression(pn);
345         else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
346                  isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
347                  isNode(pn,"bitwise_and")||isNode(pn,"equal")||
348                  isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
349                  isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
350                  isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
351                  isNode(pn,"rightshift")||isNode(pn,"sub")||
352                  isNode(pn,"add")||isNode(pn,"mult")||
353                  isNode(pn,"div")||isNode(pn,"mod")) {
354             ParseNodeVector pnv=pn.getChildren();
355             ParseNode left=pnv.elementAt(0);
356             ParseNode right=pnv.elementAt(1);
357             Operation op=new Operation(pn.getLabel());
358             return new OpNode(parseExpression(left),parseExpression(right),op);
359         } else if (isNode(pn,"unaryplus")||
360                    isNode(pn,"unaryminus")||
361                    isNode(pn,"not")) {
362             ParseNode left=pn.getFirstChild();
363             Operation op=new Operation(pn.getLabel());
364             return new OpNode(parseExpression(left),op);
365         } else if (isNode(pn,"postinc")||
366                    isNode(pn,"postdec")) {
367             ParseNode left=pn.getFirstChild();
368             AssignOperation op=new AssignOperation(pn.getLabel());
369             return new AssignmentNode(parseExpression(left),null,op);
370
371         } else if (isNode(pn,"preinc")||
372                    isNode(pn,"predec")) {
373             ParseNode left=pn.getFirstChild();
374             AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
375             return new AssignmentNode(parseExpression(left),
376                                       new LiteralNode("integer",new Integer(1)),op);
377         } else if (isNode(pn,"literal")) {
378             String literaltype=pn.getTerminal();
379             ParseNode literalnode=pn.getChild(literaltype);
380             Object literal_obj=literalnode.getLiteral();
381             return new LiteralNode(literaltype, literal_obj);
382         } else if (isNode(pn,"createobject")) {
383             TypeDescriptor td=parseTypeDescriptor(pn);
384             Vector args=parseArgumentList(pn);
385             CreateObjectNode con=new CreateObjectNode(td);
386             for(int i=0;i<args.size();i++) {
387                 con.addArgument((ExpressionNode)args.get(i));
388             }
389             /* Could have flag set or tag added here */
390             if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
391                 FlagEffects fe=new FlagEffects(null);
392                 if (pn.getChild("flag_list")!=null)
393                     parseFlagEffect(fe, pn.getChild("flag_list"));
394
395                 if (pn.getChild("tag_list")!=null)
396                     parseTagEffect(fe, pn.getChild("tag_list"));
397                 con.addFlagEffects(fe);
398             }
399             
400             return con;
401         } else if (isNode(pn,"createarray")) {
402             //System.out.println(pn.PPrint(3,true));
403             TypeDescriptor td=parseTypeDescriptor(pn);
404             Vector args=parseDimExprs(pn);
405             int num=0;
406             if (pn.getChild("dims_opt").getLiteral()!=null)
407                 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
408             for(int i=0;i<(args.size()+num);i++)
409                 td=td.makeArray(state);
410             CreateObjectNode con=new CreateObjectNode(td);
411             for(int i=0;i<args.size();i++) {
412                 con.addArgument((ExpressionNode)args.get(i));
413             }
414             return con;
415         } else if (isNode(pn,"name")) {
416             NameDescriptor nd=parseName(pn);
417             return new NameNode(nd);
418         } else if (isNode(pn,"this")) {
419             NameDescriptor nd=new NameDescriptor("this");
420             return new NameNode(nd);
421         } else if (isNode(pn,"methodinvoke1")) {
422             NameDescriptor nd=parseName(pn.getChild("name"));
423             Vector args=parseArgumentList(pn);
424             MethodInvokeNode min=new MethodInvokeNode(nd);
425             for(int i=0;i<args.size();i++) {
426                 min.addArgument((ExpressionNode)args.get(i));
427             }
428             return min;
429         } else if (isNode(pn,"methodinvoke2")) {
430             String methodid=pn.getChild("id").getTerminal();
431             ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
432             Vector args=parseArgumentList(pn);
433             MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
434             for(int i=0;i<args.size();i++) {
435                 min.addArgument((ExpressionNode)args.get(i));
436             }
437             return min;
438         } else if (isNode(pn,"fieldaccess")) { 
439             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());         String fieldname=pn.getChild("field").getTerminal();
440             return new FieldAccessNode(en,fieldname);
441         } else if (isNode(pn,"arrayaccess")) { 
442             ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
443             ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
444             return new ArrayAccessNode(en,index);
445         } else if (isNode(pn,"cast1")) { 
446             return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
447         } else if (isNode(pn,"cast2")) { 
448             return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
449         } else {
450             System.out.println("---------------------");
451             System.out.println(pn.PPrint(3,true));
452             throw new Error();
453         }
454     }
455
456     private Vector parseDimExprs(ParseNode pn) {
457         Vector arglist=new Vector();
458         ParseNode an=pn.getChild("dim_exprs");
459         if (an==null)   /* No argument list */
460             return arglist;
461         ParseNodeVector anv=an.getChildren();
462         for(int i=0;i<anv.size();i++) {
463             arglist.add(parseExpression(anv.elementAt(i)));
464         }
465         return arglist;
466     }
467
468     private Vector parseArgumentList(ParseNode pn) {
469         Vector arglist=new Vector();
470         ParseNode an=pn.getChild("argument_list");
471         if (an==null)   /* No argument list */
472             return arglist;
473         ParseNodeVector anv=an.getChildren();
474         for(int i=0;i<anv.size();i++) {
475             arglist.add(parseExpression(anv.elementAt(i)));
476         }
477         return arglist;
478     }
479
480     private Vector[] parseConsArgumentList(ParseNode pn) {
481         Vector arglist=new Vector();
482         Vector varlist=new Vector();
483         ParseNode an=pn.getChild("cons_argument_list");
484         if (an==null)   /* No argument list */
485             return new Vector[] {varlist, arglist};
486         ParseNodeVector anv=an.getChildren();
487         for(int i=0;i<anv.size();i++) {
488             ParseNode cpn=anv.elementAt(i);
489             ParseNode var=cpn.getChild("var");
490             ParseNode exp=cpn.getChild("exp").getFirstChild();
491             varlist.add(var.getTerminal());
492             arglist.add(parseExpression(exp));
493         }
494         return new Vector[] {varlist, arglist};
495     }
496
497     private ExpressionNode parseAssignmentExpression(ParseNode pn) {
498         AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
499         ParseNodeVector pnv=pn.getChild("args").getChildren();
500         
501         AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
502         return an;
503     }
504
505
506     private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
507         ParseNode headern=pn.getChild("method_header");
508         ParseNode bodyn=pn.getChild("body");
509         MethodDescriptor md=parseMethodHeader(headern);
510         BlockNode bn=parseBlock(bodyn);
511         cn.addMethod(md);
512         state.addTreeCode(md,bn);
513     }
514
515     private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
516         ParseNode mn=pn.getChild("modifiers");
517         Modifiers m=parseModifiersList(mn);
518         ParseNode cdecl=pn.getChild("constructor_declarator");
519         String name=cdecl.getChild("name").getChild("identifier").getTerminal();
520         MethodDescriptor md=new MethodDescriptor(m, name);
521         ParseNode paramnode=cdecl.getChild("parameters");
522         parseParameterList(md,paramnode);
523         ParseNode bodyn0=pn.getChild("body");
524         ParseNode bodyn=bodyn0.getChild("constructor_body");
525         cn.addMethod(md);
526         BlockNode bn=parseBlock(bodyn);
527         state.addTreeCode(md,bn);
528     }
529
530     public BlockNode parseBlock(ParseNode pn) {
531         if (pn==null||isEmpty(pn.getTerminal()))
532             return new BlockNode();
533         ParseNode bsn=pn.getChild("block_statement_list");
534         return parseBlockHelper(bsn);
535     }
536     
537     private BlockNode parseBlockHelper(ParseNode pn) {
538         ParseNodeVector pnv=pn.getChildren();
539         BlockNode bn=new BlockNode();
540         for(int i=0;i<pnv.size();i++) {
541             Vector bsv=parseBlockStatement(pnv.elementAt(i));
542             for(int j=0;j<bsv.size();j++) {
543                 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
544             }
545         }
546         return bn;
547     }
548
549     public BlockNode parseSingleBlock(ParseNode pn) {
550         BlockNode bn=new BlockNode();
551         Vector bsv=parseBlockStatement(pn);
552         for(int j=0;j<bsv.size();j++) {
553             bn.addBlockStatement((BlockStatementNode)bsv.get(j));
554         }
555         bn.setStyle(BlockNode.NOBRACES);
556         return bn;
557     }
558
559     public Vector parseBlockStatement(ParseNode pn) {
560         Vector blockstatements=new Vector();
561         if (isNode(pn,"tag_declaration")) {
562             String name=pn.getChild("single").getTerminal();
563             String type=pn.getChild("type").getTerminal();
564             
565             blockstatements.add(new TagDeclarationNode(name, type));
566         } else if (isNode(pn,"local_variable_declaration")) {
567             TypeDescriptor t=parseTypeDescriptor(pn);
568             ParseNode vn=pn.getChild("variable_declarators_list");
569             ParseNodeVector pnv=vn.getChildren();
570             for(int i=0;i<pnv.size();i++) {
571                 ParseNode vardecl=pnv.elementAt(i);
572
573             
574                 ParseNode tmp=vardecl;
575                 TypeDescriptor arrayt=t;
576                 while (tmp.getChild("single")==null) {
577                     arrayt=arrayt.makeArray(state);
578                     tmp=tmp.getChild("array");
579                 }
580                 String identifier=tmp.getChild("single").getTerminal();
581                 
582                 ParseNode epn=vardecl.getChild("initializer");
583             
584
585                 ExpressionNode en=null;
586                 if (epn!=null)
587                     en=parseExpression(epn.getFirstChild());
588                 
589                 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
590             }
591         } else if (isNode(pn,"nop")) {
592             /* Do Nothing */
593         } else if (isNode(pn,"expression")) {
594             blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
595         } else if (isNode(pn,"ifstatement")) {
596             blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
597                                        parseSingleBlock(pn.getChild("statement").getFirstChild()),
598                                        pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
599         } else if (isNode(pn,"taskexit")) {
600             Vector vfe=null;
601             if (pn.getChild("flag_effects_list")!=null)
602                 vfe=parseFlags(pn.getChild("flag_effects_list"));
603             Vector ccs=null;
604             if (pn.getChild("cons_checks")!=null)
605                 ccs=parseChecks(pn.getChild("cons_checks"));
606             
607             blockstatements.add(new TaskExitNode(vfe, ccs));
608         } else if (isNode(pn,"return")) {
609             if (isEmpty(pn.getTerminal()))
610                 blockstatements.add(new ReturnNode());
611             else {
612                 ExpressionNode en=parseExpression(pn.getFirstChild());
613                 blockstatements.add(new ReturnNode(en));
614             }
615         } else if (isNode(pn,"block_statement_list")) {
616             BlockNode bn=parseBlockHelper(pn);
617             blockstatements.add(new SubBlockNode(bn));
618         } else if (isNode(pn,"empty")) {
619             /* nop */
620         } else if (isNode(pn,"statement_expression_list")) {
621             ParseNodeVector pnv=pn.getChildren();
622             BlockNode bn=new BlockNode();
623             for(int i=0;i<pnv.size();i++) {
624                 ExpressionNode en=parseExpression(pnv.elementAt(i));
625                 blockstatements.add(new BlockExpressionNode(en));
626             }
627             bn.setStyle(BlockNode.EXPRLIST);
628         } else if (isNode(pn,"forstatement")) {
629             BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
630             BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
631             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
632             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
633             blockstatements.add(new LoopNode(init,condition,update,body));
634         } else if (isNode(pn,"whilestatement")) {
635             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
636             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
637             blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
638         } else if (isNode(pn,"dowhilestatement")) {
639             ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
640             BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
641             blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
642         } else {
643             System.out.println("---------------");
644             System.out.println(pn.PPrint(3,true));
645             throw new Error();
646         }
647         return blockstatements;
648     }
649
650     public MethodDescriptor parseMethodHeader(ParseNode pn) {
651         ParseNode mn=pn.getChild("modifiers");
652         Modifiers m=parseModifiersList(mn);
653         
654         ParseNode tn=pn.getChild("returntype");
655         TypeDescriptor returntype;
656         if (tn!=null) 
657             returntype=parseTypeDescriptor(tn);
658         else
659             returntype=new TypeDescriptor(TypeDescriptor.VOID);
660
661         ParseNode pmd=pn.getChild("method_declarator");
662         String name=pmd.getChild("name").getTerminal();
663         MethodDescriptor md=new MethodDescriptor(m, returntype, name);
664        
665         ParseNode paramnode=pmd.getChild("parameters");
666         parseParameterList(md,paramnode);
667         return md;
668     }
669
670     public void parseParameterList(MethodDescriptor md, ParseNode pn) {
671         ParseNode paramlist=pn.getChild("formal_parameter_list");
672         if (paramlist==null)
673             return;
674          ParseNodeVector pnv=paramlist.getChildren();
675          for(int i=0;i<pnv.size();i++) {
676              ParseNode paramn=pnv.elementAt(i);
677
678              if (isNode(paramn, "tag_parameter")) {
679                  String paramname=paramn.getChild("single").getTerminal();
680                  TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
681                  md.addTagParameter(type, paramname);
682              } else {
683                  TypeDescriptor type=parseTypeDescriptor(paramn);
684                  
685                  ParseNode tmp=paramn;
686                  while (tmp.getChild("single")==null) {
687                      type=type.makeArray(state);
688                      tmp=tmp.getChild("array");
689                  }
690                  String paramname=tmp.getChild("single").getTerminal();
691                  
692                  md.addParameter(type, paramname);
693              }
694          }
695     }
696
697     public Modifiers parseModifiersList(ParseNode pn) {
698         Modifiers m=new Modifiers();
699         ParseNode modlist=pn.getChild("modifier_list");
700         if (modlist!=null) {
701             ParseNodeVector pnv=modlist.getChildren();
702             for(int i=0;i<pnv.size();i++) {
703                 ParseNode modn=pnv.elementAt(i);
704                 if (isNode(modn,"public"))
705                     m.addModifier(Modifiers.PUBLIC);
706                 else if (isNode(modn,"protected"))
707                     m.addModifier(Modifiers.PROTECTED);
708                 else if (isNode(modn,"private"))
709                     m.addModifier(Modifiers.PRIVATE);
710                 else if (isNode(modn,"static"))
711                     m.addModifier(Modifiers.STATIC);
712                 else if (isNode(modn,"final"))
713                     m.addModifier(Modifiers.FINAL);
714                 else if (isNode(modn,"native"))
715                     m.addModifier(Modifiers.NATIVE);
716                 else if (isNode(modn,"synchronized"))
717                     m.addModifier(Modifiers.SYNCHRONIZED);
718                 else throw new Error("Unrecognized Modifier");
719             }
720         }
721         return m;
722     }
723
724     private boolean isNode(ParseNode pn, String label) {
725         if (pn.getLabel().equals(label))
726             return true;
727         else return false;
728     }
729
730     private static boolean isEmpty(ParseNode pn) {
731         if (pn.getLabel().equals("empty"))
732             return true;
733         else
734             return false;
735     }
736
737     private static boolean isEmpty(String s) {
738         if (s.equals("empty"))
739             return true;
740         else
741             return false;
742     }
743
744     /** Throw an exception if something is unexpected */
745     private void check(ParseNode pn, String label) {
746         if (pn == null) {
747             throw new Error(pn+ "IE: Expected '" + label + "', got null");
748         }
749         if (! pn.getLabel().equals(label)) {
750             throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");
751         }
752     }
753 }