Committing the first phase of the phpDocBuilder.
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / ast / FloatLiteral.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.ast;
12
13 import net.sourceforge.phpdt.internal.compiler.IAbstractSyntaxTreeVisitor;
14 import net.sourceforge.phpdt.internal.compiler.impl.Constant;
15 import net.sourceforge.phpdt.internal.compiler.lookup.BlockScope;
16 import net.sourceforge.phpdt.internal.compiler.lookup.TypeBinding;
17
18 public class FloatLiteral extends NumberLiteral {
19         float value;
20
21         final static float Float_MIN_VALUE = Float.intBitsToFloat(1); // work-around
22                                                                                                                                         // VAJ
23                                                                                                                                         // problem
24                                                                                                                                         // 1F6IGUU
25
26         public FloatLiteral(char[] token, int s, int e) {
27                 super(token, s, e);
28         }
29
30         public void computeConstant() {
31
32                 // the source is correctly formated so the exception should never occurs
33
34                 Float computedValue;
35                 try {
36                         computedValue = Float.valueOf(String.valueOf(source));
37                 } catch (NumberFormatException e) {
38                         return;
39                 }
40
41                 if (computedValue.doubleValue() > Float.MAX_VALUE) {
42                         return; // may be Infinity
43                 }
44                 if (computedValue.floatValue() < Float_MIN_VALUE) {
45                         // see 1F6IGUU
46                         // only a true 0 can be made of zeros
47                         // 1.00000000e-46f is illegal ....
48                         label: for (int i = 0; i < source.length; i++) {
49                                 switch (source[i]) {
50                                 case '.':
51                                 case 'f':
52                                 case 'F':
53                                 case '0':
54                                         break;
55                                 case 'e':
56                                 case 'E':
57                                         break label; // exposant are valid !....
58                                 default:
59                                         return; // error
60
61                                 }
62                         }
63                 }
64                 constant = Constant.fromValue(value = computedValue.floatValue());
65         }
66
67         /**
68          * Code generation for float literal
69          * 
70          * @param currentScope
71          *            net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
72          * @param codeStream
73          *            net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
74          * @param valueRequired
75          *            boolean
76          */
77         // public void generateCode(BlockScope currentScope, CodeStream codeStream,
78         // boolean valueRequired) {
79         // int pc = codeStream.position;
80         // if (valueRequired)
81         // if ((implicitConversion >> 4) == T_float)
82         // codeStream.generateInlinedValue(value);
83         // else
84         // codeStream.generateConstant(constant, implicitConversion);
85         // codeStream.recordPositionsFrom(pc, this.sourceStart);
86         // }
87         public TypeBinding literalType(BlockScope scope) {
88                 return FloatBinding;
89         }
90
91         public void traverse(IAbstractSyntaxTreeVisitor visitor,
92                         BlockScope blockScope) {
93                 visitor.visit(this, blockScope);
94                 visitor.endVisit(this, blockScope);
95         }
96 }