cleanup range expression doc

This commit is contained in:
Irmen de Jong 2023-08-09 20:05:23 +02:00
parent 7c0bde7310
commit dff1d9e4dd
4 changed files with 53 additions and 48 deletions

View File

@ -994,7 +994,7 @@ main {
; curly braces without newline
sub start () { foo() derp() other() }
sub foo() { cx16.r0++ }
asmsub derp() { %asm {{ nop }} %ir {{ loadr.b r0,1 }} }
asmsub derp() { %asm {{ nop }} %ir {{ load.b r0,1 }} }
; curly braces on next line
sub other()
@ -1017,7 +1017,7 @@ main {
{
%ir
{{
loadr.b r0,1
load.b r0,1
}}
}
}"""

View File

@ -438,6 +438,8 @@ The following names are reserved, they have a special meaning::
true false ; boolean values 1 and 0
.. _range-expression:
Range expression
^^^^^^^^^^^^^^^^
@ -449,9 +451,23 @@ from the starting value to (and including) the ending value::
You an provide a step value if you need something else than the default increment which is one (or,
in case of downto, a decrement of one). Because a step of minus one is so common you can just use
the downto variant to avoid having to specify the step as well.
the downto variant to avoid having to specify the step as well::
If used in the place of a literal value, it expands into the actual array of integer values::
0 to 7 ; range of values 0, 1, 2, 3, 4, 5, 6, 7
20 downto 10 step -3 ; range of values 20, 17, 14, 11
aa = 5
xx = 10
aa to xx ; range of 5, 6, 7, 8, 9, 10
byte[] array = 10 to 13 ; sets the array to [10, 11, 12, 13]
for i in 0 to 127 {
; i loops 0, 1, 2, ... 127
}
Range expressions are most often used in for loops, but can be also be used to create array initialization values::
byte[] array = 100 to 199 ; initialize array with [100, 101, ..., 198, 199]
@ -543,21 +559,10 @@ logical: ``not`` ``and`` ``or`` ``xor``
all operands from these logical expressions, even when one of them already determines the outcome!
If you don't want this to happen, you have to split and nest the if-statements yourself.
range creation: ``to``
range creation: ``to``, ``downto``
Creates a range of values from the LHS value to the RHS value, inclusive.
These are mainly used in for loops to set the loop range. Example::
0 to 7 ; range of values 0, 1, 2, 3, 4, 5, 6, 7 (constant)
aa = 5
aa = 10
aa to xx ; range of 5, 6, 7, 8, 9, 10
byte[] array = 10 to 13 ; sets the array to [1, 2, 3, 4]
for i in 0 to 127 {
; i loops 0, 1, 2, ... 127
}
These are mainly used in for loops to set the loop range.
See :ref:`range-expression` for details.
containment check: ``in``
Tests if a value is present in a list of values, which can be a string or an array.

View File

@ -1,6 +1,8 @@
TODO
====
- fix range expression problems in VM/IR (see test.p8)
- investigate McCarthy evaluation again? this may also reduce code size perhaps for things like if a>4 or a<2 ....
- IR: reduce the number of branch instructions such as BEQ, BEQR, etc (gradually), replace with CMP(I) + status branch instruction
- IR: reduce amount of CMP/CMPI after instructions that set the status bits correctly (LOADs? INC? etc), but only after setting the status bits is verified!

View File

@ -1,31 +1,29 @@
%zeropage dontuse
%import textio
%zeropage basicsafe
main {
sub start () { foo() derp() other() }
sub foo() { cx16.r0++ }
asmsub derp() { %asm {{ nop }} %ir {{ loadr.b r0,1 }} }
sub start () {
ubyte[4] array = 10 to 13
ubyte[4] array2 = 10 to 20 step 3
ubyte[4] array3 = 20 downto 10 step 3 ; TODO fix error about empty range expression
sub other()
{
cx16.r0++
asmother()
asmir()
ubyte xx
for xx in 10 to 20 step 3 { ; TODO fix IR/VM code that wraps around instead of stopping at 19
txt.print_ub(xx)
txt.spc()
}
txt.nl()
asmsub asmother()
{
%asm
{{
txa
tay
}}
for xx in 20 downto 10 step -3 { ; TODO fix IR/VM code that wraps around instead of stopping at 11
txt.print_ub(xx)
txt.spc()
}
txt.nl()
asmsub asmir()
{
%ir
{{
loadr.b r0,1
}}
byte bb
for bb in 10 downto -10 step -3 { ; TODO fix IR/VM code that wraps around instead of stopping at -8
txt.print_b(bb)
txt.spc()
}
}
}