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) {
172 ParseNode paramlist=pn.getChild("task_parameter_list");
175 ParseNodeVector pnv=paramlist.getChildren();
176 for(int i=0;i<pnv.size();i++) {
177 ParseNode paramn=pnv.elementAt(i);
178 if(paramn.getChild("optional")!=null){
180 paramn = paramn.getChild("optional").getFirstChild();
181 System.out.println("OPTIONAL FOUND!!!!!!!");
183 else { optional = false;
184 System.out.println("NOT OPTIONAL");
187 TypeDescriptor type=parseTypeDescriptor(paramn);
189 String paramname=paramn.getChild("single").getTerminal();
190 FlagExpressionNode fen=null;
191 if (paramn.getChild("flag")!=null)
192 fen=parseFlagExpression(paramn.getChild("flag").getFirstChild());
194 ParseNode tagnode=paramn.getChild("tag");
196 TagExpressionList tel=null;
198 tel=parseTagExpressionList(tagnode);
201 td.addParameter(type,paramname,fen, tel, optional);
205 public TagExpressionList parseTagExpressionList(ParseNode pn) {
206 //BUG FIX: change pn.getChildren() to pn.getChild("tag_expression_list").getChildren()
207 //To test, feed in any input program that uses tags
208 ParseNodeVector pnv=pn.getChild("tag_expression_list").getChildren();
209 TagExpressionList tel=new TagExpressionList();
210 for(int i=0;i<pnv.size();i++) {
211 ParseNode tn=pnv.elementAt(i);
212 String type=tn.getChild("type").getTerminal();
213 String name=tn.getChild("single").getTerminal();
214 tel.addTag(type, name);
219 public ClassDescriptor parseTypeDecl(ParseNode pn) {
220 ClassDescriptor cn=new ClassDescriptor(pn.getChild("name").getTerminal());
221 if (!isEmpty(pn.getChild("super").getTerminal())) {
222 /* parse superclass name */
223 ParseNode snn=pn.getChild("super").getChild("type").getChild("class").getChild("name");
224 NameDescriptor nd=parseName(snn);
225 cn.setSuper(nd.toString());
227 if (!(cn.getSymbol().equals(TypeUtil.ObjectClass)||
228 cn.getSymbol().equals(TypeUtil.TagClass)))
229 cn.setSuper(TypeUtil.ObjectClass);
231 cn.setModifiers(parseModifiersList(pn.getChild("modifiers")));
232 parseClassBody(cn, pn.getChild("classbody"));
236 private void parseClassBody(ClassDescriptor cn, ParseNode pn) {
237 ParseNode decls=pn.getChild("class_body_declaration_list");
239 ParseNodeVector pnv=decls.getChildren();
240 for(int i=0;i<pnv.size();i++) {
241 ParseNode decl=pnv.elementAt(i);
242 if (isNode(decl,"member")) {
243 parseClassMember(cn,decl);
244 } else if (isNode(decl,"constructor")) {
245 parseConstructorDecl(cn,decl.getChild("constructor_declaration"));
246 } else if (isNode(decl,"block")) {
247 } else throw new Error();
252 private void parseClassMember(ClassDescriptor cn, ParseNode pn) {
253 ParseNode fieldnode=pn.getChild("field");
255 if (fieldnode!=null) {
256 parseFieldDecl(cn,fieldnode.getChild("field_declaration"));
259 ParseNode methodnode=pn.getChild("method");
260 if (methodnode!=null) {
261 parseMethodDecl(cn,methodnode.getChild("method_declaration"));
264 ParseNode flagnode=pn.getChild("flag");
265 if (flagnode!=null) {
266 parseFlagDecl(cn, flagnode.getChild("flag_declaration"));
272 private TypeDescriptor parseTypeDescriptor(ParseNode pn) {
273 ParseNode tn=pn.getChild("type");
275 String type_st=tn.getTerminal();
276 if(type_st.equals("byte")) {
277 return state.getTypeDescriptor(TypeDescriptor.BYTE);
278 } else if(type_st.equals("short")) {
279 return state.getTypeDescriptor(TypeDescriptor.SHORT);
280 } else if(type_st.equals("boolean")) {
281 return state.getTypeDescriptor(TypeDescriptor.BOOLEAN);
282 } else if(type_st.equals("int")) {
283 return state.getTypeDescriptor(TypeDescriptor.INT);
284 } else if(type_st.equals("long")) {
285 return state.getTypeDescriptor(TypeDescriptor.LONG);
286 } else if(type_st.equals("char")) {
287 return state.getTypeDescriptor(TypeDescriptor.CHAR);
288 } else if(type_st.equals("float")) {
289 return state.getTypeDescriptor(TypeDescriptor.FLOAT);
290 } else if(type_st.equals("double")) {
291 return state.getTypeDescriptor(TypeDescriptor.DOUBLE);
292 } else if(type_st.equals("class")) {
293 ParseNode nn=tn.getChild("class");
294 return state.getTypeDescriptor(parseName(nn.getChild("name")));
295 } else if(type_st.equals("array")) {
296 ParseNode nn=tn.getChild("array");
297 TypeDescriptor td=parseTypeDescriptor(nn.getChild("basetype"));
298 Integer numdims=(Integer)nn.getChild("dims").getLiteral();
299 for(int i=0;i<numdims.intValue();i++)
300 td=td.makeArray(state);
307 private NameDescriptor parseName(ParseNode nn) {
308 ParseNode base=nn.getChild("base");
309 ParseNode id=nn.getChild("identifier");
311 return new NameDescriptor(id.getTerminal());
312 return new NameDescriptor(parseName(base.getChild("name")),id.getTerminal());
316 private void parseFlagDecl(ClassDescriptor cn,ParseNode pn) {
317 String name=pn.getChild("name").getTerminal();
318 FlagDescriptor flag=new FlagDescriptor(name);
319 if (pn.getChild("external")!=null)
324 private void parseFieldDecl(ClassDescriptor cn,ParseNode pn) {
325 ParseNode mn=pn.getChild("modifier");
326 Modifiers m=parseModifiersList(mn);
328 ParseNode tn=pn.getChild("type");
329 TypeDescriptor t=parseTypeDescriptor(tn);
330 ParseNode vn=pn.getChild("variables").getChild("variable_declarators_list");
331 ParseNodeVector pnv=vn.getChildren();
332 boolean isglobal=pn.getChild("global")!=null;
334 for(int i=0;i<pnv.size();i++) {
335 ParseNode vardecl=pnv.elementAt(i);
336 ParseNode tmp=vardecl;
337 TypeDescriptor arrayt=t;
338 while (tmp.getChild("single")==null) {
339 arrayt=arrayt.makeArray(state);
340 tmp=tmp.getChild("array");
342 String identifier=tmp.getChild("single").getTerminal();
343 ParseNode epn=vardecl.getChild("initializer");
345 ExpressionNode en=null;
347 en=parseExpression(epn.getFirstChild());
349 cn.addField(new FieldDescriptor(m, arrayt, identifier, en, isglobal));
353 private ExpressionNode parseExpression(ParseNode pn) {
354 if (isNode(pn,"assignment"))
355 return parseAssignmentExpression(pn);
356 else if (isNode(pn,"logical_or")||isNode(pn,"logical_and")||
357 isNode(pn,"bitwise_or")||isNode(pn,"bitwise_xor")||
358 isNode(pn,"bitwise_and")||isNode(pn,"equal")||
359 isNode(pn,"not_equal")||isNode(pn,"comp_lt")||
360 isNode(pn,"comp_lte")||isNode(pn,"comp_gt")||
361 isNode(pn,"comp_gte")||isNode(pn,"leftshift")||
362 isNode(pn,"rightshift")||isNode(pn,"sub")||
363 isNode(pn,"urightshift")||isNode(pn,"sub")||
364 isNode(pn,"add")||isNode(pn,"mult")||
365 isNode(pn,"div")||isNode(pn,"mod")) {
366 ParseNodeVector pnv=pn.getChildren();
367 ParseNode left=pnv.elementAt(0);
368 ParseNode right=pnv.elementAt(1);
369 Operation op=new Operation(pn.getLabel());
370 return new OpNode(parseExpression(left),parseExpression(right),op);
371 } else if (isNode(pn,"unaryplus")||
372 isNode(pn,"unaryminus")||
375 ParseNode left=pn.getFirstChild();
376 Operation op=new Operation(pn.getLabel());
377 return new OpNode(parseExpression(left),op);
378 } else if (isNode(pn,"postinc")||
379 isNode(pn,"postdec")) {
380 ParseNode left=pn.getFirstChild();
381 AssignOperation op=new AssignOperation(pn.getLabel());
382 return new AssignmentNode(parseExpression(left),null,op);
384 } else if (isNode(pn,"preinc")||
385 isNode(pn,"predec")) {
386 ParseNode left=pn.getFirstChild();
387 AssignOperation op=isNode(pn,"preinc")?new AssignOperation(AssignOperation.PLUSEQ):new AssignOperation(AssignOperation.MINUSEQ);
388 return new AssignmentNode(parseExpression(left),
389 new LiteralNode("integer",new Integer(1)),op);
390 } else if (isNode(pn,"literal")) {
391 String literaltype=pn.getTerminal();
392 ParseNode literalnode=pn.getChild(literaltype);
393 Object literal_obj=literalnode.getLiteral();
394 return new LiteralNode(literaltype, literal_obj);
395 } else if (isNode(pn,"createobject")) {
396 TypeDescriptor td=parseTypeDescriptor(pn);
397 Vector args=parseArgumentList(pn);
398 boolean isglobal=pn.getChild("global")!=null;
399 CreateObjectNode con=new CreateObjectNode(td, isglobal);
400 for(int i=0;i<args.size();i++) {
401 con.addArgument((ExpressionNode)args.get(i));
403 /* Could have flag set or tag added here */
404 if (pn.getChild("flag_list")!=null||pn.getChild("tag_list")!=null) {
405 FlagEffects fe=new FlagEffects(null);
406 if (pn.getChild("flag_list")!=null)
407 parseFlagEffect(fe, pn.getChild("flag_list"));
409 if (pn.getChild("tag_list")!=null)
410 parseTagEffect(fe, pn.getChild("tag_list"));
411 con.addFlagEffects(fe);
415 } else if (isNode(pn,"createarray")) {
416 //System.out.println(pn.PPrint(3,true));
417 boolean isglobal=pn.getChild("createarray")!=null;
418 TypeDescriptor td=parseTypeDescriptor(pn);
419 Vector args=parseDimExprs(pn);
421 if (pn.getChild("dims_opt").getLiteral()!=null)
422 num=((Integer)pn.getChild("dims_opt").getLiteral()).intValue();
423 for(int i=0;i<(args.size()+num);i++)
424 td=td.makeArray(state);
425 CreateObjectNode con=new CreateObjectNode(td, isglobal);
426 for(int i=0;i<args.size();i++) {
427 con.addArgument((ExpressionNode)args.get(i));
430 } else if (isNode(pn,"name")) {
431 NameDescriptor nd=parseName(pn);
432 return new NameNode(nd);
433 } else if (isNode(pn,"this")) {
434 NameDescriptor nd=new NameDescriptor("this");
435 return new NameNode(nd);
436 } else if (isNode(pn,"isavailable")) {
437 NameDescriptor nd=new NameDescriptor(pn.getTerminal());
438 return new OpNode(new NameNode(nd),null,new Operation(Operation.ISAVAILABLE));
439 } else if (isNode(pn,"methodinvoke1")) {
440 NameDescriptor nd=parseName(pn.getChild("name"));
441 Vector args=parseArgumentList(pn);
442 MethodInvokeNode min=new MethodInvokeNode(nd);
443 for(int i=0;i<args.size();i++) {
444 min.addArgument((ExpressionNode)args.get(i));
447 } else if (isNode(pn,"methodinvoke2")) {
448 String methodid=pn.getChild("id").getTerminal();
449 ExpressionNode exp=parseExpression(pn.getChild("base").getFirstChild());
450 Vector args=parseArgumentList(pn);
451 MethodInvokeNode min=new MethodInvokeNode(methodid,exp);
452 for(int i=0;i<args.size();i++) {
453 min.addArgument((ExpressionNode)args.get(i));
456 } else if (isNode(pn,"fieldaccess")) {
457 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild()); String fieldname=pn.getChild("field").getTerminal();
458 return new FieldAccessNode(en,fieldname);
459 } else if (isNode(pn,"arrayaccess")) {
460 ExpressionNode en=parseExpression(pn.getChild("base").getFirstChild());
461 ExpressionNode index=parseExpression(pn.getChild("index").getFirstChild());
462 return new ArrayAccessNode(en,index);
463 } else if (isNode(pn,"cast1")) {
464 return new CastNode(parseTypeDescriptor(pn.getChild("type")),parseExpression(pn.getChild("exp").getFirstChild()));
465 } else if (isNode(pn,"cast2")) {
466 return new CastNode(parseExpression(pn.getChild("type").getFirstChild()),parseExpression(pn.getChild("exp").getFirstChild()));
468 System.out.println("---------------------");
469 System.out.println(pn.PPrint(3,true));
474 private Vector parseDimExprs(ParseNode pn) {
475 Vector arglist=new Vector();
476 ParseNode an=pn.getChild("dim_exprs");
477 if (an==null) /* No argument list */
479 ParseNodeVector anv=an.getChildren();
480 for(int i=0;i<anv.size();i++) {
481 arglist.add(parseExpression(anv.elementAt(i)));
486 private Vector parseArgumentList(ParseNode pn) {
487 Vector arglist=new Vector();
488 ParseNode an=pn.getChild("argument_list");
489 if (an==null) /* No argument list */
491 ParseNodeVector anv=an.getChildren();
492 for(int i=0;i<anv.size();i++) {
493 arglist.add(parseExpression(anv.elementAt(i)));
498 private Vector[] parseConsArgumentList(ParseNode pn) {
499 Vector arglist=new Vector();
500 Vector varlist=new Vector();
501 ParseNode an=pn.getChild("cons_argument_list");
502 if (an==null) /* No argument list */
503 return new Vector[] {varlist, arglist};
504 ParseNodeVector anv=an.getChildren();
505 for(int i=0;i<anv.size();i++) {
506 ParseNode cpn=anv.elementAt(i);
507 ParseNode var=cpn.getChild("var");
508 ParseNode exp=cpn.getChild("exp").getFirstChild();
509 varlist.add(var.getTerminal());
510 arglist.add(parseExpression(exp));
512 return new Vector[] {varlist, arglist};
515 private ExpressionNode parseAssignmentExpression(ParseNode pn) {
516 AssignOperation ao=new AssignOperation(pn.getChild("op").getTerminal());
517 ParseNodeVector pnv=pn.getChild("args").getChildren();
519 AssignmentNode an=new AssignmentNode(parseExpression(pnv.elementAt(0)),parseExpression(pnv.elementAt(1)),ao);
524 private void parseMethodDecl(ClassDescriptor cn, ParseNode pn) {
525 ParseNode headern=pn.getChild("method_header");
526 ParseNode bodyn=pn.getChild("body");
527 MethodDescriptor md=parseMethodHeader(headern);
528 BlockNode bn=parseBlock(bodyn);
530 state.addTreeCode(md,bn);
533 private void parseConstructorDecl(ClassDescriptor cn, ParseNode pn) {
534 ParseNode mn=pn.getChild("modifiers");
535 Modifiers m=parseModifiersList(mn);
536 ParseNode cdecl=pn.getChild("constructor_declarator");
537 boolean isglobal=cdecl.getChild("global")!=null;
538 String name=cdecl.getChild("name").getChild("identifier").getTerminal();
539 MethodDescriptor md=new MethodDescriptor(m, name, isglobal);
540 ParseNode paramnode=cdecl.getChild("parameters");
541 parseParameterList(md,paramnode);
542 ParseNode bodyn0=pn.getChild("body");
543 ParseNode bodyn=bodyn0.getChild("constructor_body");
546 if (bodyn!=null&&bodyn.getChild("block_statement_list")!=null)
547 bn=parseBlock(bodyn);
550 if (bodyn!=null&&bodyn.getChild("superinvoke")!=null) {
551 ParseNode sin=bodyn.getChild("superinvoke");
552 NameDescriptor nd=new NameDescriptor("super");
553 Vector args=parseArgumentList(sin);
554 MethodInvokeNode min=new MethodInvokeNode(nd);
555 for(int i=0;i<args.size();i++) {
556 min.addArgument((ExpressionNode)args.get(i));
558 BlockExpressionNode ben=new BlockExpressionNode(min);
559 bn.addFirstBlockStatement(ben);
561 state.addTreeCode(md,bn);
564 public BlockNode parseBlock(ParseNode pn) {
565 if (pn==null||isEmpty(pn.getTerminal()))
566 return new BlockNode();
567 ParseNode bsn=pn.getChild("block_statement_list");
568 return parseBlockHelper(bsn);
571 private BlockNode parseBlockHelper(ParseNode pn) {
572 ParseNodeVector pnv=pn.getChildren();
573 BlockNode bn=new BlockNode();
574 for(int i=0;i<pnv.size();i++) {
575 Vector bsv=parseBlockStatement(pnv.elementAt(i));
576 for(int j=0;j<bsv.size();j++) {
577 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
583 public BlockNode parseSingleBlock(ParseNode pn) {
584 BlockNode bn=new BlockNode();
585 Vector bsv=parseBlockStatement(pn);
586 for(int j=0;j<bsv.size();j++) {
587 bn.addBlockStatement((BlockStatementNode)bsv.get(j));
589 bn.setStyle(BlockNode.NOBRACES);
593 public Vector parseBlockStatement(ParseNode pn) {
594 Vector blockstatements=new Vector();
595 if (isNode(pn,"tag_declaration")) {
596 String name=pn.getChild("single").getTerminal();
597 String type=pn.getChild("type").getTerminal();
599 blockstatements.add(new TagDeclarationNode(name, type));
600 } else if (isNode(pn,"local_variable_declaration")) {
601 TypeDescriptor t=parseTypeDescriptor(pn);
602 ParseNode vn=pn.getChild("variable_declarators_list");
603 ParseNodeVector pnv=vn.getChildren();
604 for(int i=0;i<pnv.size();i++) {
605 ParseNode vardecl=pnv.elementAt(i);
608 ParseNode tmp=vardecl;
609 TypeDescriptor arrayt=t;
610 while (tmp.getChild("single")==null) {
611 arrayt=arrayt.makeArray(state);
612 tmp=tmp.getChild("array");
614 String identifier=tmp.getChild("single").getTerminal();
616 ParseNode epn=vardecl.getChild("initializer");
619 ExpressionNode en=null;
621 en=parseExpression(epn.getFirstChild());
623 blockstatements.add(new DeclarationNode(new VarDescriptor(arrayt, identifier),en));
625 } else if (isNode(pn,"nop")) {
627 } else if (isNode(pn,"expression")) {
628 blockstatements.add(new BlockExpressionNode(parseExpression(pn.getFirstChild())));
629 } else if (isNode(pn,"ifstatement")) {
630 blockstatements.add(new IfStatementNode(parseExpression(pn.getChild("condition").getFirstChild()),
631 parseSingleBlock(pn.getChild("statement").getFirstChild()),
632 pn.getChild("else_statement")!=null?parseSingleBlock(pn.getChild("else_statement").getFirstChild()):null));
633 } else if (isNode(pn,"taskexit")) {
635 if (pn.getChild("flag_effects_list")!=null)
636 vfe=parseFlags(pn.getChild("flag_effects_list"));
638 if (pn.getChild("cons_checks")!=null)
639 ccs=parseChecks(pn.getChild("cons_checks"));
641 blockstatements.add(new TaskExitNode(vfe, ccs));
642 } else if (isNode(pn,"atomic")) {
643 BlockNode bn=parseBlockHelper(pn);
644 blockstatements.add(new AtomicNode(bn));
645 } else if (isNode(pn,"return")) {
646 if (isEmpty(pn.getTerminal()))
647 blockstatements.add(new ReturnNode());
649 ExpressionNode en=parseExpression(pn.getFirstChild());
650 blockstatements.add(new ReturnNode(en));
652 } else if (isNode(pn,"block_statement_list")) {
653 BlockNode bn=parseBlockHelper(pn);
654 blockstatements.add(new SubBlockNode(bn));
655 } else if (isNode(pn,"empty")) {
657 } else if (isNode(pn,"statement_expression_list")) {
658 ParseNodeVector pnv=pn.getChildren();
659 BlockNode bn=new BlockNode();
660 for(int i=0;i<pnv.size();i++) {
661 ExpressionNode en=parseExpression(pnv.elementAt(i));
662 blockstatements.add(new BlockExpressionNode(en));
664 bn.setStyle(BlockNode.EXPRLIST);
665 } else if (isNode(pn,"forstatement")) {
666 BlockNode init=parseSingleBlock(pn.getChild("initializer").getFirstChild());
667 BlockNode update=parseSingleBlock(pn.getChild("update").getFirstChild());
668 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
669 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
670 blockstatements.add(new LoopNode(init,condition,update,body));
671 } else if (isNode(pn,"whilestatement")) {
672 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
673 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
674 blockstatements.add(new LoopNode(condition,body,LoopNode.WHILELOOP));
675 } else if (isNode(pn,"dowhilestatement")) {
676 ExpressionNode condition=parseExpression(pn.getChild("condition").getFirstChild());
677 BlockNode body=parseSingleBlock(pn.getChild("statement").getFirstChild());
678 blockstatements.add(new LoopNode(condition,body,LoopNode.DOWHILELOOP));
680 System.out.println("---------------");
681 System.out.println(pn.PPrint(3,true));
684 return blockstatements;
687 public MethodDescriptor parseMethodHeader(ParseNode pn) {
688 ParseNode mn=pn.getChild("modifiers");
689 Modifiers m=parseModifiersList(mn);
691 ParseNode tn=pn.getChild("returntype");
692 TypeDescriptor returntype;
694 returntype=parseTypeDescriptor(tn);
696 returntype=new TypeDescriptor(TypeDescriptor.VOID);
698 ParseNode pmd=pn.getChild("method_declarator");
699 String name=pmd.getChild("name").getTerminal();
700 MethodDescriptor md=new MethodDescriptor(m, returntype, name);
702 ParseNode paramnode=pmd.getChild("parameters");
703 parseParameterList(md,paramnode);
707 public void parseParameterList(MethodDescriptor md, ParseNode pn) {
708 ParseNode paramlist=pn.getChild("formal_parameter_list");
711 ParseNodeVector pnv=paramlist.getChildren();
712 for(int i=0;i<pnv.size();i++) {
713 ParseNode paramn=pnv.elementAt(i);
715 if (isNode(paramn, "tag_parameter")) {
716 String paramname=paramn.getChild("single").getTerminal();
717 TypeDescriptor type=new TypeDescriptor(TypeDescriptor.TAG);
718 md.addTagParameter(type, paramname);
720 TypeDescriptor type=parseTypeDescriptor(paramn);
722 ParseNode tmp=paramn;
723 while (tmp.getChild("single")==null) {
724 type=type.makeArray(state);
725 tmp=tmp.getChild("array");
727 String paramname=tmp.getChild("single").getTerminal();
729 md.addParameter(type, paramname);
734 public Modifiers parseModifiersList(ParseNode pn) {
735 Modifiers m=new Modifiers();
736 ParseNode modlist=pn.getChild("modifier_list");
738 ParseNodeVector pnv=modlist.getChildren();
739 for(int i=0;i<pnv.size();i++) {
740 ParseNode modn=pnv.elementAt(i);
741 if (isNode(modn,"public"))
742 m.addModifier(Modifiers.PUBLIC);
743 else if (isNode(modn,"protected"))
744 m.addModifier(Modifiers.PROTECTED);
745 else if (isNode(modn,"private"))
746 m.addModifier(Modifiers.PRIVATE);
747 else if (isNode(modn,"static"))
748 m.addModifier(Modifiers.STATIC);
749 else if (isNode(modn,"final"))
750 m.addModifier(Modifiers.FINAL);
751 else if (isNode(modn,"native"))
752 m.addModifier(Modifiers.NATIVE);
753 else if (isNode(modn,"synchronized"))
754 m.addModifier(Modifiers.SYNCHRONIZED);
755 else if (isNode(modn,"atomic"))
756 m.addModifier(Modifiers.ATOMIC);
757 else throw new Error("Unrecognized Modifier");
763 private boolean isNode(ParseNode pn, String label) {
764 if (pn.getLabel().equals(label))
769 private static boolean isEmpty(ParseNode pn) {
770 if (pn.getLabel().equals("empty"))
776 private static boolean isEmpty(String s) {
777 if (s.equals("empty"))
783 /** Throw an exception if something is unexpected */
784 private void check(ParseNode pn, String label) {
786 throw new Error(pn+ "IE: Expected '" + label + "', got null");
788 if (! pn.getLabel().equals(label)) {
789 throw new Error(pn+ "IE: Expected '" + label + "', got '"+pn.getLabel()+"'");