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