/// Author: Max Hamulyak /// Date: 08-06-2015 /// <summary> /// Function is used to parse a WhileLoop, can be overriden in base class. /// </summary> /// <returns>The while.</returns> /// <param name="tokensToParse">Tokens to parse.</param> /// <param name="currentLine">Current line.</param> protected virtual WhileLoop ParseWhile(List<Token> tokensToParse, int currentLine) { List<Token> tokensAtCurrentLine = tokensToParse.Where (x => x.Position.Line == currentLine).ToList(); tokensToParse.RemoveAll (x => x.Position.Line == currentLine); ICondition c = ParseWhileLoopHeader (tokensAtCurrentLine, currentLine); WhileLoop whileLoop = new WhileLoop (c as Solver); 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.WHILE); RemoveTokensAtLine (tokensToParse, last + incrementIndex); List<ICodeBlock> childeren = BuildCommandBlok (childTokens, currentLine + 1, last + 1); if (childeren.Count > 0) { foreach (var item in childeren) { whileLoop.addChild (item); } whileLoop.LineNumber = currentLine; return whileLoop; } else { throw EmptyCodeBlock (currentLine, ETokenType.WHILE); } } else { throw EmptyCodeBlock (currentLine, ETokenType.WHILE); } }
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); }
public void whileLoop2() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); Solver conditions = new ConcreteInstruction (ECanInstructions.Backward); WhileLoop whileLoop = new WhileLoop (conditions); bool actual = whileLoop.execute (null); bool expected = true; Assert.AreEqual (expected, actual); }
public void whileLoop3() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); Solver concreteInstruction1 = new ConcreteInstruction (ECanInstructions.Forward); //A Solver concreteInstruction2 = new ConcreteInstruction (ECanInstructions.Forward); //A Solver notInstruction = new ConditionCombo (concreteInstruction1, ELogicOperators.Not); //!A Solver combo = new ConditionCombo (notInstruction, concreteInstruction2, ELogicOperators.And); // !A && A ICodeBlock command = new TurnRight (); WhileLoop whileLoop = new WhileLoop (combo); whileLoop.addChild (command); whileLoop.execute (null); EOrientation actual = robot.orientationEnum; EOrientation expected = EOrientation.East; Assert.AreEqual (expected, actual); }
public void whileLoop1() { EOrientation orientation = EOrientation.East; Robot robot = Robot.Create (orientation, new Map(EDifficulty.Easy)); robot.xPosition = 1; robot.yPosition = 1; Solver condition1 = new ConcreteInstruction (ECanInstructions.Right); Solver condition2 = new ConcreteInstruction (ECanInstructions.Forward); Solver combo = new ConditionCombo (condition1, condition2, ELogicOperators.Or); WhileLoop whileLoop = new WhileLoop (combo); bool actual = whileLoop.execute (null); bool expected = false; Assert.AreEqual (expected, actual); }