Hi all,

I've recently started getting my feet wet in playing around with PCT. So far so good. You can see the results of the simple language that I've implemented at http://code.google.com/p/parrotfun/source/browse

In working on the compiler for the simple functional language I noticed that the PAST nodes are not always very well suited to the job of generic language contructs. The first one that I've noticed is that PAST::Block does not support expressing what (I believe) is really common in many languages: closures

PAST::Block with :blocktype('immediate') is really good for creating a new lexical scope such as

  let x = 1
   in x

or in scheme

  (let ((x 1)) x)

PAST::Block with :blocktype('declaration') is a bit wonky, though. When it gets translated to PIR it creates a .sub with and :outer of the ancestor scope. This is ok until you try to start nesting scopes and calling the created functions. They are not real closures. In fact they are not even callable. The .subs created by 'declaration' are halfway between simple non-scoped (or globally scoped) subroutines and functions that close on their lexical scope.

I suggest splitting 'declaration' into two different things: declaration that just creates a .sub without an :outer(), and 'closure' that creates a .sub with the :outer() and calls 'newclosure' on it. You can see at http://code.google.com/p/parrotfun/source/browse/trunk/src/grammar-actions.pl lines 102-120 that that is what I needed to do in order to construct the closures.

I think by encoding some of these common HLL constructs (only need about 7 or 8 of them) pretty much all of the other languages can be built on top of them. Having to resort to using :inline() seems like a hack and makes it harder to work with the AST in later transformation steps.

Which is another thing I noticed which is that using attributes to distiguish the nodes in the AST is not as nice for extensability as actual subclasses. Subclasses allow for using the multi dispatching to deal with diffent cases much more easily than attributes. But that is another discussion. :)

So in summary:

* Leave 'immediate' as is
* Change 'declaration' to not put an :outer() on the created sub (maybe with :lex instead, but I admit that I don't fully understand what :lex does) * Add 'closure' to create a lexical closure ('immediate' but without the call)

Thanks,
Andrew Parker

Reply via email to