Tcl basic syntax
Introduction to Tcl
Tcl (originally called "Tool Command Language" and "Tool Command Language", but currently does not have this meaning, but we still call it TCL) is a scripting language. Created by John Ousterhout. TCL is easy to learn and very powerful. TCL is often used for rapid prototyping, script programming, GUI and testing.
Tcl processes a command in two steps: parsing and execution.
In the parsing step, the rule of the Tcl interpreter is: break the command into words and perform the replacement. The method of parsing each command is exactly the same. In the parsing stage, the Tcl parser does not think that each word has any specific meaning. Tcl only performs a series of simple string operations, such as $input using the string stored in it instead.
In the execution phase, each word in the command has a specific meaning. Tcl takes the first word as the command name, checks whether the command has been defined, and finds the command process that completes the function of the command. If the command has been defined, the Tcl interpreter calls the command procedure and passes all the words in the command to the procedure. The command process will distinguish the meaning of these words according to their needs.
Reference book: Tcl/Tk Introduction Classic (Second Edition) (United States) John K. Ousterhout Ken Jones, translated by Zhang Yuanzhang
Basic syntax example program
The example.tcl file contains common Tcl syntax:
#!/usr/local/bin/tclsh
set AuthorName "Sniper"
#set_app_var : set varibles only in DC
#printvar : DC varibles can be print by printvar cmd
puts "Author name: $AuthorName" ;# puts == echo
echo "Author name: $AuthorName"
#########################################################
echo "\n\n\n\[DEMO\] if-else demo.\n"
set num 0
#if-else
#Note: {} must be split by blank
#Note: this format is fixed
if {
$num == 0} {
echo "num is $num"
} else {
echo "num isn't 0, it is $num"
}
#if-elseif-else
if {
$num} {
echo "num is $num"
} elseif {
$AuthorName == "Sniper"} {
echo "name is $AuthorName"
} else {
echo "error"
}
#########################################################
echo "\n\n\n\[DEMO\] switch demo.\n"
set expression 3
#switch
switch $expression {
0 {
echo "expression is 0"}
1 {
echo "expression is 1"}
2 {
echo "expression is 2"}
3 {
echo "expression is 3"}
}
set expression xyz
#switch-default
switch $expression {
"abc" {
echo "expression is 0"}
"bcd" {
echo "expression is 1"}
default {
echo "expression is $expression"}
}
#########################################################
echo "\n\n\n\[DEMO\] while demo.\n"
set i 0
while {
$i < 10} {
echo "value is $i"
incr i 1 ;#equal to i++;
#set i [expr $i+1] ;#equal to i++;
}
#########################################################
echo "\n\n\n\[DEMO\] for demo.\n"
for {
set i 0} {
$i < 10} {
incr i 1} {
if {
$i == 6} {
echo "attenetion: i is $i"
continue ;#next loop
#break ;#end all loop
}
echo "current i is $i"
}
#########################################################
echo "\n\n\n\[DEMO\] list foreach demo.\n"
set words [list "abc" "def" 12 18]
foreach word $words {
switch $word {
"abc" {
echo "$word matched"}
12 {
echo "$word matched"}
default {
echo "$word"}
}
}
#########################################################
echo "\n\n\n\[DEMO\] file operation demo.\n"
set wfile [open data.txt w+]
set data_to_write "Hello!!\n"
puts $wfile $data_to_write
flush $wfile
close $wfile
set read_data ""
set rfile [open data.txt r]
gets $rfile read_data
close $rfile
echo $read_data
#########################################################
echo "\n\n\n\[DEMO\] function demo.\n"
#function name: max
#parameters: a b
proc max {
a b} {
if {
$a > $b} {
return $a;
} else {
return $b;
}
}
echo "function usage example: max 1 8\n"
Run the program
Under Linux, open Terminal in the directory containing the example.tcl file
(1) First start DC
dc_shell
(2) Run the program
source example.tcl
operation result
[IC@IC example]$ dc_shell
Design Compiler Graphical
DC Ultra (TM)
DFTMAX (TM)
Power Compiler (TM)
DesignWare (R)
DC Expert (TM)
Design Vision (TM)
HDL Compiler (TM)
VHDL Compiler (TM)
DFT Compiler
Design Compiler(R)
Version K-2015.06 for linux64 - May 28, 2015
Copyright (c) 1988 - 2015 Synopsys, Inc.
This software and the associated documentation are proprietary to Synopsys,
Inc. This software may only be used in accordance with the terms and conditions
of a written license agreement with Synopsys, Inc. All other use, reproduction,
or distribution of this software is strictly prohibited.
Initializing...
Initializing gui preferences from file /home/IC/.synopsys_dv_prefs.tcl
dc_shell> source example.tcl
Author name: Sniper
Author name: Sniper
[DEMO] if-else demo.
num is 0
name is Sniper
[DEMO] switch demo.
expression is 3
expression is xyz
[DEMO] while demo.
value is 0
value is 1
value is 2
value is 3
value is 4
value is 5
value is 6
value is 7
value is 8
value is 9
[DEMO] for demo.
current i is 0
current i is 1
current i is 2
current i is 3
current i is 4
current i is 5
attenetion: i is 6
current i is 7
current i is 8
current i is 9
[DEMO] list foreach demo.
abc matched
def
12 matched
18
[DEMO] file operation demo.
Hello!!
[DEMO] function demo.
function usage example: max 1 8
dc_shell> max 12 18
18
dc_shell>