KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > polyglot > ext > jl > ast > Do_c


1 package polyglot.ext.jl.ast;
2
3 import polyglot.ast.*;
4 import polyglot.types.*;
5 import polyglot.visit.*;
6 import polyglot.util.*;
7 import java.util.*;
8
9 /**
10  * A immutable representation of a Java language <code>do</code> statement.
11  * It contains a statement to be executed and an expression to be tested
12  * indicating whether to reexecute the statement.
13  */

14 public class Do_c extends Loop_c implements Do
15 {
16     protected Stmt body;
17     protected Expr cond;
18
19     public Do_c(Position pos, Stmt body, Expr cond) {
20     super(pos);
21     this.body = body;
22     this.cond = cond;
23     }
24
25     /** Get the body of the statement. */
26     public Stmt body() {
27     return this.body;
28     }
29
30     /** Set the body of the statement. */
31     public Do body(Stmt body) {
32     Do_c n = (Do_c) copy();
33     n.body = body;
34     return n;
35     }
36
37     /** Get the conditional of the statement. */
38     public Expr cond() {
39     return this.cond;
40     }
41
42     /** Set the conditional of the statement. */
43     public Do cond(Expr cond) {
44     Do_c n = (Do_c) copy();
45     n.cond = cond;
46     return n;
47     }
48
49     /** Reconstruct the statement. */
50     protected Do_c reconstruct(Stmt body, Expr cond) {
51     if (body != this.body || cond != this.cond) {
52         Do_c n = (Do_c) copy();
53         n.body = body;
54         n.cond = cond;
55         return n;
56     }
57
58     return this;
59     }
60
61     /** Visit the children of the statement. */
62     public Node visitChildren(NodeVisitor v) {
63     Stmt body = (Stmt) visitChild(this.body, v);
64     Expr cond = (Expr) visitChild(this.cond, v);
65     return reconstruct(body, cond);
66     }
67
68     /** Type check the statement. */
69     public Node typeCheck(TypeChecker tc) throws SemanticException
70     {
71         TypeSystem ts = tc.typeSystem();
72
73         if (! ts.equals(cond.type(), ts.Boolean())) {
74         throw new SemanticException(
75         "Condition of do statement must have boolean type.",
76         cond.position());
77     }
78
79     return this;
80     }
81
82     public Type childExpectedType(Expr child, AscriptionVisitor av) {
83         TypeSystem ts = av.typeSystem();
84
85         if (child == cond) {
86             return ts.Boolean();
87         }
88
89         return child.type();
90     }
91
92     public String JavaDoc toString() {
93     return "do { ... } while (" + cond + ")";
94     }
95
96     /** Write the statement to an output file. */
97     public void prettyPrint(CodeWriter w, PrettyPrinter tr)
98     {
99     w.write("do ");
100     printSubStmt(body, w, tr);
101     w.write("while(");
102     printBlock(cond, w, tr);
103     w.write("); ");
104     }
105
106
107     public Term entry() {
108         return body.entry();
109     }
110
111     public List acceptCFG(CFGBuilder v, List succs) {
112         v.push(this).visitCFG(body, cond.entry());
113
114         if (condIsConstantTrue()) {
115             v.visitCFG(cond, body.entry());
116         }
117         else {
118             v.visitCFG(cond, FlowGraph.EDGE_KEY_TRUE, body.entry(),
119                              FlowGraph.EDGE_KEY_FALSE, this);
120         }
121
122         return succs;
123     }
124
125     public Term continueTarget() {
126         return cond.entry();
127     }
128 }
129
Popular Tags