示例#1
0
        private bool Atom(out object success, out ParseException failure)
        {
            if (this.CurrentToken.Type == TokenType.Number)
            {
                NumberToken tok = this.CurrentToken as NumberToken;

                if (!this.Advance(TokenType.Number, out failure))
                {
                    success = null;
                    return(false);
                }
                else
                {
                    success = new DoubleMeasurement(tok.Value);
                    failure = null;
                    return(true);
                }
            }
            else if (this.CurrentToken.Type == TokenType.Id)
            {
                IdToken tok = this.CurrentToken as IdToken;

                if (!this.Advance(TokenType.Id, out failure))
                {
                    success = null;
                    return(false);
                }
                else
                {
                    if (this.Units.ContainsKey(tok.StringValue))
                    {
                        success = this.Units[tok.StringValue];
                        return(true);
                    }
                    else
                    {
                        success = null;
                        failure = ParseException.UndefinedUnitDiscovered(tok.StringValue);
                        return(false);
                    }
                }
            }
            else if (this.CurrentToken.Type == TokenType.OpenParen)
            {
                if (!this.Advance(TokenType.OpenParen, out failure))
                {
                    success = null;
                    return(false);
                }
                else if (!this.AddExpression(out success, out failure))
                {
                    return(false);
                }
                else if (!this.Advance(TokenType.CloseParen, out failure))
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            else if (this.CurrentToken.Type == TokenType.OpenBracket)
            {
                if (!this.Advance(TokenType.OpenBracket, out failure))
                {
                    success = null;
                    return(false);
                }
                else if (!this.AddExpression(out success, out failure))
                {
                    return(false);
                }
                else if (!this.Advance(TokenType.CloseBracket, out failure))
                {
                    return(false);
                }
                else
                {
                    return(true);
                }
            }
            else if (this.CurrentToken.Type == TokenType.Plus || this.CurrentToken.Type == TokenType.Minus)
            {
                if (!this.Advance(this.CurrentToken.Type, out failure))
                {
                    success = null;
                    return(false);
                }
                else if (!this.AddExpression(out success, out failure))
                {
                    return(false);
                }
                else
                {
                    return(Evaluation.ApplyUrnaryOperator(
                               this.AllOperators,
                               this.CurrentToken.Type == TokenType.Plus ? UrnaryOperatorType.Positation : UrnaryOperatorType.Negation,
                               success,
                               out success,
                               out failure
                               ));
                }
            }
            else
            {
                success = null;
                failure = ParseException.UnexpectedCharactersError(this.CurrentToken.StringValue);
                return(false);
            }
        }
        public static bool ApplyBinaryOperator(IEnumerable <Operator> ops, BinaryOperatorType opType, object first, object second, out object success, out ParseException failure)
        {
            Validate.NonNull(ops, nameof(ops));
            Validate.NonNull(opType, nameof(opType));
            Validate.NonNull(first, nameof(first));
            Validate.NonNull(second, nameof(second));

            bool worked = ApplyBinaryOperators(
                FindBinaryOperators(
                    ops,
                    opType,
                    first.GetType(),
                    second.GetType()
                    ),
                first,
                second,
                out success
                );

            if (!worked)
            {
                failure = ParseException.BinaryOperatorEvaluationFailed(opType.GetSymbolString(), first.ToString(), second.ToString());
                return(false);
            }
            else
            {
                failure = null;
                return(true);
            }
        }