コード例 #1
0
        int Term()
        {
            var result = this.Factor();

            while (MathsOperators.IsMultDiv(_currentToken.TokenType))
            {
                Token operation = _currentToken;
                this.Eat(operation.TokenType);
                result = MathsOperators.Operate(operation.TokenType, result, this.Factor());
            }
            return(result);
        }
コード例 #2
0
        int Expr()
        {
            // this enforces the structure of mathiness on what we are doing. It expects the following format
            // Term We check for operators with higher precedence (*/ /) and act on them first
            // return the result
            int result = this.Term();

            // OP -- at this point we have moved forward (via Term method) and so should be at an the operator.
            // however if the expression is unary aka '3' there is no next token (EOF), so skip to the end and return the result.s
            while (MathsOperators.IsPlusMinus(_currentToken.TokenType))
            {
                Token operation = _currentToken;
                // moves it forward to the right
                // reassigns current token too.
                this.Eat(operation.TokenType);
                // DO THE MATHS!
                // NOTE we have a Term operation here, just in case there are more terms than expected
                result = MathsOperators.Operate(operation.TokenType, result, this.Term());
            }

            return(result);
        }