• somehow the "return -level" has a problem

    From aotto1968@21:1/5 to All on Fri Apr 7 08:27:44 2023
    hi,

    this is probably already known BUT the "-level" option in "return" does NOT cover the nested loop

    the read the following code:

    ./level-bug.tcl
    --------------------------------------
    #!/bin/env tclsh

    set lvl [lindex $argv 0]

    while true {
    while true {
    puts [info frame $lvl]
    return -level $lvl -code break
    puts 0
    break
    }
    puts 1
    break
    }
    puts 2
    ------------------------------------

    ./level-bug.tcl 0
    type source line 7 file /home/dev1usr/test/level-bug.tcl cmd {info frame $lvl} level 0
    1
    2


    ./level-bug.tcl 1
    type source line 5 file /home/dev1usr/test/level-bug.tcl cmd {while true {
    while true {
    puts [info frame $lvl]
    return -level $lvl -code break
    puts 0
    break
    }
    puts 1
    break
    }} level 0
    invoked "break" outside of a loop
    while executing
    "while true {
    while true {
    puts [info frame $lvl]
    return -level $lvl -code break
    puts 0
    break
    }
    puts 1
    ..."
    (file "./level-bug.tcl" line 5)




    obvious the while count NOT as level/frame

    mfg

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Andreas Leitgeb@21:1/5 to Andreas Leitgeb on Thu May 11 11:48:16 2023
    Andreas Leitgeb <[email protected]> wrote:
    aotto1968 <[email protected]> wrote:
    this is probably already known BUT the "-level" option in "return" does NOT cover the nested loop
    You're misunderstanding the "-level" of return (even with -code)

    proc break3code {} { return 42 } ;# define an arbitrary numeric code once
    proc break3 {} { return -code [break3code] } ;# -level 1 is default for return
    try {
    while true {
    while true {
    while true {
    break3
    }
    }
    }
    } on [break3code] {} {};# we broke out of all loops, now continue happily.

    Yes, it's a bit awkward, but breaking out of multiple loops isn't all that common.
    That example was just to show how it *could* be done with exceptional
    exits...

    A more common approach instead would rather look like this:

    set emergencybreak 0
    while true {
    while true {
    while true {
    set emergencybreak 1; break
    }
    if {$emergencybreak} { break }
    }
    if {$emergencybreak} { break }
    }

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Steve Bennett@21:1/5 to Andreas Leitgeb on Fri May 12 17:30:14 2023
    On Thursday, May 11, 2023 at 9:50:12 PM UTC+10, Andreas Leitgeb wrote:
    Andreas Leitgeb <[email protected]> wrote:
    aotto1968 <[email protected]> wrote:
    this is probably already known BUT the "-level" option in "return" does NOT cover the nested loop
    You're misunderstanding the "-level" of return (even with -code)

    proc break3code {} { return 42 } ;# define an arbitrary numeric code once proc break3 {} { return -code [break3code] } ;# -level 1 is default for return
    try {
    while true {
    while true {
    while true {
    break3
    }
    }
    }
    } on [break3code] {} {};# we broke out of all loops, now continue happily.

    Yes, it's a bit awkward, but breaking out of multiple loops isn't all that common.
    That example was just to show how it *could* be done with exceptional exits...

    A more common approach instead would rather look like this:

    set emergencybreak 0
    while true {
    while true {
    while true {
    set emergencybreak 1; break
    }
    if {$emergencybreak} { break }
    }
    if {$emergencybreak} { break }
    }

    This prompted me to propose support for multi-level break and continue in Jim Tcl.
    See:

    https://github.com/msteveb/jimtcl/pull/261

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