前言:
TCL(tool common language)是一种通用工具语言,很多eda tool都支持tcl,学习了解一些tcl基本语法还是很有必要的。
1:基础概念
解释器: #!/usr/bin/tclsh
打印: puts -> puts "hello ,world"
注释: 单行注释用 # ,多行可以用: if 0 {xxx}
语法结构:commandName argument1 argument2 ... argumentN
数据类型:只支持string类型
变量类型:支持变量和数组,都不用提前申明,直接使用
TCL解释器对一个命令的求值分为了:分析和执行
分析:把命令分解为独立的单词,进行必要的置换动作。
执行:把第一个单词当做命令,查看这个命令是否有定义,有的话激活其对应的内部动作。
2: 基本语法操作
2.1 替换
-
命令替换:[] ,下面是一个简单的例子,功能是相加:
#!/usr/bin/tclsh puts [expr 1 + 6 + 9] //输出结果为:16
当TCL解释器遇到字符 [ ,它就会把随后expr作为一个命令名,从而激活expr对应的动作,如果我们去掉[],TCL会报错,正常情况下只把命令行中的第一个单词作为命令,注意[]中必须是一个合法的TCL脚本,长度不限。[]中的脚本的值为最后一个命令的返回值。
-
变量替换:$,在变量名之前加上$,功能是返回该变量的内容。
#!/usr/bin/tclsh set a 3 puts $a
- 反斜杠替换:\ ,很多其他脚本语言中也有,其实就是 转义
#!/usr/bin/tclsh set a 3 puts "\$a=$a" //结果:$a=3
2.2 条件控制
1. if
if {boolean_expression} {
# statement(s) will execute if the boolean expression is true
} else {
# other statement(s)
}
也可以嵌套:
if {boolean_expression 1} {
# Executes when the boolean expression 1 is true
} elseif {boolean_expression 2} {
# Executes when the boolean expression 2 is true
} elseif {boolean_expression 3} {
# Executes when the boolean expression 3 is true
} else {
# executes when the none of the above condition is true
}
需要注意的是:else/elseif必须要和if的后面一个}在同一行。
2. switch
switch switchingString {
matchString1 {
body1
}
matchString2 {
body2
}
...
matchStringn {
bodyn
}
}
2.3 循环
while循环:
while {condition} {
statement(s)
}
ex:
set i 0
while {$i<10} {
incr i
}
for循环 :
for {initialization} {condition} {increment} {
statement(s);
}
ex:
for {set i 0} {$i<10} {incr i} {
puts "i:$i ,i+4:expr[$i+4]"
}
foreach:
foreach loop_var loop_list {
statements
}
ex:
set list_a {2 3 4 5 6}
foreach i $list_a{
puts "list_value:$i"
}
也可以多个list一起编列:
set list_a {2 3 4 5 6}
set list_b {12 13 14 15 16}
foreach i $list_a j $list_b {
puts "listb-lista: $j - $i"
}
循环退出:
break:退出整个循环
continue:退出当前循环,继续执行下次循环
3.变量及其操作
3.1 数组
设置数组的方法:
set array_name(index) value
index 可以是整数也可以是字符串,value也是,相当于都是关联数组或者哈希。
数组相关的函数:
//返回数组大小
[array size array_name]
//返回数组索引,类似于python中的dict.keys
[array names array_name]
数组的迭代:
#!/usr/bin/tclsh
set score(English) 85
set score(Math) 95
foreach index [array names score] {
puts "score($index): $score($index)"
}
结果:
score(English):85
score(Math):95
3.2 字符串
相关函数:
格式化输出:
3.3 列表
//创建列表
set listName { item1 item2 item3 .. itemn }
# or
set listName [list item1 item2 item3]
# or
set listName [split "items separated by a character" split_character]
ex:
set colorList1 {red green blue}
set colorList2 [list red green blue]
set colorList3 [split "red_green_blue" _]
相关函数:
//返回列表长度
llength listName
//返回列表索引值index对应的内容
lindex listname index
//从index位置插入value1-n
linsert listname index value1 value2..valuen
//替换列表从firstindex到lastindex的内容,替换为value1-n
lreplace listname firstindex lastindex value1 value2..valuen
//将index位置的内容改写为value
lset listname index value
//将list的值依次赋值给var1-n
lassign listname variable1 variable2.. variablen
//按字典顺序排序
lsort listname
3.4 字典
申明字典:
dict set dictname key value
# or
dict create dictname key1 value1 key2 value2 .. keyn valuen
ex:
dict set colours colour1 red
dict set colours colour2 green
set colours [dict create colour1 "black" colour2 "white"]
相关函数:
//返回字典大小
[dict size dictname]
//返回keyname对应的value
[dict get $dictname $keyname]
//返回字典的所有key
[dict keys $dictname]
//返回字典所有的value
[dict values $dictname]