001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist.compiler.ast;
017:
018: import javassist.compiler.CompileError;
019: import javassist.compiler.TokenId;
020:
021: /**
022: * Double constant.
023: */
024: public class DoubleConst extends ASTree {
025: protected double value;
026: protected int type;
027:
028: public DoubleConst(double v, int tokenId) {
029: value = v;
030: type = tokenId;
031: }
032:
033: public double get() {
034: return value;
035: }
036:
037: public void set(double v) {
038: value = v;
039: }
040:
041: /* Returns DoubleConstant or FloatConstant
042: */
043: public int getType() {
044: return type;
045: }
046:
047: public String toString() {
048: return Double.toString(value);
049: }
050:
051: public void accept(Visitor v) throws CompileError {
052: v.atDoubleConst(this );
053: }
054:
055: public ASTree compute(int op, ASTree right) {
056: if (right instanceof IntConst)
057: return compute0(op, (IntConst) right);
058: else if (right instanceof DoubleConst)
059: return compute0(op, (DoubleConst) right);
060: else
061: return null;
062: }
063:
064: private DoubleConst compute0(int op, DoubleConst right) {
065: int newType;
066: if (this .type == TokenId.DoubleConstant
067: || right.type == TokenId.DoubleConstant)
068: newType = TokenId.DoubleConstant;
069: else
070: newType = TokenId.FloatConstant;
071:
072: return compute(op, this .value, right.value, newType);
073: }
074:
075: private DoubleConst compute0(int op, IntConst right) {
076: return compute(op, this .value, (double) right.value, this .type);
077: }
078:
079: private static DoubleConst compute(int op, double value1,
080: double value2, int newType) {
081: double newValue;
082: switch (op) {
083: case '+':
084: newValue = value1 + value2;
085: break;
086: case '-':
087: newValue = value1 - value2;
088: break;
089: case '*':
090: newValue = value1 * value2;
091: break;
092: case '/':
093: newValue = value1 / value2;
094: break;
095: case '%':
096: newValue = value1 % value2;
097: break;
098: default:
099: return null;
100: }
101:
102: return new DoubleConst(newValue, newType);
103: }
104: }
|