Closed boggle closed 4 years ago
That should be possible by simply using the self-hosted compiler as a library. Or do you have some more specific functionality in mind?
Great that the compiler is a library but a little help can go a long way. How much support syntax/utility functionality would be needed to make this as nice as say evaluating an symbolic expression in lisp? What would be needed so that the compiled code can partially depend on state available in the context?
Ideally Im looking for something similar to what one can do with staging in scala or runtime code evaluation in Julia or lisp, including
with the goal of making it easy to write a code generator by simply composing syntax objects
I guess #68 is related and incremental builds since it practically implements half of this with the rest being an interface issue.
Yes, hot code loading is one half of this. The other half is type system and syntax support. Scala 3/dotty supports staging like so: https://dotty.epfl.ch/docs/reference/metaprogramming/staging.html (Paper: https://biboudis.github.io/papers/pcp-gpce18.pdf)
"Code generation" covers quite a lot of diverse territory, and each application has different requirements and ergonomics. In the interest of not getting them all mixed up, here are some personal thoughts on each of them. I hope this is not too far off topic.
#run
and #bake
comptime
and inlining directives. Arbitrary compiletime code execution and argument inlining would expand the scope further.In summary, compiletime code specialization is already supported, although some capabilities could still be added. Runtime specialization would be interesting, but I'm not sure what it would look like. Syntactic metaprogramming probably goes against the spirit of Zig. Zig as a backend: probably yes, but too early to tell, since the stage 2 is still pretty new. Zig REPL / embedded Zig: can be done, but unclear benefits.
There are no plans to make zig anything beyond an Ahead Of Time compiler.
zigs cross-platform focus makes it appealing for writing compilers or as IL for compilers.
What would it take to add runtime code generation to zig, i.e. a facility for explicitly constructing zig ast (either by parsing string literals or programmatically, using some API), compiling such an ast in-process, and running the result? Is anything like that on the roadmap?
Even if execution isn't immediately feasible for all targets, compilation to e.g. webassembly follwed by interpretation might still be useful.