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);
        }
Пример #3
0
 /// Author: Max Hamulyak
 /// Date:	29-06-2015
 /// <summary>
 /// Defines the variable from token, it also checks if a variable is not a keyword in the given language.
 /// </summary>
 /// <returns>The variable from token.</returns>
 /// <param name="instructionLine">Instruction line.</param>
 /// <param name="currentLine">Current line.</param>
 protected virtual CmdDefineVariable DefineVariableFromToken(List<Token> instructionLine, int currentLine)
 {
     List<Token> tokensAtCurrentLine = instructionLine.Where (x => x.Position.Line == currentLine).ToList ();
     instructionLine.RemoveAll (x => x.Position.Line == currentLine);
     string variableName = tokensAtCurrentLine.First ().Value;
     if (languageKeywords.Contains (variableName)) {
         throw KeyWordAsVariable (currentLine, variableName, EGameLanguage.Python);
     } else {
         VariableSolver solver = ParseVariableDefinition (tokensAtCurrentLine, currentLine);
         DefineVariable myVar = new DefineVariable (solver);
         CmdDefineVariable myVariable = new CmdDefineVariable (tokensAtCurrentLine.First().Value,myVar);
         return myVariable;
     }
 }
        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);
        }
Пример #6
0
        protected override CmdDefineVariable DefineVariableFromToken(List<Token> instructionLine, int currentLine)
        {
            List<Token> tokensAtCurrentLine = instructionLine.Where (x => x.Position.Line == currentLine).ToList ();
            instructionLine.RemoveAll (x => x.Position.Line == currentLine);
            if (tokensAtCurrentLine.Count == 2 && tokensAtCurrentLine [0].Type == ETokenType.VARIABLE && tokensAtCurrentLine [1].Type == ETokenType.EOL) {
                Token t = tokensAtCurrentLine.First ();
                List<String> result = new List<string> ();
                GroupCollection groups = varDeclarationRegex.Match (t.Value).Groups;
                if (groups.Count == 3) {
                    foreach (var item in varDeclarationRegex.GetGroupNames()) {
                        result.Add (string.Format ("Group: {0}, Value: {1}", item, groups [item].Value));
                    }

                    string variableName = groups ["variableName"].Value;
                    string variableType = groups ["variableType"].Value;

                    if (languageKeywords.Contains (variableName)) {
                        throw KeyWordAsVariable (currentLine, variableName, EGameLanguage.Python);
                    }
                    if (!definedVariables.ContainsKey (variableName)) {
                        definedVariables.Add (variableName, variableType);
                    } else {
                        throw VariableNameHasAlreadyBeenDeclared (currentLine, variableName);
                    }
                    List<IAssignment> assignments = new List<IAssignment> ();
                    assignments.Add (new ConcreteVariable (new Variable (0, EVariableType.Int)));
                    VariableSolver solver = BuildDefineVariable (assignments, EMathOperator.None);
                    DefineVariable myVar = new DefineVariable (solver);
                    CmdDefineVariable myVariable = new CmdDefineVariable (variableName, myVar);
                    return myVariable;
                } else {
                    return base.DefineVariableFromToken (tokensAtCurrentLine, currentLine);
                }
            } else {
                return base.DefineVariableFromToken (tokensAtCurrentLine, currentLine);
            }
        }
 /// Author: Bert van Montfort
 /// <summary>
 /// Creates a new CmdDefineVariable
 /// </summary>
 /// <param name="variableName">Variable name.</param>
 /// <param name="varValue">Variable value.</param>
 public CmdDefineVariable(string variableName, DefineVariable varValue)
 {
     this.variableName = variableName;
     this.varValue = varValue;
 }