mirror of
https://gitlab.com/camelot/kickc.git
synced 2025-01-18 22:30:05 +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
|
||||
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<Comment> comments = ensureUnusedComments(getCommentsSymbol(ctx));
|
||||
// TODO: Add comments to next stmt
|
||||
@ -1768,7 +1773,7 @@ public class Pass0GenerateStatementSequence extends KickCBaseVisitor<Object> {
|
||||
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));
|
||||
}
|
||||
|
@ -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");
|
||||
|
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…
x
Reference in New Issue
Block a user