1   // Copyright 2013 Bill Campbell, Swami Iyer and Bahar Akbal-Delibas
2   
3   package jminusminus;
4   
5   import static jminusminus.CLConstants.*;
6   
7   /**
8    * The AST node for a char literal.
9    */
10  
11  class JLiteralChar extends JExpression {
12  
13      /** String representation of the char. */
14      private String text;
15  
16      /**
17       * Construct an AST node for a char literal given its line number and text
18       * representation.
19       * 
20       * @param line
21       *            line in which the literal occurs in the source file.
22       * @param text
23       *            string representation of the literal.
24       */
25  
26      public JLiteralChar(int line, String text) {
27          super(line);
28          this.text = text;
29      }
30  
31      /**
32       * Analyzing a char literal is trivial.
33       * 
34       * @param context
35       *            context in which names are resolved (ignored here).
36       * @return the analyzed (and possibly rewritten) AST subtree.
37       */
38  
39      public JExpression analyze(Context context) {
40          type = Type.CHAR;
41          return this;
42      }
43  
44      /**
45       * Generating code for a char literal means generating code to push it onto
46       * the stack.
47       * 
48       * @param output
49       *            the code emitter (basically an abstraction for producing the
50       *            .class file).
51       */
52  
53      public void codegen(CLEmitter output) {
54          // Unescape the escaped escapes
55          String s = Util.unescape(text);
56  
57          // The string representation is padded (by hand-written
58          // and JavaCC scanner) with single quotes, so we extract
59          // the char at 1
60          char c = s.charAt(1);
61          int i = (int) c;
62          switch (i) {
63          case 0:
64              output.addNoArgInstruction(ICONST_0);
65              break;
66          case 1:
67              output.addNoArgInstruction(ICONST_1);
68              break;
69          case 2:
70              output.addNoArgInstruction(ICONST_2);
71              break;
72          case 3:
73              output.addNoArgInstruction(ICONST_3);
74              break;
75          case 4:
76              output.addNoArgInstruction(ICONST_4);
77              break;
78          case 5:
79              output.addNoArgInstruction(ICONST_5);
80              break;
81          default:
82              if (i >= 6 && i <= 127) {
83                  output.addOneArgInstruction(BIPUSH, i);
84              } else if (i >= 128 && i <= 32767) {
85                  output.addOneArgInstruction(SIPUSH, i);
86              } else {
87                  output.addLDCInstruction(i);
88              }
89          }
90      }
91  
92      /**
93       * @inheritDoc
94       */
95  
96      public void writeToStdOut(PrettyPrinter p) {
97          p.printf("<JLiteralChar line=\"%d\" type=\"%s\" " + "value=\"%s\"/>\n",
98                  line(), ((type == null) ? "" : type.toString()), Util
99                          .escapeSpecialXMLChars(text));
100     }
101 
102 }
103