6.26.1 How do I read outer locals?

As long as you only read the value of locals, you can duplicate them as needed, so a way to convert an access to an outer local for flat closures is to just pass the values on the stack to the closures and define them again as locals there. Here’s an example: Consider the following code for a hypothetical Gforth with a quotation-like syntax for lexical-scoping closures:

\ does not work; [:d would dictionary-allocate the closure
: ...
  ... {: a b :} ...
  [:d ...
    ... {: c d :} ...
    [:d ... a b c d ... ;]
    ...
  ;]
  ... ;

you can convert it to flat closures as follows:

: ...
  ... {: a b :} ...
  a b [{: a b :}d ...
    ... {: c d :} ...
    a b c d [{: a b c d :}d
      ... a b c d ... ;]
    ...
  ;]
  ... ;

Only those locals that are read in the closure need to be passed in.

This process is called closure conversion in the programming language implementation literature.