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