8 public BuildIR(State state) {
11 public void buildtree() {
12 for(Iterator it=state.parsetrees.iterator();it.hasNext();) {
13 ParseNode pn=(ParseNode)it.next();
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();
24 ParseNode ipn=pn.getChild("imports").getChild("import_decls_list");
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);
36 ParseNode ppn=pn.getChild("packages").getChild("package");
38 packages=parseName(pn.getChild("name"));
40 ParseNode tpn=pn.getChild("type_declaration_list");
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 */
47 if (isNode(type_pn,"class_declaration")) {
48 ClassDescriptor cn=parseTypeDecl(type_pn);
50 } else if (isNode(type_pn,"task_declaration")) {
51 TaskDescriptor td=parseTaskDecl(type_pn);
54 throw new Error(type_pn.getLabel());
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")));
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);
82 public FlagEffects parseFlagEffects(ParseNode pn) {
83 if (isNode(pn,"flag_effect")) {
84 String flagname=pn.getChild("name").getTerminal();
85 FlagEffects fe=new FlagEffects(flagname);
86 if (pn.getChild("flag_list")!=null)
87 parseFlagEffect(fe, pn.getChild("flag_list"));
88 if (pn.getChild("tag_list")!=null)
89 parseTagEffect(fe, pn.getChild("tag_list"));
91 } else throw new Error();
94 public void parseTagEffect(FlagEffects fes, ParseNode pn) {
95 ParseNodeVector pnv=pn.getChildren();
96 for(int i=0;i<pnv.size();i++) {
97 ParseNode pn2=pnv.elementAt(i);
99 if (isNode(pn2,"not")) {
101 pn2=pn2.getChild("name");
103 String name=pn2.getTerminal();
104 fes.addTagEffect(new TagEffect(name,status));
108 public void parseFlagEffect(FlagEffects fes, ParseNode pn) {
109 ParseNodeVector pnv=pn.getChildren();
110 for(int i=0;i<pnv.size();i++) {
111 ParseNode pn2=pnv.elementAt(i);
113 if (isNode(pn2,"not")) {
115 pn2=pn2.getChild("name");
117 String name=pn2.getTerminal();
118 fes.addEffect(new FlagEffect(name,status));
122 public FlagExpressionNode parseFlagExpression(ParseNode pn) {
123 if (isNode(pn,"or")) {
124 ParseNodeVector pnv=pn.getChildren();
125 ParseNode left=pnv.elementAt(0);
126 ParseNode right=pnv.elementAt(1);
127 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_OR));
128 } else if (isNode(pn,"and")) {
129 ParseNodeVector pnv=pn.getChildren();
130 ParseNode left=pnv.elementAt(0);
131 ParseNode right=pnv.elementAt(1);
132 return new FlagOpNode(parseFlagExpression(left), parseFlagExpression(right), new Operation(Operation.LOGIC_AND));
133 } else if (isNode(pn, "not")) {
134 ParseNodeVector pnv=pn.getChildren();
135 ParseNode left=pnv.elementAt(0);
136 return new FlagOpNode(parseFlagExpression(left), new Operation(Operation.LOGIC_NOT));
138 } else if (isNode(pn,"name")) {
139 return new FlagNode(pn.getTerminal());
145 public Vector parseChecks(ParseNode pn) {
146 Vector ccs=new Vector();
147 ParseNodeVector pnv=pn.getChildren();
148 for(int i=0;i<pnv.size();i++) {
149 ParseNode fn=pnv.elementAt(i);
150 ConstraintCheck cc=parseConstraintCheck(fn);
156 public ConstraintCheck parseConstraintCheck(ParseNode pn) {
157 if (isNode(pn,"cons_check")) {
158 String specname=pn.getChild("name").getChild("identifier").getTerminal();
159 Vector[] args=parseConsArgumentList(pn);
160 ConstraintCheck cc=new ConstraintCheck(specname);
161 for(int i=0;i<args[0].size();i++) {
162 cc.addVariable((String)args[0].get(i));
163 cc.addArgument((ExpressionNode)args[1].get(i));
166 } else throw new Error();
169 public void parseParameterList(TaskDescriptor td, ParseNode pn) {
170 ParseNode paramlist=pn.getChild("task_parameter_list");
173 ParseNodeVector pnv=paramlist.getChildren();
174 for(int i=0;i<pnv.size();i++) {
175 ParseNode paramn=pnv.elementAt(i);
176 TypeDescriptor type=parseTypeDescriptor(paramn);
178 String paramname=paramn.getChild("single").getTerminal();
179 FlagExpressionNode fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
181 ParseNode tagnode=paramn.getChild("tag");
183 TagExpressionList tel=null;
185 tel=parseTagExpressionList(tagnode);
188 td.addParameter(type,paramname,fen, tel);
192 public TagExpressionList parseTagExpressionList(ParseNode pn) {
193 ParseNodeVector pnv=pn.getChildren();
194 TagExpressionList tel=new TagExpressionList();
195 for(int i=0;i<pnv.size();i++) {
196 ParseNode tn=pnv.elementAt(i);
197 String type=tn.getChild("type").getTerminal();
198 String name=tn.getChild("single").getTerminal();
199 tel.addTag(type, name);
204 public ClassDescriptor parseTypeDecl(ParseNode pn) {
205 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
206 if (!isEmpty(pn.getChild("super").getTerminal())) {
207 /* parse superclass name */
208 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
209 NameDescriptor nd=parseName(snn);
210 cn.setSuper(nd.toString());
212 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
213 cn.getSymbol().equals(TypeUtil.TagClass)))
214 cn.setSuper(TypeUtil.ObjectClass);
216 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
217 parseClassBody(cn, pn.getChild("classbody"));
221 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
222 ParseNode decls=pn.getChild("class_body_declaration_list");
224 ParseNodeVector pnv=decls.getChildren();
225 for(int i=0;i<pnv.size();i++) {
226 ParseNode decl=pnv.elementAt(i);
227 if (isNode(decl,"member")) {
228 parseClassMember(cn,decl);
229 } else if (isNode(decl,"constructor")) {
230 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
231 } else if (isNode(decl,"block")) {
232 } else throw new Error();
237 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
238 ParseNode fieldnode=pn.getChild("field");
240 if (fieldnode!=null) {
241 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
244 ParseNode methodnode=pn.getChild("method");
245 if (methodnode!=null) {
246 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
249 ParseNode flagnode=pn.getChild("flag");
250 if (flagnode!=null) {
251 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
257 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
258 ParseNode tn=pn.getChild("type");
260 String type_st=tn.getTerminal();
261 if(type_st.equals("byte")) {
262 return state.getTypeDescriptor(TypeDescriptor.BYTE);
263 } else if(type_st.equals("short")) {
264 return state.getTypeDescriptor(TypeDescriptor.SHORT);
265 } else if(type_st.equals("boolean")) {
266 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
267 } else if(type_st.equals("int")) {
268 return state.getTypeDescriptor(TypeDescriptor.INT);
269 } else if(type_st.equals("long")) {
270 return state.getTypeDescriptor(TypeDescriptor.LONG);
271 } else if(type_st.equals("char")) {
272 return state.getTypeDescriptor(TypeDescriptor.CHAR);
273 } else if(type_st.equals("float")) {
274 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
275 } else if(type_st.equals("double")) {
276 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
277 } else if(type_st.equals("class")) {
278 ParseNode nn=tn.getChild("class");
279 return state.getTypeDescriptor(parseName(nn.getChild("name")));
280 } else if(type_st.equals("array")) {
281 ParseNode nn=tn.getChild("array");
282 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
283 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
284 for(int i=0;i<numdims.intValue();i++)
285 td=td.makeArray(state);
292 private NameDescriptor parseName(ParseNode nn) {
293 ParseNode base=nn.getChild("base");
294 ParseNode id=nn.getChild("identifier");
296 return new NameDescriptor(id.getTerminal());
297 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
301 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
302 String name=pn.getChild("name").getTerminal();
303 FlagDescriptor flag=new FlagDescriptor(name);
304 if (pn.getChild("external")!=null)
309 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
310 ParseNode mn=pn.getChild("modifier");
311 Modifiers m=parseModifiersList(mn);
313 ParseNode tn=pn.getChild("type");
314 TypeDescriptor t=parseTypeDescriptor(tn);
315 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
316 ParseNodeVector pnv=vn.getChildren();
317 for(int i=0;i<pnv.size();i++) {
318 ParseNode vardecl=pnv.elementAt(i);
321 ParseNode tmp=vardecl;
322 TypeDescriptor arrayt=t;
323 while (tmp.getChild("single")==null) {
324 arrayt=arrayt.makeArray(state);
325 tmp=tmp.getChild("array");
327 String identifier=tmp.getChild("single").getTerminal();
329 ParseNode epn=vardecl.getChild("initializer");
331 ExpressionNode en=null;
333 en=parseExpression(epn.getFirstChild());
335 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
340 private ExpressionNode parseExpression(ParseNode pn) {
341 if (isNode(pn,"assignment"))
342 return parseAssignmentExpression(pn);
343 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
344 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
345 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
346 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
347 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
348 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
349 isNode(pn,"rightshift")||isNode(pn,"sub")||
350 isNode(pn,"add")||isNode(pn,"mult")||
351 isNode(pn,"div")||isNode(pn,"mod")) {
352 ParseNodeVector pnv=pn.getChildren();
353 ParseNode left=pnv.elementAt(0);
354 ParseNode right=pnv.elementAt(1);
355 Operation op=new Operation(pn.getLabel());
356 return new OpNode(parseExpression(left),parseExpression(right),op);
357 } else if (isNode(pn,"unaryplus")||
358 isNode(pn,"unaryminus")||
360 ParseNode left=pn.getFirstChild();
361 Operation op=new Operation(pn.getLabel());
362 return new OpNode(parseExpression(left),op);
363 } else if (isNode(pn,"postinc")||
364 isNode(pn,"postdec")) {
365 ParseNode left=pn.getFirstChild();
366 AssignOperation op=new AssignOperation(pn.getLabel());
367 return new AssignmentNode(parseExpression(left),null,op);
369 } else if (isNode(pn,"preinc")||
370 isNode(pn,"predec")) {
371 ParseNode left=pn.getFirstChild();
372 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
373 return new AssignmentNode(parseExpression(left),
374 new LiteralNode("integer",new Integer(1)),op);
375 } else if (isNode(pn,"literal")) {
376 String literaltype=pn.getTerminal();
377 ParseNode literalnode=pn.getChild(literaltype);
378 Object literal_obj=literalnode.getLiteral();
379 return new LiteralNode(literaltype, literal_obj);
380 } else if (isNode(pn,"createobject")) {
381 TypeDescriptor td=parseTypeDescriptor(pn);
382 Vector args=parseArgumentList(pn);
383 CreateObjectNode con=new CreateObjectNode(td);
384 for(int i=0;i<args.size();i++) {
385 con.addArgument((ExpressionNode)args.get(i));
387 /* Could have flag set or tag added here */
388 if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
389 FlagEffects fe=new FlagEffects(null);
390 if (pn.getChild("flag_list")!=null)
391 parseFlagEffect(fe, pn.getChild("flag_list"));
393 if (pn.getChild("tag_list")!=null)
394 parseTagEffect(fe, pn.getChild("tag_list"));
395 con.addFlagEffects(fe);
399 } else if (isNode(pn,"createarray")) {
400 //System.out.println(pn.PPrint(3,true));
401 TypeDescriptor td=parseTypeDescriptor(pn);
402 Vector args=parseDimExprs(pn);
404 if (pn.getChild("dims_opt").getLiteral()!=null)
405 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
406 for(int i=0;i<(args.size()+num);i++)
407 td=td.makeArray(state);
408 CreateObjectNode con=new CreateObjectNode(td);
409 for(int i=0;i<args.size();i++) {
410 con.addArgument((ExpressionNode)args.get(i));
413 } else if (isNode(pn,"name")) {
414 NameDescriptor nd=parseName(pn);
415 return new NameNode(nd);
416 } else if (isNode(pn,"this")) {
417 NameDescriptor nd=new NameDescriptor("this");
418 return new NameNode(nd);
419 } else if (isNode(pn,"methodinvoke1")) {
420 NameDescriptor nd=parseName(pn.getChild("name"));
421 Vector args=parseArgumentList(pn);
422 MethodInvokeNode min=new MethodInvokeNode(nd);
423 for(int i=0;i<args.size();i++) {
424 min.addArgument((ExpressionNode)args.get(i));
427 } else if (isNode(pn,"methodinvoke2")) {
428 String methodid=pn.getChild("id").getTerminal();
429 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
430 Vector args=parseArgumentList(pn);
431 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
432 for(int i=0;i<args.size();i++) {
433 min.addArgument((ExpressionNode)args.get(i));
436 } else if (isNode(pn,"fieldaccess")) {
437 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
438 return new FieldAccessNode(en,fieldname);
439 } else if (isNode(pn,"arrayaccess")) {
440 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
441 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
442 return new ArrayAccessNode(en,index);
443 } else if (isNode(pn,"cast1")) {
444 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
445 } else if (isNode(pn,"cast2")) {
446 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
448 System.out.println("---------------------");
449 System.out.println(pn.PPrint(3,true));
454 private Vector parseDimExprs(ParseNode pn) {
455 Vector arglist=new Vector();
456 ParseNode an=pn.getChild("dim_exprs");
457 if (an==null) /* No argument list */
459 ParseNodeVector anv=an.getChildren();
460 for(int i=0;i<anv.size();i++) {
461 arglist.add(parseExpression(anv.elementAt(i)));
466 private Vector parseArgumentList(ParseNode pn) {
467 Vector arglist=new Vector();
468 ParseNode an=pn.getChild("argument_list");
469 if (an==null) /* No argument list */
471 ParseNodeVector anv=an.getChildren();
472 for(int i=0;i<anv.size();i++) {
473 arglist.add(parseExpression(anv.elementAt(i)));
478 private Vector[] parseConsArgumentList(ParseNode pn) {
479 Vector arglist=new Vector();
480 Vector varlist=new Vector();
481 ParseNode an=pn.getChild("cons_argument_list");
482 if (an==null) /* No argument list */
483 return new Vector[] {varlist, arglist};
484 ParseNodeVector anv=an.getChildren();
485 for(int i=0;i<anv.size();i++) {
486 ParseNode cpn=anv.elementAt(i);
487 ParseNode var=cpn.getChild("var");
488 ParseNode exp=cpn.getChild("exp").getFirstChild();
489 varlist.add(var.getTerminal());
490 arglist.add(parseExpression(exp));
492 return new Vector[] {varlist, arglist};
495 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
496 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
497 ParseNodeVector pnv=pn.getChild("args").getChildren();
499 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
504 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
505 ParseNode headern=pn.getChild("method_header");
506 ParseNode bodyn=pn.getChild("body");
507 MethodDescriptor md=parseMethodHeader(headern);
508 BlockNode bn=parseBlock(bodyn);
510 state.addTreeCode(md,bn);
513 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
514 ParseNode mn=pn.getChild("modifiers");
515 Modifiers m=parseModifiersList(mn);
516 ParseNode cdecl=pn.getChild("constructor_declarator");
517 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
518 MethodDescriptor md=new MethodDescriptor(m, name);
519 ParseNode paramnode=cdecl.getChild("parameters");
520 parseParameterList(md,paramnode);
521 ParseNode bodyn0=pn.getChild("body");
522 ParseNode bodyn=bodyn0.getChild("constructor_body");
524 BlockNode bn=parseBlock(bodyn);
525 state.addTreeCode(md,bn);
528 public BlockNode parseBlock(ParseNode pn) {
529 if (pn==null||isEmpty(pn.getTerminal()))
530 return new BlockNode();
531 ParseNode bsn=pn.getChild("block_statement_list");
532 return parseBlockHelper(bsn);
535 private BlockNode parseBlockHelper(ParseNode pn) {
536 ParseNodeVector pnv=pn.getChildren();
537 BlockNode bn=new BlockNode();
538 for(int i=0;i<pnv.size();i++) {
539 Vector bsv=parseBlockStatement(pnv.elementAt(i));
540 for(int j=0;j<bsv.size();j++) {
541 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
547 public BlockNode parseSingleBlock(ParseNode pn) {
548 BlockNode bn=new BlockNode();
549 Vector bsv=parseBlockStatement(pn);
550 for(int j=0;j<bsv.size();j++) {
551 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
553 bn.setStyle(BlockNode.NOBRACES);
557 public Vector parseBlockStatement(ParseNode pn) {
558 Vector blockstatements=new Vector();
559 if (isNode(pn,"tag_declaration")) {
560 String name=pn.getChild("single").getTerminal();
561 String type=pn.getChild("type").getTerminal();
563 blockstatements.add(new TagDeclarationNode(name, type));
564 } else if (isNode(pn,"local_variable_declaration")) {
565 TypeDescriptor t=parseTypeDescriptor(pn);
566 ParseNode vn=pn.getChild("variable_declarators_list");
567 ParseNodeVector pnv=vn.getChildren();
568 for(int i=0;i<pnv.size();i++) {
569 ParseNode vardecl=pnv.elementAt(i);
572 ParseNode tmp=vardecl;
573 TypeDescriptor arrayt=t;
574 while (tmp.getChild("single")==null) {
575 arrayt=arrayt.makeArray(state);
576 tmp=tmp.getChild("array");
578 String identifier=tmp.getChild("single").getTerminal();
580 ParseNode epn=vardecl.getChild("initializer");
583 ExpressionNode en=null;
585 en=parseExpression(epn.getFirstChild());
587 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
589 } else if (isNode(pn,"nop")) {
591 } else if (isNode(pn,"expression")) {
592 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
593 } else if (isNode(pn,"ifstatement")) {
594 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
595 parseSingleBlock(pn.getChild("statement").getFirstChild()),
596 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
597 } else if (isNode(pn,"taskexit")) {
599 if (pn.getChild("flag_effects_list")!=null)
600 vfe=parseFlags(pn.getChild("flag_effects_list"));
602 if (pn.getChild("cons_checks")!=null)
603 ccs=parseChecks(pn.getChild("cons_checks"));
605 blockstatements.add(new TaskExitNode(vfe, ccs));
606 } else if (isNode(pn,"return")) {
607 if (isEmpty(pn.getTerminal()))
608 blockstatements.add(new ReturnNode());
610 ExpressionNode en=parseExpression(pn.getFirstChild());
611 blockstatements.add(new ReturnNode(en));
613 } else if (isNode(pn,"block_statement_list")) {
614 BlockNode bn=parseBlockHelper(pn);
615 blockstatements.add(new SubBlockNode(bn));
616 } else if (isNode(pn,"empty")) {
618 } else if (isNode(pn,"statement_expression_list")) {
619 ParseNodeVector pnv=pn.getChildren();
620 BlockNode bn=new BlockNode();
621 for(int i=0;i<pnv.size();i++) {
622 ExpressionNode en=parseExpression(pnv.elementAt(i));
623 blockstatements.add(new BlockExpressionNode(en));
625 bn.setStyle(BlockNode.EXPRLIST);
626 } else if (isNode(pn,"forstatement")) {
627 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
628 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
629 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
630 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
631 blockstatements.add(new LoopNode(init,condition,update,body));
632 } else if (isNode(pn,"whilestatement")) {
633 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
634 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
635 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
636 } else if (isNode(pn,"dowhilestatement")) {
637 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
638 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
639 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
641 System.out.println("---------------");
642 System.out.println(pn.PPrint(3,true));
645 return blockstatements;
648 public MethodDescriptor parseMethodHeader(ParseNode pn) {
649 ParseNode mn=pn.getChild("modifiers");
650 Modifiers m=parseModifiersList(mn);
652 ParseNode tn=pn.getChild("returntype");
653 TypeDescriptor returntype;
655 returntype=parseTypeDescriptor(tn);
657 returntype=new TypeDescriptor(TypeDescriptor.VOID);
659 ParseNode pmd=pn.getChild("method_declarator");
660 String name=pmd.getChild("name").getTerminal();
661 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
663 ParseNode paramnode=pmd.getChild("parameters");
664 parseParameterList(md,paramnode);
668 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
669 ParseNode paramlist=pn.getChild("formal_parameter_list");
672 ParseNodeVector pnv=paramlist.getChildren();
673 for(int i=0;i<pnv.size();i++) {
674 ParseNode paramn=pnv.elementAt(i);
675 TypeDescriptor type=parseTypeDescriptor(paramn);
677 ParseNode tmp=paramn;
678 while (tmp.getChild("single")==null) {
679 type=type.makeArray(state);
680 tmp=tmp.getChild("array");
682 String paramname=tmp.getChild("single").getTerminal();
684 md.addParameter(type,paramname);
688 public Modifiers parseModifiersList(ParseNode pn) {
689 Modifiers m=new Modifiers();
690 ParseNode modlist=pn.getChild("modifier_list");
692 ParseNodeVector pnv=modlist.getChildren();
693 for(int i=0;i<pnv.size();i++) {
694 ParseNode modn=pnv.elementAt(i);
695 if (isNode(modn,"public"))
696 m.addModifier(Modifiers.PUBLIC);
697 else if (isNode(modn,"protected"))
698 m.addModifier(Modifiers.PROTECTED);
699 else if (isNode(modn,"private"))
700 m.addModifier(Modifiers.PRIVATE);
701 else if (isNode(modn,"static"))
702 m.addModifier(Modifiers.STATIC);
703 else if (isNode(modn,"final"))
704 m.addModifier(Modifiers.FINAL);
705 else if (isNode(modn,"native"))
706 m.addModifier(Modifiers.NATIVE);
707 else if (isNode(modn,"synchronized"))
708 m.addModifier(Modifiers.SYNCHRONIZED);
709 else throw new Error("Unrecognized Modifier");
715 private boolean isNode(ParseNode pn, String label) {
716 if (pn.getLabel().equals(label))
721 private static boolean isEmpty(ParseNode pn) {
722 if (pn.getLabel().equals("empty"))
728 private static boolean isEmpty(String s) {
729 if (s.equals("empty"))
735 /** Throw an exception if something is unexpected */
736 private void check(ParseNode pn, String label) {
738 throw new Error(pn+ "IE: Expected '" + label + "', got null");
740 if (! pn.getLabel().equals(label)) {
741 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");