From efef205fcfb0989f0b714813c94e10292921b9ac Mon Sep 17 00:00:00 2001 From: Irmen de Jong Date: Mon, 23 Mar 2020 01:24:41 +0100 Subject: [PATCH] doc --- docs/source/programming.rst | 18 +++++++++------- examples/test.p8 | 42 ++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/docs/source/programming.rst b/docs/source/programming.rst index 71a29de45..4f441c179 100644 --- a/docs/source/programming.rst +++ b/docs/source/programming.rst @@ -135,14 +135,18 @@ Scopes are created using either of these two statements: - blocks (top-level named scope) - subroutines (nested named scope) -.. note:: - Unlike many other programming languages, a new scope is *not* created inside - for, while and repeat statements, nor for the if statement and branching conditionals. - This can be a bit restrictive because as a programmer you have to think harder about what variables you - want to use inside a subroutine. But it is done precisely for this reason: memory in prog8's +.. important:: + Unlike most other programming languages, a new scope is *not* created inside + for, while and repeat statements, the if statement, and the branching conditionals. + These all share the same scope from the subroutine they're defined in. + You can define variables in these blocks, but these will be treated as if they + were defined in the subroutine instead. + This can seem a bit restrictive because you have to think harder about what variables you + want to use inside the subroutine, to avoid clashes. + But this decision was made for a good reason: memory in prog8's target systems is usually very limited and it would be a waste to allocate a lot of variables. The prog8 compiler is not yet advanced enough to be able to share or overlap - variables intelligently. So for now that is something the programmer has to think about. + variables intelligently. So for now that is something you have to think about yourself. Program Start and Entry Point @@ -174,7 +178,7 @@ Variables and values -------------------- Variables are named values that can change during the execution of the program. -They can be defined inside any scope (blocks, subroutines, for loops, etc.) See :ref:`Scopes `. +They can be defined inside any scope (blocks, subroutines etc.) See :ref:`Scopes `. When declaring a numeric variable it is possible to specify the initial value, if you don't want it to be zero. For other data types it is required to specify that initial value it should get. Values will usually be part of an expression or assignment statement:: diff --git a/examples/test.p8 b/examples/test.p8 index 0e9fe783c..ee023f394 100644 --- a/examples/test.p8 +++ b/examples/test.p8 @@ -5,29 +5,33 @@ main { - sub subje(ubyte arg1) { - ubyte yy=33 + sub subje() { + ubyte a1 + ubyte a2 ubyte zz - ubyte[5] array1 = [1,2,3,4,5] - - c64scr.print_ub(arg1) - c64.CHROUT(',') - c64scr.print_ub(yy) - c64.CHROUT(',') - c64scr.print_ub(zz) - c64.CHROUT('\n') - yy++ - A=zz - Y=array1[2] + for a1 in 1 to 3 { + ubyte qq = 100 ; TODO should be inited here, and not in the subroutine. + for a2 in 1 to 3 { + ubyte zz = 100 ; TODO should be inited here, and not in the subroutine. + c64scr.print_ub(a1) + c64.CHROUT(',') + c64scr.print_ub(a2) + c64.CHROUT(',') + c64scr.print_ub(qq) ; TODO qq shoud be 100..103 repeated three times... + c64.CHROUT(',') + c64scr.print_ub(zz) ; TODO zz should always be 100... + c64.CHROUT('\n') + qq++ + zz++ + } + } } sub start() { - ubyte zz2 - A=zz2 - subje(111) - subje(112) - subje(113) - subje(114) + subje() + c64.CHROUT('\n') + subje() + c64.CHROUT('\n') return ; ubyte ub1