1   // Copyright 2012- Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2   
3   package jminusminus;
4   
5   import java.util.ArrayList;
6   
7   import static jminusminus.CLConstants.*;
8   
9   /**
10   * The AST node for a for-statement.
11   */
12  class JForStatement extends JStatement {
13      // Initialization.
14      private ArrayList<JStatement> init;
15  
16      // Test expression
17      private JExpression condition;
18  
19      // Update.
20      private ArrayList<JStatement> update;
21  
22      // The body.
23      private JStatement body;
24  
25      /**
26       * Constructs an AST node for a for-statement.
27       *
28       * @param line      line in which the for-statement occurs in the source file.
29       * @param init      the initialization.
30       * @param condition the test expression.
31       * @param update    the update.
32       * @param body      the body.
33       */
34      public JForStatement(int line, ArrayList<JStatement> init, JExpression condition,
35                           ArrayList<JStatement> update, JStatement body) {
36          super(line);
37          this.init = init;
38          this.condition = condition;
39          this.update = update;
40          this.body = body;
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      public JForStatement analyze(Context context) {
47          // TODO
48          return this;
49      }
50  
51      /**
52       * {@inheritDoc}
53       */
54      public void codegen(CLEmitter output) {
55          // TODO
56      }
57  
58      /**
59       * {@inheritDoc}
60       */
61      public void toJSON(JSONElement json) {
62          JSONElement e = new JSONElement();
63          json.addChild("JForStatement:" + line, e);
64          if (init != null) {
65              JSONElement e1 = new JSONElement();
66              e.addChild("Init", e1);
67              for (JStatement stmt : init) {
68                  stmt.toJSON(e1);
69              }
70          }
71          if (condition != null) {
72              JSONElement e1 = new JSONElement();
73              e.addChild("Condition", e1);
74              condition.toJSON(e1);
75          }
76          if (update != null) {
77              JSONElement e1 = new JSONElement();
78              e.addChild("Update", e1);
79              for (JStatement stmt : update) {
80                  stmt.toJSON(e1);
81              }
82          }
83          if (body != null) {
84              JSONElement e1 = new JSONElement();
85              e.addChild("Body", e1);
86              body.toJSON(e1);
87          }
88      }
89  }
90