Esempio n. 1
0
        public NumberLiteral ReadNumberLiteral()
        {
            var location0 = Tokenizer.GetCurrentLocation();

            if (CurToken.Type != TokenType.Number)
            {
                ThrowExpect("number", CurToken);
            }

            if (CurToken.Value.StartsWith("0x", StringComparison.InvariantCulture))
            {
                var literal = new NumberLiteral(location0)
                {
                    HexFormat = true
                };

                if (CurToken.Value.Contains(".") ||
                    CurToken.Value.Contains("p") ||
                    CurToken.Value.Contains("P")
                    )
                {
                    literal.Value = LexerUtils.ParseHexFloat(CurToken);
                }
                else
                {
                    literal.Value = LexerUtils.ParseHexInteger(CurToken);
                }

                //if (!int.TryParse(CurToken.Value.Substring(2),
                //    System.Globalization.NumberStyles.AllowHexSpecifier,
                //    null,
                //    out int hexvalue))
                //{
                //}

                Move();

                return(literal);
            }

            if (!double.TryParse(CurToken.Value, out double value))
            {
                ThrowExpect("number", CurToken);
            }

            Move();
            return(new NumberLiteral(location0)
            {
                Value = value
            });
        }
Esempio n. 2
0
        public static double ParseHexFloat(string s)
        {
            try
            {
                if ((s.Length < 2) || (s[0] != '0' && (char.ToUpper(s[1]) != 'X')))
                {
                    throw new InternalErrorException("hex float must start with '0x' near '{0}'", s);
                }

                s = s.Substring(2);

                double value = 0.0;
                int    dummy, exp = 0;

                s = LexerUtils.ReadHexProgressive(s, ref value, out dummy);

                if (s.Length > 0 && s[0] == '.')
                {
                    s = s.Substring(1);
                    s = LexerUtils.ReadHexProgressive(s, ref value, out exp);
                }

                exp *= -4;

                if (s.Length > 0 && char.ToUpper(s[0]) == 'P')
                {
                    if (s.Length == 1)
                    {
                        throw new InternalErrorException("invalid hex float format near '{0}'", s);
                    }

                    s = s.Substring(s[1] == '+' ? 2 : 1);

                    int exp1 = int.Parse(s, CultureInfo.InvariantCulture);

                    exp += exp1;
                }

                double result = value * Math.Pow(2, exp);

                return(result);
            }
            catch (FormatException)
            {
                throw new InternalErrorException("malformed number near '{0}'", s);
            }
        }