Re: How to implement lexical closures?

Gene <gene.ressler@gmail.com>
Wed, 12 May 2010 23:05:51 -0700 (PDT)

          From comp.compilers

Related articles
How to implement lexical closures? grom358@gmail.com (grom) (2010-05-06)
Re: How to implement lexical closures? sleepdev@gmail.com (andy johnson) (2010-05-09)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-09)
Re: How to implement lexical closures? cr88192@hotmail.com (BGB / cr88192) (2010-05-09)
Re: How to implement lexical closures? torbenm@diku.dk (2010-05-10)
Re: How to implement lexical closures? grom358@gmail.com (grom) (2010-05-11)
Re: How to implement lexical closures? cr88192@hotmail.com (BGB / cr88192) (2010-05-12)
Re: How to implement lexical closures? gene.ressler@gmail.com (Gene) (2010-05-12)
Re: How to implement lexical closures? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-05-13)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-15)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-15)
Re: How to implement lexical closures? gah@ugcs.caltech.edu (glen herrmannsfeldt) (2010-05-16)
Re: How to implement lexical closures? gneuner2@comcast.net (George Neuner) (2010-05-17)
Re: How to implement lexical closures? cfc@shell01.TheWorld.com (Chris F Clark) (2010-05-17)
[3 later articles]
| List of all articles for this month |
From: Gene <gene.ressler@gmail.com>
Newsgroups: comp.compilers
Date: Wed, 12 May 2010 23:05:51 -0700 (PDT)
Organization: Compilers Central
References: 10-05-031
Keywords: storage, symbols
Posted-Date: 13 May 2010 13:02:06 EDT

On May 7, 12:22 am, grom <grom...@gmail.com> wrote:
> I'm working on a toy interpreter (http://code.google.com/p/zemscript/)
> and trying to implement lexical closures (http://code.google.com/p/
> zemscript/source/browse/#svn/branches/lexical_scope) ....


> However since it copies the symbol table the following does *not*
> work:
> create = function() {
> x = 0;
> return {
> "get" : function() { return x; },
> "set" : function(v) { x = v; }
> };
>
> };
>
> o = create();
> o["set"](42);
> println(o["get"]()); // Should print 42
>
> What I can't work out is how I can get the second example to work
> without breaking the first example.


A closure is a pair consisting of code and an activation record. The
activation record is an array of slots for values of parameters (and
local variables if your language has them). In lanuages like yours
with lexical nesting, there will also be a frame pointer to the
activation record of the lexically enclosing scope. The code must
refer to activation record slots. If you even minimally compile
(rewrite) the input program, you can convert identifiers into
activation record offsets. In some contexts this is called Boehm
numbering. Alternately, you can - as has been said - keep runtime
symbol tables for each function that map identifiers to offsets. In a
stack-oriented language, the activation records are what go on the
stack. In languages where functions are first class values, they are
generally allocated on the heap and therefore must be garbage
collected. In many cases, optimizers for such languages look for
opportunities to use a stack in lieu of heap wherever possible to
reduce garbage.



Post a followup to this message

Return to the comp.compilers page.
Search the comp.compilers archives again.