request 1152810, implementing formatting of array initializers
[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
19 public class FloatLiteral extends NumberLiteral {
20         float value;
21         final static float Float_MIN_VALUE = Float.intBitsToFloat(1); // work-around VAJ problem 1F6IGUU
22 public FloatLiteral(char[] token, int s, int e) {
23         super(token, s,e);
24 }
25 public void computeConstant() {
26
27         //the source is correctly formated so the exception should never occurs
28
29         Float computedValue;
30         try {
31                 computedValue = Float.valueOf(String.valueOf(source));
32         } catch (NumberFormatException e) {
33                 return;
34         } 
35
36         if (computedValue.doubleValue() > Float.MAX_VALUE){
37                 return; //may be Infinity
38         }
39         if (computedValue.floatValue() < Float_MIN_VALUE){
40                 // see 1F6IGUU
41                 //only a true 0 can be made of zeros
42                 //1.00000000e-46f is illegal ....
43                 label : for (int i = 0; i < source.length; i++) { 
44                         switch (source[i]) {
45                                 case '.' :
46                                 case 'f' :
47                                 case 'F' :
48                                 case '0' :
49                                         break;
50                                 case 'e' :
51                                 case 'E' :
52                                         break label; //exposant are valid !....
53                                 default :
54                                         return; //error
55
56
57                         }
58                 }
59         }
60         constant = Constant.fromValue(value = computedValue.floatValue());
61 }
62 /**
63  * Code generation for float literal
64  *
65  * @param currentScope net.sourceforge.phpdt.internal.compiler.lookup.BlockScope
66  * @param codeStream net.sourceforge.phpdt.internal.compiler.codegen.CodeStream
67  * @param valueRequired boolean
68  */ 
69 //public void generateCode(BlockScope currentScope, CodeStream codeStream, boolean valueRequired) {
70 //      int pc = codeStream.position;
71 //      if (valueRequired)
72 //              if ((implicitConversion >> 4) == T_float)
73 //                      codeStream.generateInlinedValue(value);
74 //              else
75 //                      codeStream.generateConstant(constant, implicitConversion);
76 //      codeStream.recordPositionsFrom(pc, this.sourceStart);
77 //}
78 public TypeBinding literalType(BlockScope scope) {
79         return FloatBinding;
80 }
81 public void traverse(IAbstractSyntaxTreeVisitor visitor, BlockScope blockScope) {
82         visitor.visit(this, blockScope);
83         visitor.endVisit(this, blockScope);
84 }
85 }