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