[grammarians] variable scoping
MenTaLguY
mental at rydia.net
Sun Nov 27 15:54:29 EST 2005
On Sun, 2005-11-27 at 10:56 -0800, Terence Parr wrote:
> Good to know. So a scope is a lexical scope of the method? For the
> random code I type outside of a method, what "method" is it a part of?
Well, there is an enclosing file-level scope for local variables in that
case which gets used when you're outside a class or method definition.
> Where is there an informal description of scoping rules and general
> syntax?
I've got a bad feeling we're writing it.
The best resources I can think of offhand would be the Pickaxe book
(http://www.rubycentral.com/book/index.html), and Why's (poignant) Guide
to Ruby (http://www.poignantguide.net/ruby), though in the latter case
it's spread pretty thin and very informal indeed.
[ However, the poignant guide is recommended reading simply for its own
sake. ]
Files, class/module definitions, and method definitions all have their
own independent scopes for local variables. A method definition cannot
see local variables in its enclosing class/module definition, and a
class/module definition cannot see local variables in any enclosing
things.
For blocks (that is, anonymous functions), variables introduced within
the block (this includes its parameters) are not visible outside it, but
blocks still capture variables from the enclosing lexical scope. A
block parameter named the same as an already visible variable will alias
that variable rather than shadowing it.
No other constructs (including begin/if/case/rescue/etc...) affect local
variable scoping.
The scoping rules for self, @instance_variables_like_this,
@@class_variables_like_this, and CONSTANTS_LIKE_THIS, are different but
(I believe) are not relevent to parsing.
Basically though, as far as local variable scoping rules go, they are
mostly reflected in the AST.
(scope ...) indicates a "blank slate" scope for local variables;
variables from the parent node (if applicable) are not visible within it
(block ...) indicates a scope in which new local variables can be
declared by first-assignment; existing variables are inherited from the
parent node (enabling lexical closure)
The following ruby fragment:
foo = bar
class Eek
baz = zum
def meep( tot )
zot = um
foo.each { |plok| la = 32 }
end
end
parses as:
(lasgn foo
(vcall bar))
(class Eek
(scope
(block
(lasgn baz
(vcall zum))
(defn meep
(scope
(block
(args tot)
(lasgn zot
(vcall um))
(iter
(call
(vcall foo) each)
(dasgn_curr plok)
(block
(dasgn_curr la)
(dasgn_curr la
(lit #<32>))))))))))
Obviously there is also some evaluator junk in there which has little to
do with syntax, though.
n.b. the current dump-ruby.rb strips the outermost [file-level] (scope
(block ... )) in its output, which now that I think about it is probably
wrong. But I don't know what the actual parser does.
-mental
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
Url : http://rubyforge.org/pipermail/rubygrammar-grammarians/attachments/20051127/d5c37b7b/attachment.bin
More information about the Rubygrammar-grammarians
mailing list