public void AlgorithmInterpreterError() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("MyVar")); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmVariableReferenceExpression("MyVar2"))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 5); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.Count(), 1); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.First().Stack.First().Variables.Count, 1); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.First().Stack.First().Variables[0].Name, "MyVar"); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public void InstanciateClassNotFound() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression("SecondClass")))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 5); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Reference to the class : SecondClass"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.Error.Exception.Message, "Unable to find the class 'SecondClass' because it does not exist or it is not accessible."); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public static void RunProgramWithoutDebug(AlgorithmProgram program, bool mustCrash = false) { var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: false); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 4); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); if (mustCrash) { Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); } else { Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); } algorithmInterpreter.Dispose(); }
public void BinaryOperatorIncompatibleTypes() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("result")); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("result"), new AlgorithmBinaryOperatorExpression(new AlgorithmPrimitiveExpression(new JObject()), AlgorithmBinaryOperatorType.GreaterThan, new AlgorithmPrimitiveExpression(new DateTime())))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 10); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[9].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.Error.Exception.Message, "Operator 'GreaterThan' cannot be applied to operands of type 'Newtonsoft.Json.Linq.JObject' and 'System.DateTime'"); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public void PropertyReferenceCore() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("var1")); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("var1"), new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression("System", "Version"), new AlgorithmPrimitiveExpression("1.0.0.0")))); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPropertyReferenceExpression(new AlgorithmVariableReferenceExpression("var1"), "Major"))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 15); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[13].LogMessage, "(Main) Return : '1' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[14].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void InstanciateCore() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression("System", "Version"), new AlgorithmPrimitiveExpression("1.0.0.0")))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 8); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Reference to the class : System.Version"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Creating a new instance of 'System.Version'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Primitive value : '1.0.0.0' (type:System.String)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].LogMessage, "(Main) Return : '1.0.0.0' (type:System.Version)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void IterationInfiniteLoop() { // DO WHILE (true) // object myVar // LOOP var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmIterationStatement(null, null, new AlgorithmPrimitiveExpression(true), false, new AlgorithmStatementCollection() { new AlgorithmVariableDeclaration("myVar") })); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(1))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); Task.Delay(TimeSpan.FromMilliseconds(100)).Wait(); algorithmInterpreter.Stop(); Task.Delay(TimeSpan.FromSeconds(3)).Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Last().State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); }
public void IterationDoWhile() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); /* * FUNCTION Main() * y = 0 * i = 0 * DO * y = y + 10 * i = i + 1 * LOOP WHILE (i < 10) * RETURN y * END FUNCTION */ entryPoint.Statements.Add(new AlgorithmVariableDeclaration("y") { DefaultValue = new AlgorithmPrimitiveExpression(0) }); var initialization = new AlgorithmVariableDeclaration("i") { DefaultValue = new AlgorithmPrimitiveExpression(0) }; var incrementation = new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("i"), new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("i"), AlgorithmBinaryOperatorType.Addition, new AlgorithmPrimitiveExpression(1))); var condition = new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("i"), AlgorithmBinaryOperatorType.LessThan, new AlgorithmPrimitiveExpression(10)); var stmts = new AlgorithmStatementCollection(); stmts.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("y"), new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("y"), AlgorithmBinaryOperatorType.Addition, new AlgorithmPrimitiveExpression(10)))); entryPoint.Statements.Add(new AlgorithmIterationStatement(initialization, incrementation, condition, true, stmts)); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmVariableReferenceExpression("y"))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 160); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[158].LogMessage, "(Main) Return : '100' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[159].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void AlgorithmInterpreterStates() { var program = CreateBasicProgram(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 9); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[8].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void AlgorithmInterpreterBreakpointStop() { var program = CreateBasicRecursivityProgramWithOneBreakpoint(); var algorithmInterpreter = new AlgorithmInterpreter(program); algorithmInterpreter.StartAsync(debugMode: true); Task.Delay(TimeSpan.FromSeconds(5)).Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[95].State, AlgorithmInterpreterState.PauseBreakpoint); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.First().Stack.Count, 8); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.First().Stack.First().Variables[0].Name, "num"); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.First().Stack.First().Variables[0].Value, (long)4); algorithmInterpreter.Stop(); Task.Delay(TimeSpan.FromSeconds(2)).Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[96].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void InvokeCoreMethodAsyncAwait() { var program = GetAsyncProgram(true); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 8); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Calling core method 'System.Threading.Tasks.Task.Delay'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Reference to the class : System.Threading.Tasks.Task"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Primitive value : '50' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].LogMessage, "(Main) Return : {null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void InstanciateCoreWithBadArgumentsCount() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression("System", "Version")))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 6); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Reference to the class : System.Version"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Creating a new instance of 'System.Version'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.Error.Exception.Message, "Constructor on type 'System.Version' not found."); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public void InvokeMethodAsyncAwait() { var program = GetAsyncProgram(true); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 16); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Variable 'MyVar' declared in the class => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Assign 'MyVar' to 'new FirstClass()'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Value of the variable 'MyVar' is {null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].LogMessage, "Reference to the class : FirstClass"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].LogMessage, "Creating a new instance of 'FirstClass'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[8].LogMessage, "Variable 'MyVar' declared in the class => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[9].LogMessage, "'MyVar' is now equal to 'Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter' (type:Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[10].LogMessage, "Calling method 'MyVar.FirstMethod'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[11].LogMessage, "Value of the variable 'MyVar' is 'Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter' (type:Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[12].LogMessage, "Primitive value : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[13].LogMessage, "(FirstMethod) Return : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[14].LogMessage, "(Main) Return : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[15].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void Return() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(123))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 6); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Primitive value : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "(Main) Return : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void BinaryOperatorNullValue() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("result")); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("result"), new AlgorithmBinaryOperatorExpression(new AlgorithmPrimitiveExpression(null), AlgorithmBinaryOperatorType.Equals, new AlgorithmPrimitiveExpression(2)))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 11); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[10].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void BinaryOperatorDivideByZero() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("result")); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("result"), new AlgorithmBinaryOperatorExpression(new AlgorithmPrimitiveExpression(123), AlgorithmBinaryOperatorType.Division, new AlgorithmPrimitiveExpression(0)))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 10); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[9].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.Error.Exception.Message, "Attempted to divide by zero."); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public void InvokeMethodRecursivityException() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); // FirstMethod(num) // { // if (num == 2) // object num; // throw an error because "num" already exists. // else if (num > 1) // return FirstMethod(num - 1) // return num; // } var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Arguments.Add(new AlgorithmParameterDeclaration("num")); firstMethod.Statements.Add(new AlgorithmConditionStatement(new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.Equality, new AlgorithmPrimitiveExpression(2)), new AlgorithmStatementCollection() { new AlgorithmVariableDeclaration("num") }, new AlgorithmStatementCollection() { new AlgorithmConditionStatement(new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.GreaterThan, new AlgorithmPrimitiveExpression(1)), new AlgorithmStatementCollection() { new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.Subtraction, new AlgorithmPrimitiveExpression(1)))) }, null) })); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmVariableReferenceExpression("num"))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmPrimitiveExpression(10)))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 107); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.First().Stack.Count, 10); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.First().Stack.First().Variables[0].Name, "num"); Assert.AreEqual(algorithmInterpreter.DebugInfo.CallStackService.CallStacks.First().Stack.First().Variables[0].Value, (long)2); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[106].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
private void Initialize() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); // The following code is simulated /* * FUNCTION FirstMethod(num) * IF (num > 1) * RETURN FirstMethod(num - 1) * END IF * RETURN num * END FUNCTION * * VARIABLE stopWatch = new StopWatch() * stopWatch.Start() * * FirstMethod(100) * * stopWatch.Stop() * VARIABLE messageDialog = new MessageDialog() * messageDialog.ShowAsync(stopWatch.Elapsed.TotalMilliseconds.ToString()) */ var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Arguments.Add(new AlgorithmParameterDeclaration("num")); firstMethod.Statements.Add(new AlgorithmConditionStatement(new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.GreaterThan, new AlgorithmPrimitiveExpression(1)), new AlgorithmStatementCollection() { new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.Subtraction, new AlgorithmPrimitiveExpression(1)))) }, null)); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmVariableReferenceExpression("num"))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("stopWatch")); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("stopWatch"), new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression(typeof(Stopwatch))))); entryPoint.Statements.Add(new AlgorithmExpressionStatement(new AlgorithmInvokeCoreMethodExpression(new AlgorithmVariableReferenceExpression("stopWatch"), "Start", null))); entryPoint.Statements.Add(new AlgorithmExpressionStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmPrimitiveExpression(100)))); entryPoint.Statements.Add(new AlgorithmExpressionStatement(new AlgorithmInvokeCoreMethodExpression(new AlgorithmVariableReferenceExpression("stopWatch"), "Stop", null))); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("messageDialog")); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("messageDialog"), new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression(typeof(MessageDialog)), new AlgorithmInvokeCoreMethodExpression(new AlgorithmPropertyReferenceExpression(new AlgorithmPropertyReferenceExpression(new AlgorithmVariableReferenceExpression("stopWatch"), "Elapsed"), "TotalMilliseconds"), "ToString", null)))); entryPoint.Statements.Add(new AlgorithmExpressionStatement(new AlgorithmInvokeCoreMethodExpression(new AlgorithmVariableReferenceExpression("messageDialog"), "ShowAsync", null))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); _algorithmInterpreter = new AlgorithmInterpreter(program); }
public void InstanciateWithSeveralConstructors() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression("SecondClass"), new AlgorithmPrimitiveExpression(123)))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); var secondClass = new AlgorithmClassDeclaration("SecondClass"); secondClass.Members.Add(new AlgorithmClassPropertyDeclaration("field1")); secondClass.Members.Add(new AlgorithmClassConstructorDeclaration()); var ctor = new AlgorithmClassConstructorDeclaration(new AlgorithmParameterDeclaration("arg1")); ctor.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("field1"), new AlgorithmVariableReferenceExpression("arg1"))); secondClass.Members.Add(ctor); program.Classes.Add(secondClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 15); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Reference to the class : SecondClass"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Creating a new instance of 'SecondClass'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Variable 'field1' declared in the class => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].LogMessage, "Primitive value : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].LogMessage, "Calling a constructor of 'SecondClass'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[8].LogMessage, "Variable 'arg1' declared in the method's argument => IsArray:False, DefaultValue:123"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[9].LogMessage, "Assign 'field1' to 'arg1'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[10].LogMessage, "Value of the variable 'field1' is {null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[11].LogMessage, "Value of the variable 'arg1' is '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[12].LogMessage, "'field1' is now equal to '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[13].LogMessage, "(Main) Return : 'Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter' (type:Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[14].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void ArrayIndexer() { /* * * FUNCTION Main() * VARIABLE result * VARIABLE var1[] = { "item1", "item2 } * result = var1[1] * var1[0] = result * return var1[0] * END FUNCTION * */ var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("result")); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("var1", true)); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("var1"), new AlgorithmPrimitiveExpression(new List <object>() { "item1", "item2" }))); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("result"), new AlgorithmArrayIndexerExpression(new AlgorithmVariableReferenceExpression("var1"), new AlgorithmPrimitiveExpression(1)))); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmArrayIndexerExpression(new AlgorithmVariableReferenceExpression("var1"), new AlgorithmPrimitiveExpression(0)), new AlgorithmVariableReferenceExpression("result"))); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmArrayIndexerExpression(new AlgorithmVariableReferenceExpression("var1"), new AlgorithmPrimitiveExpression(0)))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 26); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[24].LogMessage, "(Main) Return : 'item2' (type:System.String)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[25].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void AlgorithmInterpreterPauseResume() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Arguments.Add(new AlgorithmParameterDeclaration("num")); firstMethod.Statements.Add(new AlgorithmConditionStatement(new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.GreaterThan, new AlgorithmPrimitiveExpression(1)), new AlgorithmStatementCollection() { new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.Subtraction, new AlgorithmPrimitiveExpression(1)))) }, null)); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmVariableReferenceExpression("num"))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmPrimitiveExpression(90000000)))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); Task.Delay(TimeSpan.FromSeconds(1)).Wait(); algorithmInterpreter.Pause(); Task.Delay(TimeSpan.FromSeconds(2)).Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Last().State, AlgorithmInterpreterState.Pause); algorithmInterpreter.Resume(); Task.Delay(TimeSpan.FromSeconds(2)).Wait(); algorithmInterpreter.Stop(); Task.Delay(TimeSpan.FromSeconds(2)).Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Last().State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); }
public void InvokeMethodAwaitButNotAsync() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); firstClass.Members.Add(new AlgorithmClassPropertyDeclaration("MyVar")); var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(123))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("MyVar"), new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression("FirstClass")))); var invoke = new AlgorithmInvokeMethodExpression(new AlgorithmVariableReferenceExpression("MyVar"), "FirstMethod"); invoke.Await = true; entryPoint.Statements.Add(new AlgorithmReturnStatement(invoke)); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 13); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Variable 'MyVar' declared in the class => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Assign 'MyVar' to 'new FirstClass()'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Value of the variable 'MyVar' is {null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].LogMessage, "Reference to the class : FirstClass"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].LogMessage, "Creating a new instance of 'FirstClass'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[8].LogMessage, "Variable 'MyVar' declared in the class => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[9].LogMessage, "'MyVar' is now equal to 'Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter' (type:Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[10].LogMessage, "Calling method 'MyVar.FirstMethod'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[11].LogMessage, "Value of the variable 'MyVar' is 'Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter' (type:Algo.Runtime.Build.Runtime.Interpreter.Interpreter.ClassInterpreter)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[12].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public void InvokeMethodRecursivityStackOverflow() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); // FirstMethod(num) // { // if (num > 1) // return FirstMethod(num - 1) // return num; // } var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Arguments.Add(new AlgorithmParameterDeclaration("num")); firstMethod.Statements.Add(new AlgorithmConditionStatement(new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.GreaterThan, new AlgorithmPrimitiveExpression(1)), new AlgorithmStatementCollection() { new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.Subtraction, new AlgorithmPrimitiveExpression(1)))) }, null)); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmVariableReferenceExpression("num"))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmPrimitiveExpression(90000000)))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 90016); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[90015].Error.Exception.Message, "You called too many (more than 10000) methods in the same thread."); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[90015].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public void BinaryOperator() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("var1")); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("var2")); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("result")); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("var1"), new AlgorithmPrimitiveExpression(3.14))); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("var2"), new AlgorithmPrimitiveExpression(5))); PlayOperator(AlgorithmBinaryOperatorType.Addition, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.Subtraction, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.Division, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.Multiply, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.Modulus, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.GreaterThan, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.GreaterThanOrEqual, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.LessThan, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.LessThanOrEqual, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.Equals, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.Equality, ref entryPoint); PlayOperator(AlgorithmBinaryOperatorType.Inequality, ref entryPoint); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 87); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[86].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void InvokeMethod() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(123))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod"))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 9); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Calling method 'This.FirstMethod'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Reference to the current instance : FirstClass"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Primitive value : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].LogMessage, "(FirstMethod) Return : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].LogMessage, "(Main) Return : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[8].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void InvokeMethodRecursivity() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); // FirstMethod(num) // { // if (num > 1) // num = FirstMethod(num - 1) // return num; // } var firstMethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); firstMethod.Arguments.Add(new AlgorithmParameterDeclaration("num")); firstMethod.Statements.Add(new AlgorithmConditionStatement(new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.GreaterThan, new AlgorithmPrimitiveExpression(1)), new AlgorithmStatementCollection() { new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("num"), new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmBinaryOperatorExpression(new AlgorithmVariableReferenceExpression("num"), AlgorithmBinaryOperatorType.Subtraction, new AlgorithmPrimitiveExpression(1)))) }, null)); firstMethod.Statements.Add(new AlgorithmReturnStatement(new AlgorithmVariableReferenceExpression("num"))); firstClass.Members.Add(firstMethod); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInvokeMethodExpression(new AlgorithmThisReferenceExpression(), "FirstMethod", new AlgorithmPrimitiveExpression(5000)))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 70000); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[69998].LogMessage, "(Main) Return : '1' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[69999].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void InstanciateWithBadArgumentsCount() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression("SecondClass")))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); var secondClass = new AlgorithmClassDeclaration("SecondClass"); secondClass.Members.Add(new AlgorithmClassPropertyDeclaration("field1")); var ctor = new AlgorithmClassConstructorDeclaration(new AlgorithmParameterDeclaration("arg1")); ctor.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("field1"), new AlgorithmVariableReferenceExpression("arg1"))); secondClass.Members.Add(ctor); program.Classes.Add(secondClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 7); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].LogMessage, "Reference to the class : SecondClass"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Creating a new instance of 'SecondClass'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Variable 'field1' declared in the class => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.StoppedWithError); Assert.AreEqual(algorithmInterpreter.Error.Exception.Message, "There is no constructor with 0 argument(s) in the class 'SecondClass'."); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program, true); }
public void ArrayMethodAndProperties() { /* * * FUNCTION Main() * VARIABLE var1[] * var1.Add("item1") * return var1.Count * END FUNCTION * */ var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("var1", true) { DefaultValue = new AlgorithmPrimitiveExpression(new List <object>()) }); entryPoint.Statements.Add(new AlgorithmExpressionStatement(new AlgorithmInvokeCoreMethodExpression(new AlgorithmVariableReferenceExpression("var1"), "Add", new[] { typeof(object) }, new AlgorithmPrimitiveExpression("item1")))); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPropertyReferenceExpression(new AlgorithmVariableReferenceExpression("var1"), "Count"))); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 13); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[11].LogMessage, "(Main) Return : '1' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[12].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void PropertyReferenceAlgorithmClass() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var entryPoint = new AlgorithmEntryPointMethod(); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("var1")); entryPoint.Statements.Add(new AlgorithmAssignStatement(new AlgorithmVariableReferenceExpression("var1"), new AlgorithmInstanciateExpression(new AlgorithmClassReferenceExpression("SecondClass")))); entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmBinaryOperatorExpression(new AlgorithmPropertyReferenceExpression(new AlgorithmVariableReferenceExpression("var1"), "Property1"), AlgorithmBinaryOperatorType.Addition, new AlgorithmPropertyReferenceExpression(new AlgorithmThisReferenceExpression(), "Property2")))); firstClass.Members.Add(entryPoint); firstClass.Members.Add(new AlgorithmClassPropertyDeclaration("Property2") { DefaultValue = new AlgorithmPrimitiveExpression(123) }); program.Classes.Add(firstClass); var secondClass = new AlgorithmClassDeclaration("SecondClass"); secondClass.Members.Add(new AlgorithmClassPropertyDeclaration("Property1") { DefaultValue = new AlgorithmPrimitiveExpression(123) }); program.Classes.Add(secondClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 20); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[18].LogMessage, "(Main) Return : '246' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[19].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }
public void VariableDeclaration() { var program = new AlgorithmProgram("MyApp"); var firstClass = new AlgorithmClassDeclaration("FirstClass"); var firstmethod = new AlgorithmClassMethodDeclaration("FirstMethod", false); var entryPoint = new AlgorithmEntryPointMethod(); firstmethod.Arguments.Add(new AlgorithmParameterDeclaration("methodArg")); entryPoint.Statements.Add(new AlgorithmVariableDeclaration("methodVar")); entryPoint.Statements.Add(new AlgorithmExpressionStatement(new AlgorithmInvokeMethodExpression("FirstMethod", new AlgorithmPrimitiveExpression(123)))); firstClass.Members.Add(new AlgorithmClassPropertyDeclaration("classField")); program.Variables.Add(new AlgorithmVariableDeclaration("publicVar")); firstClass.Members.Add(firstmethod); firstClass.Members.Add(entryPoint); program.Classes.Add(firstClass); program.UpdateEntryPointPath(); var algorithmInterpreter = new AlgorithmInterpreter(program); var task = algorithmInterpreter.StartAsync(debugMode: true); task.Wait(); Assert.AreEqual(algorithmInterpreter.StateChangeHistory.Count, 11); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[0].State, AlgorithmInterpreterState.Ready); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[1].State, AlgorithmInterpreterState.Preparing); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].State, AlgorithmInterpreterState.Log); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[2].LogMessage, "Variable 'publicVar' declared in the program => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[3].State, AlgorithmInterpreterState.Running); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Variable 'classField' declared in the class => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "Variable 'methodVar' declared in the method => IsArray:False, DefaultValue:{null}"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].LogMessage, "Calling method 'This.FirstMethod'"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[7].LogMessage, "Reference to the current instance : FirstClass"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[8].LogMessage, "Primitive value : '123' (type:System.Int32)"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[9].LogMessage, "Variable 'methodArg' declared in the method's argument => IsArray:False, DefaultValue:123"); Assert.AreEqual(algorithmInterpreter.StateChangeHistory[10].State, AlgorithmInterpreterState.Stopped); Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped); AlgorithmInterpreter_Test.RunProgramWithoutDebug(program); }