Tcl Source Code

Artifact [9c39f3a13b]
Login

Artifact 9c39f3a13b3f5176a910400349c42b0f586507a54529eacfe28086ceb6776123:

Attachment "switch_test.tcl" to ticket [5d5b105228] added by juliannoble2 2025-11-10 08:04:31.


namespace eval ::tswitch {

    #2arg: matchstring + single patternscript body
    proc switch_nested_2arg {s} {
        set ch1 [::tcl::string::index $s 0]
        switch $ch1 {
            a {
                set ch2 [::tcl::string::index $s 1]
                switch $ch2 {
                    a {
                        return aa
                    }
                    b {
                        return ab
                    }
                    c {
                        return ac
                    }
                    d {
                        return ad
                    }
                }
            }
            default {
                return _
            }
        }
    }
    #3arg: one switch + matchstring + single patternscript body
    proc switch_nested_3arg {s} {
        set ch1 [::tcl::string::index $s 0]
        switch $ch1 {
            a {
                set ch2 [::tcl::string::index $s 1]
                switch  -- $ch2 {
                    a {
                        return aa
                    }
                    b {
                        return ab
                    }
                    c {
                        return ac
                    }
                    d {
                        return ad
                    }
                }
            }
            default {
                return _
            }
        }
    }
    #4arg: 2 switches + matchstring + single patternscript body
    proc switch_nested_4arg {s} {
        set ch1 [::tcl::string::index $s 0]
        switch $ch1 {
            a {
                set ch2 [::tcl::string::index $s 1]
                switch -exact -nocase $ch2 {
                    a {
                        return aa
                    }
                    b {
                        return ab
                    }
                    c {
                        return ac
                    }
                    d {
                        return ad
                    }
                }
            }
            default {
                return _
            }
        }
    }
    #4arg: 2 switches + matchstring + single patternscript body
    proc switch_nested_5arg {s} {
        set ch1 [::tcl::string::index $s 0]
        switch $ch1 {
            a {
                set ch2 [::tcl::string::index $s 1]
                switch -exact -nocase -- $ch2 {
                    a {
                        return aa
                    }
                    b {
                        return ab
                    }
                    c {
                        return ac
                    }
                    d {
                        return ad
                    }
                }
            }
            default {
                return _
            }
        }
    }

    #2 switches + matchstring + individual patterns and bodies
    #no problem apparent - excluded from test because linenumbers appear correct (relative to switch command in this case, not script-bases)
    #proc switch_nested_presplit {s} {
    #    set ch1 [::tcl::string::index $s 0]
    #    switch $ch1 {
    #        a {
    #            set ch2 [::tcl::string::index $s 1]
    #            switch -exact -- $ch2 a {
    #                                        return aa
    #                               }  b {
    #                                        return ab
    #                               }  c {
    #                                        return ac
    #                               }  d {
    #                                        return ad
    #                               }
    #        }
    #        default {
    #            return _
    #        }
    #    }
    #}
    proc showreturn {target args} {
        set callinfo    [::tcl::info::frame -2]
        if {[dict get $callinfo type] eq "eval"} {
            variable returnline_results
            set cmdword [lindex [dict get $callinfo cmd] 0]
            if {$cmdword eq "return"} {
                if {![dict exists $returnline_results $target]} {
                    dict set returnline_results $target [list $callinfo]
                } else {
                    dict lappend returnline_results $target $callinfo
                }
                #puts "$target $callinfo"
            }
        }
    }
    variable returnline_results
    set returnline_results [dict create]
    set procnames [lsort [info procs ::tswitch::switch_*]]
    foreach procname $procnames {
        rename $procname ${procname}_aside
        proc $procname {s} [info body ${procname}_aside]
        rename ${procname}_aside ""
        trace add execution $procname enterstep [list ::tswitch::showreturn $procname]
    }

    foreach procname $procnames {
        $procname aa
        $procname ab
        $procname ac
        $procname ad
        trace remove execution $procname enterstep [list ::tswitch::showreturn $procname]
    }
    dict for {target returns} $returnline_results {
        foreach frame $returns {
            set ln [dict get $frame line]
            if {$ln ne "2"} {
                set msg "!!"
            } else {
                set msg "ok"
            }
            puts "$msg $target $frame"
        }
        puts "------------"
    }
}