return when desugaring to closures

Neil Mix nmix at pandora.com
Fri Aug 22 07:27:28 PDT 2008


>>
>> Since generators are shallow, it seems clear that there
>> should be some local cps-style transform of generators into the
>> remainder of the language. However, it would be a relief to know what
>> that cps-style transform actually looks like, and what edge cases it
>> has. Anyone care to post a proposal?
>
> Can't be done without defining the entire language in CPS. Yes, it's
> shallow, but a captured activation can involve any arbitrary  
> combination
> of expressions other than function call.

I've done meta-circular implementations of generators (or something  
like them) twice now.  It's amateur work ;) so put it in the FWIW  
category, but in case you find it useful "research":

A) http://www.neilmix.com/narrativejs/doc/ This does source  
transformation of code into suspendable/resumable "partial  
continuations" (see http://groups.google.com/group/narrativejs/browse_thread/thread/9e4ade2924bd8ab0 
  )

This project was more ambitious than implementing generators since it  
was capturing multiple stack frames.  Even so, I found the most  
difficult part of the transformation to be handling flow control  
without goto.  I'm pretty sure that I never got it quite right,  
especially in exception handlers that contain break or continue.

B) http://neilmix.googlecode.com/svn/trunk/generators/js.js A meta- 
circular implementation of a JS evaluator that supported generators  
(intended to be used with function.toString(), so the parser is overly  
forgiving, ignore the parser.)

My strategy in this one was to parse into "bytecode" and run in a mini- 
interpreter for the bytecode.  This was way easier because you get to  
use goto in the interpreter and the current state of the interpreter  
can be captured in a closure, making it easily suspendable/resumable.   
It was also much easier to discover and fix the bugs that I never even  
considered in narrativejs.









More information about the Es-discuss mailing list