• Reverse Stack Oder, simple but... ( gForth)

    From SpainHackForth@21:1/5 to All on Thu Aug 24 04:15:45 2023
    Hi all, trying to reverse order, I'm working of the stack although I will be using a file and buffer for the end result, I created what seem like the logical word defines as:

    : REVERSE ( i*x i -- i*y ) 0 DO I DUP ROLL . LOOP ;

    Here is the interactive successions ( or is that frustration :D )
    At fist I was missing a character, then it all was weird...

    Thoughts... I leave the interactive session for the enjoyment of the gurus! :D .S <0> ok
    0 1 2 3 4 5 6 7 8 9 ok
    .S <10> 1 2 3 4 5 6 7 8 9 ok
    : REVERSE ( i*x i -- i*y ) 0 DO I DUP ROLL . LOOP ; redefined REVERSE ok REVERSE 0 8 7 6 5 4 3 2 1 ok
    .s <9> 0 1 2 3 4 5 6 7 8 ok
    : REVERSE ( i*x i -- i*y ) 0 DO I DUP ROLL . DROP LOOP ; redefined REVERSE ok abort
    :148: Aborted
    abort<<<
    Backtrace:
    $7F8B79A03A78 throw
    .S <0> ok
    0 1 2 3 4 5 6 7 8 9 ok
    .S <10> 1 2 3 4 5 6 7 8 9 ok
    REVERSE 0 7 5 3 1
    :152: Stack underflow
    REVERSE<<<
    Backtrace:
    $7F8B79A031D0 swap
    $7F8B79A031C0 roll
    $2
    $7F8B79A031C0 roll
    $4
    $7F8B79A031C0 roll
    $6
    $7F8B79A031C0 roll
    $5
    $7F8B79A57178 roll
    $5
    $9
    : REVERSE ( i*x i -- i*y ) 0 DO I ROLL . LOOP ; redefined REVERSE ok
    .S <0> ok
    0 1 2 3 4 5 6 7 8 9 ok
    REVERSE 8 6 4 2 0
    :156: Stack underflow
    REVERSE<<<
    Backtrace:
    $7F8B79A031A0 swap
    $7F8B79A031C0 roll
    $1
    $7F8B79A031C0 roll
    $3
    $7F8B79A031C0 roll
    $5
    $7F8B79A031C0 roll
    $7
    $7F8B79A57208 roll
    $5
    $9
    .S <0> ok
    ok
    ok
    ok
    0 1 2 3 4 5 6 7 8 9 ok
    REVERSE 8 6 4 2 0
    :162: Stack underflow
    REVERSE<<<
    Backtrace:
    $7F8B79A031A0 swap
    $7F8B79A031C0 roll
    $1
    $7F8B79A031C0 roll
    $3
    $7F8B79A031C0 roll
    $5
    $7F8B79A031C0 roll
    $7
    $7F8B79A57208 roll
    $5
    $9
    : REVERSE ( i*x i -- i*y ) 0 DO I DUP ROLL . DROP LOOP ; redefined REVERSE ok
    ok
    ok
    0 1 2 3 4 5 6 7 8 9 ok
    REVERSE 0 7 5 3 1
    :167: Stack underflow
    REVERSE<<<
    Backtrace:
    $7F8B79A031D0 swap
    $7F8B79A031C0 roll
    $2
    $7F8B79A031C0 roll
    $4
    $7F8B79A031C0 roll
    $6
    $7F8B79A031C0 roll
    $5
    $7F8B79A57298 roll
    $5
    $9
    ok
    ok
    ok
    ok
    bye

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From goblinrieur@gmail.com@21:1/5 to All on Thu Aug 24 04:48:48 2023
    Le jeudi 24 août 2023 à 13:15:48 UTC+2, SpainHackForth a écrit :
    Hi all, trying to reverse order, I'm working of the stack although I will be using a file and buffer for the end result, I created what seem like the logical word defines as:

    : REVERSE ( i*x i -- i*y ) 0 DO I DUP ROLL . LOOP ;

    Here is the interactive successions ( or is that frustration :D )
    At fist I was missing a character, then it all was weird...

    Thoughts... I leave the interactive session for the enjoyment of the gurus! :D
    .S <0> ok
    0 1 2 3 4 5 6 7 8 9 ok
    .S <10> 1 2 3 4 5 6 7 8 9 ok
    : REVERSE ( i*x i -- i*y ) 0 DO I DUP ROLL . LOOP ; redefined REVERSE ok REVERSE 0 8 7 6 5 4 3 2 1 ok
    .s <9> 0 1 2 3 4 5 6 7 8 ok
    : REVERSE ( i*x i -- i*y ) 0 DO I DUP ROLL . DROP LOOP ; redefined REVERSE ok
    abort
    :148: Aborted
    abort<<<
    Backtrace:
    $7F8B79A03A78 throw
    .S <0> ok
    0 1 2 3 4 5 6 7 8 9 ok
    .S <10> 1 2 3 4 5 6 7 8 9 ok
    REVERSE 0 7 5 3 1
    :152: Stack underflow
    REVERSE<<<
    Backtrace:
    $7F8B79A031D0 swap
    $7F8B79A031C0 roll
    $2
    $7F8B79A031C0 roll
    $4
    $7F8B79A031C0 roll
    $6
    $7F8B79A031C0 roll
    $5
    $7F8B79A57178 roll
    $5
    $9
    : REVERSE ( i*x i -- i*y ) 0 DO I ROLL . LOOP ; redefined REVERSE ok
    .S <0> ok
    0 1 2 3 4 5 6 7 8 9 ok
    REVERSE 8 6 4 2 0
    :156: Stack underflow
    REVERSE<<<
    Backtrace:
    $7F8B79A031A0 swap
    $7F8B79A031C0 roll
    $1
    $7F8B79A031C0 roll
    $3
    $7F8B79A031C0 roll
    $5
    $7F8B79A031C0 roll
    $7
    $7F8B79A57208 roll
    $5
    $9
    .S <0> ok
    ok
    ok
    ok
    0 1 2 3 4 5 6 7 8 9 ok
    REVERSE 8 6 4 2 0
    :162: Stack underflow
    REVERSE<<<
    Backtrace:
    $7F8B79A031A0 swap
    $7F8B79A031C0 roll
    $1
    $7F8B79A031C0 roll
    $3
    $7F8B79A031C0 roll
    $5
    $7F8B79A031C0 roll
    $7
    $7F8B79A57208 roll
    $5
    $9
    : REVERSE ( i*x i -- i*y ) 0 DO I DUP ROLL . DROP LOOP ; redefined REVERSE ok
    ok
    ok
    0 1 2 3 4 5 6 7 8 9 ok
    REVERSE 0 7 5 3 1
    :167: Stack underflow
    REVERSE<<<
    Backtrace:
    $7F8B79A031D0 swap
    $7F8B79A031C0 roll
    $2
    $7F8B79A031C0 roll
    $4
    $7F8B79A031C0 roll
    $6
    $7F8B79A031C0 roll
    $5
    $7F8B79A57298 roll
    $5
    $9
    ok
    ok
    ok
    ok
    bye


    I think you just forget to check the stack depth

    clearstack ok
    ok
    ok
    0 1 2 3 4 5 6 7 8 9 \ last is 9 ok 10
    . 9 ok 9
    : rev depth 0 do I roll loop ; ok 9
    rev ok 9
    \ should go 0 1 2 .. ok 9
    . . . . 0 1 2 3 ok 5

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From SpainHackForth@21:1/5 to gobli...@gmail.com on Thu Aug 24 05:32:35 2023
    On Thursday, August 24, 2023 at 1:48:50 PM UTC+2, gobli...@gmail.com wrote:

    clearstack ok
    ok
    ok
    0 1 2 3 4 5 6 7 8 9 \ last is 9 ok 10
    . 9 ok 9
    : rev depth 0 do I roll loop ; ok 9
    rev ok 9
    \ should go 0 1 2 .. ok 9
    . . . . 0 1 2 3 ok 5
    Well,

    I was trying to do something simple on the stack, I would use a buffer like: VARIABLE REVERSE-BUFFER

    : REVERSE-STACK ( -- )
    DEPTH REVERSE-BUFFER ALLOT \ Allocate memory for the buffer

    DEPTH 0 DO
    I -1 * REVERSE-BUFFER + !
    LOOP ;

    : USE-REVERSED-ORDER ( -- )
    REVERSE-BUFFER DEPTH 0 DO
    I REVERSE-BUFFER + @ .
    LOOP ;

    I've not tried that, just meta from what I'm considering... Just the different results even though the stack was clear is what is troubling me.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Zbig@21:1/5 to All on Thu Aug 24 06:32:46 2023
    : revs
    depth >r
    depth cells allot
    depth 0 do here i cells - ! loop
    0 do here i cells - @ loop
    depth cells negate allot
    ;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Zbig@21:1/5 to All on Thu Aug 24 07:05:41 2023
    : revs
    depth >r
    depth cells allot
    depth 0 do here i cells - ! loop
    0 do here i cells - @ loop
    depth cells negate allot
    ;

    Oh, but actually why allocating anything for that brief moment?
    It can be shorter:

    : reverse-stack ( -- )
    depth dup >r 0 do here i cells + ! loop
    r> 0 do here i cells + @ loop ;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From SpainHackForth@21:1/5 to Zbig on Thu Aug 24 08:29:35 2023
    On Thursday, August 24, 2023 at 4:05:43 PM UTC+2, Zbig wrote:
    : revs
    depth >r
    depth cells allot
    depth 0 do here i cells - ! loop
    0 do here i cells - @ loop
    depth cells negate allot
    ;
    Oh, but actually why allocating anything for that brief moment?
    It can be shorter:

    : reverse-stack ( -- )
    depth dup >r 0 do here i cells + ! loop
    0 do here i cells + @ loop ;
    I just tried it, it is the same as my initial issue, it misses the "9" on the stack....

    : reverse-stack ( -- ) compiled
    depth dup >r 0 do here i cells + ! loop compiled
    0 do here i cells + @ loop ; ok
    0 1 2 3 4 5 6 7 8 9 ok
    reverse-stack ok
    .S <10> 8 7 6 5 4 3 2 1 0 ok

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hans Bezemer@21:1/5 to All on Thu Aug 24 09:02:52 2023
    On Thursday, August 24, 2023 at 5:45:04 PM UTC+2, Zbig wrote:
    If your compiler uses the return stack as an actual return stack, you can try this:

    Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
    Type `help' for basic help
    : reverse begin dup while 1- swap r> swap >r >r repeat drop ; ok
    : test 1 2 3 4 5 6 6 reverse r> . r> . r> . r> . r> . r> . ; ok
    ok
    test 1 2 3 4 5 6 ok

    Hans Bezemer

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Zbig@21:1/5 to All on Thu Aug 24 08:45:02 2023
    czwartek, 24 sierpnia 2023 o 17:29:38 UTC+2 SpainHackForth napisał(a):
    On Thursday, August 24, 2023 at 4:05:43 PM UTC+2, Zbig wrote:
    : revs
    depth >r
    depth cells allot
    depth 0 do here i cells - ! loop
    0 do here i cells - @ loop
    depth cells negate allot
    ;
    Oh, but actually why allocating anything for that brief moment?
    It can be shorter:

    : reverse-stack ( -- )
    depth dup >r 0 do here i cells + ! loop
    0 do here i cells + @ loop ;
    I just tried it, it is the same as my initial issue, it misses the "9" on the stack....

    : reverse-stack ( -- ) compiled
    depth dup >r 0 do here i cells + ! loop compiled
    0 do here i cells + @ loop ; ok
    0 1 2 3 4 5 6 7 8 9 ok
    reverse-stack ok
    .S <10> 8 7 6 5 4 3 2 1 0 ok

    It doesn't missing anything; Gforth displays just the nine
    „highest” values from the stack. You didn't notice „ten” in
    triangles directly before value list? It displays the stack's depth.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Zbig@21:1/5 to All on Thu Aug 24 09:06:52 2023
    czwartek, 24 sierpnia 2023 o 18:02:54 UTC+2 Hans Bezemer napisał(a):
    On Thursday, August 24, 2023 at 5:45:04 PM UTC+2, Zbig wrote:
    If your compiler uses the return stack as an actual return stack, you can try this:

    Gforth comes with ABSOLUTELY NO WARRANTY; for details type `license'
    Type `help' for basic help
    : reverse begin dup while 1- swap r> swap >r >r repeat drop ; ok
    : test 1 2 3 4 5 6 6 reverse r> . r> . r> . r> . r> . r> . ; ok
    ok
    test 1 2 3 4 5 6 ok

    Type `bye' to exit
    : reverse begin dup while 1- swap r> swap >r >r repeat drop ; ok
    1 2 3 4 5 6 7 8 9 0 ok
    reverse ok
    .s <9> 1 2 3 4 5 6 7 8 9 ok

    „Houston…”

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Hans Bezemer@21:1/5 to Zbig on Thu Aug 24 09:13:12 2023
    On Thursday, August 24, 2023 at 6:06:56 PM UTC+2, Zbig wrote:
    „Houston…”

    Untrue - you're making an error: ( i*x i -- i*y )

    The number of elements to be reverse has to be correct- which isn't the case:

    1 2 3 4 5 6 7 8 9 0 ok
    reverse ok

    Since you specify ZERO elements.

    Hans Bezemer

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Zbig@21:1/5 to All on Thu Aug 24 09:27:40 2023
    Oh, sorry — indeed I should've create appropriate 'test' word,
    but — you know — I dare to to judge my solution being not as
    brilliant, but somewhat more practical.
    By adding at the beginning one more line as „security fuse” it
    accepts any stack depth and it doesn't require any „special”
    words to get the job done:

    : reverse-stack ( -- )
    depth 0= if ." Nothing to reverse; the stack is empty" exit then
    depth dup >r 0 do here i cells + ! loop
    r> 0 do here i cells + @ loop ;

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Zbig@21:1/5 to All on Thu Aug 24 09:29:01 2023
    nine „highest” values from the stack. You didn't notice „ten” in triangles directly before value list? It displays the stack's depth.
    Really? Man, the rat holes I have been trough... I did not know that...

    Just enter twelve values and then type the usual „.S”… :D

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From SpainHackForth@21:1/5 to Zbig on Thu Aug 24 09:26:45 2023
    On Thursday, August 24, 2023 at 5:45:04 PM UTC+2, Zbig wrote:
    czwartek, 24 sierpnia 2023 o 17:29:38 UTC+2 SpainHackForth napisał(a):
    On Thursday, August 24, 2023 at 4:05:43 PM UTC+2, Zbig wrote:
    : revs
    depth >r
    depth cells allot
    depth 0 do here i cells - ! loop
    0 do here i cells - @ loop
    depth cells negate allot
    ;
    Oh, but actually why allocating anything for that brief moment?
    It can be shorter:

    : reverse-stack ( -- )
    depth dup >r 0 do here i cells + ! loop
    0 do here i cells + @ loop ;
    I just tried it, it is the same as my initial issue, it misses the "9" on the stack....

    : reverse-stack ( -- ) compiled
    depth dup >r 0 do here i cells + ! loop compiled
    0 do here i cells + @ loop ; ok
    0 1 2 3 4 5 6 7 8 9 ok
    reverse-stack ok
    .S <10> 8 7 6 5 4 3 2 1 0 ok
    It doesn't missing anything; Gforth displays just the nine
    „highest” values from the stack. You didn't notice „ten” in triangles directly before value list? It displays the stack's depth.
    Really? Man, the rat holes I have been trough... I did not know that...

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Zbig@21:1/5 to All on Thu Aug 24 10:19:48 2023
    Really? Man, the rat holes I have been trough... I did not know that...

    It's worth to take a peek into „F… ine Manual” sometimes. Consider
    one more example:

    Gforth:
    1 2 3 4 5 6 7 8 9 10 11 12 ok
    .s <12> 4 5 6 7 8 9 10 11 12 ok
    pForth:
    1 2 3 4 5 6 7 8 9 10 11 12 ok
    Stack<10> 1 2 3 4 5 6 7 8 9 10 11 12

    So twelve values, but pForth says „ten” in triangles. A bug?
    Something wrong on the stack? No!
    Just pForth reports in triangles the current BASE (in decimal),
    not the stack's depth. Have a look:
    hex ok
    Stack<16> 1 2 3 4 5 6 7 8 9 A B C

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From dxforth@21:1/5 to SpainHackForth on Fri Aug 25 15:06:20 2023
    On 25/08/2023 2:26 am, SpainHackForth wrote:
    On Thursday, August 24, 2023 at 5:45:04 PM UTC+2, Zbig wrote:
    czwartek, 24 sierpnia 2023 o 17:29:38 UTC+2 SpainHackForth napisał(a):
    On Thursday, August 24, 2023 at 4:05:43 PM UTC+2, Zbig wrote:
    : revs
    depth >r
    depth cells allot
    depth 0 do here i cells - ! loop
    0 do here i cells - @ loop
    depth cells negate allot
    ;
    Oh, but actually why allocating anything for that brief moment?
    It can be shorter:

    : reverse-stack ( -- )
    depth dup >r 0 do here i cells + ! loop
    0 do here i cells + @ loop ;
    I just tried it, it is the same as my initial issue, it misses the "9" on the stack....

    : reverse-stack ( -- ) compiled
    depth dup >r 0 do here i cells + ! loop compiled
    0 do here i cells + @ loop ; ok
    0 1 2 3 4 5 6 7 8 9 ok
    reverse-stack ok
    .S <10> 8 7 6 5 4 3 2 1 0 ok
    It doesn't missing anything; Gforth displays just the nine
    „highest” values from the stack. You didn't notice „ten” in
    triangles directly before value list? It displays the stack's depth.
    Really? Man, the rat holes I have been trough... I did not know that...

    The computer knows what's good for you - don't question it.
    Apparently today's calculators are like this. I'll find out soon
    enough having just ordered one :)

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)