Directory
Tcl–Tool Command Language
The following is a grammar summary of the Tcl language, with a total of 12 items.
[1] Command
[2] Evaluation
[3] Words
[4] Double quotes
[5] Argument expansion
[6] Braces
[7] Command substitution
[8] Variable substitution
[9] Backslash substitution
[10] Comments
[11] Order of substitution
[12] Substitution and word boundaries
Tcl – Tool Command Language
everything-is-string
The following is a summary of the syntax of the Tcl language, a total of 12 items.
[1] Command
A Tcl script is a string of one or more command commands. Semicolons Semi-colons and newlines newlines are used as command separators unless quoted. During command substitution substitution, the closing square bracket ] is used as a command terminator unless quoted.
# define some variables set var1 10 set var2 20 # perform some arithmetic operations set x [expr {$var1 + $var2}]; puts "The sum is: $x" set y [expr {$var1 - $var2}]; puts "The difference is: $y" # define procedure with parameters and call it proc greet {name} { puts "Hello, $name" } greet "Alice"; greet "Bob" # Use curly braces to quote command arguments containing spaces set msg {Hello Tcl!} puts $msg # Use curly braces to quote strings containing newlines set fruits {apple banana orange } puts "The fruits are: $fruits" # Define a variable value containing square brackets through curly braces or backslashes set var [0] ; # Writing programs like this will report an error invalid command name "0" set var \[0\] ; puts $var ;# implemented by \ escape set var {[0]} ; puts $var ;# via {} reference
[2] Evaluation
In Tcl, command execution is evaluated in two steps: first, the Tcl interpreter interpreter breaks the command into words words and Perform substitutions, including using braces to protect words, performing command substitution, and variable substitution. Next, the interpreter will use the first word to find and locate the command procedure to be executed, and then all the words will be passed to the command procedure. The command procedure can interpret each word in an arbitrary way, such as integer, variable name, list or Tcl script.
In Tcl, each command procedure has its own parameter parsing rules, and the interpreter will interpret the words passed by each command procedure as needed. The resolution rules for built-in commands in Tcl can be determined according to their function and behavior.
For example, in the following code sample:
set x 10
The Tcl interpreter first splits the above command into 3 words, and uses the first word set to locate and find set command procedure, and pass the remaining words x and 10 to the procedure, respectively. The set command procedure interprets the second word x as a variable name and the third word 10 as the value of the variablex, and set it to 10. Therefore, in this code example, the value of the variable x is set to 10.
# When performing command substitution, the interpreter will execute the commands nested in square brackets and output the result as the replaced string. # [expr 2 + 3] is replaced by the string 5 # After the replacement, execute the process according to the set command set x [expr 2 + 3] # When performing variable substitution, the interpreter will replace the variable reference with its corresponding value through the dollar sign puts $x ;# output result 5 # The curly braces protect the word from being affected by the Tcl interpreter, and the curly braces are used as string literals instead of variable values puts {$x} ;# output result $x
[3] Words
In Tcl, words in commands are separated by white space white space (except newline newlines, which represent command separators command separators )
[4] Double quotes
In Tcl, if the first character of a word is a double quote quotes, the end of the word is terminated by the next double quote character. If semicolons, or space characters (including newlines) appear between double quotes, they are treated as ordinary characters and included in the word. In Tcl, characters enclosed in double quotes can undergo Command , variable and backslash substitutions. And the double quotes are not preserved as part of the word.
# wrong usage puts "This is a "quote" inside a string." # output result extra characters after close-quote while executing "puts "This is a "q" # correct usage puts "This is a "quote" inside a string." # output result This is a "quote" inside a string. # There are spaces, square brackets, semicolons, and newlines between double quotes puts "; and \[\] and {} and newlines" # output result ; and [] and {} and newlines
[5] Argument expansion
If a word begins with the string {*} followed by a non-whitespace character, then {*} will be removed and the rest of the word will be parsed and replaced for any other word. The {*} syntax is used to expand the values in the list into individual parameters.
proc max {args} { set max_value [lindex [lsort -real -decreasing $args] 0] puts "The max value is $max_value" } set my_list {1 2 3 4} max {*}$my_list
What this code does is, find the largest value in the list and output it.
First a procedure called max is defined and accepts a variable number of input arguments using the args keyword. And use the lsort command to sort the list, -real means sort by value, -decreasing means descending order. Use lindex to get the first element after sorting, which is the maximum value. And store it in the variable max_value, and finally output it with the puts command.
A list named my_list is defined, and the list elements are broken up by the {*} symbol, which is passed as a parameter to the max procedure.
[6] Braces
For the characters between a pair of curly braces {} , Tcl does the following: if the first character of a word is an open bracket { , then the terminator of the word is a matching closing parenthesis } . Characters within curly braces are preserved, and the braces themselves are ignored. The characters between the curly braces do not perform any replacement except backslash+newline, which will be replaced with a single space.
set x 1 puts {$x is\ 1} # output result $x is\1 puts {$x is\ 1} # output result $x is 1 ;# Found that there is no backslash, this is the only replacement that can occur within curly braces
[7] Command substitution
If a word contains an opening square bracket [ , then TCL will perform command substitution. To do this, it recursively calls the TCL interpreter to process the characters following the square brackets as a TCL script. A script can contain any number of commands and must end with a closing square bracket ]. The result of the script (that is, the result of its last command) is substituted into the words, with all characters between them replaced. There may be any number of command substitutions within a word. Command substitution will not be performed on words enclosed in curly braces.
# The TCL interpreter will recursively call itself 6 times, each time performing a new command substitution. # In each recursive call, the TCL interpreter interprets and executes the characters in the square brackets as a new TCL command # until all command substitutions have been executed. puts [expr {1 + [expr {2 + [expr {3 + [expr {4 + [expr 5]}]}]}]}] # In this example, the final result is 15 because we calculated the sum of 1 + 2 + 3 + 4 + 5 # output result 15
[8] Variable substitution
In TCL, if a word contains a dollar sign $ and one of the following forms, TCL performs variable substitution: the dollar sign and the characters following it are replaced by the value of the variable. Variable substitution can take any of the following forms:
- $name
- $name(index)
- The ${name} braces ensure that Tcl recognizes variable boundaries correctly
set fruit apple puts "I like $fruit!";# No error will be reported, because Tcl thinks ! It is a special character, encountered in variable substitution! automatic cutoff puts "I like ${fruit}!" ;# The output of both is the same # output result I like apple! # Curly braces are used to ensure that Tcl correctly recognizes variable boundaries puts "I like ${fruit}s." # output result I like apples. # The following is the wrong usage puts "I like $fruits" # output result can't read "fruits": no such variable while executing "puts "I like $fruits" "
[9] Backslash substitution
Backslash sequence | replace result |
\t | tab |
\ n | line break |
\v | Vertical tab |
\r | Enter< /strong> |
\f | Form break |
\b | Delete |
\ | Backslash itself |
\a | Warning sound |
\ |
< strong>A space character |
\ooo | ASCII code of octal number |
\ \xhh | hh is a two-digit hexadecimal number |
\uhhhh | where hhhh is a four digit Hexadecimal number |
\uhhhhhhhh | where hhhhhhhh represents the value of the 8-digit hexadecimal representation of the character |
# Tabs set str "Name:\tBob\tAge:\t25" puts $str # line break set str_1 "Name:\tBob\\ Age:\t25" puts $str_1 # vertical tab set str_2 "Name:\tBob\vAge:\t25" puts $str_2 # carriage return set str_3 "Name:\tBob\rAge:\t25" puts $str_3 # Form feed set str_4 "Name:\tBob\fAge:\t25" puts $str_4 # delete character set str_5 "Name:\tBob\bAge:\t25" puts $str_5 # backslash itself set str_6 "A backslash: \" puts $str_6 # warning sound # When this statement executes, the computer will emit a hum. # Note that on different systems, there may be different sound effects, or no sound effects at all. # Warning sounds may be uncommon in actual programming, but may be useful in some scenarios, such as when users need to pay attention. puts "Beep! \a" # In TCL, backslashes and newlines can be used to split a long line of code into multiple lines, making it easier to read and maintain. # In the code, backslash and newline must be used at the end of the line. set str_7 "This is a very long string\ that spans multiple lines." puts $str_7
The output of the above code is as follows:
In TCL, the \ooo escape sequence can be used to represent the ASCII code for an octal number. Among them, ooo is a three-digit octal number, which can be any combination of numbers from 0 to 7. For example, \101 represents an uppercase letter A and \142 represents a lowercase letter b . This escape sequence is often used to insert ASCII characters in code. For example, in the following example we use \101 to insert a capital letter A into the string:
set my_string "Hello \101 world!" puts $my_string ;# output: Hello A world!In TCL, arbitrary ASCII codes can also be represented using the \xhh escape sequence, where hh is a two-digit hexadecimal number. For example, \x41 is equivalent to \101 for the uppercase letter A . This escape sequence is often used to insert binary data or special characters in code.
set my_string "Hello \x41 world!" puts $my_string ;# output: Hello A world!In TCL, \uhhhh is a Unicode escape sequence used to insert Unicode characters in a string, where hhhh is a four-digit hexadecimal number representing the Unicode character to be inserted serial number. For example, \\Ж represents the Unicode code point U+0416 , which corresponds to the characters Ж. It should be noted that not all Unicode characters are supported in TCL, so it is necessary to first determine whether the characters used are supported by TCL. If a string contains the \u escape sequence, TCL will automatically convert it to a Unicode character. For example, in the following example we use \\Ж to insert the Unicode character Ж into the string:
\Uhhhhhhhh This looks like the terminal is not compatible, and the output is wrong!
[10] Comments
In Tcl, if a hash character # occurs where the interpreter expects the first character of the first word of a command, start with the hash character and continue until the next newline Any characters in the position where the break appears will be treated as a comment and ignored. Comment characters have meaning only when they appear at the beginning of a command.
# This is valid comment puts "Hello, Tcl!" puts "Hello,Tcl!" ;# This is valid comment puts "Hello,Tcl!" # This is invalid comment
[11] Order of substitution
When creating a command’s word, the Tcl interpreter processes each character once. For example, if variable substitution occurs, no other substitutions are performed on the variable’s value; the variable’s value is inserted into the word unchanged. If command substitution occurs, the nested commands are fully processed by the recursively calling Tcl interpreter; no other substitutions are performed before the recursive call is made, and no other substitutions are performed on the results of the nested script. Permutations are performed from left to right.
set y [set x 0][incr x][incr x] puts $y # output result 012
[12] Substitution and word boundaries
In Tcl, substitution operations do not affect word boundaries of commands, except in parameter expansion as specified in rule [5]. For example, during variable substitution, the entire value of the variable becomes part of a single word, even if the variable’s value contains spaces.
set my_list {abc {ab c} {a b c}} foreach element $my_list { set length [string length $element] puts "The string length of "$element" is $length" } # output result The string length of "abc" is 3 The string length of "ab c" is 4 The string length of "a b c" is 5
Reference: https://www.tcl-lang.org/man/tcl/TclCmd/Tcl.htm#M12