From e29867a3f53770af96d2ee5061ad1f6e1244a89e Mon Sep 17 00:00:00 2001 From: Simon Forsberg Date: Sat, 10 Dec 2016 18:49:22 +0100 Subject: [PATCH] Add compilation exception if trying to finish a non-existing loop --- src/main/groovy/net/zomis/brainf/model/ast/tree/Parser.java | 3 +++ src/test/groovy/net/zomis/brainf/BrainTest.groovy | 4 +--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/groovy/net/zomis/brainf/model/ast/tree/Parser.java b/src/main/groovy/net/zomis/brainf/model/ast/tree/Parser.java index a823333..21f9d17 100644 --- a/src/main/groovy/net/zomis/brainf/model/ast/tree/Parser.java +++ b/src/main/groovy/net/zomis/brainf/model/ast/tree/Parser.java @@ -55,6 +55,9 @@ public SyntaxTree parse(List 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); diff --git a/src/test/groovy/net/zomis/brainf/BrainTest.groovy b/src/test/groovy/net/zomis/brainf/BrainTest.groovy index 9ab6d00..677a8e6 100644 --- a/src/test/groovy/net/zomis/brainf/BrainTest.groovy +++ b/src/test/groovy/net/zomis/brainf/BrainTest.groovy @@ -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