David Kastrup <d...@gnu.org> writes: > Basically I need to evaluate dynamic code in a given lexical environment > rather than at top and/or module level. > > For a language that is supposed to be a building block for extension > languages, not really a concept that is all that unusual I would think.
Guile 2 is an excellent base for building extension languages, but not in the way that you'd like to do it. Unfortunately, I see no way to support `procedure-environment' on arbitrary procedures without abandoning our optimizing compiler and going back to a simple evaluator. I suspect it would be possible to implement a special form that captures its lexical environment in such a way that arbitrary code could later be evaluated within that lexical environment. The presence of this special form would impose onerous constraints on the optimizer within the top-level form containing it. In fact, I can't think of an optimization that would still be possible, because the compiler would have to assume the worst: that some other thread could, at any time, mutate any lexical variable or call any lexical procedure visible from the special form. It gets even worse when you consider first-class continuations. I believe that this is the wrong approach, though it may be worth considering for the sake of allowing Lilypond to continue using its existing implementation strategy. In general, the _right_ way to build a custom extension language using Guile 2 is to write a compiler that converts your language into one of the other languages that Guile 2 supports. If there's something about Lilypond's language that you believe would make compilation impractical, let's talk about it. Maybe the Guile experts on this list can find a clever solution, or else maybe we can enhance Guile to support Lilypond's language in a straightforward manner. I would be glad to help with this. In the long run, it might be less work for us Guile hackers to implement a nice compiler for Lilypond than to implement and forever maintain the "capture-lexical-environment" special form, and it would almost certainly have better results. Regards, Mark