Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Initialize a new instance of <see cref="BlockInterpreter"/>
 /// </summary>
 /// <param name="statements">the list of statements to interpret</param>
 /// <param name="debugMode">defines is the debug mode is enabled or not</param>
 /// <param name="parentProgramInterpreter">the parent program interpreter</param>
 /// <param name="parentMethodInterpreter">the parent method interpreter</param>
 /// <param name="parentBlockInterpreter">the parent block interpreter</param>
 /// <param name="parentClassInterpreter">the parent class interpreter</param>
 internal BlockInterpreter(AlgorithmStatementCollection statements, bool debugMode, ProgramInterpreter parentProgramInterpreter, MethodInterpreter parentMethodInterpreter, BlockInterpreter parentBlockInterpreter, ClassInterpreter parentClassInterpreter)
     : base(debugMode)
 {
     ParentProgramInterpreter = parentProgramInterpreter;
     ParentMethodInterpreter  = parentMethodInterpreter;
     ParentBlockInterpreter   = parentBlockInterpreter;
     ParentClassInterpreter   = parentClassInterpreter;
     Statements = statements;
 }
Exemplo n.º 3
0
        public void ReturnInIteration()
        {
            var program    = new AlgorithmProgram("MyApp");
            var firstClass = new AlgorithmClassDeclaration("FirstClass");
            var entryPoint = new AlgorithmEntryPointMethod();

            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 AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(true)));
            stmts.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(null)));

            entryPoint.Statements.Add(new AlgorithmIterationStatement(initialization, incrementation, condition, false, stmts));
            entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(false)));

            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[8].LogMessage, "Primitive value : 'True' (type:System.Boolean)");
            Assert.AreEqual(algorithmInterpreter.StateChangeHistory[9].LogMessage, "(Main) Return : 'True' (type:System.Boolean)");

            Assert.AreEqual(algorithmInterpreter.StateChangeHistory[10].State, AlgorithmInterpreterState.Stopped);
            Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped);

            AlgorithmInterpreter_Test.RunProgramWithoutDebug(program);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Dispose the resources
        /// </summary>
        public override void Dispose()
        {
            Task.Run(() =>
            {
                Statements = null;

                if (Variables != null)
                {
                    foreach (var variable in Variables)
                    {
                        var value = variable.Value as IDisposable;
                        if (value != null)
                        {
                            value.Dispose();
                        }
                    }
                    Variables.Clear();
                }
                Variables = null;
            });
        }
Exemplo n.º 5
0
        public void ReturnInCondition()
        {
            var program    = new AlgorithmProgram("MyApp");
            var firstClass = new AlgorithmClassDeclaration("FirstClass");
            var entryPoint = new AlgorithmEntryPointMethod();

            var stmts = new AlgorithmStatementCollection();

            stmts.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(true)));
            stmts.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(null)));

            entryPoint.Statements.Add(new AlgorithmConditionStatement(new AlgorithmPrimitiveExpression(true), stmts, null));
            entryPoint.Statements.Add(new AlgorithmReturnStatement(new AlgorithmPrimitiveExpression(false)));

            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, 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, "Primitive value : 'True' (type:System.Boolean)");
            Assert.AreEqual(algorithmInterpreter.StateChangeHistory[4].LogMessage, "Primitive value : 'True' (type:System.Boolean)");
            Assert.AreEqual(algorithmInterpreter.StateChangeHistory[5].LogMessage, "(Main) Return : 'True' (type:System.Boolean)");

            Assert.AreEqual(algorithmInterpreter.StateChangeHistory[6].State, AlgorithmInterpreterState.Stopped);
            Assert.AreEqual(algorithmInterpreter.State, AlgorithmInterpreterState.Stopped);

            AlgorithmInterpreter_Test.RunProgramWithoutDebug(program);
        }