Generators in lone lisp

Source: matheusmoreira.com | Rating: ⭐⭐⭐⭐ (4/5)

Article exploring the implementation of generators in Lone Lisp, progressing from delimited continuations to coroutines to semicoroutines.

Key Insights:
  • Delimited continuations are too powerful for generators - the memcpy cost is too high for hot paths
  • Coroutines provide separate stacks that can be switched instead of copied
  • Semicoroutines restrict control flow to return to the caller, simplifying implementation
  • Generator state is represented by stack: empty = finished, null top = not started

The author explains how they evolved from using delimited continuations (which required copying entire stack frames) to a more efficient approach using stack swapping. The key insight is that generators need to yield control back to their callers, making semicoroutines the ideal abstraction.

The implementation tracks three states: not started (stack top equals base), executing (has caller stack), suspended (no caller stack), and finished (top is null).

Lisp Generators Coroutines Continuations Programming Languages