/// <summary> /// Not Operand. /// The operand must be a bool: final operand, expression that return a bool. /// </summary> /// <param name="exprExecResult"></param> /// <param name="exprLogicalNot"></param> /// <param name="exprExecBase"></param> /// <returns></returns> private bool ExecExpressionLogicalNot(ExecResult exprExecResult, ExprLogicalNot exprLogicalNot, out ExpressionExecBase exprExecBase) { exprExecBase = null; //----first step is to decode/compile the operand ExpressionExecBase exprExecOperand; ExecExpression(exprExecResult, exprLogicalNot.ExprBase, out exprExecOperand); // the operand must be a bool value ExprExecValueBool exprExecValueBool = exprExecOperand as ExprExecValueBool; if (exprExecValueBool == null) { // bool type expected for the operand inner the not logical expression, but the operand has another type string strPosition = "0"; string strToken = ""; if (exprLogicalNot.ExprBase.Token != null) { strPosition = exprLogicalNot.ExprBase.Token.Position.ToString(); strToken = exprLogicalNot.ExprBase.Token.Value.ToString(); } _expressionData.ExprExecResult.AddErrorExec(ErrorCode.ExprLogicalNotOperator_InnerOperandBoolTypeExpected, "Token", strToken, "Position", strPosition); return(false); } // ok, the operand is a bool value // ok, execute the NOT logical ExprExecValueBool exprValueBoolResult = new ExprExecValueBool(); exprValueBoolResult.Value = !(exprExecValueBool.Value); exprExecBase = exprValueBoolResult; return(true); }
// get the single param and check the type public bool GetCheckParamBool(ExecResult exprExecResult, string functionCallName, ExpressionExecBase param, out ExprExecValueBool exprParamBool) { exprParamBool = param as ExprExecValueBool; if (exprParamBool == null) { exprExecResult.AddErrorExec(ErrorCode.FunctionCallParamTypeWrong, "FunctionCallName", functionCallName, "ParamTypeExpected", "bool"); //, "ParamType", param.GetType().ToString()); return(false); } return(true); }
/// <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); }
/// <summary> /// Execute the logical expression. /// exp: (a AND b) /// /// Only bool type are authorized. /// </summary> /// <param name="exprExecResult"></param> /// <param name="exprComparison"></param> /// <param name="exprExecBase"></param> /// <returns></returns> private bool ExecExpressionLogical(ExecResult exprExecResult, ExprLogical exprLogical, out ExpressionExecBase exprExecBase) { exprExecBase = null; //----first step is to execute both operands ExpressionExecBase exprExecBaseLeft; ExecExpression(exprExecResult, exprLogical.ExprLeft, out exprExecBaseLeft); ExpressionExecBase exprExecBaseRight; ExecExpression(exprExecResult, exprLogical.ExprRight, out exprExecBaseRight); //---Are the operands boolean type? 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.ExprLogicalOperandsTypeMismatchBoolExpected, "Position", exprLogical.ExprRight.Token.Position.ToString()); return(false); } ExprExecValueBool exprValueBoolResult = new ExprExecValueBool(); // is the operator AND? if (exprLogical.Operator == OperatorLogicalCode.And) { // ok, execute the comparison exprValueBoolResult.Value = exprExecValueLeftBool.Value && exprExecValueRightBool.Value; exprExecBase = exprValueBoolResult; return(true); } // is the operator OR? if (exprLogical.Operator == OperatorLogicalCode.Or) { // ok, execute the comparison exprValueBoolResult.Value = (exprExecValueLeftBool.Value || exprExecValueRightBool.Value); exprExecBase = exprValueBoolResult; return(true); } // is the operator XOR? if (exprLogical.Operator == OperatorLogicalCode.Xor) { // ok, execute the comparison, the operator doesn't exists so do firts a OR exprValueBoolResult.Value = (exprExecValueLeftBool.Value || exprExecValueRightBool.Value); // the xor case: both operands are true so the result is false if (exprExecValueLeftBool.Value && exprExecValueRightBool.Value) { exprValueBoolResult.Value = false; } exprExecBase = exprValueBoolResult; return(true); } // other operators are not allowed on the boolean type (only bool) exprExecResult.AddErrorExec(ErrorCode.ExprLogicalOperatorNotAllowed, "Position", exprLogical.ExprLeft.Token.Position.ToString(), "Operator", exprLogical.Operator.ToString()); return(false); } //---Are the operands others type? -> error exprExecResult.AddErrorExec(ErrorCode.ExprLogicalOperandTypeNotAllowed, "Position", exprLogical.ExprLeft.Token.Position.ToString(), "Token", exprExecBaseLeft.GetType().ToString()); return(false); }
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!"); }