fix: allow scoped variables such as cx16.rX as loop variable in forloops

This commit is contained in:
Irmen de Jong 2021-12-10 14:59:04 +01:00
parent e342311bef
commit 33061aaa0d
6 changed files with 8 additions and 6 deletions

View File

@ -545,7 +545,7 @@ private fun Prog8ANTLRParser.BranchconditionContext.toAst() = BranchCondition.va
) )
private fun Prog8ANTLRParser.ForloopContext.toAst(): ForLoop { private fun Prog8ANTLRParser.ForloopContext.toAst(): ForLoop {
val loopvar = identifier().toAst() val loopvar = scoped_identifier().toAst()
val iterable = expression()!!.toAst() val iterable = expression()!!.toAst()
val scope = val scope =
if(statement()!=null) if(statement()!=null)

View File

@ -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 *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. 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. The *while*-loop is used to repeat a piece of code while a certain condition is still true.

View File

@ -643,8 +643,7 @@ Loops
for loop for loop
^^^^^^^^ ^^^^^^^^
The loop variable must be a byte or word variable, The loop variable must be a byte or word variable, and it must be defined separately first.
and must be defined first in the local scope of the for loop.
The expression that you loop over can be anything that supports iteration (such as ranges like ``0 to 100``, 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). array variables and strings) *except* floating-point arrays (because a floating-point loop variable is not supported).

View File

@ -3,7 +3,6 @@ TODO
For next compiler release (7.5) For next compiler release (7.5)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
- allow cx16.rX as loop variable in forloops (now requires unqualified identifiername)
... ...

View File

@ -20,10 +20,14 @@ main {
txt.print_ub(main.derp.xx) txt.print_ub(main.derp.xx)
txt.spc() txt.spc()
} }
derp()
txt.nl() txt.nl()
} }
sub derp() { sub derp() {
ubyte xx ubyte xx
xx++
} }
} }

View File

@ -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' ; 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) ; whileloop: 'while' expression EOL? (statement | statement_block) ;