@thejire, in fact under the hood the script compiled into a class and instantiation of this class executes the script body. (With REPL the mechanics is a bit more complex though.) Using internal APIs you can do almost precisely what you described, this is how it works e.g. in https://github.com/gradle/gradle-script-kotlin.
We are investigating possibilities to expose these APIs to the public, and the