Ejemplo n.º 1
0
        private void EvaluateBinaryElements(Operator operatorType, Func <long, long, long> expression)
        {
            for (int i = 0; i < elementCount; i++)
            {
                var current = elements[i];

                if (current.Operator == operatorType)
                {
                    int leftOperandIndex, rightOperandIndex;
                    var leftOperand  = this.GetPreviousDecimalValue(i, out leftOperandIndex);
                    var rightOperand = this.GetNextDecimalValue(i, out rightOperandIndex);

                    if (leftOperand == null || rightOperand == null)
                    {
                        throw new Exception("Expected operand.");
                    }

                    long left   = leftOperand.GetValue();
                    long right  = rightOperand.GetValue();
                    long result = expression(left, right);

                    elements[i] = new IntegralExpressionElement(result.ToString(), ExpressionElement.DecimalNumber, Operator.NotAnOperator);
                    elements[leftOperandIndex] = elements[rightOperandIndex] = IntegralExpressionElement.Empty;
                }
            }
        }
Ejemplo n.º 2
0
        private void EvaluateTernaryElements(Operator operatorType, Func <long, long, long, long> expression)
        {
            for (int i = 0; i < elementCount; i++)
            {
                var current = elements[i];

                if (current.Operator == operatorType)
                {
                    int operand1Index, operand2Index, operand3Index;
                    var operand1            = GetPreviousDecimalValue(i, out operand1Index);
                    var operand2            = GetNextDecimalValue(i, out operand2Index);
                    var operand3            = GetNextDecimalValue(operand2Index, out operand3Index);
                    int secondOperatorIndex = -1;

                    for (int j = operand2Index; j < operand3Index; j++)
                    {
                        if (elements[j].ElementType == ExpressionElement.TernaryOperator)
                        {
                            secondOperatorIndex = j;
                            break;
                        }
                        else if (j == operand3Index - 1)
                        {
                            throw new Exception("Expected matching operator.");
                        }
                    }

                    elements[secondOperatorIndex] = IntegralExpressionElement.Empty;

                    long value1 = operand1.GetValue();
                    long value2 = operand2.GetValue();
                    long value3 = operand3.GetValue();
                    long result = expression(value1, value2, value3);

                    elements[i]             = new IntegralExpressionElement(result.ToString(), ExpressionElement.DecimalNumber, Operator.NotAnOperator);
                    elements[operand1Index] = elements[operand2Index] = elements[operand3Index] = IntegralExpressionElement.Empty;
                }
            }
        }
Ejemplo n.º 3
0
        private void EvaluateUnaryPostfixElements(Operator operatorType, Func <long, long> expression)
        {
            for (int i = 0; i < elementCount; i++)
            {
                var current = elements[i];

                if (current.Operator == operatorType)
                {
                    int operandIndex;
                    var operand = this.GetPreviousDecimalValue(i, out operandIndex);

                    if (operand == null)
                    {
                        throw new Exception("Expected operand.");
                    }

                    long value = operand.GetValue();
                    value = expression(value);

                    elements[operandIndex] = new IntegralExpressionElement(value.ToString(), ExpressionElement.DecimalNumber, Operator.NotAnOperator);
                    elements[i]            = IntegralExpressionElement.Empty;
                }
            }
        }
Ejemplo n.º 4
0
        private void EvaluateUnaryPrefixElements(Operator operatorType, Func<long, long> expression)
        {
            for (int i = 0; i < elementCount; i++)
            {
                var current = elements[i];

                if (current.Operator == operatorType)
                {
                    int operandIndex;
                    var operand = this.GetNextDecimalValue(i, out operandIndex);

                    if (operand == null)
                    {
                        throw new Exception("Expected operand.");
                    }

                    long value = operand.GetValue();
                    value = expression(value);

                    elements[operandIndex] = new IntegralExpressionElement(value.ToString(), ExpressionElement.DecimalNumber, Operator.NotAnOperator);
                    elements[i] = IntegralExpressionElement.Empty;
                }
            }
        }
Ejemplo n.º 5
0
        private void EvaluateTernaryElements(Operator operatorType, Func<long, long, long, long> expression)
        {
            for (int i = 0; i < elementCount; i++)
            {
                var current = elements[i];

                if (current.Operator == operatorType)
                {
                    int operand1Index, operand2Index, operand3Index;
                    var operand1 = GetPreviousDecimalValue(i, out operand1Index);
                    var operand2 = GetNextDecimalValue(i, out operand2Index);
                    var operand3 = GetNextDecimalValue(operand2Index, out operand3Index);
                    int secondOperatorIndex = -1;

                    for (int j = operand2Index; j < operand3Index; j++)
                    {
                        if (elements[j].ElementType == ExpressionElement.TernaryOperator)
                        {
                            secondOperatorIndex = j;
                            break;
                        }
                        else if (j == operand3Index - 1)
                        {
                            throw new Exception("Expected matching operator.");
                        }
                    }

                    elements[secondOperatorIndex] = IntegralExpressionElement.Empty;

                    long value1 = operand1.GetValue();
                    long value2 = operand2.GetValue();
                    long value3 = operand3.GetValue();
                    long result = expression(value1, value2, value3);

                    elements[i] = new IntegralExpressionElement(result.ToString(), ExpressionElement.DecimalNumber, Operator.NotAnOperator);
                    elements[operand1Index] = elements[operand2Index] = elements[operand3Index] = IntegralExpressionElement.Empty;
                }
            }
        }
Ejemplo n.º 6
0
        private void EvaluateBinaryElements(Operator operatorType, Func<long, long, long> expression)
        {
            for (int i = 0; i < elementCount; i++)
            {
                var current = elements[i];

                if (current.Operator == operatorType)
                {
                    int leftOperandIndex, rightOperandIndex;
                    var leftOperand = this.GetPreviousDecimalValue(i, out leftOperandIndex);
                    var rightOperand = this.GetNextDecimalValue(i, out rightOperandIndex);

                    if (leftOperand == null || rightOperand == null)
                    {
                        throw new Exception("Expected operand.");
                    }

                    long left = leftOperand.GetValue();
                    long right = rightOperand.GetValue();
                    long result = expression(left, right);

                    elements[i] = new IntegralExpressionElement(result.ToString(), ExpressionElement.DecimalNumber, Operator.NotAnOperator);
                    elements[leftOperandIndex] = elements[rightOperandIndex] = IntegralExpressionElement.Empty;
                }
            }
        }