예제 #1
0
        /// <summary>
        /// check that all operand types are expected: int or double only.
        /// TODO: string? for concatenation? later.
        ///
        /// </summary>
        /// <param name="execResult"></param>
        /// <param name="listExpr"></param>
        /// <returns></returns>
        private bool CheckTypeOperandExprCalculation(ExecResult execResult, List <ExpressionExecBase> listExprExecBase)
        {
            bool ret = true;

            foreach (ExpressionExecBase exprExecBase in listExprExecBase)
            {
                // is it an int?
                ExprExecValueInt exprInt = exprExecBase as ExprExecValueInt;
                if (exprInt != null)
                {
                    // yes!
                    continue;
                }

                // is it a double?
                ExprExecValueDouble exprDouble = exprExecBase as ExprExecValueDouble;
                if (exprDouble == null)
                {
                    // neither an int or a double , error!
                    execResult.AddErrorExec(ErrorCode.ExprCalculationOperandTypeUnExcepted, "Content", exprExecBase.Expr.Token.Value, "Position", exprExecBase.Expr.Token.Position.ToString());
                    ret = false;
                }
            }
            return(ret);
        }
예제 #2
0
 public bool GetCheckParamDouble(ExecResult exprExecResult, string functionCallName, ExpressionExecBase param, out ExprExecValueDouble exprParamDouble)
 {
     exprParamDouble = param as ExprExecValueDouble;
     if (exprParamDouble == null)
     {
         exprExecResult.AddErrorExec(ErrorCode.FunctionCallParamTypeWrong, "FunctionCallName", functionCallName, "ParamTypeExpected", "double"); //, "ParamType", param.GetType().ToString());
         return(false);
     }
     return(true);
 }
        /// <summary>
        /// Do the calculation: res := operandLeft operator operandRight.
        /// Both operand must be number, int or double.
        /// the operator can be: +, -, *, /.
        /// </summary>
        /// <param name="execResult"></param>
        /// <param name="exprExecBase"></param>
        /// <returns></returns>
        private bool DoCalculationTwoOperands(ExecResult execResult, ExprCalculation exprCalculation, ExpressionExecBase exprExecBaseLeft, ExpressionExecBase exprExecBaseRight, ExprOperatorCalculation operatorCalc, out ExpressionExecBase exprExecCalcResult)
        {
            exprExecCalcResult = null;

            CalcIntOrDouble calcIntOrDoubleLeft  = CalcIntOrDouble.NotDefined;
            CalcIntOrDouble calcIntOrDoubleRight = CalcIntOrDouble.NotDefined;

            int    valLeftInt     = 0;
            int    valRightInt    = 0;
            double valLeftDouble  = 0;
            double valRightDouble = 0;

            //---parse the left operand
            ExprExecValueInt exprExecValueLeftInt = exprExecBaseLeft as ExprExecValueInt;

            if (exprExecValueLeftInt != null)
            {
                valLeftInt          = exprExecValueLeftInt.Value;
                calcIntOrDoubleLeft = CalcIntOrDouble.IsInt;
            }
            else
            {
                ExprExecValueDouble exprExecValueLeftDouble = exprExecBaseLeft as ExprExecValueDouble;
                if (exprExecValueLeftDouble != null)
                {
                    valLeftDouble       = exprExecValueLeftDouble.Value;
                    calcIntOrDoubleLeft = CalcIntOrDouble.IsDouble;
                }
            }

            //---parse the right operand
            ExprExecValueInt exprExecValueRightInt = exprExecBaseRight as ExprExecValueInt;

            if (exprExecValueRightInt != null)
            {
                valRightInt          = exprExecValueRightInt.Value;
                calcIntOrDoubleRight = CalcIntOrDouble.IsInt;
            }
            else
            {
                ExprExecValueDouble exprExecValueRightDouble = exprExecBaseRight as ExprExecValueDouble;
                if (exprExecValueRightDouble != null)
                {
                    valRightDouble       = exprExecValueRightDouble.Value;
                    calcIntOrDoubleRight = CalcIntOrDouble.IsDouble;
                }
            }

            //----wrong type
            if (calcIntOrDoubleLeft == CalcIntOrDouble.NotDefined)
            {
                // other operators are not allowed on the boolean type (only bool)
                execResult.AddErrorExec(ErrorCode.ExprCalculationOperandTypeUnExcepted, "Position", exprExecBaseLeft.Expr.Token.Position.ToString(), "Operand", exprExecBaseLeft.Expr.Token.Value);

                return(false);
            }
            if (calcIntOrDoubleRight == CalcIntOrDouble.NotDefined)
            {
                // other operators are not allowed on the boolean type (only bool)
                execResult.AddErrorExec(ErrorCode.ExprCalculationOperandTypeUnExcepted, "Position", exprExecBaseRight.Expr.Token.Position.ToString(), "Operand", exprExecBaseRight.Expr.Token.Value);

                return(false);
            }

            // if one of both operand is an double, convert the other to a double
            if (calcIntOrDoubleLeft == CalcIntOrDouble.IsDouble || calcIntOrDoubleRight == CalcIntOrDouble.IsDouble)
            {
                if (calcIntOrDoubleLeft == CalcIntOrDouble.IsInt)
                {
                    valLeftDouble = (double)valLeftInt;
                }

                if (calcIntOrDoubleRight == CalcIntOrDouble.IsInt)
                {
                    valRightDouble = (double)valRightInt;
                }

                // used to define the calculation type
                calcIntOrDoubleLeft = CalcIntOrDouble.IsDouble;
            }

            //----calculate, depending on the operator: Plus
            if (operatorCalc.Operator == OperatorCalculationCode.Plus)
            {
                if (calcIntOrDoubleLeft == CalcIntOrDouble.IsDouble)
                {
                    ExprExecValueDouble resDouble = new ExprExecValueDouble();
                    resDouble.Value    = valLeftDouble + valRightDouble;
                    exprExecCalcResult = resDouble;
                    return(true);
                }

                ExprExecValueInt resInt = new ExprExecValueInt();
                resInt.Value       = valLeftInt + valRightInt;
                exprExecCalcResult = resInt;
                return(true);
            }

            //----calculate, depending on the operator: Minus
            if (operatorCalc.Operator == OperatorCalculationCode.Minus)
            {
                if (calcIntOrDoubleLeft == CalcIntOrDouble.IsDouble)
                {
                    ExprExecValueDouble resDouble = new ExprExecValueDouble();
                    resDouble.Value    = valLeftDouble - valRightDouble;
                    exprExecCalcResult = resDouble;
                    return(true);
                }

                ExprExecValueInt resInt = new ExprExecValueInt();
                resInt.Value       = valLeftInt - valRightInt;
                exprExecCalcResult = resInt;
                return(true);
            }

            //----calculate, depending on the operator: Multiplication
            if (operatorCalc.Operator == OperatorCalculationCode.Multiplication)
            {
                if (calcIntOrDoubleLeft == CalcIntOrDouble.IsDouble)
                {
                    ExprExecValueDouble resDouble = new ExprExecValueDouble();
                    resDouble.Value    = valLeftDouble * valRightDouble;
                    exprExecCalcResult = resDouble;
                    return(true);
                }

                ExprExecValueInt resInt = new ExprExecValueInt();
                resInt.Value       = valLeftInt * valRightInt;
                exprExecCalcResult = resInt;
                return(true);
            }

            //----calculate, depending on the operator: Division
            if (operatorCalc.Operator == OperatorCalculationCode.Division)
            {
                if (calcIntOrDoubleLeft == CalcIntOrDouble.IsDouble)
                {
                    ExprExecValueDouble resDouble = new ExprExecValueDouble();
                    resDouble.Value    = valLeftDouble / valRightDouble;
                    exprExecCalcResult = resDouble;
                    return(true);
                }

                // the result can be a double!
                double res  = (double)valLeftInt / (double)valRightInt;
                double res2 = res - Math.Truncate(res);

                if (res2 == 0)
                {
                    ExprExecValueInt resInt = new ExprExecValueInt();
                    resInt.Value       = valLeftInt / valRightInt;
                    exprExecCalcResult = resInt;
                    return(true);
                }
                else
                {
                    ExprExecValueDouble resDouble = new ExprExecValueDouble();
                    resDouble.Value    = res; // valLeftInt / valRightInt;
                    exprExecCalcResult = resDouble;
                    return(true);
                }
                return(true);
            }

            // operator not yet implemented
            execResult.AddErrorExec(ErrorCode.ExprCalculationOperatorNotYetImplemented, "Position", exprExecBaseLeft.Expr.Token.Position.ToString(), "Operand", exprExecBaseLeft.Expr.Token.Value);
            return(false);
        }
예제 #4
0
        /// <summary>
        /// execute the expression, execute the right part, execute the left part, then exec the expr (as 2 value operands)
        ///
        /// Returns always a bool value.
        /// exp: a=12,  a>=13
        ///
        /// managed type; int and double.
        ///  for the bool and the string type, only equals and diff are authorized.
        /// </summary>
        /// <param name="exprComparison"></param>
        /// <returns></returns>
        private bool ExecExpressionComparison(ExecResult exprExecResult, ExprComparison exprComparison, out ExpressionExecBase exprExecBase)
        {
            exprExecBase = null;

            //----first step is to decode/compile both operands
            ExpressionExecBase exprExecBaseLeft;

            ExecExpression(exprExecResult, exprComparison.ExprLeft, out exprExecBaseLeft);

            ExpressionExecBase exprExecBaseRight;

            ExecExpression(exprExecResult, exprComparison.ExprRight, out exprExecBaseRight);

            //---Are the operands boolean?
            ExprExecValueBool exprExecValueLeftBool = exprExecBaseLeft as ExprExecValueBool;

            if (exprExecValueLeftBool != null)
            {
                // decode the right operand, should be a bool value too
                ExprExecValueBool exprExecValueRightBool = exprExecBaseRight as ExprExecValueBool;
                if (exprExecValueRightBool == null)
                {
                    // the type of both operands mismatch!
                    exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperandsTypeMismatchBoolExpected, "Position", exprComparison.ExprRight.Token.Position.ToString());
                    return(false);
                }

                ExprExecValueBool exprValueBoolResult = new ExprExecValueBool();

                // is the operator Equals?
                if (exprComparison.Operator == OperatorComparisonCode.Equals)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftBool.Value == exprExecValueRightBool.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator different?
                if (exprComparison.Operator == OperatorComparisonCode.Different)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftBool.Value != exprExecValueRightBool.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // other operators are not allowed on the boolean type
                exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperatorNotAllowedForBoolType, "Position", exprComparison.ExprLeft.Token.Position.ToString(), "Operator", exprComparison.Operator.ToString());

                return(false);
            }

            //---Are the operands integer?
            ExprExecValueInt exprExecValueLeftInt = exprExecBaseLeft as ExprExecValueInt;

            if (exprExecValueLeftInt != null)
            {
                // decode the right operand, should be an int value too
                ExprExecValueInt exprExecValueRightInt = exprExecBaseRight as ExprExecValueInt;
                if (exprExecValueRightInt == null)
                {
                    // the type of both operands mismatch!
                    exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperandsTypeMismatchIntExpected, "Position", exprComparison.ExprRight.Token.Position.ToString());
                    return(false);
                }

                ExprExecValueBool exprValueBoolResult = new ExprExecValueBool();

                // is the operator Equals?
                if (exprComparison.Operator == OperatorComparisonCode.Equals)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftInt.Value == exprExecValueRightInt.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator different?
                if (exprComparison.Operator == OperatorComparisonCode.Different)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftInt.Value != exprExecValueRightInt.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator greater than?
                if (exprComparison.Operator == OperatorComparisonCode.Greater)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftInt.Value > exprExecValueRightInt.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator lesser than?
                if (exprComparison.Operator == OperatorComparisonCode.Less)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftInt.Value < exprExecValueRightInt.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator greater or equals than?
                if (exprComparison.Operator == OperatorComparisonCode.GreaterEquals)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftInt.Value >= exprExecValueRightInt.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator lesser or equals than?
                if (exprComparison.Operator == OperatorComparisonCode.LessEquals)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftInt.Value <= exprExecValueRightInt.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // other operators are not allowed on the boolean type (impossible to be there!)
                exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperatorNotYetImplementedForIntType, "Position", exprComparison.ExprLeft.Token.Position.ToString(), "Operator", exprComparison.Operator.ToString());

                return(false);
            }

            //---Are the operands double?
            ExprExecValueDouble exprExecValueLeftDouble = exprExecBaseLeft as ExprExecValueDouble;

            if (exprExecValueLeftDouble != null)
            {
                // decode the right operand, should be an int value too
                ExprExecValueDouble exprExecValueRightDouble = exprExecBaseRight as ExprExecValueDouble;
                if (exprExecValueRightDouble == null)
                {
                    // the type of both operands mismatch!
                    exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperandsTypeMismatchDoubleExpected, "Position", exprComparison.ExprRight.Token.Position.ToString());
                    return(false);
                }

                // the result is always a bool (its a comparison)
                ExprExecValueBool exprValueBoolResult = new ExprExecValueBool();

                // is the operator Equals?
                if (exprComparison.Operator == OperatorComparisonCode.Equals)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftDouble.Value == exprExecValueRightDouble.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator different?
                if (exprComparison.Operator == OperatorComparisonCode.Different)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftDouble.Value != exprExecValueRightDouble.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator greater than?
                if (exprComparison.Operator == OperatorComparisonCode.Greater)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftDouble.Value > exprExecValueRightDouble.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator lesser than?
                if (exprComparison.Operator == OperatorComparisonCode.Less)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftDouble.Value < exprExecValueRightDouble.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator greater or equals than?
                if (exprComparison.Operator == OperatorComparisonCode.GreaterEquals)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftDouble.Value >= exprExecValueRightDouble.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator lesser or equals than?
                if (exprComparison.Operator == OperatorComparisonCode.LessEquals)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftDouble.Value <= exprExecValueRightDouble.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // other operators are not allowed on the boolean type (impossible to be there!)
                exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperatorNotYetImplementedForDoubleType, "Position", exprComparison.ExprLeft.Token.Position.ToString(), "Operator", exprComparison.Operator.ToString());

                return(false);
            }

            //---Are the operands string?
            ExprExecValueString exprExecValueLeftString = exprExecBaseLeft as ExprExecValueString;

            if (exprExecValueLeftString != null)
            {
                // decode the right operand, should be a bool value too
                ExprExecValueString exprExecValueRightString = exprExecBaseRight as ExprExecValueString;
                if (exprExecValueRightString == null)
                {
                    // the type of both operands mismatch!
                    exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperandsTypeMismatchStringExpected, "Position", exprComparison.ExprRight.Token.Position.ToString());
                    return(false);
                }

                ExprExecValueBool exprValueBoolResult = new ExprExecValueBool();

                // is the operator Equals?
                if (exprComparison.Operator == OperatorComparisonCode.Equals)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftString.Value == exprExecValueRightString.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // is the operator different?
                if (exprComparison.Operator == OperatorComparisonCode.Different)
                {
                    // ok, execute the comparison
                    exprValueBoolResult.Value = exprExecValueLeftString.Value != exprExecValueRightString.Value;
                    exprExecBase = exprValueBoolResult;
                    return(true);
                }

                // other operators are not allowed on the boolean type
                exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperatorNotAllowedForStringType, "Position", exprComparison.ExprLeft.Token.Position.ToString(), "Operator", exprComparison.Operator.ToString());

                return(false);
            }

            // if reached, the operand type is not yet implemented (or unknowed)
            exprExecResult.AddErrorExec(ErrorCode.ExprComparisonOperandTypeNotYetImplemented, "Position", exprComparison.ExprLeft.Token.Position.ToString(), "Token", exprExecBaseLeft.Expr.Token.ToString());
            return(false);
        }
예제 #5
0
        private bool ExecExpressionFinalOperand(ExecResult exprExecResult, ExprFinalOperand finalOperand, out ExpressionExecBase exprExecBase)
        {
            // is the operand a variable or a call function?
            if (finalOperand.ContentType == OperandType.ObjectName)
            {
                // is it a variable?
                ExprVariableBase exprVariableBase = _expressionData.ExprExecResult.ListExprVariable.Find(v => v.Name.Equals(finalOperand.Operand, StringComparison.InvariantCultureIgnoreCase));
                if (exprVariableBase != null)
                {
                    // the operand is a string variable?
                    ExprVariableString exprVariableString = exprVariableBase as ExprVariableString;
                    if (exprVariableString != null)
                    {
                        // create a var instance, depending on the type
                        ExprExecVarString exprExecVar = new ExprExecVarString();
                        exprExecVar.ExprVariableString = exprVariableString;
                        exprExecVar.Expr = finalOperand;

                        // set the value
                        exprExecVar.Value = exprVariableString.Value;
                        exprExecBase      = exprExecVar;
                        return(true);
                    }

                    // the operand is an int variable?
                    ExprVariableInt exprVariableInt = exprVariableBase as ExprVariableInt;
                    if (exprVariableInt != null)
                    {
                        // create a var instance, depending on the type
                        ExprExecVarInt exprExecVar = new ExprExecVarInt();
                        exprExecVar.ExprVariableInt = exprVariableInt;
                        exprExecVar.Expr            = finalOperand;

                        // set the value
                        exprExecVar.Value = exprVariableInt.Value;
                        exprExecBase      = exprExecVar;
                        return(true);
                    }


                    // the operand is a double variable?
                    ExprVariableDouble exprVariableDouble = exprVariableBase as ExprVariableDouble;
                    if (exprVariableDouble != null)
                    {
                        // create a var instance, depending on the type
                        ExprExecVarDouble exprExecVar = new ExprExecVarDouble();
                        exprExecVar.ExprVariableDouble = exprVariableDouble;
                        exprExecVar.Expr = finalOperand;

                        // set the value
                        exprExecVar.Value = exprVariableDouble.Value;
                        exprExecBase      = exprExecVar;
                        return(true);
                    }

                    // the operand is a bool variable?
                    ExprVariableBool exprVariableBool = exprVariableBase as ExprVariableBool;
                    if (exprVariableBool != null)
                    {
                        // create a var instance, depending on the type
                        ExprExecVarBool exprExecVar = new ExprExecVarBool();
                        exprExecVar.ExprVariableBool = exprVariableBool;
                        exprExecVar.Expr             = finalOperand;

                        // set the value
                        exprExecVar.Value = exprVariableBool.Value;
                        exprExecBase      = exprExecVar;
                        return(true);
                    }

                    // todo; create an error
                    throw new Exception("The operand variable type is not yet implemented!");
                }

                // so its a function call
                // todo: rechercher dans la liste des fonctions?
                throw new Exception("The function call is not yet implemented!");
            }

            // is the operand a string value (a const string value)?
            if (finalOperand.ContentType == OperandType.ValueString)
            {
                ExprExecValueString exprValueString = new ExprExecValueString();
                exprValueString.Value = finalOperand.Operand;
                exprExecBase          = exprValueString;
                exprValueString.Expr  = finalOperand;

                return(true);
            }

            // is the operand an int value?
            if (finalOperand.ContentType == OperandType.ValueInt)
            {
                ExprExecValueInt exprValueInt = new ExprExecValueInt();
                exprValueInt.Expr  = finalOperand;
                exprValueInt.Value = finalOperand.ValueInt;
                exprExecBase       = exprValueInt;
                return(true);
            }

            // is the operand a double value?
            if (finalOperand.ContentType == OperandType.ValueDouble)
            {
                ExprExecValueDouble exprValueDouble = new ExprExecValueDouble();
                exprValueDouble.Expr  = finalOperand;
                exprValueDouble.Value = finalOperand.ValueDouble;
                exprExecBase          = exprValueDouble;
                return(true);
            }

            // is the operand a bool value?
            if (finalOperand.ContentType == OperandType.ValueBool)
            {
                ExprExecValueBool exprValueBool = new ExprExecValueBool();
                exprValueBool.Expr  = finalOperand;
                exprValueBool.Value = finalOperand.ValueBool;
                exprExecBase        = exprValueBool;
                return(true);
            }

            // todo: create an error
            throw new Exception("The operand const value is not yet implemented!");
        }