/// <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); }
public bool GetCheckParamString(ExecResult exprExecResult, string functionCallName, ExpressionExecBase param, out ExprExecValueString exprParamString) { exprParamString = param as ExprExecValueString; if (exprParamString == null) { exprExecResult.AddErrorExec(ErrorCode.FunctionCallParamTypeWrong, "FunctionCallName", functionCallName, "ParamTypeExpected", "string"); //, "ParamType", param.GetType().ToString()); return(false); } return(true); }
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!"); }