mirror of
https://github.com/irmen/prog8.git
synced 2024-11-18 19:12:44 +00:00
error when 'else' choice in when statemetn isn't the last one, also generate slightly better code for when statements
This commit is contained in:
parent
edffe92a24
commit
ff69da3fa2
@ -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<choiceBlocks.size-1)
|
||||
jmp(endLabel)
|
||||
}
|
||||
out(endLabel)
|
||||
}
|
||||
|
@ -253,6 +253,13 @@ internal class StatementReorderer(val program: Program,
|
||||
}
|
||||
|
||||
override fun after(whenStatement: WhenStatement, parent: Node): Iterable<IAstModification> {
|
||||
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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user