How to perform expansions in a string in the context of a calling function in tcl -
How to perform expansions in a string in the context of a calling function in tcl -
how can implement expand
in script below (expanding commands , variables normal in tcl), want print out:
' hello { $b } {tcl world} '
this how imagine script look:
proc clever_func {script} { set script [uplevel 1 expand [list $script]] puts "'$script'" } proc isolated_client_function {} { set hello set b hard set c tcl set d world clever_func { $a { $b } [list $c $d] } } isolated_client_function
an illustration isn't sufficient replacement specification, seems there no built-in tcl facility doing want. possible perform substitution on single command tail (by prepending list
command , eval
ling @ level want), not pseudo-script 2 "commands". (it's possible string interpolation does, using subst
, know why it's not want: expand $b
).
i see 2 possibilities want:
tokenize input using sugar::scripttolist
, perform substitution manually in list of tokens, convert result textual form sugar::listtoscript
. these features of sugar
macroprocessor designed allow modify scripts semantically while preserving formatting, comments , layout.
break downwards input separate "commands": first split @ each newline , semicolon, utilize info complete
collect pieces corresponding finish "commands" (iirc there corner cases backslash-newline continuation: beware). on each finish "command" utilize trick of prepending "list "
, evaluating result in necessary context (uplevel
). each command you'll list substitution has been performed when appropiate. lose nuances of formatting within each "command" (like number of spaces separating words , kind of space), , lose original command separators unless take care remember them yourself. might not bad if goal kind of "pre-expanded script" eval
uated later in other context.
tcl
Comments
Post a Comment