Static error detection in Tcl scripts

I developed some code and I ran into a problem with a Tcl interpreter error message on a Linux machine.

#!/usr/bin/tclsh if {1} { puts "abc1" } elseif {} { puts "abc2" } 

The code above does not put an error for the elseif condition until it enters the elseif condition. Is there a way to check for this type of typo error made unintentionally.

Thanks in advance!

+6
source share
4 answers

To talk about Donal's answer, Tcl does not find errors at compile time, because in general it cannot be done, any code executed before if could override the if command, so it can be valid, the only way to determine if it is this is to run the code (i.e. this is a problem with stopping)

consider this script:

 gets stdin input if {$input == "fail"} { rename if if_ proc if {arg1 arg2 arg3} { puts "ha ha" } } if {1} { puts "success"} 

it is clearly impossible to statically determine if line {1} has the correct number of arguments without running the program

TCL really has virtually no syntax, there is nothing that the compiler can check, the best you can do is Lint style warnings, which will only be accurate in some cases

+2
source

Tcl finds no errors at compile time, and in the example above, it can determine that it will never have to consider elseif clauses in the first place; it just issues puts first.

Now, in the case where there is a non-trivial first condition, it happens that errors in the elseif expression are not reported until they are reached. This defines the semantics of the Tcl command - and especially the if command; errors in the evaluation (as opposed to the basic syntax) are reported at run time. I can understand your disappointment in this matter and suggest that you check the Tcler Wiki page on static parsing tools that can identify potential problems for you (under very modest assumptions, which are almost always true). In particular, I heard good things about Frink and the verification tool in TDK (the latter is a commercial tool, but very high quality).

+5
source

Tcl finds no errors at compile time, but we can check the syntax with regexp . Match the pattern "elseif {", if present, check to see if there are any characters in the brace "}". If there is nothing, print an error message.

0
source

There is tcl static syntax that may find such problems.

Here is a list of such checks: http://wiki.tcl.tk/3162

ttclchecker http://www.xdobry.de/ttclcheck displays the following error message for this short script

 stackoverflow.tcl:4: error in expression missing operator <<{}>> 
0
source

Source: https://habr.com/ru/post/901692/


All Articles