Exemple #1
0
        private static IArithExpression ParseExpression(Token start, Func <Token, bool> endOfExpression, TokenCursor tokens, bool moveToPrevAtEnd)
        {
            var current = start;
            IArithExpression resultExpr = null;

            //while next && end(next).not()
            while (current.IsNotNull())
            {
                resultExpr = ParseToken(current, resultExpr, tokens);

                current = tokens.Next();
                if (current.IsNotNull() && endOfExpression(current))
                {
                    if (moveToPrevAtEnd)
                    {
                        tokens.MovePrev();
                    }
                    break;
                }
            }
            return(resultExpr);

            //operators precedence:
            //- test for multiplicative operator - build one first
            //- test for expression group - (....)
            //- test for function call - ID(args)
            //prevExpression is not applicable in case of ROperand parsing
        }
 private MultiplicativeOperatorExpression(IArithExpression lOperand, IArithExpression rOperand, bool isDivision) : base(lOperand, rOperand)
 {
     if (LOperand == null)
     {
         throw new ParsingException("LOperand is required");
     }
     IsDivision = isDivision;
 }
 protected OperatorExpression(IArithExpression lOperand, IArithExpression rOperand)
 {
     if (rOperand == null)
     {
         throw new ParsingException("ROperand is required");
     }
     LOperand = lOperand;
     ROperand = rOperand;
 }
        public static OperatorExpression Create(string op, IArithExpression lOperand, IArithExpression rOperand)
        {
            switch (op)
            {
            case "+":
                return(AdditiveOperatorExpression.Addition(lOperand, rOperand));

            case "-":
                return(AdditiveOperatorExpression.Subtraction(lOperand, rOperand));

            case "*":
                return(MultiplicativeOperatorExpression.Multiplication(lOperand, rOperand));

            case "/":
                return(MultiplicativeOperatorExpression.Division(lOperand, rOperand));

            default:
                throw new ParsingException("Unexpected operator " + op);
            }
        }
Exemple #5
0
        private static IArithExpression ParseToken(Token token, IArithExpression prevExpr, TokenCursor tokens)
        {
            switch (token.Type)
            {
            case TokenType.Value:
                return(ParseValue(token));

            case TokenType.AdditiveOperator:
            case TokenType.MultiplicativeOperator:
                return(ParseOperator(token, prevExpr, tokens));

            case TokenType.OParenthesis:
                return(ParseGroup(tokens.Next(), tokens));

            case TokenType.CParenthesis:
                return(prevExpr);

            default:
                throw new ParsingException("Unexpected token: " + token);
            }
        }
 public static MultiplicativeOperatorExpression Division(IArithExpression lOperand, IArithExpression rOperand)
 {
     return(new MultiplicativeOperatorExpression(lOperand, rOperand, true));
 }
Exemple #7
0
        private static IArithExpression ParseOperator(Token operatorToken, IArithExpression lOperand, TokenCursor tokens)
        {
            var rOperand = ParseRightOperand(tokens.Next(), operatorToken, tokens);

            return(OperatorExpression.Create(operatorToken.Value, lOperand, rOperand));
        }
Exemple #8
0
 public static AdditiveOperatorExpression Subtraction(IArithExpression lOperand, IArithExpression rOperand)
 {
     return(new AdditiveOperatorExpression(lOperand, rOperand, true));
 }
Exemple #9
0
 public static AdditiveOperatorExpression Addition(IArithExpression lOperand, IArithExpression rOperand)
 {
     return(new AdditiveOperatorExpression(lOperand, rOperand, false));
 }
Exemple #10
0
 private AdditiveOperatorExpression(IArithExpression lOperand, IArithExpression rOperand, bool isSubtraction) : base(lOperand, rOperand)
 {
     IsSubtraction = isSubtraction;
 }