1
3 package jminusminus;
4
5 import static jminusminus.CLConstants.*;
6
7
10 class JLiteralBoolean extends JExpression {
11 private String text;
13
14
20 public JLiteralBoolean(int line, String text) {
21 super(line);
22 this.text = text;
23 }
24
25
30 public boolean toBoolean() {
31 return text.equals("true");
32 }
33
34
37 public JExpression analyze(Context context) {
38 type = Type.BOOLEAN;
39 return this;
40 }
41
42
45 public void codegen(CLEmitter output) {
46 output.addNoArgInstruction(toBoolean() ? ICONST_1 : ICONST_0);
47 }
48
49
52 public void codegen(CLEmitter output, String targetLabel, boolean onTrue) {
53 boolean b = toBoolean();
54 if (b && onTrue || !b && !onTrue) {
55 output.addBranchInstruction(GOTO, targetLabel);
56 }
57 }
58
59
62 public void toJSON(JSONElement json) {
63 JSONElement e = new JSONElement();
64 json.addChild("JLiteralBoolean:" + line, e);
65 e.addAttribute("type", type == null ? "" : type.toString());
66 e.addAttribute("value", text);
67 }
68 }
69