On Mon, Nov 28, 2005 at 03:22:10PM -0500, Will Coleda wrote:
> Tcl variables can be either globals or lexicals (inside a [proc]).  
> So, all the code to get a PMC from a varname was factored out to a  
> single routine that returns the right PMC (global or lexical)  
> depending on call level.

I think I understand: You're creating a subroutine to manipulate a
pad, and the subroutine is a common utility which can't specify :outer
for all its callers, and find_lex only follows :outer.  So you have to
use interpreter introspection to find the pad.

>
> >>.sub find_lex_pdd20
> >>  .param string variable_name
> >>
> >>  .local pmc interp, lexpad, variable
> >>  .local int depth
> >>  interp = getinterp
> >>  depth = 2 # we know it's not us or our direct caller.
> >>
> >>get_lexpad:
> >>  # Is there a lexpad at this depth?
> >>  lexpad = interp["lexpad";depth]
> >>  unless_null lexpad, got_lexpad
> >>
> >>  # try again
> >>  inc depth
> >>  goto get_lexpad
> >>got_lexpad:
> >>  variable = lexpad[variable_name]
> >>  .return(variable)
> >>.end
> 

So: This subroutine's loop is technically unnecessary, but convenient:
The depth of your utility subroutine from the user-written subroutine
with :lex is knowable at compile time, so you could rewrite the
function to take a depth parameter and just grab the right LexPad on
the first try.  But you find it more convenient to loop up the call
stack rather than have to calculate the depth everywhere.  (Right?)

Also: If it weren't for the fact you want to search lexicals and
globals every time, you could just use find_lex.

Therefore: Something along the lines of find_var could help you.

PS: I'd forgotten the lex opcodes used to follow the *call stack*.
    My unholy $DEITY, they were even more broken than I remembered.
-- 
Chip Salzenberg <[EMAIL PROTECTED]>

Reply via email to