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 switch-statement.
11   */
12  public class JSwitchStatement extends JStatement {
13      // Test expression.
14      private JExpression condition;
15  
16      // List of switch-statement groups.
17      private ArrayList<SwitchStatementGroup> stmtGroup;
18  
19      /**
20       * Constructs an AST node for a switch-statement.
21       *
22       * @param line      line in which the switch-statement occurs in the source file.
23       * @param condition test expression.
24       * @param stmtGroup list of statement groups.
25       */
26      public JSwitchStatement(int line, JExpression condition,
27                              ArrayList<SwitchStatementGroup> stmtGroup) {
28          super(line);
29          this.condition = condition;
30          this.stmtGroup = stmtGroup;
31      }
32  
33      /**
34       * {@inheritDoc}
35       */
36      public JStatement analyze(Context context) {
37          // TODO
38          return this;
39      }
40  
41      /**
42       * {@inheritDoc}
43       */
44      public void codegen(CLEmitter output) {
45          // TODO
46      }
47  
48      /**
49       * {@inheritDoc}
50       */
51      public void toJSON(JSONElement json) {
52          JSONElement e = new JSONElement();
53          json.addChild("JSwitchStatement:" + line, e);
54          JSONElement e1 = new JSONElement();
55          e.addChild("Condition", e1);
56          condition.toJSON(e1);
57          for (SwitchStatementGroup group : stmtGroup) {
58              group.toJSON(e);
59          }
60      }
61  }
62  
63  /**
64   * A switch statement group consists of case labels and a block of statements.
65   */
66  class SwitchStatementGroup {
67      // Case labels.
68      private ArrayList<JExpression> switchLabels;
69  
70      // Block of statements.
71      private ArrayList<JStatement> block;
72  
73      /**
74       * Constructs a switch-statement group.
75       *
76       * @param switchLabels case labels.
77       * @param block        block of statements.
78       */
79      public SwitchStatementGroup(ArrayList<JExpression> switchLabels, ArrayList<JStatement> block) {
80          this.switchLabels = switchLabels;
81          this.block = block;
82      }
83  
84      /**
85       * Stores information about this switch statement group in JSON format.
86       *
87       * @param json the JSON emitter.
88       */
89      public void toJSON(JSONElement json) {
90          JSONElement e = new JSONElement();
91          json.addChild("SwitchStatementGroup", e);
92          for (JExpression label : switchLabels) {
93              JSONElement e1 = new JSONElement();
94              if (label != null) {
95                  e.addChild("Case", e1);
96                  label.toJSON(e1);
97              } else {
98                  e.addChild("Default", e1);
99              }
100         }
101         if (block != null) {
102             for (JStatement stmt : block) {
103                 stmt.toJSON(e);
104             }
105         }
106     }
107 }
108