예제 #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);
        }
예제 #2
0
        private TokenEvaluatorResult EvaluateVariableDeclaration(string text, string[] splittedText, EvaluatorArgument evaluatorArgument)
        {
            var variableName = splittedText[2];
            var isArray      = variableName.EndsWith("[]");

            variableName = variableName.Replace("[]", string.Empty);

            // TODO : block
            if (!_inMethod && _inClass)
            {
                var propertyDeclaration = new AlgorithmClassPropertyDeclaration(variableName, isArray);
                if (isArray)
                {
                    propertyDeclaration.DefaultValue = new AlgorithmPrimitiveExpression(new List <object>());
                }
                return(new TokenEvaluatorResult(SyntaxTreeTokenType.PropertyDeclaration, TokenType.StatementSeparator, propertyDeclaration));
            }
            if (_inMethod || _inProgram)
            {
                var variableDeclaration = new AlgorithmVariableDeclaration(variableName, isArray);
                if (isArray)
                {
                    variableDeclaration.DefaultValue = new AlgorithmPrimitiveExpression(new List <object>());
                }
                return(new TokenEvaluatorResult(SyntaxTreeTokenType.VariableDeclaration, TokenType.StatementSeparator, variableDeclaration));
            }
            throw new ValidationException();
        }
예제 #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);
        }