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.setSuper(TypeUtil.ObjectClass);
215 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
216 parseClassBody(cn, pn.getChild("classbody"));
220 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
221 ParseNode decls=pn.getChild("class_body_declaration_list");
223 ParseNodeVector pnv=decls.getChildren();
224 for(int i=0;i<pnv.size();i++) {
225 ParseNode decl=pnv.elementAt(i);
226 if (isNode(decl,"member")) {
227 parseClassMember(cn,decl);
228 } else if (isNode(decl,"constructor")) {
229 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
230 } else if (isNode(decl,"block")) {
231 } else throw new Error();
236 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
237 ParseNode fieldnode=pn.getChild("field");
239 if (fieldnode!=null) {
240 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
243 ParseNode methodnode=pn.getChild("method");
244 if (methodnode!=null) {
245 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
248 ParseNode flagnode=pn.getChild("flag");
249 if (flagnode!=null) {
250 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
256 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
257 ParseNode tn=pn.getChild("type");
259 String type_st=tn.getTerminal();
260 if(type_st.equals("byte")) {
261 return state.getTypeDescriptor(TypeDescriptor.BYTE);
262 } else if(type_st.equals("short")) {
263 return state.getTypeDescriptor(TypeDescriptor.SHORT);
264 } else if(type_st.equals("boolean")) {
265 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
266 } else if(type_st.equals("int")) {
267 return state.getTypeDescriptor(TypeDescriptor.INT);
268 } else if(type_st.equals("long")) {
269 return state.getTypeDescriptor(TypeDescriptor.LONG);
270 } else if(type_st.equals("char")) {
271 return state.getTypeDescriptor(TypeDescriptor.CHAR);
272 } else if(type_st.equals("float")) {
273 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
274 } else if(type_st.equals("double")) {
275 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
276 } else if(type_st.equals("class")) {
277 ParseNode nn=tn.getChild("class");
278 return state.getTypeDescriptor(parseName(nn.getChild("name")));
279 } else if(type_st.equals("array")) {
280 ParseNode nn=tn.getChild("array");
281 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
282 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
283 for(int i=0;i<numdims.intValue();i++)
284 td=td.makeArray(state);
291 private NameDescriptor parseName(ParseNode nn) {
292 ParseNode base=nn.getChild("base");
293 ParseNode id=nn.getChild("identifier");
295 return new NameDescriptor(id.getTerminal());
296 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
300 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
301 String name=pn.getChild("name").getTerminal();
302 FlagDescriptor flag=new FlagDescriptor(name);
303 if (pn.getChild("external")!=null)
308 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
309 ParseNode mn=pn.getChild("modifier");
310 Modifiers m=parseModifiersList(mn);
312 ParseNode tn=pn.getChild("type");
313 TypeDescriptor t=parseTypeDescriptor(tn);
314 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
315 ParseNodeVector pnv=vn.getChildren();
316 for(int i=0;i<pnv.size();i++) {
317 ParseNode vardecl=pnv.elementAt(i);
320 ParseNode tmp=vardecl;
321 TypeDescriptor arrayt=t;
322 while (tmp.getChild("single")==null) {
323 arrayt=arrayt.makeArray(state);
324 tmp=tmp.getChild("array");
326 String identifier=tmp.getChild("single").getTerminal();
328 ParseNode epn=vardecl.getChild("initializer");
330 ExpressionNode en=null;
332 en=parseExpression(epn.getFirstChild());
334 cn.addField(new FieldDescriptor(m,arrayt,identifier, en));
339 private ExpressionNode parseExpression(ParseNode pn) {
340 if (isNode(pn,"assignment"))
341 return parseAssignmentExpression(pn);
342 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
343 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
344 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
345 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
346 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
347 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
348 isNode(pn,"rightshift")||isNode(pn,"sub")||
349 isNode(pn,"add")||isNode(pn,"mult")||
350 isNode(pn,"div")||isNode(pn,"mod")) {
351 ParseNodeVector pnv=pn.getChildren();
352 ParseNode left=pnv.elementAt(0);
353 ParseNode right=pnv.elementAt(1);
354 Operation op=new Operation(pn.getLabel());
355 return new OpNode(parseExpression(left),parseExpression(right),op);
356 } else if (isNode(pn,"unaryplus")||
357 isNode(pn,"unaryminus")||
359 ParseNode left=pn.getFirstChild();
360 Operation op=new Operation(pn.getLabel());
361 return new OpNode(parseExpression(left),op);
362 } else if (isNode(pn,"postinc")||
363 isNode(pn,"postdec")) {
364 ParseNode left=pn.getFirstChild();
365 AssignOperation op=new AssignOperation(pn.getLabel());
366 return new AssignmentNode(parseExpression(left),null,op);
368 } else if (isNode(pn,"preinc")||
369 isNode(pn,"predec")) {
370 ParseNode left=pn.getFirstChild();
371 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
372 return new AssignmentNode(parseExpression(left),
373 new LiteralNode("integer",new Integer(1)),op);
374 } else if (isNode(pn,"literal")) {
375 String literaltype=pn.getTerminal();
376 ParseNode literalnode=pn.getChild(literaltype);
377 Object literal_obj=literalnode.getLiteral();
378 return new LiteralNode(literaltype, literal_obj);
379 } else if (isNode(pn,"createobject")) {
380 TypeDescriptor td=parseTypeDescriptor(pn);
381 Vector args=parseArgumentList(pn);
382 CreateObjectNode con=new CreateObjectNode(td);
383 for(int i=0;i<args.size();i++) {
384 con.addArgument((ExpressionNode)args.get(i));
386 /* Could have flag set or tag added here */
387 if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
388 FlagEffects fe=new FlagEffects(null);
389 if (pn.getChild("flag_list")!=null)
390 parseFlagEffect(fe, pn.getChild("flag_list"));
392 if (pn.getChild("tag_list")!=null)
393 parseTagEffect(fe, pn.getChild("tag_list"));
394 con.addFlagEffects(fe);
398 } else if (isNode(pn,"createarray")) {
399 //System.out.println(pn.PPrint(3,true));
400 TypeDescriptor td=parseTypeDescriptor(pn);
401 Vector args=parseDimExprs(pn);
403 if (pn.getChild("dims_opt").getLiteral()!=null)
404 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
405 for(int i=0;i<(args.size()+num);i++)
406 td=td.makeArray(state);
407 CreateObjectNode con=new CreateObjectNode(td);
408 for(int i=0;i<args.size();i++) {
409 con.addArgument((ExpressionNode)args.get(i));
412 } else if (isNode(pn,"name")) {
413 NameDescriptor nd=parseName(pn);
414 return new NameNode(nd);
415 } else if (isNode(pn,"this")) {
416 NameDescriptor nd=new NameDescriptor("this");
417 return new NameNode(nd);
418 } else if (isNode(pn,"methodinvoke1")) {
419 NameDescriptor nd=parseName(pn.getChild("name"));
420 Vector args=parseArgumentList(pn);
421 MethodInvokeNode min=new MethodInvokeNode(nd);
422 for(int i=0;i<args.size();i++) {
423 min.addArgument((ExpressionNode)args.get(i));
426 } else if (isNode(pn,"methodinvoke2")) {
427 String methodid=pn.getChild("id").getTerminal();
428 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
429 Vector args=parseArgumentList(pn);
430 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
431 for(int i=0;i<args.size();i++) {
432 min.addArgument((ExpressionNode)args.get(i));
435 } else if (isNode(pn,"fieldaccess")) {
436 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
437 return new FieldAccessNode(en,fieldname);
438 } else if (isNode(pn,"arrayaccess")) {
439 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
440 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
441 return new ArrayAccessNode(en,index);
442 } else if (isNode(pn,"cast1")) {
443 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
444 } else if (isNode(pn,"cast2")) {
445 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
447 System.out.println("---------------------");
448 System.out.println(pn.PPrint(3,true));
453 private Vector parseDimExprs(ParseNode pn) {
454 Vector arglist=new Vector();
455 ParseNode an=pn.getChild("dim_exprs");
456 if (an==null) /* No argument list */
458 ParseNodeVector anv=an.getChildren();
459 for(int i=0;i<anv.size();i++) {
460 arglist.add(parseExpression(anv.elementAt(i)));
465 private Vector parseArgumentList(ParseNode pn) {
466 Vector arglist=new Vector();
467 ParseNode an=pn.getChild("argument_list");
468 if (an==null) /* No argument list */
470 ParseNodeVector anv=an.getChildren();
471 for(int i=0;i<anv.size();i++) {
472 arglist.add(parseExpression(anv.elementAt(i)));
477 private Vector[] parseConsArgumentList(ParseNode pn) {
478 Vector arglist=new Vector();
479 Vector varlist=new Vector();
480 ParseNode an=pn.getChild("cons_argument_list");
481 if (an==null) /* No argument list */
482 return new Vector[] {varlist, arglist};
483 ParseNodeVector anv=an.getChildren();
484 for(int i=0;i<anv.size();i++) {
485 ParseNode cpn=anv.elementAt(i);
486 ParseNode var=cpn.getChild("var");
487 ParseNode exp=cpn.getChild("exp").getFirstChild();
488 varlist.add(var.getTerminal());
489 arglist.add(parseExpression(exp));
491 return new Vector[] {varlist, arglist};
494 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
495 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
496 ParseNodeVector pnv=pn.getChild("args").getChildren();
498 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
503 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
504 ParseNode headern=pn.getChild("method_header");
505 ParseNode bodyn=pn.getChild("body");
506 MethodDescriptor md=parseMethodHeader(headern);
507 BlockNode bn=parseBlock(bodyn);
509 state.addTreeCode(md,bn);
512 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
513 ParseNode mn=pn.getChild("modifiers");
514 Modifiers m=parseModifiersList(mn);
515 ParseNode cdecl=pn.getChild("constructor_declarator");
516 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
517 MethodDescriptor md=new MethodDescriptor(m, name);
518 ParseNode paramnode=cdecl.getChild("parameters");
519 parseParameterList(md,paramnode);
520 ParseNode bodyn0=pn.getChild("body");
521 ParseNode bodyn=bodyn0.getChild("constructor_body");
523 BlockNode bn=parseBlock(bodyn);
524 state.addTreeCode(md,bn);
527 public BlockNode parseBlock(ParseNode pn) {
528 if (pn==null||isEmpty(pn.getTerminal()))
529 return new BlockNode();
530 ParseNode bsn=pn.getChild("block_statement_list");
531 return parseBlockHelper(bsn);
534 private BlockNode parseBlockHelper(ParseNode pn) {
535 ParseNodeVector pnv=pn.getChildren();
536 BlockNode bn=new BlockNode();
537 for(int i=0;i<pnv.size();i++) {
538 Vector bsv=parseBlockStatement(pnv.elementAt(i));
539 for(int j=0;j<bsv.size();j++) {
540 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
546 public BlockNode parseSingleBlock(ParseNode pn) {
547 BlockNode bn=new BlockNode();
548 Vector bsv=parseBlockStatement(pn);
549 for(int j=0;j<bsv.size();j++) {
550 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
552 bn.setStyle(BlockNode.NOBRACES);
556 public Vector parseBlockStatement(ParseNode pn) {
557 Vector blockstatements=new Vector();
558 if (isNode(pn,"tag_declaration")) {
559 String name=pn.getChild("single").getTerminal();
560 String type=pn.getChild("type").getTerminal();
562 blockstatements.add(new TagDeclarationNode(name, type));
563 } else if (isNode(pn,"local_variable_declaration")) {
564 TypeDescriptor t=parseTypeDescriptor(pn);
565 ParseNode vn=pn.getChild("variable_declarators_list");
566 ParseNodeVector pnv=vn.getChildren();
567 for(int i=0;i<pnv.size();i++) {
568 ParseNode vardecl=pnv.elementAt(i);
571 ParseNode tmp=vardecl;
572 TypeDescriptor arrayt=t;
573 while (tmp.getChild("single")==null) {
574 arrayt=arrayt.makeArray(state);
575 tmp=tmp.getChild("array");
577 String identifier=tmp.getChild("single").getTerminal();
579 ParseNode epn=vardecl.getChild("initializer");
582 ExpressionNode en=null;
584 en=parseExpression(epn.getFirstChild());
586 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
588 } else if (isNode(pn,"nop")) {
590 } else if (isNode(pn,"expression")) {
591 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
592 } else if (isNode(pn,"ifstatement")) {
593 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
594 parseSingleBlock(pn.getChild("statement").getFirstChild()),
595 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
596 } else if (isNode(pn,"taskexit")) {
598 if (pn.getChild("flag_effects_list")!=null)
599 vfe=parseFlags(pn.getChild("flag_effects_list"));
601 if (pn.getChild("cons_checks")!=null)
602 ccs=parseChecks(pn.getChild("cons_checks"));
604 blockstatements.add(new TaskExitNode(vfe, ccs));
605 } else if (isNode(pn,"return")) {
606 if (isEmpty(pn.getTerminal()))
607 blockstatements.add(new ReturnNode());
609 ExpressionNode en=parseExpression(pn.getFirstChild());
610 blockstatements.add(new ReturnNode(en));
612 } else if (isNode(pn,"block_statement_list")) {
613 BlockNode bn=parseBlockHelper(pn);
614 blockstatements.add(new SubBlockNode(bn));
615 } else if (isNode(pn,"empty")) {
617 } else if (isNode(pn,"statement_expression_list")) {
618 ParseNodeVector pnv=pn.getChildren();
619 BlockNode bn=new BlockNode();
620 for(int i=0;i<pnv.size();i++) {
621 ExpressionNode en=parseExpression(pnv.elementAt(i));
622 blockstatements.add(new BlockExpressionNode(en));
624 bn.setStyle(BlockNode.EXPRLIST);
625 } else if (isNode(pn,"forstatement")) {
626 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
627 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
628 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
629 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
630 blockstatements.add(new LoopNode(init,condition,update,body));
631 } else if (isNode(pn,"whilestatement")) {
632 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
633 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
634 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
635 } else if (isNode(pn,"dowhilestatement")) {
636 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
637 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
638 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
640 System.out.println("---------------");
641 System.out.println(pn.PPrint(3,true));
644 return blockstatements;
647 public MethodDescriptor parseMethodHeader(ParseNode pn) {
648 ParseNode mn=pn.getChild("modifiers");
649 Modifiers m=parseModifiersList(mn);
651 ParseNode tn=pn.getChild("returntype");
652 TypeDescriptor returntype;
654 returntype=parseTypeDescriptor(tn);
656 returntype=new TypeDescriptor(TypeDescriptor.VOID);
658 ParseNode pmd=pn.getChild("method_declarator");
659 String name=pmd.getChild("name").getTerminal();
660 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
662 ParseNode paramnode=pmd.getChild("parameters");
663 parseParameterList(md,paramnode);
667 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
668 ParseNode paramlist=pn.getChild("formal_parameter_list");
671 ParseNodeVector pnv=paramlist.getChildren();
672 for(int i=0;i<pnv.size();i++) {
673 ParseNode paramn=pnv.elementAt(i);
674 TypeDescriptor type=parseTypeDescriptor(paramn);
676 ParseNode tmp=paramn;
677 while (tmp.getChild("single")==null) {
678 type=type.makeArray(state);
679 tmp=tmp.getChild("array");
681 String paramname=tmp.getChild("single").getTerminal();
683 md.addParameter(type,paramname);
687 public Modifiers parseModifiersList(ParseNode pn) {
688 Modifiers m=new Modifiers();
689 ParseNode modlist=pn.getChild("modifier_list");
691 ParseNodeVector pnv=modlist.getChildren();
692 for(int i=0;i<pnv.size();i++) {
693 ParseNode modn=pnv.elementAt(i);
694 if (isNode(modn,"public"))
695 m.addModifier(Modifiers.PUBLIC);
696 else if (isNode(modn,"protected"))
697 m.addModifier(Modifiers.PROTECTED);
698 else if (isNode(modn,"private"))
699 m.addModifier(Modifiers.PRIVATE);
700 else if (isNode(modn,"static"))
701 m.addModifier(Modifiers.STATIC);
702 else if (isNode(modn,"final"))
703 m.addModifier(Modifiers.FINAL);
704 else if (isNode(modn,"native"))
705 m.addModifier(Modifiers.NATIVE);
706 else if (isNode(modn,"synchronized"))
707 m.addModifier(Modifiers.SYNCHRONIZED);
708 else throw new Error("Unrecognized Modifier");
714 private boolean isNode(ParseNode pn, String label) {
715 if (pn.getLabel().equals(label))
720 private static boolean isEmpty(ParseNode pn) {
721 if (pn.getLabel().equals("empty"))
727 private static boolean isEmpty(String s) {
728 if (s.equals("empty"))
734 /** Throw an exception if something is unexpected */
735 private void check(ParseNode pn, String label) {
737 throw new Error(pn+ "IE: Expected '" + label + "', got null");
739 if (! pn.getLabel().equals(label)) {
740 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");