DesignCompiler学习(2)- Tcl基础语法

Tcl简介

Tcl (最早称为“工具命令语言”“Tool Command Language”, 但是目前已经不是这个含义,不过我们仍然称呼它为TCL)是一种 脚本语言。 由John Ousterhout创建。 TCL很好学,功能很强大。TCL经常被用于快速原型开发、脚本编程、 GUI和测试等方面。

Tcl处理一个命令分两步:解析和执行。
在这里插入图片描述

在解析这一步,Tcl解释器的规则是:将命令分解为单词,并执行替换。对每个命令进行解析的方法是完全一样的。在解析阶段,Tcl解析器不认为各单词有任何具体的意义,Tcl只是进行一系列简单的字符串操作,如$input使用其中存放的字符串代替。
在执行阶段,命令中的各单词都有了具体的意义。Tcl把第一个单词作为命令名称,检查这个命令是否已经定义,并且查找完成该命令功能的命令过程。如果命令已经定义,Tcl解释器就调用该命令过程,把命令中的全部单词传递给该过程。命令过程会根据自己的需要来分辨这些单词的意义。

参考书:Tcl/Tk入门经典(第二版) (美)John K. Ousterhout Ken Jones 著 张元章 译

基础语法实例程序

example.tcl 文件中包含了常见的Tcl语法:

#!/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"

运行程序

Linux下,在包含example.tcl文件的目录中打开Terminal

(1)首先启动DC

dc_shell

(2)运行程序

source example.tcl

运行结果

[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> 

猜你喜欢

转载自blog.csdn.net/meng1506789/article/details/109727307