Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: static vs. dynamic scoping

On Nov 9,  3:08pm, Eric Blake wrote:
} 1. Implementation aspect:
}   How hard would it be to add static scoping to zsh?

Just to elaborate on this, as I've been fooling with parameter scopes a
bit lately (http://www.zsh.org/mla/workers//2010/msg00719.html) ...

Zsh currently maintains scopes as [what amounts to] a parallel stack
alongside the function call stack, with scope depth indexed by the
call stack depth.  A variable expansions finds its object at the
uppermost frame of the scope stack in which that variable's name has
been "mentioned" with either typeset/local/declare or undef; if it is
not found at all, assignment creates it in the global frame.  Note,
though, that the global frame is simply the bottom of the stack, not
handled separately except for some special cases for stack depth 0.

Therefore it's at least moderately difficult to add static scoping.

}   Is it something that can be added in addition to dynamic scoping, via
} the use of an option to select the non-default mode (for example, 'local
} -d' to force dynamic, 'local -s' to force static, and 'local' to go with
} default scoping)?

It might be possible to flag a variable in a given frame as static and
thereby cause it to be skipped when the current call depth does not
match the stack depth where the variable was "mentioned", but due to
the details of the implementation [*] I can't think of any reasonable
way to mark entire frames static or dynamic as implied by having two
different function styles.  Maybe that doesn't matter as long as all
undeclared variables in either kind of scope are global, because then
the state of the frame matters only when "local" is used in that frame.

[*] Cf. "what amounts to" -- in reality, every variable has its own
independent stack which comes into being only when the variable is
"mentioned", so that it's not necessary to walk the stack to find
the uppermost frame for a given variable.

}   If both scoping forms are supported, is it worth making the default
} scoping dependent on posix compliance (for example, 'local' means
} dynamic scoping for 'emulate zsh' but static scoping for 'emulate sh'),
} or should it be the same default for both modes?

As it'd probably be controlled by an option, this probably doesn't
change the difficulty either way.

Messages sorted by: Reverse Date, Date, Thread, Author