示例#1
0
        /// <summary>
        /// Add a number operand: int or double.
        /// </summary>
        /// <param name="exprExecBase"></param>
        /// <returns></returns>
        public bool AddOperandNumber(ExpressionExecBase exprExecBase)
        {
            ExprExecCalcValue calcValue = new ExprExecCalcValue();

            // save the value
            calcValue.ExprExecValue = exprExecBase;
            ListExprExecCalcValue.Add(calcValue);
            return(true);
        }
        /// <summary>
        /// Do the calculation of the multiplication/division expr comme l'add mais le rs va remplacer l'objet Mul dans le main add)
        ///
        /// </summary>
        /// <param name="execResult"></param>
        /// <param name="exprCalculation"></param>
        /// <param name="mainCalcAdd"></param>
        /// <param name="exprExecCalcResult"></param>
        /// <returns></returns>
        private bool DoCalculationExprMul(ExecResult execResult, ExprCalculation exprCalculation, ExprExecCalcMul execCalcMul, out ExprExecCalcValue execCalcValueResult)
        {
            // the temporary result, to start the addition calculation
            ExprExecValueInt resInt = new ExprExecValueInt();

            // the integer neutral value, the first calculation is 1*operand[0]
            resInt.Value = 1;
            ExpressionExecBase exprExecCalcResult = resInt;

            ExpressionExecBase exprExecTmpResult = null;
            int pos = 0;

            // scan addition expressions, two by two operands
            foreach (ExprExecCalcValue execCalcValue in execCalcMul.ListExprExecCalcValue)
            {
                // the item should be a number: an int or a double

                // get the operator
                ExprOperatorCalculation operatorCalc;

                // the first calc is special: res := 0 + firstOperand
                if (pos == 0)
                {
                    // put the default neutral operator: +
                    operatorCalc          = new ExprOperatorCalculation();
                    operatorCalc.Operator = OperatorCalculationCode.Multiplication;
                    //operatorCalc.Token = mainCalcAdd.ListOperator[0].Token;
                }
                else
                {
                    // there one operator less than operand
                    operatorCalc = execCalcMul.ListOperator[pos - 1];
                }

                // Do the calculation:   tmpRes := res +/- currentValue
                DoCalculationTwoOperands(execResult, exprCalculation, exprExecCalcResult, execCalcValue.ExprExecValue, operatorCalc, out exprExecTmpResult);

                // the current temporary result
                exprExecCalcResult = exprExecTmpResult;

                pos++;
            }

            // then replace the calc mul expr by the result
            execCalcValueResult = new ExprExecCalcValue();
            execCalcValueResult.ExprExecValue = exprExecTmpResult;
            //mainCalcAdd.ListExprExecCalc[posMulInMainAdd] = execCalcValueResult;

            return(true);
        }
        /// <summary>
        /// Execute/evaluate the addition main expression.
        /// </summary>
        /// <param name="execResult"></param>
        /// <param name="exprCalculation"></param>
        /// <param name="listCalcAdd"></param>
        /// <returns></returns>
        private bool DoCalculationMainExprAdditions(ExecResult execResult, ExprCalculation exprCalculation, ExprExecCalcAdd mainCalcAdd, out ExpressionExecBase exprExecCalcResult)
        {
            // the temporary result, to start the addition calculation
            ExprExecValueInt resInt = new ExprExecValueInt();

            // the integer neutral value, the first calculation is 0+operand[0]
            resInt.Value       = 0;
            exprExecCalcResult = resInt;

            ExpressionExecBase exprExecTmpResult;
            int pos = 0;

            // scan addition expressions, two by two operands
            foreach (ExprExecCalcBase execCalcBase in mainCalcAdd.ListExprExecCalc)
            {
                // the item should be a number: an int or a double
                ExprExecCalcValue execCalcValue = execCalcBase as ExprExecCalcValue;

                // get the operator
                ExprOperatorCalculation operatorCalc;

                // the first calc is special: res := 0 + firstOperand
                if (pos == 0)
                {
                    // put the default neutral operator: +
                    operatorCalc          = new ExprOperatorCalculation();
                    operatorCalc.Operator = OperatorCalculationCode.Plus;
                    if (mainCalcAdd.ListOperator.Count > 0)
                    {
                        operatorCalc.Token = mainCalcAdd.ListOperator[0].Token;
                    }
                }
                else
                {
                    // there one operator less than operand
                    operatorCalc = mainCalcAdd.ListOperator[pos - 1];
                }

                // Do the calculation:   tmpRes := res +/- currentValue
                DoCalculationTwoOperands(execResult, exprCalculation, exprExecCalcResult, execCalcValue.ExprExecValue, operatorCalc, out exprExecTmpResult);

                // the current temporary result
                exprExecCalcResult = exprExecTmpResult;

                pos++;
            }

            return(true);
        }