From 8bbddf56518d74b8add2f906de62ee98f8b0f09c Mon Sep 17 00:00:00 2001 From: jespergravgaard Date: Wed, 21 Aug 2019 17:33:11 +0200 Subject: [PATCH] Fixed problem with switch not working unless it is inside a loop. Closes #280 --- .../kickc/passes/Pass0GenerateStatementSequence.java | 11 ++++++++--- .../java/dk/camelot64/kickc/test/TestPrograms.java | 5 +++++ src/test/kc/switch-2.kc | 12 ++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) create mode 100644 src/test/kc/switch-2.kc diff --git a/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java b/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java index 2569ead26..ce673fd9f 100644 --- a/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java +++ b/src/main/java/dk/camelot64/kickc/passes/Pass0GenerateStatementSequence.java @@ -974,12 +974,17 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor { @Override public Object visitStmtSwitch(KickCParser.StmtSwitchContext ctx) { - Loop containingLoop = loopStack.peek(); + Loop containingLoop = null; + if(!loopStack.isEmpty()) { + containingLoop = loopStack.peek(); + } // Create a block scope - to keep all statements of the loop inside it BlockScope blockScope = getCurrentScope().addBlockScope(); scopeStack.push(blockScope); Loop switchLoop = new Loop(blockScope); - switchLoop.setContinueLabel(containingLoop.getOrCreateContinueLabel()); + if(containingLoop != null) { + switchLoop.setContinueLabel(containingLoop.getOrCreateContinueLabel()); + } loopStack.push(switchLoop); List comments = ensureUnusedComments(getCommentsSymbol(ctx)); // TODO: Add comments to next stmt @@ -1768,7 +1773,7 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor { charText = charText.substring(1, charText.length() - 1); char constChar = ConstantChar.charEscapeToAscii(charText); return new ConstantChar(constChar, currentEncoding); - } catch (CompileError e) { + } catch(CompileError e) { // Rethrow adding source location throw new CompileError(e.getMessage(), new StatementSource(ctx)); } diff --git a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java index 0d3dbfdad..e513f7d90 100644 --- a/src/test/java/dk/camelot64/kickc/test/TestPrograms.java +++ b/src/test/java/dk/camelot64/kickc/test/TestPrograms.java @@ -86,6 +86,11 @@ public class TestPrograms { // compileAndCompare("loophead-problem"); //} + @Test + public void testSwitch2() throws IOException, URISyntaxException { + compileAndCompare("switch-2"); + } + @Test public void testSwitch1() throws IOException, URISyntaxException { compileAndCompare("switch-1"); diff --git a/src/test/kc/switch-2.kc b/src/test/kc/switch-2.kc new file mode 100644 index 000000000..de5572402 --- /dev/null +++ b/src/test/kc/switch-2.kc @@ -0,0 +1,12 @@ +// Tests simple switch()-statement - including a continue statement for the enclosing loop + +void main() { + char* SCREEN = 0x0400; + char b=0; + char v64 = 0; + switch(v64){ + case 0: b = 1; break; + default: + } + SCREEN[0] = b; +} \ No newline at end of file