public void forLoop1()
        {
            EOrientation orientation = EOrientation.East;
            Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy));

            MainCode mainCode = new MainCode ();

            // Create (int i = 0; i < 5; i++)

            //Create i
            ConcreteVariable iVar = new ConcreteVariable (new Variable (0, EVariableType.Int));
            DefineVariable iDefine = new DefineVariable (iVar);
            CmdDefineVariable iDefineCommand = new CmdDefineVariable ("i", iDefine);

            //create a variable thats always 5
            Variable max = new Variable (5, EVariableType.Int);
            VariableSolver maxS = new ConcreteVariable (max);

            //Create i < 5
            ValueSolver solver = new ValueSolver (iVar, maxS, EComparisonOperator.ValueLessThan);

            // Create i++
            ConcreteVariable staticOneVar = new ConcreteVariable (new Variable (1, EVariableType.Int));
            ConcreteVariable iGather = new ConcreteVariable ("i"); // Gathers i, this creates a reference to iVar
            VariableSolver iPlusOneSolver = new VariableCombo (iGather, staticOneVar, EMathOperator.Add);
            DefineVariable iPlusOneDefine = new DefineVariable (iPlusOneSolver);
            CmdDefineVariable iPlusOneSolverCommand = new CmdDefineVariable ("i", iPlusOneDefine);

            Composite forLoop = new ForLoop (solver, iDefineCommand, iPlusOneSolverCommand);

            mainCode.addChild (forLoop);
            forLoop.addChild (new TurnRight ());

            mainCode.execute ();

            EOrientation actual = robot.orientationEnum;
            EOrientation expected = EOrientation.South;
            Assert.AreEqual (expected, actual);
        }
Exemplo n.º 2
0
        /// Author: Max Hamulyak
        /// Date:	19-06-2015
        /// <summary>
        /// Parses the forloop based on all its childeren.
        /// </summary>
        /// <returns>The forloop.</returns>
        /// <param name="tokensToParse">Tokens to parse.</param>
        /// <param name="currentLine">Current line.</param>
        protected virtual ForLoop ParseForloop(List<Token> tokensToParse, int currentLine)
        {
            List<Token> tokensAtCurrentLine = tokensToParse.Where (x => x.Position.Line == currentLine).ToList ();
            tokensToParse.RemoveAll (x => x.Position.Line == currentLine);

            Tuple<Solver, CmdDefineVariable, CmdDefineVariable> header = ParseForLoopHeader (tokensAtCurrentLine, currentLine);
            ForLoop forLoop = new ForLoop (header.Item1, header.Item2, header.Item3);
            List<Token> childTokens = BuildCodeBlock (tokensToParse, currentLine);
            if (childTokens.Count > 0) {
                int last = childTokens.Last ().Position.Line;
                Token lastToken = tokensToParse.First (x => x.Position.Line == last + incrementIndex);
                EndOfBlockTokenCorrect (lastToken, "end;", ETokenType.FOR);
                RemoveTokensAtLine (tokensToParse, last + incrementIndex);
                List<ICodeBlock> childeren = BuildCommandBlok (childTokens, currentLine + 1, last + 1);
                if (childeren.Count > 0) {
                    foreach (var item in childeren) {
                        forLoop.addChild (item);
                    }
                    forLoop.LineNumber = currentLine;
                    return forLoop;
                } else {
                    throw EmptyCodeBlock (currentLine, ETokenType.FOR);
                }
            } else {
                throw EmptyCodeBlock (currentLine, ETokenType.FOR);
            }
        }
        public void forLoop2()
        {
            EOrientation orientation = EOrientation.East;
            Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy));

            MainCode mainCode = new MainCode ();

            // Create (int i = 1; i < 5; i*i)

            //Create i
            ConcreteVariable iVar = new ConcreteVariable (new Variable (1, EVariableType.Int));
            DefineVariable iDefine = new DefineVariable (iVar);
            CmdDefineVariable iDefineCommand = new CmdDefineVariable ("i", iDefine);

            //create a variable thats always 2
            Variable staticTwoVar = new Variable (2, EVariableType.Int);
            VariableSolver staticTwoVarS = new ConcreteVariable (staticTwoVar);

            // Create i*i
            ConcreteVariable iGather = new ConcreteVariable ("i"); // Gathers i, this creates a reference to iVar
            VariableSolver ixiSolver = new VariableCombo (iGather, staticTwoVarS, EMathOperator.Multiply);
            DefineVariable ixiDefine = new DefineVariable (ixiSolver);
            CmdDefineVariable ixiSolverCommand = new CmdDefineVariable ("i", ixiDefine);

            //create a variable thats always 5
            Variable max = new Variable (5, EVariableType.Int);
            VariableSolver maxS = new ConcreteVariable (max);

            //Create i < 4
            ValueSolver solver = new ValueSolver (iGather, maxS, EComparisonOperator.ValueLessThan);

            ForLoop forLoop = new ForLoop (solver, iDefineCommand, ixiSolverCommand);

            //forLoop.variables["i"] = new Variable(1, EVariableType.Int);

            mainCode.addChild (forLoop);
            forLoop.addChild (new TurnRight ());

            mainCode.execute ();

            EOrientation actual = robot.orientationEnum;
            EOrientation expected = EOrientation.North;
            Assert.AreEqual (expected, actual);
        }