tcl: how to rebuid foreach_in_collection as proc in tcl

218 Views Asked by At

for some reason, I need to rebuild foreach_in_collection (SDC command) as "foreach" in tcl.

here's snapshot code:

proc set_disable_timing { argv } { body ... }
proc get_cells { argv } { body ... }

and here has a proc "foreach_in_collection" I try to build like foreach:

proc foreach_in_collection {item collection script } {
  foreach collection $collection_list {
     uplevel [list set $item $collection]
     uplevel 1 $script
  }
}

and my source file include one code like

set all_cells [get_cells -hier my_cell*]
foreach_in_collection cell $all_cells {
   set_disable_timing $cell
}

but tcl report invalid command "-hier" how to fix issue

2

There are 2 best solutions below

0
glenn jackman On BEST ANSWER

It looks like you want

proc foreach_in_collection {item collection script } {
  upvar x $item
  foreach x $collection {
     uplevel $script
  }
}

When x is set to a value in foreach_in_collection, then cell is set to the same value in the context where foreach_in_collection is called.

Then when you uplevel $script, that is invoking set_disable_timing with the correct value.

0
Cyan Ogilvie On

Isn't this just:

interp alias {} foreach_in_collection {} foreach

That is: it's really just a different spelling of the command foreach.

The native foreach command is more powerful than what is provided for these shims, if you need it to be an error if the args are anything other than item collection script then I think you could do it like:

proc foreach_in_collection {item collection script} {
    tailcall ::foreach $item $collection $script
}