From dff1d9e4ddfe98ed77f29e882c9290c8e4e5cebf Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Wed, 9 Aug 2023 20:05:23 +0200 Subject: [PATCH] cleanup range expression doc --- compiler/test/ast/TestProg8Parser.kt | 4 +-- docs/source/syntaxreference.rst | 49 +++++++++++++++------------- docs/source/todo.rst | 2 ++ examples/test.p8 | 46 +++++++++++++------------- 4 files changed, 53 insertions(+), 48 deletions(-) diff --git a/compiler/test/ast/TestProg8Parser.kt b/compiler/test/ast/TestProg8Parser.kt index 3b775fd18..69fe2bc1d 100644 --- a/compiler/test/ast/TestProg8Parser.kt +++ b/compiler/test/ast/TestProg8Parser.kt @@ -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 }} } }""" diff --git a/docs/source/syntaxreference.rst b/docs/source/syntaxreference.rst index 0d70806e3..5ead81555 100644 --- a/docs/source/syntaxreference.rst +++ b/docs/source/syntaxreference.rst @@ -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] @@ -516,15 +532,15 @@ assignment: ``=`` Note that an assignment sometimes is not possible or supported. augmented assignment: ``+=`` ``-=`` ``*=`` ``/=`` ``&=`` ``|=`` ``^=`` ``<<=`` ``>>=`` - This is syntactic sugar; ``aa += xx`` is equivalent to ``aa = aa + xx`` + This is syntactic sugar; ``aa += xx`` is equivalent to ``aa = aa + xx`` postfix increment and decrement: ``++`` ``--`` - Syntactic sugar; ``aa++`` is equivalent to ``aa = aa + 1``, and ``aa--`` is equivalent to ``aa = aa - 1``. - Because these operations are so common, we have these short forms. + Syntactic sugar; ``aa++`` is equivalent to ``aa = aa + 1``, and ``aa--`` is equivalent to ``aa = aa - 1``. + Because these operations are so common, we have these short forms. comparison: ``!=`` ``<`` ``>`` ``<=`` ``>=`` - Equality, Inequality, Less-than, Greater-than, Less-or-Equal-than, Greater-or-Equal-than comparisons. - The result is a 'boolean' value 'true' or 'false' (which in reality is just a byte value of 1 or 0). + Equality, Inequality, Less-than, Greater-than, Less-or-Equal-than, Greater-or-Equal-than comparisons. + The result is a 'boolean' value 'true' or 'false' (which in reality is just a byte value of 1 or 0). logical: ``not`` ``and`` ``or`` ``xor`` These operators are the usual logical operations that are part of a logical expression to reason @@ -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`` - 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 - } +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. + 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. diff --git a/docs/source/todo.rst b/docs/source/todo.rst index 90da1f90c..3f9f343f5 100644 --- a/docs/source/todo.rst +++ b/docs/source/todo.rst @@ -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! diff --git a/examples/test.p8 b/examples/test.p8 index 1e4a952cc..2615e9ace 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -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() + } } }