From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 25437 invoked by alias); 10 Nov 2010 16:50:41 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 28402 Received: (qmail 22400 invoked from network); 10 Nov 2010 16:50:40 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <101110085030.ZM6206@torch.brasslantern.com> Date: Wed, 10 Nov 2010 08:50:30 -0800 In-reply-to: <4CD9C64E.8060307@redhat.com> Comments: In reply to Eric Blake "static vs. dynamic scoping" (Nov 9, 3:08pm) References: <4CD9C64E.8060307@redhat.com> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: Eric Blake , zsh-workers@zsh.org Subject: Re: static vs. dynamic scoping MIME-version: 1.0 Content-type: text/plain; charset=us-ascii 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.