public static void OP_A_Ou_b_CP_true() { string expr = "(a Ou b)"; Console.WriteLine("\n====The expression is: " + expr); ExpressionEval evaluator = new ExpressionEval(); // set the tool in french evaluator.SetLang(Language.Fr); //====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:=false, b=true"); evaluator.DefineVarBool("a", false); evaluator.DefineVarBool("b", true); //====3/Execute the expression ExecResult execResult = evaluator.Exec(); //====4/get the result, its a bool value Console.WriteLine("Execution Result: " + execResult.ResultBool); }
public void fct_OP_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()"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value //ExprExecResult execResult = evaluator.InitExec(); // attach the function code FctFalse() to the function call: Fct() evaluator.AttachFunction("Fct", FctFalse); //====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(false, valueBool.Value, "The result value should be: false"); }
private IMember CreateSpecificReturnFromClassType(IPythonClassType selfClassType, PythonClassType returnClassType, IArgumentSet args) { // -> A[_T1, _T2, ...] // Match arguments IReadOnlyList <IPythonType> typeArgs = null; var classGenericParameters = selfClassType?.GenericParameters.Keys.ToArray() ?? Array.Empty <string>(); if (classGenericParameters.Length > 0 && selfClassType != null) { // Declaring class is specific and provides definitions of generic parameters typeArgs = classGenericParameters .Select(n => selfClassType.GenericParameters.TryGetValue(n, out var t) ? t : null) .ExcludeDefault() .ToArray(); } else if (args != null) { typeArgs = ExpressionEval.GetTypeArgumentsFromParameters(this, args); } if (typeArgs != null) { var specificReturnValue = returnClassType.CreateSpecificType(new ArgumentSet(typeArgs)); return(new PythonInstance(specificReturnValue)); } return(null); }
public void Exec_A_Diff_B_Bool_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.DefineVarBool("a", false); evaluator.DefineVarBool("b", true); //====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> /// 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_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"); }
/// <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); }
/// <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); }
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> /// 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 void fct_OP_not_OP_a_CP_CP_retBool_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 = "fct(not (a))"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables and functions evaluator.DefineVarBool("a", true); // link function body to function call evaluator.AttachFunction("Fct", Fct_RetNot); //====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"); }
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 Double_Exposant_SepIsDot_false() { 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 = 12.45E3)"; 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 //ExprExecResult execResult = evaluator.InitExec(); evaluator.DefineVarDouble("a", 1345); //====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 Assert.IsFalse(execResult.ResultBool, "The result value should be false"); }
/// <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_CP_retBool_paramMissing_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()"; ParseResult parseResult = evaluator.Parse(expr); //====2/prepare the execution, provide all used variables: type and value //ExprExecResult execResult = evaluator.InitExec(); // link function body to function call evaluator.AttachFunction("fct", FctRetBool_P1Bool); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.IsTrue(execResult.HasError, "The exec of the expression should finish with error"); Assert.AreEqual(ErrorCode.FunctionCallParamCountWrong, execResult.ListError[0].Code, "The exec of the expression should finish with success"); Assert.AreEqual("FunctionCallName", execResult.ListError[0].ListErrorParam[0].Key, "The exec of the expression should finish with success"); Assert.AreEqual("fct", execResult.ListError[0].ListErrorParam[0].Value, "The exec of the expression should finish with success"); }
public void Bra_A_Eq_Minus_12_Bra() { 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=-12)"; //-1---parse the string, return a syntax tree ParseResult parseResult = evaluator.Parse(expr); Assert.IsFalse(parseResult.HasError, "the expression process should return success"); // check the root node ExprComparison rootBinExpr = parseResult.RootExpr as ExprComparison; Assert.IsNotNull(rootBinExpr, "The root node type should be BoolBinExpr"); // check the left part of the root node ExprFinalOperand operandLeft = rootBinExpr.ExprLeft as ExprFinalOperand; Assert.IsNotNull(operandLeft, "The left root node type should be BoolBinExprOperand"); Assert.AreEqual(operandLeft.Operand, "A", "The left operand should be A"); // check the right part of the root node ExprFinalOperand operandRight = rootBinExpr.ExprRight as ExprFinalOperand; Assert.IsNotNull(operandRight, "The left root node type should be BoolBinExprOperand"); Assert.AreEqual(operandRight.Operand, "-12", "The left operand should be -12"); // check the root node operator Assert.AreEqual(rootBinExpr.Operator, OperatorComparisonCode.Equals, "The root operator should be ="); }
/// <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); }
public void fct_OP_10_Sep_bonjour_CP_retBool_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); evaluator.SetStringTagCode(StringTagCode.Quote); string expr = "fct(10, 'bonjour')"; evaluator.Parse(expr); // link function body to function call evaluator.AttachFunction("Fct", FctP1Int_P2String); //====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"); }
public void Expr_4_Mul_5_Plus_1_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 = "4*5-1"; //-1---parse the string, return a syntax tree ParseResult parseResult = evaluator.Parse(expr); Assert.IsFalse(parseResult.HasError, "the expression process should finish successfully"); // check the root node ExprCalculation rootExprCalc = parseResult.RootExpr as ExprCalculation; Assert.IsNotNull(rootExprCalc, "The root node type should be an ExprCalculation"); // the calc expr should contains 3 operands and 2 operators Assert.AreEqual(3, rootExprCalc.ListExprOperand.Count, "The ExprCalculation should contains 3 operands"); Assert.AreEqual(2, rootExprCalc.ListOperator.Count, "The ExprCalculation should contains 2 operators"); // check (just) the fisrt operator: */Mul ExprOperatorCalculation operatorTwo = rootExprCalc.ListOperator[0] as ExprOperatorCalculation; Assert.IsNotNull(operatorTwo, "The left root node type should be a ExprFinalOperand"); Assert.AreEqual(OperatorCalculationCode.Multiplication, operatorTwo.Operator, "The Second operator should be: *"); // check (just) the last operand: 1 ExprFinalOperand operandThree = rootExprCalc.ListExprOperand[2] as ExprFinalOperand; Assert.IsNotNull(operandThree, "The left root node type should be a ExprFinalOperand"); Assert.AreEqual(1, operandThree.ValueInt, "The left operand should be 11"); }
public void fct_3Params_FunctionCall_OneMissing_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); string expr = "fct(true, true)"; evaluator.Parse(expr); // link function body to function call Func3ParamsRetBoolMapper <bool, bool, bool> func3ParamsRetBoolMapper = new Pierlam.ExpressionEval.Func3ParamsRetBoolMapper <bool, bool, bool>(); func3ParamsRetBoolMapper.SetFunction(Fct3ParamsBool); evaluator.AttachFunction("fct", func3ParamsRetBoolMapper); //====3/execute l'expression booléenne ExecResult execResult = evaluator.Exec(); Assert.IsTrue(execResult.HasError, "The exec of the expression should finish with error"); Assert.AreEqual(ErrorCode.FunctionCallParamCountWrong, execResult.ListError[0].Code, "The exec of the expression should finish with success"); Assert.AreEqual("FunctionCallName", execResult.ListError[0].ListErrorParam[0].Key, "The exec of the expression should finish with success"); Assert.AreEqual("fct", execResult.ListError[0].ListErrorParam[0].Value, "The exec of the expression should finish with success"); }
private IDocumentAnalysis CreateAnalysis(IDependencyChainNode <PythonAnalyzerEntry> node, IDocument document, PythonAst ast, int version, ModuleWalker walker, bool isCanceled) { var canHaveLibraryAnalysis = false; // Don't try to drop builtins; it causes issues elsewhere. // We probably want the builtin module's AST and other info for evaluation. switch (document.ModuleType) { case ModuleType.Library: case ModuleType.Stub: case ModuleType.Compiled: canHaveLibraryAnalysis = true; break; } var createLibraryAnalysis = !isCanceled && node != null && !node.HasMissingDependencies && canHaveLibraryAnalysis && !document.IsOpen && node.HasOnlyWalkedDependencies && node.IsValidVersion; if (!createLibraryAnalysis) { return(new DocumentAnalysis(document, version, walker.GlobalScope, walker.Eval, walker.StarImportMemberNames)); } ast.Reduce(x => x is ImportStatement || x is FromImportStatement); document.SetAst(ast); var eval = new ExpressionEval(walker.Eval.Services, document, ast); return(new LibraryAnalysis(document, version, walker.GlobalScope, eval, walker.StarImportMemberNames)); }
public void fct_OP_false_Sep_false_CP_retBool_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 = "fct(true, true, true)"; evaluator.Parse(expr); // link function body to function call var funcMapper = new Func3ParamsRetBoolMapper <bool, bool, bool>(); funcMapper.SetFunction(Fct3ParamsBool); evaluator.AttachFunction("Fct", funcMapper); //====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"); }
public void Double_a_xor_b_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); //evaluator.SetDoubleDecimalSeparator(ExpressionEvalDef.DoubleDecimalSeparator.Dot); string expr = "a xor 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.DefineVarBool("a", false); evaluator.DefineVarBool("b", true); //====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 Assert.IsTrue(execResult.IsResultBool, "The result should be a bool"); Assert.IsTrue(execResult.ResultBool, "The result value should be true"); }
public void fct_OP_12_Sep_bonjour_8dot5_CP_retInt_21_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(4, \"bonjour\", 8.5)"; evaluator.Parse(expr); // link function body to function call var funcMapper = new Func3ParamsRetIntMapper <int, string, double>(); funcMapper.SetFunction(FctP1Int_P2String_P3Double); evaluator.AttachFunction("Fct", funcMapper); //====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) ExprExecValueInt valueInt = execResult.ExprExec as ExprExecValueInt; Assert.IsNotNull(execResult, "The result value should be a bool"); Assert.AreEqual(21, valueInt.Value, "The result value should be: 18"); }
/// <summary> /// A dev!! /// </summary> static void TestExprLogicalSeveralOperands() { string expr = "(A and B or C)"; 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.DefineVarBool("a", true); evaluator.DefineVarBool("b", true); evaluator.DefineVarBool("c", false); //====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); } }
public EmptyAnalysis(IServiceContainer services, IDocument document) { Document = document ?? throw new ArgumentNullException(nameof(document)); GlobalScope = new EmptyGlobalScope(document); Ast = AstUtilities.MakeEmptyAst(document.Uri); ExpressionEvaluator = new ExpressionEval(services, document); }
private IMember GetSpecificReturnType(IPythonClassType selfClassType, IArgumentSet args) { var returnValueType = StaticReturnValue.GetPythonType(); switch (returnValueType) { case PythonClassType cls when cls.IsGeneric(): return(CreateSpecificReturnFromClassType(selfClassType, cls, args)); // -> A[_T1, _T2, ...] case IGenericTypeDefinition gtd1 when selfClassType != null: return(CreateSpecificReturnFromTypeVar(selfClassType, gtd1)); // -> _T case IGenericTypeDefinition gtd2 when args != null: // -> T on standalone function. return(args.Arguments.FirstOrDefault(a => gtd2.Equals(a.Type))?.Value as IMember); case IGenericType gt when args != null: // -> CLASS[T] on standalone function (i.e. -> List[T]). var typeArgs = ExpressionEval.GetTypeArgumentsFromParameters(this, args); if (typeArgs != null) { return(gt.CreateSpecificType(typeArgs)); } break; } return(StaticReturnValue); }
public void fct_OP_a_CP_retDouble_P1String_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(); // 3 letters -> return 3x1.5=4.5 evaluator.DefineVarString("a", "abc"); // link function body to function call evaluator.AttachFunction("Fct", FctDouble_String); //====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) ExprExecValueDouble valueDouble = execResult.ExprExec as ExprExecValueDouble; Assert.IsNotNull(valueDouble, "The result value should be a double"); Assert.AreEqual(4.5, valueDouble.Value, "The result value should be: true"); }
internal static void DeclareParametersInScope(this IArgumentSet args, ExpressionEval eval) { if (eval == null) { return; } // For class method no need to add extra parameters, but first parameter type should be the class. // For static and unbound methods do not add or set anything. // For regular bound methods add first parameter and set it to the class. foreach (var a in args.Arguments) { if (a.Value is IMember m && !string.IsNullOrEmpty(a.Name)) { eval.DeclareVariable(a.Name, m, VariableSource.Declaration, a.Location); } } if (args.ListArgument != null && !string.IsNullOrEmpty(args.ListArgument.Name)) { var type = new PythonCollectionType(null, BuiltinTypeId.List, eval.Interpreter, false); var list = new PythonCollection(type, args.ListArgument.Values); eval.DeclareVariable(args.ListArgument.Name, list, VariableSource.Declaration, args.ListArgument.Location); } if (args.DictionaryArgument != null) { foreach (var kvp in args.DictionaryArgument.Arguments) { eval.DeclareVariable(kvp.Key, kvp.Value, VariableSource.Declaration, args.DictionaryArgument.Location); } } }
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"); }