1 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); public FloatLiteral(char[] token, int s, int e) { 24 super(token, s, e); 25 } 26 public void computeConstant() { 27 Float computedValue; 28 try { 29 computedValue = Float.valueOf(String.valueOf(source)); 30 } catch (NumberFormatException e) { 31 try { 34 float v = FloatUtil.valueOfHexFloatLiteral(source); 35 if (v == Float.POSITIVE_INFINITY) { 36 return; 38 } 39 if (Float.isNaN(v)) { 40 return; 42 } 43 value = v; 44 constant = FloatConstant.fromValue(v); 45 } catch (NumberFormatException e1) { 46 } 48 return; 49 } 50 51 final float floatValue = computedValue.floatValue(); 52 if (floatValue > Float.MAX_VALUE) { 53 return; 55 } 56 if (floatValue < Float.MIN_VALUE) { 57 boolean isHexaDecimal = false; 61 label : for (int i = 0; i < source.length; i++) { 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 break label; 82 case 'p' : 83 case 'P' : 84 break label; 85 default : 86 return; 88 } 89 } 90 } 91 value = floatValue; 92 constant = FloatConstant.fromValue(value); 93 } 94 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 |