Skip to content

Commit

Permalink
Add compilation exception if trying to finish a non-existing loop
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Dec 10, 2016
1 parent f74ebb4 commit e29867a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/main/groovy/net/zomis/brainf/model/ast/tree/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ public SyntaxTree parse(List<Token> tokens) {
}
if (bft.command == BrainFCommand.END_WHILE) {
SyntaxTree loopSyntax = depth.pop();
if (depth.isEmpty()) {
throw new BrainfuckCompilationException("No more loops to finish.");
}
SyntaxTree current = depth.peek();
current.getTokens().addAll(inner.getTokens());
current.syntax.add(loopSyntax);
Expand Down
4 changes: 1 addition & 3 deletions src/test/groovy/net/zomis/brainf/BrainTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,9 @@ public class BrainTest extends BrainfuckTest {
assert output.toString() == "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
}

@Test
@Test(expected = BrainfuckCompilationException)
public void unbalanced1() {
useCode("++[->+<] ]")
analyze(new WhileLoopAnalysis())
assert !analyze.get(WhileLoopAnalysis).bracketsMatching
}

@Test
Expand Down

0 comments on commit e29867a

Please sign in to comment.