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 try-catch-finally statement.
11   */
12  class JTryStatement extends JStatement {
13      // The try block.
14      private JBlock tryBlock;
15  
16      // The catch parameters.
17      private ArrayList<JFormalParameter> parameters;
18  
19      // The catch blocks.
20      private ArrayList<JBlock> catchBlocks;
21  
22      // The finally block.
23      private JBlock finallyBlock;
24  
25      /**
26       * Constructs an AST node for a try-statement.
27       *
28       * @param line         line in which the while-statement occurs in the source file.
29       * @param tryBlock     the try block.
30       * @param parameters   the catch parameters.
31       * @param catchBlocks  the catch blocks.
32       * @param finallyBlock the finally block.
33       */
34      public JTryStatement(int line, JBlock tryBlock, ArrayList<JFormalParameter> parameters,
35                           ArrayList<JBlock> catchBlocks, JBlock finallyBlock) {
36          super(line);
37          this.tryBlock = tryBlock;
38          this.parameters = parameters;
39          this.catchBlocks = catchBlocks;
40          this.finallyBlock = finallyBlock;
41      }
42  
43      /**
44       * {@inheritDoc}
45       */
46      public JTryStatement 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("JTryStatement:" + line, e);
64          JSONElement e1 = new JSONElement();
65          e.addChild("TryBlock", e1);
66          tryBlock.toJSON(e1);
67          if (catchBlocks != null) {
68              for (int i = 0; i < catchBlocks.size(); i++) {
69                  JFormalParameter param = parameters.get(i);
70                  JBlock catchBlock = catchBlocks.get(i);
71                  JSONElement e2 = new JSONElement();
72                  e.addChild("CatchBlock", e2);
73                  String s = String.format("[\"%s\", \"%s\"]", param.name(), param.type() == null ?
74                          "" : param.type().toString());
75                  e2.addAttribute("parameter", s);
76                  catchBlock.toJSON(e2);
77              }
78          }
79          if (finallyBlock != null) {
80              JSONElement e2 = new JSONElement();
81              e.addChild("FinallyBlock", e2);
82              finallyBlock.toJSON(e2);
83          }
84      }
85  }
86