diff --git a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt index 63c08605a..ce4fa873a 100644 --- a/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt +++ b/codeGeneration/src/prog8/compiler/target/cpu6502/codegen/AsmGen.kt @@ -1293,7 +1293,6 @@ $repeatLabel lda $counterVar if(choice.values==null) { // the else choice translate(choice.statements) - jmp(endLabel) } else { choiceBlocks.add(choiceLabel to choice.statements) for (cv in choice.values!!) { @@ -1313,10 +1312,11 @@ $repeatLabel lda $counterVar } } jmp(endLabel) - for(choiceBlock in choiceBlocks) { - out(choiceBlock.first) - translate(choiceBlock.second) - jmp(endLabel) + for(choiceBlock in choiceBlocks.withIndex()) { + out(choiceBlock.value.first) + translate(choiceBlock.value.second) + if(choiceBlock.index { + val lastChoiceValues = whenStatement.choices.lastOrNull()?.values + if(lastChoiceValues?.isNotEmpty()==true) { + val elseChoice = whenStatement.choices.indexOfFirst { it.values==null || it.values?.isEmpty()==true } + if(elseChoice>=0) + errors.err("else choice must be the last one", whenStatement.choices[elseChoice].position) + } + val choices = whenStatement.choiceValues(program).sortedBy { it.first?.first() ?: Int.MAX_VALUE } diff --git a/docs/source/todo.rst b/docs/source/todo.rst index d58f014c3..cf98e7348 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,8 +3,7 @@ TODO For next compiler release (7.5) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- fix: when statements generate unneeded branches to choice_end? - +... Blocked by an official Commander-x16 v39 release diff --git a/examples/test.p8 b/examples/test.p8 index 1796054c5..c7d33e8ea 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -4,14 +4,18 @@ main { sub start() { - ubyte @shared bb = @(cx16.r0) - } + ubyte @shared bb - sub count() -> ubyte { - repeat { - %asm {{ - rts - }} + when bb { + 0,1,2 -> { + bb ++ + } + 3 -> { + bb-- + } + else -> { + bb=0 + } } } }