/// <summary> /// Get variables used in the expression. /// /// The execution will displays: /// Var #1, Name=a /// Var #2, Name=b /// </summary> public static void GetVariableOfExpression() { string expr = "a=b"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result // scan all variables found in the expression (found the variable named 'a') int i = 0; foreach (ExprVarUsed exprVar in parseResult.ListExprVarUsed) { i++; Console.WriteLine("Var #" + i + ", Name=" + exprVar.Name); } Console.WriteLine("Define variables: a:=12, b:=12"); evaluator.DefineVarInt("a", 12); evaluator.DefineVarInt("b", 12); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result (should be true): " + execResult.ResultBool); }
public static void a_plus_b_mul_c_ret_14() { string expr = "a+b*c"; Console.WriteLine("\n====The expression is: " + expr); Console.WriteLine("The component Apply operator priority: a+(b*c)"); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result Console.WriteLine("Define variables: a:=2, b:=3, c:= 4"); evaluator.DefineVarInt("a", 2); evaluator.DefineVarInt("b", 3); evaluator.DefineVarInt("c", 4); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result is an int (true)?: " + execResult.IsResultInt); Console.WriteLine("Execution Result (should be 14): " + execResult.ResultInt); }
/// <summary> /// Execute a function having 2 int parameters. /// </summary> public static void CallFunctionWith3IntParams_ret_10() { string expr = "fct(a,b,c)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression evaluator.Parse(expr); //====2/prepare the execution, attach function Console.WriteLine("Attach function code to Fct() and set value to param: a=2, b=3; c=5"); evaluator.DefineVarInt("a", 2); evaluator.DefineVarInt("b", 3); evaluator.DefineVarInt("c", 5); // attach the 3 params function to the function call Func3ParamsRetIntMapper <int, int, int> func3ParamsRetIntMapper = new Func3ParamsRetIntMapper <int, int, int>(); func3ParamsRetIntMapper.SetFunction(FctRetInt_Int_Int_Int); evaluator.AttachFunction("fct", func3ParamsRetIntMapper); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution has error? (should be false): " + execResult.HasError); Console.WriteLine("Execution Result is an int type?: " + execResult.IsResultInt); Console.WriteLine("Execution Result is (should be 10): " + execResult.ResultInt); }
/// <summary> /// Expression: (A=B) /// A boolean expression using two variables. /// returns always a boolean value result. /// /// Particularity: /// Execute the same expression 2 times. /// The first time, both variables A and B are defined as integer. /// The second time, A and B are then defined as boolean. /// /// The execution finish successfully. /// </summary> public static void A_Eq_B_Exec2Times() { string expr = "(A = B)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result Console.WriteLine("Define variables: A=15; B=15 "); evaluator.DefineVarInt("a", 15); evaluator.DefineVarInt("b", 15); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result: " + execResult.ResultBool); //====================================================== //====2/prepare the execution, provide all used variables: type and value, remove the previous result Console.WriteLine("\nExecute again the same provided expression but changes variables types and values:"); Console.WriteLine("Define variables: A=false; B=false"); evaluator.DefineVarBool("a", false); evaluator.DefineVarBool("b", false); //====3/execute l'expression booléenne execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result: " + execResult.ResultBool); }
public void Exec_A_Diff_B_Int_True_Ok() { ExpressionEval evaluator = new ExpressionEval(); // definir langue: fr ou en, sert pour les opérateurs: ET/AND, OU/OR, NON/NOT. evaluator.SetLang(Language.En); string expr = "(A<>B)"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value //ExprExecResult execResult = evaluator.InitExec(); evaluator.DefineVarInt("a", 10); evaluator.DefineVarInt("b", 12); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success"); // check the final result value ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool; Assert.IsNotNull(valueBool, "The result value should be a bool"); Assert.AreEqual(true, valueBool.Value, "The result value should be: true"); }
/// <summary> /// Execute a function call two times with different values. /// /// fct(a) a:=8 ->return false /// fct(a) a:=12 ->return true /// /// </summary> public static void CallFunctionWithIntParam_TwoTimes() { string expr = "fct(a)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression evaluator.Parse(expr); //====2/prepare the execution, attach function Console.WriteLine("Attach function code to Fct() and set value to param: a=8"); evaluator.DefineVarInt("a", 8); evaluator.AttachFunction("fct", FctRetBool_Int); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result (should return false): " + execResult.ResultBool); //============================================================ //====2/prepare the execution, attach function Console.WriteLine("Set value to param: a=12"); evaluator.DefineVarInt("a", 12); evaluator.AttachFunction("fct", FctRetBool_Int); //====3/Execute the expression execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result (should return true): " + execResult.ResultBool); }
/// <summary> /// error, the variables types mismatch. /// Can't compare an integer and a boolean. /// /// ====The expression is: (a=b) /// The expr '(a=b)' has errors, nb = 1 /// Error code: ExprComparisonOperandsTypeMismatchIntExpected /// Error param: Position= 3 /// /// </summary> public static void A_Eq_B_TypeMismatch() { string expr = "(a=b)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); // 1-decode the expression evaluator.Parse(expr); // 2-set variables evaluator.DefineVarInt("a", 12); evaluator.DefineVarBool("b", true); // 3-execute the expression ExecResult execResult = evaluator.Exec(); // 4-check error if (execResult.HasError) { // display the error code Console.WriteLine("The expr '" + expr + "' has errors, nb=" + execResult.ListError.Count); ExprError error = execResult.ListError[0]; Console.WriteLine("Error code: " + error.Code); // display the error parameter (the position of the wrong token) Console.WriteLine("Error param: " + error.ListErrorParam[0].Key + "= " + error.ListErrorParam[0].Value); return; } Console.WriteLine("The expr " + expr + " parse finished sucessfully!"); }
public void fct_OP_a_CP_retBool_false_ok() { ExpressionEval evaluator = new ExpressionEval(); // definir langue: fr ou en, sert pour les opérateurs: ET/AND, OU/OR, NON/NOT. evaluator.SetLang(Language.En); string expr = "fct(a)"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value //ExprExecResult execResult = evaluator.InitExec(); evaluator.DefineVarInt("a", 23); // link function body to function call evaluator.AttachFunction("Fct", FctInt); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.AreEqual(false, execResult.HasError, "The exec of the expression should finish with success"); // check the final result value (is ExprExecFunctionCallBool override ExprExecValueBool) ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool; Assert.IsNotNull(execResult, "The result value should be a bool"); Assert.AreEqual(true, valueBool.Value, "The result value should be: true"); }
/// <summary> /// Define a variable but the syntax name is wrong: has space. /// </summary> public static void DefineVarSyntaxName() { string expr = "Not(A)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result Console.WriteLine("Define variables: A=12"); evaluator.DefineVarBool("a", false); Console.WriteLine("Define wrong variable name: 'a b c' -> don't stop the execution of the evaluation of the expression!"); evaluator.DefineVarInt("a b c", 12); List <ExprError> listConfigError = evaluator.GetListErrorExprConfig(); Console.WriteLine("DefineVar failed, err (VarNameSyntaxWrong): " + listConfigError[0].Code); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result (true): " + execResult.ResultBool); }
public static void Not_OP_A_CP_A_Int_Err() { string expr = "Not(A)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result Console.WriteLine("Define variables: A=12"); evaluator.DefineVarInt("a", 12); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); if (execResult.HasError) { // error: VariableNotCreated Console.WriteLine("Execution Result failed, err: " + execResult.ListError[0].Code); // Key: VarName, Value: a Console.WriteLine("Execution Result failed, ParamKey: " + execResult.ListError[0].ListErrorParam[0].Key + ", ParamValue: " + execResult.ListError[0].ListErrorParam[0].Value); return; } //====4/get the result, its a bool value Console.WriteLine("Execution Result: " + execResult.ResultBool); }
public void Exec_Not_OP_a_CP_VarTypeWrong() { ExpressionEval evaluator = new ExpressionEval(); // definir langue: fr ou en, sert pour les opérateurs: ET/AND, OU/OR, NON/NOT. evaluator.SetLang(Language.En); string expr = "Not(A)"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value //ExprExecResult execResult = evaluator.InitExec(); // define the var a as an int in place of a bool!!! evaluator.DefineVarInt("a", 12); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.AreEqual(true, execResult.HasError, "The exec of the expression should finish with success"); // get the error ExprError error = execResult.ListError[0]; Assert.AreEqual(ErrorType.Exec, error.ErrorType, "The errorType should be: Exec"); Assert.AreEqual(ErrorCode.ExprLogicalNotOperator_InnerOperandBoolTypeExpected, error.Code, "The errCode should be: VariableNotCreated"); ErrorParam errParam = error.ListErrorParam[0]; Assert.AreEqual("Token", errParam.Key, "The errParam key should be: VarName"); Assert.AreEqual("A", errParam.Value, "The errParam value should be: a"); ErrorParam errParam2 = error.ListErrorParam[1]; Assert.AreEqual("Position", errParam2.Key, "The errParam key should be: VarName"); Assert.AreEqual("4", errParam2.Value, "The errParam value should be: a"); }
public void Double_and_OneVarTypeWrong_err() { ExpressionEval evaluator = new ExpressionEval(); // definir langue: fr ou en, sert pour les opérateurs: ET/AND, OU/OR, NON/NOT. evaluator.SetLang(Language.En); //evaluator.SetDoubleDecimalSeparator(ExpressionEvalDef.DoubleDecimalSeparator.Dot); string expr = "a and b"; ParseResult parseResult = evaluator.Parse(expr); Assert.IsFalse(parseResult.HasError, "the parse should finish successfully"); //====2/prepare the execution, provide all used variables: type and value evaluator.DefineVarInt("a", 12); evaluator.DefineVarBool("b", true); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.IsTrue(execResult.HasError, "The exec of the expression should finish with error"); // todo: pas bon!! corriger Assert.AreEqual(ErrorCode.ExprLogicalOperandTypeNotAllowed, execResult.ListError[0].Code, "The exec of the expression should finish with success"); }
static void OP_A_Eq_B_CP() { string expr = "(A = B)"; ExpressionEval evaluator = new ExpressionEval(); //====1/décode l'expression booléenne ParseResult parseResult = evaluator.Parse(expr); // displays variables used/found in the expression foreach (ExprVarUsed exprVarUsed in parseResult.ListExprVarUsed) { Console.WriteLine("Var, Name=" + exprVarUsed.Name); // ExprVariableInt } //====2/prepare the execution, provide all used variables: type and value, remove the previous result //ExprExecResult execResult = evaluator.InitExec(); evaluator.DefineVarInt("a", 15); evaluator.DefineVarInt("b", 15); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); //====4/get the result if (execResult.HasError) { Console.WriteLine("Execution Result: error!"); } if (execResult.IsResultBool) { Console.WriteLine("Execution Result: " + execResult.ResultBool); } // replace: //ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool; //Console.WriteLine("Execution Result: " + valueBool.Value.ToString()); }
public void Exec_a_gt_OP_b_Plus_5_CP_retFalse_ok() { ExpressionEval evaluator = new ExpressionEval(); evaluator.SetLang(Language.En); string expr = "a > (b+5)"; ParseResult parseResult = evaluator.Parse(expr); evaluator.DefineVarInt("a", 2); evaluator.DefineVarInt("b", 10); ExecResult execResult = evaluator.Exec(); Assert.IsFalse(execResult.HasError, "The exec of the expression should finish with success"); // check the final result value ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool; Assert.IsNotNull(valueBool, "The result value should be a bool"); Assert.AreEqual(false, valueBool.Value, "The result value should be: false"); }
/// <summary> /// Expression: (A=B) /// A boolean expression using two variables. /// returns always a boolean value result. /// /// The execution finish successfully. /// </summary> public static void A_Eq_B_true() { string expr = "(A = B)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result Console.WriteLine("Define variables: A=15; B=15 "); evaluator.DefineVarInt("a", 15); evaluator.DefineVarInt("b", 15); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result: " + execResult.ResultBool); }
static void OP_A_Eq_B_CP_Exec2Times() { string expr = "(A = B)"; ExpressionEval evaluator = new ExpressionEval(); //====1/décode l'expression booléenne ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result //ExprExecResult execResult = evaluator.InitExec(); evaluator.DefineVarInt("a", 15); evaluator.DefineVarInt("b", 15); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value ExprExecValueBool valueBool = execResult.ExprExec as ExprExecValueBool; Console.WriteLine("Execution Result: " + valueBool.Value.ToString()); //====================================================== //====2/prepare the execution, provide all used variables: type and value, remove the previous result //execResult = evaluator.InitExec(); evaluator.DefineVarBool("a", false); evaluator.DefineVarBool("b", false); //====3/execute l'expression booléenne execResult = evaluator.Exec(); //====4/get the result, its a bool value valueBool = execResult.ExprExec as ExprExecValueBool; Console.WriteLine("Execution Result: " + valueBool.Value.ToString()); }
public static void a_plus_b_ret_9() { string expr = "a+b"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result Console.WriteLine("Define variables: a:=4, b:=5"); evaluator.DefineVarInt("a", 4); evaluator.DefineVarInt("b", 5); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result is an int (true)?: " + execResult.IsResultInt); Console.WriteLine("Execution Result (should be 9): " + execResult.ResultInt); }
/// <summary> /// Execute a function having 2 int parameters. /// </summary> public static void CallFunctionWith2IntParams_ret_5() { string expr = "fct(a,b)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); //====1/decode the expression evaluator.Parse(expr); //====2/prepare the execution, attach function Console.WriteLine("Attach function code to Fct() and set value to param: a=8"); evaluator.DefineVarInt("a", 2); evaluator.DefineVarInt("b", 3); evaluator.AttachFunction("fct", FctRetInt_Int_Int); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result is an int type?: " + execResult.IsResultInt); Console.WriteLine("Execution Result is (should be 5): " + execResult.ResultInt); }
static void TestEpressionEval() { //string expr = "not A"; string expr = "A and B"; ExpressionEval evaluator = new ExpressionEval(); ParseResult parseResult = ParseExpr(evaluator, expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result evaluator.DefineVarInt("a", 12); evaluator.DefineVarBool("b", false); //evaluator.SetVariableValueInt("b", 12); Execute(evaluator, expr); }
static void TestErrorDefinitionVarFunc() { //string expr = "not A"; string expr = "A and B"; ExpressionEval evaluator = new ExpressionEval(); ParseResult parseResult = ParseExpr(evaluator, expr); //====2/prepare the execution, provide all used variables: type and value, remove the previous result evaluator.DefineVarInt("a", 12); evaluator.DefineVarBool("b", false); //evaluator.SetVariableValueInt("b", 12); // check definition (var/func) errors List <ExprError> listError = evaluator.GetListErrorExprConfig(); Execute(evaluator, expr); }
public void ParseOk_InitExec_Exec_InitExec2_Exec2_ok() { ExpressionEval evaluator = new ExpressionEval(); string expr = "a"; ParseResult parseResult = evaluator.Parse(expr); Assert.IsFalse(parseResult.HasError, "The parse of the expression should finish with success"); //====2/prepare the execution, provide all used variables: type and value //ExprExecResult execResult = evaluator.InitExec(); //Assert.IsFalse(execResult.HasError, "The parse of the expression should finish with success"); //=====2.1/Init var evaluator.DefineVarBool("a", true); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.IsFalse(execResult.HasError, "The init exec of the expression should failed"); Assert.IsTrue(execResult.IsResultBool, "The exec result be a bool"); Assert.IsTrue(execResult.ResultBool, "The exec result be true"); //====4/prepare the execution, provide all used variables: type and value //execResult = evaluator.InitExec(); Assert.IsFalse(execResult.HasError, "The parse of the expression should finish with success"); //=====2.1/Init var evaluator.DefineVarInt("a", 14); //====3/execute l'expression booléenne execResult = evaluator.Exec(); Assert.IsFalse(execResult.HasError, "The init exec of the expression should failed"); Assert.IsTrue(execResult.IsResultInt, "The exec result be an int"); Assert.AreEqual(14, execResult.ResultInt, "The exec result be 14"); //====5/execute l'expression booléenne execResult = evaluator.Exec(); Assert.IsFalse(execResult.HasError, "The init exec of the expression should failed"); }