I have implemented many TCL extensions for a specific tool in the field of formal methods (extensions are implemented in C, but I do not want the solution to rely on this fact). Thus, users of my tool can use TCL for prototyping algorithms. Many of them are just a linear list of commands (they are powerful), for example:
my_read_file f my_do_something abc my_do_something_else abc
Now I'm interested in time. You can modify the script to get:
puts [time [my_read_file f] 1] puts [time [my_do_something abc] 1] puts [time [my_do_something_else abc] 1]
Instead, I want to define an xsource procedure that executes a TCL script and input / write time for all my commands. Some kind of profiler. I wrote a naive implementation where the main idea is this:
set f [open [lindex $argv 0] r] set inputLine "" while {[gets $f line] >= 0} { set d [expr [string length $line] - 1] if { $d >= 0 } { if { [string index $line 0] != "#" } { if {[string index $line $d] == "\\"} { set inputLine "$inputLine [string trimright [string range $line 0 [expr $d - 1]]]" } else { set inputLine "$inputLine $line" set inputLine [string trimleft $inputLine] puts $inputLine puts [time {eval $inputLine} 1] } set inputLine "" } } }
It works for a linear list of commands and even allows you to add comments and commands over several lines. But it fails if the user uses instructions, loops, and procedure definitions. Can you suggest a better approach? It should be a clean TCL script with as many extensions as possible.
source share