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