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