1
0
mirror of https://gitlab.com/camelot/kickc.git synced 2024-10-21 02:24:34 +00:00

Fixed problem with switch not working unless it is inside a loop. Closes #280

This commit is contained in:
jespergravgaard 2019-08-21 17:33:11 +02:00
parent dd5c06b9f9
commit 8bbddf5651
3 changed files with 25 additions and 3 deletions

View File

@ -974,12 +974,17 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
@Override @Override
public Object visitStmtSwitch(KickCParser.StmtSwitchContext ctx) { 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 // Create a block scope - to keep all statements of the loop inside it
BlockScope blockScope = getCurrentScope().addBlockScope(); BlockScope blockScope = getCurrentScope().addBlockScope();
scopeStack.push(blockScope); scopeStack.push(blockScope);
Loop switchLoop = new Loop(blockScope); Loop switchLoop = new Loop(blockScope);
if(containingLoop != null) {
switchLoop.setContinueLabel(containingLoop.getOrCreateContinueLabel()); switchLoop.setContinueLabel(containingLoop.getOrCreateContinueLabel());
}
loopStack.push(switchLoop); loopStack.push(switchLoop);
List<Comment> comments = ensureUnusedComments(getCommentsSymbol(ctx)); List<Comment> comments = ensureUnusedComments(getCommentsSymbol(ctx));
// TODO: Add comments to next stmt // TODO: Add comments to next stmt

View File

@ -86,6 +86,11 @@ public class TestPrograms {
// compileAndCompare("loophead-problem"); // compileAndCompare("loophead-problem");
//} //}
@Test
public void testSwitch2() throws IOException, URISyntaxException {
compileAndCompare("switch-2");
}
@Test @Test
public void testSwitch1() throws IOException, URISyntaxException { public void testSwitch1() throws IOException, URISyntaxException {
compileAndCompare("switch-1"); compileAndCompare("switch-1");

12
src/test/kc/switch-2.kc Normal file
View File

@ -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;
}