hy icon indicating copy to clipboard operation
hy copied to clipboard

Make `HyASTCompiler` (more) stateless

Open brandonwillard opened this issue 5 years ago • 0 comments

In order for a compiler instance to—for example—simply [re]use itself when calling hy_eval (e.g. during eval-and-compile), we need to make the compiler "stateless". Current HyASTCompiler class members imports and temp_if attach state to a compiler instance that prevent its immediate reuse.

To replace imports, we could enable consistent tracking/use of Result objects and the imports within those. Basically, we can—and should—keep all "state" within Result objects (or a suitable replacement/analog, e.g. annotated/stateful AST type). This looks like it will involve an interface-like change to the compiler, though; namely, that all HyASTCompiler.compile_* methods must return and take Result objects. While that's often the case (well, the former at least), it's not always true, and this inconsistency isn't good for the compiler's design.

For instance, HyASTCompiler.compile_symbol doesn't return a Result object, but it can be made to do so, and—with that change—it can assign stdlib auto-imports to the current state of compilation via Result.imports instead of the actual compiler instance. Likewise, with that in place, we can make HyASTCompiler.imports_as_stmts produce AST for a Results's imports and return those in another Result object—bringing it more in-line with its adjacent compile_* methods. Additionally, the logic currently in hy_compile could then be moved to HyASTCompiler.imports_as_stmts and disentangle essential compilation logic from hy_compile.

At the very least, these changes would get us closer to a compiler that better isolates its functionality and is significantly easier to extend and debug.

Regarding HyASTCompiler.temp_if, I'm having a hard time understanding exactly what its functional "intentions" are and the exact scope/constraints of its role. I can see that it's attempting to avoid the unnecessary use/creation of temporary variables in an if*, but how it's using the compiler state to do this isn't very clear or well contained. I can't tell whether or not the addition of a temp_if field to Result would work, or if there's something much simpler that would avoid such an undesirable addition.

Related issues and ideas: #1542, #1482, this comment in #1697.

Originally posted by @brandonwillard in https://github.com/hylang/hy/pull/1700#issuecomment-437704110

brandonwillard avatar Nov 11 '18 21:11 brandonwillard