示例#1
0
        private bool TryReadFrac(
            out FloatLiteral frac,
            int fracBase,
            Func <BigInteger, char, BigInteger?> tryAccumulateFracDigit)
        {
            (BigInteger, int)pair;
            bool parsed = TryReadNum(out pair, (BigInteger.Zero, 0), (acc, c) =>
            {
                var(i, n)    = acc;
                var maybeAcc = tryAccumulateFracDigit(i, c);
                if (maybeAcc.HasValue)
                {
                    return(maybeAcc.Value, n + 1);
                }
                else
                {
                    return(null);
                }
            });

            if (parsed)
            {
                var(i, n) = pair;
                frac      = FloatLiteral.Number(i, fracBase, -n);
                return(true);
            }
            else
            {
                frac = FloatLiteral.Zero(fracBase);
                return(false);
            }
        }
示例#2
0
        private bool TryReadUnsignedNumber(
            bool negate,
            out object result,
            IntegerReader tryReadNum,
            FloatReader tryReadFrac,
            char exponentChar,
            int exponent)
        {
            BigInteger num;

            if (tryReadNum(out num))
            {
                if (Expect('.'))
                {
                    FloatLiteral frac;
                    if (!tryReadFrac(out frac))
                    {
                        frac = FloatLiteral.Zero(exponent);
                    }
                    var floatNum = num + frac;

                    if (Expect(exponentChar) || Expect(char.ToUpperInvariant(exponentChar)))
                    {
                        floatNum = floatNum.ChangeBase(exponent);
                        if (!TryAppendExponent(floatNum, out floatNum))
                        {
                            result = null;
                            return(false);
                        }
                    }

                    result = MaybeNegate(floatNum, negate);
                }
                else if (Expect(exponentChar) || Expect(char.ToUpperInvariant(exponentChar)))
                {
                    FloatLiteral floatNum;
                    if (!TryAppendExponent(FloatLiteral.Number(num, exponent), out floatNum))
                    {
                        result = null;
                        return(false);
                    }

                    result = MaybeNegate(floatNum, negate);
                }
                else
                {
                    result = MaybeNegate(num, negate);
                }
                return(true);
            }
            else
            {
                result = null;
                return(false);
            }
        }