How to implement continuations?


I'm working on a Scheme interpreter written in C. Currently it uses the C runtime stack as its own stack, which is presenting a minor problem with implementing continuations. My current solution is manual copying of the C stack to the heap then copying it back when needed. Aside from not being standard C, this solution is hardly ideal.

What is the simplest way to implement continuations for Scheme in C?


If you are starting from scratch, you really should look in to Continuation Passing Style (CPS) transformation.

Good sources include "LISP in small pieces" and Marc Feeley's Scheme in 90 minutes presentation.

The traditional way is to use setjmp and longjmp, though there are caveats.

Here's a reasonably good explanation

Besides the nice answers you've got so far, I recommend Andrew Appel's Compiling with Continuations. It's very well written and while not dealing directly with C, it is a source of really nice ideas for compiler writers.

The Chicken Wiki also has pages that you'll find very interesting, such as internal structure and compilation process (where CPS is explained with an actual example of compilation).

By : Jay

This video can help you solving your question :)
By: admin