diff --git a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt index 8f79ee685..14ad63aff 100644 --- a/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt +++ b/codeGenCpu6502/src/prog8/codegen/cpu6502/AsmGen.kt @@ -929,7 +929,7 @@ $repeatLabel""") } private fun translate(stmt: PtWhen) { - val endLabel = makeLabel("choice_end") + val endLabel = makeLabel("when_end") val choiceBlocks = mutableListOf>() val conditionDt = stmt.value.type if(conditionDt.isByte) @@ -939,23 +939,28 @@ $repeatLabel""") for(choiceNode in stmt.choices.children) { val choice = choiceNode as PtWhenChoice - val choiceLabel = makeLabel("choice") + var choiceLabel = makeLabel("choice") if(choice.isElse) { translate(choice.statements) } else { - choiceBlocks.add(choiceLabel to choice.statements) + if(choice.statements.children.isEmpty()) { + // no statements for this choice value, jump to the end immediately + choiceLabel = endLabel + } else { + choiceBlocks.add(choiceLabel to choice.statements) + } for (cv in choice.values.children) { val value = (cv as PtNumber).number.toInt() if(conditionDt.isByte) { out(" cmp #${value.toHex()} | beq $choiceLabel") } else { out(""" - cmp #<${value.toHex()} - bne + - cpy #>${value.toHex()} - beq $choiceLabel + cmp #<${value.toHex()} + bne + + cpy #>${value.toHex()} + beq $choiceLabel + - """) + """) } } } @@ -964,7 +969,7 @@ $repeatLabel""") for(choiceBlock in choiceBlocks.withIndex()) { out(choiceBlock.value.first) translate(choiceBlock.value.second) - if(choiceBlock.index bool @Pc { - ; -- convenience wrapper for screen_mode() to just set a new mode (and return success) +asmsub set_screen_mode(ubyte mode @A) clobbers(A,X,Y) { + ; -- convenience wrapper for screen_mode() to just set a new mode and ignore any return values %asm {{ clc jmp screen_mode diff --git a/docs/source/comparing.rst b/docs/source/comparing.rst index 1546e247f..d91161a40 100644 --- a/docs/source/comparing.rst +++ b/docs/source/comparing.rst @@ -21,6 +21,7 @@ The language - Ternary operator ``x ? value1 : value2`` is available in the form of an *if-expression*: ``if x value1 else value2`` - There's a Swift/Zig/Go style ``defer`` statement for delayed cleanup is available in the subroutine scope. - Qualified names are searched from within the top level namespace (so you have to provide the full qualified name). Unqualified names are locally scoped. +- A trailing comma is allowed optionally in array literals: [1,2,3,] is a valid array of values 1, 2 and 3. No linker diff --git a/docs/source/variables.rst b/docs/source/variables.rst index b687e1e06..91a8df8ec 100644 --- a/docs/source/variables.rst +++ b/docs/source/variables.rst @@ -264,7 +264,8 @@ Arrays ^^^^^^ Arrays can be created from a list of booleans, bytes, words, floats, or addresses of other variables (such as explicit address-of expressions, strings, or other array variables) - values in an array literal -always have to be constants. Here are some examples of arrays:: +always have to be constants. A trailing comma is allowed, sometimes this is easier when copying values +or when adding more stuff to the array later. Here are some examples of arrays:: byte[10] array ; array of 10 bytes, initially set to 0 byte[] array = [1, 2, 3, 4] ; initialize the array, size taken from value diff --git a/examples/cx16/interpolation.p8 b/examples/cx16/interpolation.p8 index 7e0009805..11d4e3b28 100644 --- a/examples/cx16/interpolation.p8 +++ b/examples/cx16/interpolation.p8 @@ -15,7 +15,7 @@ main { uword mx, my sub start() { - void cx16.set_screen_mode(3) + cx16.set_screen_mode(3) cx16.mouse_config2(1) sprites.set_mousepointer_hand() diff --git a/examples/test.p8 b/examples/test.p8 index 136932984..058f56f05 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -1,8 +1,22 @@ +%import textio +%zeropage basicsafe +%option no_sysinit + main { sub start() { - cx16.r0 = $aabb - cx16.r1 = $1122 - - goto cx16.r1+256 + ubyte[] array = [ + 11, + 22, + 33, + 44, + ] + for cx16.r0L in [1,2,3,4,] { + txt.print_ub(cx16.r0L) + txt.nl() + } + for cx16.r0L in array { + txt.print_ub(cx16.r0L) + txt.nl() + } } } diff --git a/parser/src/main/antlr/Prog8ANTLR.g4 b/parser/src/main/antlr/Prog8ANTLR.g4 index d8d5f77bf..ad022dbc6 100644 --- a/parser/src/main/antlr/Prog8ANTLR.g4 +++ b/parser/src/main/antlr/Prog8ANTLR.g4 @@ -256,7 +256,7 @@ integerliteral : intpart=(DEC_INTEGER | HEX_INTEGER | BIN_INTEGER) ; booleanliteral : 'true' | 'false' ; -arrayliteral : '[' EOL? expression (',' EOL? expression)* EOL? ']' ; // you can split the values over several lines +arrayliteral : '[' EOL? expression (',' EOL? expression)* ','? EOL? ']' ; // you can split the values over several lines, trailing comma allowed stringliteral : (encoding=NAME ':')? STRING ;