BBS水木清华站∶精华区

发信人: scaner (又一个年头), 信区: Linux 
标  题: TCL的语法 
发信站: BBS 水木清华站 (Fri Dec 22 01:52:17 2000) WWW-POST 
 
觉得里面挺科学的, 感觉比较舒服. 
又郁闷就无聊就这样折腾一点东西出来. 
share吧,大家有兴趣就看看 
 
==== 
Source: http://dev.scriptcs.com/scripting/syntax.html 
Translator: scaner.smth-bbs 
 
1.简单语法 
cmd arg arg arg 
A Tcl command is formed by words separated by white space.  
The first word is the name of the command, and the remaining  
words are arguments to the command.  
Tcl的确命令是由空格分隔的字符串组成.第一个字符串是命令 
的名字, 余下的字符串是命令的参数. 
 
 
$foo 
The dollar sign ($) substitutes the value of a variable.  
In this example, the variable name is foo.  
$代表(替换)变量的值. $foo, 变量的名字就是foo 
 
[clock secounds] 
Square brackets execute a nested command. For example,  
if you want to pass the result of one command as the argument to  
another, you use this syntax. In this example, the nested  
command is clock seconds, which gives the current time in seconds.  
[]执行一内签的命令. 比如说, 
如果你想将一个命令的结果作为参数传给另一个命令, 你可以 
使用这个方式. 在上面的例子中, 命令是clock seconds. 
这个命令用以获得秒表示的时间. 
 
"some stuff" 
Double quotation marks group words as a single argument  
to a command. Dollar signs and square brackets are interpreted  
inside double quotation marks.  
霜引号表示一组字符串作为一个参数传递给命令. 在引号中的 
$, []都将被解释. 
 
{some stuff} 
Curly braces also group words into a single argument.  
In this case, however, elements within the braces are not interpreted.  
花括号同样是用来聚合字符串成为单一的参数的. 但是在{} 
中的元素将不会被解释. 
 

The backslash (\) is used to quote special characters.  
For example, \n generates a newline. The backslash also  
is used to "turn off" the special meanings of the dollar sign,  
quotation marks, square brackets, and curly braces.  
=用来引用特殊的字符. 比如说\n代表换行. \同样用来 
关闭$,".[]{}的特殊意义. 
 
A Little Example  
Below is a Tcl command that prints the current time.  
It uses three Tcl commands: set, clock, and puts. The  
set command assigns the variable. The clock command  
manipulates time values. The puts command prints the values.  
 
set seconds [clock seconds] 
puts "The time is [clock format $seconds]" 
下面是一个tcl命令, 用来打印当前时间. 
他使用了三个Tcl命令:set, clock and puts. set命令 
用来给变量赋值. clock命令处理时间, put命令打印 
变量. 
 
Note that you do not use $ when assigning to a  
variable. Only when you want the value do you use $.  
The seconds variable isn't needed in the previous example.  
You could print the current time with one command:  
 
puts "The time is [clock format [clock seconds]]" 
请注意在赋值时是没有使用$的. 只有档你想获得 
值的时候, 才使用$. 另外变量是不必须的, 你可以直接 
用一条命令打印当前的时间. 
 
Grouping and Substitution 
The Tcl syntax is used to guide the Tcl parser  
through three steps: argument grouping, result  
substitution, and command dispatch 
 
分组于替换 
Tcl的语法用来指导Tcl的解析经过三个过程, 
参数分组(argument grouping), 结果替换(result substitution) 
与命令分派(command dispatch) 
 
Argument grouping. ? 
Tcl needs to determine how to organize  
the arguments to the commands. In the simplest case,  
white space separates arguments. As stated earlier,  
the quotation marks and braces syntax is used to group  
multiple words into one argument. In the previous example,  
double quotation marks are used to group a single argument  
to the puts command.  
Tcl需要知道如何组织命令的参数. 简单的情况瞎, 空格分隔 
每个参数. 正如前面的规定,引用语法用来将多个字符串 
聚合成一个参数. 如前面的例子, 双引号标记被用来聚合 
puts的参数. 
 
Result substitution.  
After the arguments are grouped, Tcl performs string  
substitutions. Put simply, it replaces $foo with the value  
of the variable foo, and it replaces bracketed commands  
with their result. That substitutions are done after grouping  
is crucial. This sequence ensures that unusual values  
do not complicate the structure of commands.  
在参数分组后, Tcl 执行字符串替换. 将$foo替换成变量 
foo 的值, 同时替换[]引用命令的结果. 结果替换在参数 
分组以后进行, 是非常重要的, 这样的顺序保真特别的 
值不会复杂命令的结构. 
 
 
Command dispatch.  
After substitution, Tcl uses the command name as a key into a  
dispatch table. It calls the C procedure identified in the table,  
and the C procedure implements the command. You also can write  
command procedures in Tcl. There are simple conventions about  
argument passing and handling errors.  
结果替换以后, Tcl 用命令名子作为关键字查找派遣表. 调用 
表中确定的C 过程, 这个C 过程实现了这个命令. 你也可以用 
Tcl 编写命令处理过程. 那里有关于变量传递与错误处理的简单 
约定. 
 
Another Example 
Here is another example:  
 
set i 0 
while {$i < 10} { 
    puts "$i squared = [expr $i*$i]" 
    incr i 

 
Here, curly braces are used to group arguments without doing  
any substitutions. The Tcl parser knows nothing special about  
the while command. It treats it like any other command. It is  
the implementation of the while command knows that the first  
argument is an expression, and the second argument is more Tcl  
commands. The braces group two arguments: the boolean expression  
that controls the loop and the commands in the loop body.  
这里, {}用于引用不做任何替换的参数. Tcl 解释器不知道while命令的 
任何特别信息. 它只是把它当成一个普通的命令. while 命令的实现, 
知道第一个参数, 是一个表达式, 第二个参数是多个Tcl 命令的集合. 
{}聚集了两个参数: 用来控制循环的逻辑表达式与循环命令集合. 
 
We also see two math expressions: the boolean comparison  
and multiplication. The while command automatically evaluates  
its first argument as an expression. In other cases you must  
explicitly use the expr command to perform math evaluation.  
我们同时看见两个表达式, 布尔比较与乘法. while 命令自动 
对第一个参数求值. 在别的情况, 你必须明确的使用expr命令 
执行数字表达式. 
 
Command Dispatch 
Lastly, Tcl calls something else to do the hard work.  
We've seen that Tcl uses expr to perform math functions,  
puts to handle output functions, and set to assign variables.  
These Tcl commands are implemented by a C procedure that has  
registered itself with Tcl. The C command procedures take the  
string arguments from the Tcl command and return a new string  
as their result. It is very easy to write C command procedures.  
They can do everything from accessing databases to creating  
graphical user interfaces. Tcl, the language, doesn't really know  
what the commands do. It just groups arguments, substitutes  
results, and dispatches commands.  
最后Tcl 调用点别的东西, 来完成复杂的工作. 我们已经看见 
Tcl 利用expr来执行数学运算, puts来完成输出功能, set  
用于变量定值. 这些Tcl 命令都是由 C过程实现的, 它们都是 
由Tcl 已经注册好的. C 实现的命令从Tcl 命令中获得 
字符串参数, 同时返回一个字符串作为结果返回给Tcl 命令. 
编写C 命令处理过程是非常简单. 它们可以做从访问数据库 
到建立GUI 的任何事情. Tcl 语言本生并不知道命令完成功能. 
它只是执行groups arguments, substitutes results and dispatches commands 
这三步操作. 
 
 
One Last Example 
Here is the factorial procedure:  
 
proc fac {x} { 
    if {$x < 0} { 
        error "Invalid argument $x: must be a positive integer" 
    } elseif {$x <= 1} { 
        return 1 
    } else { 
        return [expr $x * [fac [expr $x-1]]] 
    } 

 
Sign: z9e4+M7S1+7H17CutcTIyw== 
-- 
她在电话的另一头默默不语,久久地保持沉默, 
如同全世界所有的细雨落在全世界所有的草坪上. 
 
 
 
 
 
※ 修改:·scaner 於 Dec 22 01:55:41 修改本文·[FROM: 166.111.215.235] 
※ 来源:·BBS 水木清华站 smth.org·[FROM: 166.111.215.235]  

BBS水木清华站∶精华区