KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > compiler > ast > FloatLiteral


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.compiler.ast;
12
13 import org.eclipse.jdt.internal.compiler.ASTVisitor;
14 import org.eclipse.jdt.internal.compiler.codegen.CodeStream;
15 import org.eclipse.jdt.internal.compiler.impl.FloatConstant;
16 import org.eclipse.jdt.internal.compiler.lookup.BlockScope;
17 import org.eclipse.jdt.internal.compiler.lookup.TypeBinding;
18 import org.eclipse.jdt.internal.compiler.util.FloatUtil;
19
20 public class FloatLiteral extends NumberLiteral {
21     float value;
22     final static float Float_MIN_VALUE = Float.intBitsToFloat(1); // work-around VAJ problem 1F6IGUU
23
public FloatLiteral(char[] token, int s, int e) {
24         super(token, s, e);
25     }
26     public void computeConstant() {
27         Float JavaDoc computedValue;
28         try {
29             computedValue = Float.valueOf(String.valueOf(source));
30         } catch (NumberFormatException JavaDoc e) {
31             // hex floating point literal
32
// being rejected by 1.4 libraries where Float.valueOf(...) doesn't handle hex decimal floats
33
try {
34                 float v = FloatUtil.valueOfHexFloatLiteral(source);
35                 if (v == Float.POSITIVE_INFINITY) {
36                     // error: the number is too large to represent
37
return;
38                 }
39                 if (Float.isNaN(v)) {
40                     // error: the number is too small to represent
41
return;
42                 }
43                 value = v;
44                 constant = FloatConstant.fromValue(v);
45             } catch (NumberFormatException JavaDoc e1) {
46                 // if the computation of the constant fails
47
}
48             return;
49         }
50
51         final float floatValue = computedValue.floatValue();
52         if (floatValue > Float.MAX_VALUE) {
53             // error: the number is too large to represent
54
return;
55         }
56         if (floatValue < Float.MIN_VALUE) {
57             // see 1F6IGUU
58
// a true 0 only has '0' and '.' in mantissa
59
// 1.0e-5000d is non-zero, but underflows to 0
60
boolean isHexaDecimal = false;
61             label : for (int i = 0; i < source.length; i++) { //it is welled formated so just test against '0' and potential . D d
62
switch (source[i]) {
63                     case '0' :
64                     case '.' :
65                         break;
66                     case 'x' :
67                     case 'X' :
68                         isHexaDecimal = true;
69                         break;
70                     case 'e' :
71                     case 'E' :
72                     case 'f' :
73                     case 'F' :
74                     case 'd' :
75                     case 'D' :
76                         if (isHexaDecimal) {
77                             return;
78                         }
79                         // starting the exponent - mantissa is all zero
80
// no exponent - mantissa is all zero
81
break label;
82                     case 'p' :
83                     case 'P' :
84                         break label;
85                     default :
86                         // error: the number is too small to represent
87
return;
88                 }
89             }
90         }
91         value = floatValue;
92         constant = FloatConstant.fromValue(value);
93     }
94     /**
95      * Code generation for float literal
96      *
97      * @param currentScope org.eclipse.jdt.internal.compiler.lookup.BlockScope
98      * @param codeStream org.eclipse.jdt.internal.compiler.codegen.CodeStream
99      * @param valueRequired boolean
100      */

101     public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
102         int pc = codeStream.position;
103         if (valueRequired) {
104             codeStream.generateConstant(constant, implicitConversion);
105         }
106         codeStream.recordPositionsFrom(pc, this.sourceStart);
107     }
108     public TypeBinding literalType(BlockScope scope) {
109         return TypeBinding.FLOAT;
110     }
111     public void traverse(ASTVisitor visitor, BlockScope scope) {
112         visitor.visit(this, scope);
113         visitor.endVisit(this, scope);
114     }
115 }
116
Popular Tags