コード例 #1
0
 /// Author: Max Hamulyak
 /// Date:	08-06-2015
 /// <summary>
 /// Takes a single <see cref="Token"/> and parses it to a robot instruction.
 /// Like <see cref="PickUp"/>, <see cref="Forward"/> and <see cref="TurnRight"/>
 /// that are all of the return type <see cref="Command"/>
 /// </summary>
 /// <returns>The <see cref="Command"/> from token.</returns>
 /// <param name="tokenToParse">Command token.</param>
 protected override Command SimpleCommandFromToken(List<Token> tokensToParse, int currentLine)
 {
     Token tokenToParse = tokensToParse.First ();
     tokensToParse.Remove (tokenToParse);
     tokensToParse.RemoveAll (x => x.Type == ETokenType.CommentLine || x.Type == ETokenType.EOL || x.Type == ETokenType.WhiteSpace);
     if (tokensToParse.Count > 0) {
         throw TokenAfterFunctionCall (currentLine, tokenToParse.Value, tokensToParse.First ().Value);
     } else {
         Command robotCommand;
         if (tokenToParse.Value == "moveForward()") {
             robotCommand = new Forward ();
         } else if (tokenToParse.Value == "rotateRight()") {
             robotCommand = new TurnRight ();
         } else {
             int quoteIndex = tokenToParse.Value.IndexOf ("'");
             String desiredObject = tokenToParse.Value.Substring (quoteIndex + 1);
             desiredObject = desiredObject.TrimEnd ("')".ToCharArray ());
             robotCommand = new PickUp (desiredObject);
         }
         robotCommand.LineNumber = tokenToParse.Position.Line;
         return robotCommand;
     }
 }
コード例 #2
0
        public void whileLoop4()
        {
            EOrientation orientation = EOrientation.South;
            Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy));
            robot.xPosition = 1;

            Solver canForward = new ConcreteInstruction (ECanInstructions.Forward);

            ICodeBlock command = new Forward();

            WhileLoop whileLoop = new WhileLoop (canForward);
            whileLoop.addChild (command);

            MainCode mainCode = new MainCode ();
            mainCode.addChild (whileLoop);

            mainCode.execute ();

            int actual = robot.yPosition;
            int expected = 2;
            Assert.AreEqual (expected, actual);
        }