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