diff --git a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt index 118f5f5e2..96c977d1e 100644 --- a/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt +++ b/compilerAst/src/prog8/ast/antlr/Antlr2Kotlin.kt @@ -545,7 +545,7 @@ private fun Prog8ANTLRParser.BranchconditionContext.toAst() = BranchCondition.va ) private fun Prog8ANTLRParser.ForloopContext.toAst(): ForLoop { - val loopvar = identifier().toAst() + val loopvar = scoped_identifier().toAst() val iterable = expression()!!.toAst() val scope = if(statement()!=null) diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 6fc7cb6bd..a0ce43fb4 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -433,7 +433,7 @@ Loops ----- The *for*-loop is used to let a variable iterate over a range of values. Iteration is done in steps of 1, but you can change this. -The loop variable must be declared as byte or word earlier so you can reuse it for multiple occasions. +The loop variable must be declared separately as byte or word earlier, so that you can reuse it for multiple occasions. Iterating with a floating point variable is not supported. If you want to loop over a floating-point array, use a loop with an integer index variable instead. The *while*-loop is used to repeat a piece of code while a certain condition is still true. diff --git a/docs/source/syntaxreference.rst b/docs/source/syntaxreference.rst index b24862de1..ef3a0fe6f 100644 --- a/docs/source/syntaxreference.rst +++ b/docs/source/syntaxreference.rst @@ -643,8 +643,7 @@ Loops for loop ^^^^^^^^ -The loop variable must be a byte or word variable, -and must be defined first in the local scope of the for loop. +The loop variable must be a byte or word variable, and it must be defined separately first. The expression that you loop over can be anything that supports iteration (such as ranges like ``0 to 100``, array variables and strings) *except* floating-point arrays (because a floating-point loop variable is not supported). diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 5dbf3feb2..18a337e15 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -3,7 +3,6 @@ TODO For next compiler release (7.5) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -- allow cx16.rX as loop variable in forloops (now requires unqualified identifiername) ... diff --git a/examples/test.p8 b/examples/test.p8 index bca37af23..b4ed85812 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -20,10 +20,14 @@ main { txt.print_ub(main.derp.xx) txt.spc() } + + derp() txt.nl() } sub derp() { ubyte xx + + xx++ } } diff --git a/parser/antlr/Prog8ANTLR.g4 b/parser/antlr/Prog8ANTLR.g4 index 9308f125e..af773671e 100644 --- a/parser/antlr/Prog8ANTLR.g4 +++ b/parser/antlr/Prog8ANTLR.g4 @@ -283,7 +283,7 @@ branch_stmt : branchcondition EOL? (statement | statement_block) EOL? else_part? branchcondition: 'if_cs' | 'if_cc' | 'if_eq' | 'if_z' | 'if_ne' | 'if_nz' | 'if_pl' | 'if_pos' | 'if_mi' | 'if_neg' | 'if_vs' | 'if_vc' ; -forloop : 'for' identifier 'in' expression EOL? (statement | statement_block) ; +forloop : 'for' scoped_identifier 'in' expression EOL? (statement | statement_block) ; whileloop: 'while' expression EOL? (statement | statement_block) ;