Tcl 脚本还是 Perl?

Tcl Script OR Perl?

我希望使用脚本替换以下 verilog 代码。

assign x0 = in0 + in7;

我想搜索上面的“+”号并将整行替换为下面的行:

KSA_32 U1(.A(in0), .B(in7), .Sum(x0));

对此有什么建议和示例脚本吗?

如果您的 Verilog 文件能够轻松地放入内存中,您可以简单地执行以下操作:

# Read in the file
set f [open $verilogfile r]
set contents [read $f]
close $f

# Perform the transform across the whole contents
regsub -all {assign\s+(\w+)\s*=\s*(\w+)\s*\+\s*(\w+);} $contents \
        {KSA_32 U1(.A(), .B(), .Sum());} contents

# Write the results out to a new file (different filename so you can check the results by hand)
set f [open $verilogfile.new w]
puts -nonewline $f $contents
close $f

第一块和第三块是用于文件操作的标准 Tcl 模式。第二个是标准的正则表达式替换,我通过获取您的要求并猜测什么是模板来实现。注意文字 + 需要转义,空格最好匹配为 \s+\s*.