01: /*
02: * $Id: ConstantNode.java,v 1.17 2002/09/16 08:05:04 jkl Exp $
03: *
04: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
05: *
06: * Use is subject to license terms, as defined in
07: * Anvil Sofware License, Version 1.1. See LICENSE
08: * file, or http://njet.org/license-1.1.txt
09: */
10: package anvil.script.expression;
11:
12: import anvil.core.Any;
13: import anvil.codec.Code;
14: import anvil.script.compiler.ByteCompiler;
15: import anvil.script.Context;
16: import java.io.IOException;
17:
18: /**
19: * class ConstantNode
20: *
21: * @author: Jani Lehtimäki
22: */
23: public class ConstantNode extends Node {
24:
25: public static final ConstantNode NULL = new ConstantNode(Any.NULL);
26: public static final ConstantNode UNDEFINED = new ConstantNode(
27: Any.UNDEFINED);
28: public static final ConstantNode TRUE = new ConstantNode(Any.TRUE);
29: public static final ConstantNode FALSE = new ConstantNode(Any.FALSE);
30: public static final ConstantNode INF = new ConstantNode(Any.INF);
31: public static final ConstantNode NEG_INF = new ConstantNode(
32: Any.NEG_INF);
33: public static final ConstantNode SPACE = new ConstantNode(Any
34: .create(" "));
35:
36: private Any _const;
37: private String _image = null;
38:
39: public ConstantNode() {
40: _const = Any.NULL;
41: }
42:
43: public ConstantNode(Any constant) {
44: super ();
45: _const = constant;
46: }
47:
48: public ConstantNode(String image, long number) {
49: super ();
50: _const = Any.create(number);
51: _image = image;
52: }
53:
54: public ConstantNode(String string) {
55: super ();
56: _const = Any.create(string);
57: }
58:
59: public ConstantNode(boolean bool) {
60: super ();
61: _const = Any.create(bool);
62: }
63:
64: public int typeOf() {
65: return Node.EXPR_CONSTANT;
66: }
67:
68: public boolean isConstant() {
69: return true;
70: }
71:
72: public String getImage() {
73: return _image;
74: }
75:
76: public Node optimize() {
77: return this ;
78: }
79:
80: public String toString() {
81: return super .toString() + '(' + _const.toAnvil() + ')';
82: }
83:
84: public Any eval() {
85: return (Any) _const.copy();
86: }
87:
88: public void compile(ByteCompiler context, int operation) {
89: Code code = context.getCode();
90: if (operation == GET_BOOLEAN) {
91: code.iconst(_const.toBoolean());
92: } else {
93: context.constant(_const, true);
94: }
95: }
96:
97: }
|