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