Exemplo n.º 1
0
        private Double Consume_Number()
        {
            Double value = CurrentToken.Value;

            _tokens.MoveNext();
            CurrentToken = _tokens.Current;
            return(value);
        }
Exemplo n.º 2
0
        private Double Consume_Name()
        {
            string name = CurrentToken.Name;

            _tokens.MoveNext();
            CurrentToken = _tokens.Current;
            Double val = _nameValueTable[name];

            return(val);
        }
Exemplo n.º 3
0
        // Comp ::= Exp
        // Comp ::= Exp == Exp

        // Exp ::= Term
        // Exp ::= Term (+/-) Exp

        // Term ::= Fac
        // Term ::= Fac (*|/) Term
        // Fac ::= Number

        // Fac ::= ( Comp )

        public Double Parse(string expression)
        {
            _scanner = new MathExpressionScanner(expression);
            _tokens  = _scanner.Scan().GetEnumerator();
            _tokens.MoveNext();
            CurrentToken = _tokens.Current;
            Double value = P_Comparison();

            return(value);
        }
Exemplo n.º 4
0
        public IEnumerable <MathExpressionToken> Scan()
        {
            MathExpressionToken returnToken = null;

            while (_currentOffset < _expression.Length && returnToken == null)
            {
                char cur = _expression[_currentOffset++];
                if (cur != ' ')
                {
                    if (Char.IsDigit(cur))
                    {
                        returnToken = GetNumber();
                    }
                    else if (Char.IsLetter(cur) || cur == '_')
                    {
                        returnToken = GetName();
                    }
                    else if (cur == '+' || cur == '-')
                    {
                        returnToken = new MathExpressionToken(MathTokenType.AddOp, cur);
                    }
                    else if (cur == '*' || cur == '/')
                    {
                        returnToken = new MathExpressionToken(MathTokenType.MultiOp, cur);
                    }
                    else if (cur == '(')
                    {
                        returnToken = new MathExpressionToken(MathTokenType.Open);
                    }
                    else if (cur == ')')
                    {
                        returnToken = new MathExpressionToken(MathTokenType.Close);
                    }
                    else if (cur == '=' || cur == '!')
                    {
                        returnToken = GetCompOp(cur);
                    }
                    else
                    {
                        throw new InvalidOperationException(String.Format("Invalid Character '{0}'", cur));
                    }
                }
                if (returnToken != null)
                {
                    yield return(returnToken);
                }
                returnToken = null;
            }
        }
Exemplo n.º 5
0
 private void Consume_CloseParen()
 {
     _tokens.MoveNext();
     CurrentToken = _tokens.Current;
 }
Exemplo n.º 6
0
 private void Consume_MultiOp()
 {
     _tokens.MoveNext();
     CurrentToken = _tokens.Current;
 }
Exemplo n.º 7
0
 private void Consume_EqualsOp()
 {
     _tokens.MoveNext();
     CurrentToken = _tokens.Current;
 }