TCL 学习
有幸看到南京集成电路大学STA在线课程,里面涉及到基础tcl的讲解。就根据视频总结下来并在其基础上扩充:
[https://www.iccollege.cn/portal/session/unitNavigation/193.mooc]
1. 概述
tcl:tool command language,作为公式命令语言,在EDA工具里面使用颇为广泛(C、M、S家都会广泛使用)。且大多数EDA工具都兼容tcl,如下图所示:
因此学会tcl会对工具的使用起到很大的作用。
2. 数量、数组、列表、字典
2.1.数量:
>$ [] \ "" {} #
% set a 5
5
% puts $a
5
% set a [expr 3+4]
7
% puts $a
7
% set a [expr 3+\
% 4]
7
% set b "$a"
7
% set c [$a]
7
% set d {$a}
$a
% #set d {$a} 注释
% #set a 5 注释
% set a 5 ; # test
5
% set a 2
2
% puts $a
2
% puts $a_1
can't read "a_1", no such var
% puts {$a}_1
2_1
2.2.数组
% set cell_1(m) "a"
a
% set cell_1(n) "b"
b
% array size cell_1
2
% array name cell_1
m n
2.3 列表
>contact lindex llength lappend lsort linsert lreplace lset lassign
% set list_ [split "a1 a2 a3"]
a1 a2 a3
% set list_ "a1 a2 a3"
a1 a2 a3
% set list_ [a1 a2 a3]
a1 a2 a3
% set list_0 {a1 a2 a3}
a1 a2 a3
% set list_1 {a4 a5 a6}
a4 a5 a6
% contact $list_0 $list_1
a1 a2 a3 a4 a5 a6
% lappend list_0 $list_1
a1 a2 a3 { a4 a5 a6 }
% lindex $list_0 3
a4 a5 a6
% lindex [lindex $list_0 3] 2
a6
% set var {1 2 3}
1 2 3
% set var [linsert $var 2 4]
1 2 4 3
% set var [lreplace $var 2 3 7 8]
1 2 7 8
% set var [lset $var 0 3]
3 2 7 8
% lassign $var num1 num2 nnum3
% puts $num1 $num2 $num3
3 2 7
2.4 字典
>dict set dictname key value
>dict create dictname key1 value1 key2 value2 ... keyn valuen
% 字典定义
% dict set color color1 black color2 white
% puts $dict
color1 black color2 white
% set color [dict create color color1 "green" color2 "red"]
% puts $color
green red
% 字典大小
% puts [dict size $color]
2
% 字典键
% puts [dict keys $color]
color1 color2
%字典键值
% puts [dict get $color color1]
green
%字典的值
% puts [dict value $color]
green red
%判断键值是否存在
% puts [dict exists $color color1]
1
% puts [dict exists $color color4]
0
e. expr
tcl 作为一种动态类型语言,变量的值可以在需要的时候被动态转换为所需要的的类型。例如一个数字,如果被存储为字符,在做算数运算的时候会被当做数字5参与运算如下:
% set a '5'
% puts [expr $a + 6]
11
tcl默认的精度为12位数字,如果除数与被除数均为正数,那么商也为四舍五入的正数。但凡除数与被除数有一个小数,商就是12位的小数。也可以通过如下命令设置得到的商的位数为5:
% set tcl_precision 5
>\+ - * / % > < >= <=
3. 控制流
>if else
the end "}" of if block must be on the upper line
%set a 0
% if { a < 100 } {
% puts $a } else {
% puts [expr $a-1]
% }
>foreach
% foreach i $list {
% puts $i
% }
break
jump from loop
continue
only end current loop
>while
% set m 0
% while { m <10 } {
% set m [expr $m + 1]
% if {$m == 5} {
% break
% }
% }
% puts $m
5
>for
% for {set i 0} {$i < 10} {incr i 1} {
% if {$i == 9 } {
continue
% }
% }
% puts $i
10
>switch
% set i "bad"
% switch $i {
% good {
% puts "a"
% }
% bad {
% puts "b"
% }
% default {
% puts "no"
% }
% }
b
4. 过程函数
>proc 全局变量和局部变量。global定义全局变量
% proc add {a,b} {
% set sum [expr $a + $b]
% return $sum
% }
% add 3 4
7
% 隐式参数默认值
% proc add {m {n 10}} {
% return [expr $m + $n]
% }
%
% puts [add 10 20]
30
% pust [add 10]
20
%过程可变参数
% proc average_ {numbers} {
% set sum 0
% foreach number $numbers {
% set sum [expr $sum + $number]
% }
% set ave [expr $sum/[llength $numbers]]
% return $ave
% }
%
% puts [average_ [1 2 3 4 5]]
3
% puts [average_ [10 20 30]]
20
%递归
% proc self_call {number} {
% if {$number <=1} {
% return1
% }
% return [expr $number*[self_call [expr $number -1]]]
% }
%
% puts [self_call 3]
6
% pust [self_call 4]
24
5. 正则匹配
regexp
\w 匹配一个字母、数字、下划线
\d 匹配一个数字
* 匹配零次或多次
+ 匹配一次货多次
? 匹配零次或一次
^ 匹配开头
$ 匹配结尾
\s 匹配空格
. 匹配任意字符
abc123
\w+\d+
\w*\d*
\d*\w*\d*
% set var abc456
% regexp {\w+\d+} $var
1
% regexp {^\d.*\d$} "1_ d fsa1 a1"
1
% set sentence "She is 30 years old"
% regexp {\s(\d+).*} $sentence total age
1
% ptus $total
She is 30 years old
% puts $age
30
6. 字符串相关
a.字符串转义:
\a 警报或铃
\b 退格
\n 换行
\f 换页
\r 回车
\t 制表
\v 垂直制表
b.字符串命令:
%string +
% compare string1 string2
比较string1和string2 字符串,如果相等返回0,
string1的定义出现在string2前,返回-1,否则返回1
% string1 string2
返回string2中string1的第一次出现的索引的位置,
没找到,返回-1
% last string1 string2
返回索引string1 在string2中出现的最后一次,
如果没有,返回-1
%index string index
返回索引字符
%length string
返回字符长度
%match pattern string
匹配返回1
%range string index1 index2
返回string index1和index2 间的字符
%tolower string
返回小写字符
%toupper string
返回大写字符
%trim string trimcharacterics
删除sting两端的字符trimcharacteristic,默认空白
%trimleft string trimcharacterics
删除string左侧
%trimright string trimcharacteristic
删除string 右侧
%wordend findstring index
返回索引字符fingstring包含字符索引单词
%wordstart findstring index
返回findstring中第一个字符的含有索引中的字符索引的单词
7. 文本处理
>open -- close get read puts
> r/w/a/r+/w+/a+
file_test:
a is a character
b is after a
% set input_file [open file_test r]
% set file_read [read $input_file]
% while {[get $input_file line] >=0 } {
% puts "$line"
%}
a is a character
b is after a
% puts $file_read
a is a character
b is after a
% set output_file [open file_ w]
% puts $output_file "Hello"
% puts -nonewline $output_file "you"
% puts $output_file "good"
% close $output_file
file_:
Hello
yougood