Beispiel #1
0
 public override Token Minify(Evaluator evaluator)
 {
     LeftSideOperand  = LeftSideOperand.Minify(evaluator);
     RightSideOperand = RightSideOperand.Minify(evaluator);
     if (LeftSideOperand is ConstantToken && RightSideOperand is ConstantToken)
     {
         return(new ConstantToken(Evaluate(evaluator)));
     }
     return(this);
 }
Beispiel #2
0
        internal override double Evaluate(Evaluator evaluator)
        {
            if (Operator == Operators.Set)
            {
                VariableToken variable = LeftSideOperand as VariableToken;
                if (variable == null)
                {
                    throw new EvaluationException(
                              string.Format("Could assign a value to a token of type `{0}`.",
                                            LeftSideOperand.GetType().Name),
                              this);
                }
                double result = RightSideOperand.Evaluate(evaluator);
                evaluator.Variables[variable.VariableName] = result;
                return(result);
            }

            double a, b;

            a = LeftSideOperand.Evaluate(evaluator);
            b = RightSideOperand.Evaluate(evaluator);
            switch (Operator)
            {
            case Operators.Addition:
                return(a + b);

            case Operators.Substraction:
                return(a - b);

            case Operators.Multiplication:
                return(a * b);

            case Operators.Division:
                return(a / b);

            case Operators.Carry:
                return(a % b);

            case Operators.Exponentiation:
                return(Math.Pow(a, b));

            default:
                throw new Exception();
            }
        }
Beispiel #3
0
        public override string ToString()
        {
            string leftOpr  = LeftSideOperand.ToString();
            string rightOpr = RightSideOperand.ToString();

            if (LeftSideOperand is OperatorToken)
            {
                if (Utils.Presedence(Operator, (LeftSideOperand as OperatorToken).Operator) > 0)
                {
                    leftOpr = "(" + leftOpr + ")";
                }
            }
            if (RightSideOperand is OperatorToken)
            {
                if (Utils.Presedence(Operator, (RightSideOperand as OperatorToken).Operator) > 0)
                {
                    rightOpr = "(" + rightOpr + ")";
                }
            }
            return(string.Format("{0} {1} {2}", leftOpr, (char)Operator, rightOpr));
        }