1
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7
10
11 class JLiteralChar extends JExpression {
12
13
14 private String text;
15
16
25
26 public JLiteralChar(int line, String text) {
27 super(line);
28 this.text = text;
29 }
30
31
38
39 public JExpression analyze(Context context) {
40 type = Type.CHAR;
41 return this;
42 }
43
44
52
53 public void codegen(CLEmitter output) {
54 String s = Util.unescape(text);
56
57 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
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