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);
        }
        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.º 3
0
        /// Author: Max Hamulyak
        /// Date:	30-06-2015
        /// <summary>
        /// Builds the solver needed for an define variable statement, using a list of conditions.
        /// based on a set of rules it can be checked how the list should be parsed, recursivly. 
        /// </summary>
        /// <returns>The define variable.</returns>
        /// <param name="conditions">Conditions.</param>
        /// <param name="priorityOperator">Priority operator.</param>
        protected virtual VariableSolver BuildDefineVariable(List<IAssignment> conditions, EMathOperator priorityOperator)
        {
            int index = 0;
            bool stopLoop = false;

            while (index < conditions.Count && !stopLoop) {

                if (conditions.Count > 3) {
                    int loopIndex = 0;
                    while (loopIndex < conditions.Count) {
                        IAssignment assignment = conditions.ElementAt (loopIndex);
                        if (assignment is MathOperator) {

                            if ((assignment as MathOperator).MathOperator2 == priorityOperator) {
                                int itemIndex = conditions.IndexOf (assignment);
                                IAssignment left = conditions.ElementAt (itemIndex - 1);
                                IAssignment right = conditions.ElementAt (itemIndex + 1);
                                conditions.Remove (left);
                                conditions.Remove (right);
                                conditions.Remove (assignment);
                                if ((left is VariableSolver) && (right is VariableSolver)) {
                                    VariableCombo combo = new VariableCombo ((left as VariableSolver), (right as VariableSolver), priorityOperator);
                                    conditions.Insert (itemIndex - 1, combo);
                                } else {
                                    throw InvalidAssignment (left.LineNumber);
                                }
                            }
                        }
                        loopIndex++;
                    }
                    BuildDefineVariable (conditions, priorityOperator + 1);
                } else {
                    stopLoop = true;
                }
            }
            IAssignment returnAssignment = null;
            if (conditions.Count == 1) {
                //Should be concrete
                returnAssignment = conditions.ElementAt(0);

            } else if (conditions.Count == 2) {
                throw InvalidAssignmentTwoAssignmentValues (conditions);
            } else if (conditions.Count == 3) {
                if (conditions.ElementAt (1) is MathOperator) {
                    if (conditions.ElementAt (0) is VariableSolver && conditions.ElementAt (2) is VariableSolver) {

                        returnAssignment = new VariableCombo ((conditions.ElementAt (0) as VariableSolver), (conditions.ElementAt (2) as VariableSolver), (conditions.ElementAt (1) as MathOperator).MathOperator2);
                    } else {
                        throw InvalidAssignment (conditions.ElementAt (1).LineNumber);
                    }
                } else {
                    throw InvalidAssignmentMathOperator(conditions.ElementAt(1).LineNumber);
                }
            } else {
                throw InvalidAssignment (conditions.ElementAt (0).LineNumber);
            }

            return returnAssignment as VariableSolver;
        }
        public void variableStringSubstract()
        {
            MainCode mainCode = new MainCode ();

            // create i = 10
            ConcreteVariable iVar = new ConcreteVariable (new Variable ("10", EVariableType.String));
            DefineVariable iDefine = new DefineVariable (iVar);
            CmdDefineVariable iDefineCommand = new CmdDefineVariable ("i", iDefine);

            // create x = 5;
            ConcreteVariable xVar = new ConcreteVariable (new Variable (5, EVariableType.Int));
            DefineVariable xDefine = new DefineVariable (xVar);
            CmdDefineVariable xDefineCommand = new CmdDefineVariable ("x", xDefine);

            // Gather the variables
            ConcreteVariable iGather = new ConcreteVariable ("i");
            ConcreteVariable xGather = new ConcreteVariable ("x");

            // calculate and put the values in ii
            VariableSolver calculation = new VariableCombo (iGather, xGather, EMathOperator.Subtract);
            DefineVariable calcCommand = new DefineVariable (calculation);
            CmdDefineVariable executeCalculation = new CmdDefineVariable ("ii", calcCommand);

            mainCode.addChild (xDefineCommand);
            mainCode.addChild (iDefineCommand);
            mainCode.addChild (executeCalculation);
            mainCode.execute ();

            //ConcreteVariable iiGather = new ConcreteVariable ("ii");
        }
        public void variableStringAdd()
        {
            MainCode mainCode = new MainCode ();

            // create i = 10
            ConcreteVariable iVar = new ConcreteVariable (new Variable ("10", EVariableType.String));
            DefineVariable iDefine = new DefineVariable (iVar);
            CmdDefineVariable iDefineCommand = new CmdDefineVariable ("i", iDefine);

            // create x = 5;
            ConcreteVariable xVar = new ConcreteVariable (new Variable (5, EVariableType.Int));
            DefineVariable xDefine = new DefineVariable (xVar);
            CmdDefineVariable xDefineCommand = new CmdDefineVariable ("x", xDefine);

            // Gather the variables
            ConcreteVariable iGather = new ConcreteVariable ("i");
            ConcreteVariable xGather = new ConcreteVariable ("x");

            // calculate and put the values in ii
            VariableSolver calculation = new VariableCombo (iGather, xGather, EMathOperator.Add);
            DefineVariable calcCommand = new DefineVariable (calculation);
            CmdDefineVariable executeCalculation = new CmdDefineVariable ("ii", calcCommand);

            mainCode.addChild (xDefineCommand);
            mainCode.addChild (iDefineCommand);
            mainCode.addChild (executeCalculation);
            mainCode.execute ();

            //ConcreteVariable iiGather = new ConcreteVariable ("ii");

            string expected = "105";
            string actual = mainCode.variables["ii"].Value;
            Assert.AreEqual (expected, actual);
        }