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:
parent
dd5c06b9f9
commit
8bbddf5651
@ -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
|
||||||
|
@ -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
12
src/test/kc/switch-2.kc
Normal 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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user