1
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7
10 class JLiteralInt extends JExpression {
11 private String text;
13
14
20 public JLiteralInt(int line, String text) {
21 super(line);
22 this.text = text;
23 }
24
25
30 public int toInt() {
31 return Integer.parseInt(text);
32 }
33
34
37 public JExpression analyze(Context context) {
38 type = Type.INT;
39 return this;
40 }
41
42
45 public void codegen(CLEmitter output) {
46 int i = toInt();
47 switch (i) {
48 case 0:
49 output.addNoArgInstruction(ICONST_0);
50 break;
51 case 1:
52 output.addNoArgInstruction(ICONST_1);
53 break;
54 case 2:
55 output.addNoArgInstruction(ICONST_2);
56 break;
57 case 3:
58 output.addNoArgInstruction(ICONST_3);
59 break;
60 case 4:
61 output.addNoArgInstruction(ICONST_4);
62 break;
63 case 5:
64 output.addNoArgInstruction(ICONST_5);
65 break;
66 default:
67 if (i >= 6 && i <= 127) {
68 output.addOneArgInstruction(BIPUSH, i);
69 } else if (i >= 128 && i <= 32767) {
70 output.addOneArgInstruction(SIPUSH, i);
71 } else {
72 output.addLDCInstruction(i);
73 }
74 }
75 }
76
77
80 public void toJSON(JSONElement json) {
81 JSONElement e = new JSONElement();
82 json.addChild("JLiteralInt:" + line, e);
83 e.addAttribute("type", type == null ? "" : type.toString());
84 e.addAttribute("value", text);
85 }
86 }
87