I have developed some code, and I'm facing problem with error flagging of the Tcl interpreter on a Linux machine.
#!/usr/bin/tclsh
if {1} {
puts "abc1"
} elseif {} {
puts "abc2"
}
The above code is not flagging error for "elseif"
condition until it get into the
elseif
condition.
Is there any way to check this kind of typo error done unintentionally.
The catch command may be used to prevent errors from aborting command interpretation. The catch command calls the Tcl interpreter recursively to execute script, and always returns without raising an error, regardless of any errors that might occur while executing script.
The catch command may be used to prevent errors from aborting command interpretation. Catch calls the Tcl interpreter recursively to execute script, and always returns a TCL_OK code, regardless of any errors that might occur while executing script.
The end command can also be used to stop a TCL command that was sent to another line by the tcl command, or to terminate a phantom job. The end command clears an active list and requires a sys2 privilege level. If the port number and user-ID are not specified, the end command stops the process on the current line.
Tcl does not find errors at compilation time, and in the the sample above it can determine that it will never need to examine the elseif
clauses in the first place; it just issues that first puts
directly.
Now, in the case where there is a non-trivial first condition it is the case that the errors in the elseif
expression are not reported until they are reached. This is how the semantics of Tcl — and especially the if
command — are defined; errors in evaluation (as opposed to gross major syntax) are reported at the time of execution of the command. I can understand your frustration with this, and suggest that you check out the Tcler's Wiki page on static syntax analysis tools, which can flag up potential problems for you (under very modest assumptions that are virtually always true). In particular, I have heard good things about Frink and the checker tool in TDK (the latter being a commercial tool, but very high quality).
To elaborate on Donal's answer, Tcl does not find errors at compile time because in the general case it cannot be done, any code executed before the if might have redefined the if command, so it could be valid, the only way to determine if this is the case is to run the code (i.e. this is the halting problem)
consider this script:
gets stdin input
if {$input == "fail"} {
rename if if_
proc if {arg1 arg2 arg3} {
puts "ha ha"
}
}
if {1} { puts "success"}
clearly it is impossible to statically determine if the if {1} line has the right number of arguments without running the program
TCL really has virtually no syntax, there is nothing a compiler can check, the best you can do is Lint style warnings, which will only be accurate in some cases
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With