01: /*
02: * $Id: ConstantVariableNode.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 anvil.ErrorListener;
17: import anvil.script.ConstantVariableType;
18: import anvil.script.Type;
19: import anvil.script.statements.ModuleStatement;
20: import java.io.IOException;
21:
22: /**
23: * class ConstantVariableNode
24: *
25: * @author: Jani Lehtimäki
26: */
27: public class ConstantVariableNode extends Node {
28:
29: protected ConstantVariableType _constant;
30:
31: public ConstantVariableNode(ConstantVariableType constant) {
32: super ();
33: _constant = constant;
34: }
35:
36: public int typeOf() {
37: return Node.EXPR_CONSTANT_VARIABLE;
38: }
39:
40: public boolean isConstant() {
41: return _constant.hasConstantInitializer();
42: }
43:
44: public Node optimize() {
45: return this ;
46: }
47:
48: public Any eval() {
49: return _constant.getValue();
50: }
51:
52: public void compile(ByteCompiler context, Node child) {
53: child.compile(context, GET);
54: Code code = context.getCode();
55: code.dup();
56: code.putstatic(_constant.getTypeRef(code.getPool()));
57: }
58:
59: public void compile(ByteCompiler context, int operation) {
60: Code code = context.getCode();
61: int field = _constant.getTypeRef(context.getPool());
62: switch (operation) {
63: case GET:
64: case CHECK:
65: if (isConstant()) {
66: context.constant(_constant.getValue(), false);
67: } else {
68: code.getstatic(field);
69: }
70: break;
71:
72: case GET_BOOLEAN:
73: if (isConstant()) {
74: code.iconst(_constant.getValue().toBoolean());
75: } else {
76: code.getstatic(field);
77: context.any2boolean();
78: }
79: break;
80:
81: case DELETE:
82: code.iconst(false);
83: break;
84: }
85: }
86:
87: }
|