4 ms·
Nim can run almost any code (that doesn’t interface with C FFI) at compile time, since in addition to a compiler they have a bytecode VM that executes a large s
by cyber_kinetist 5y ago
Nim can run almost any code (that doesn’t interface with C FFI) at compile time, since in addition to a compiler they have a bytecode VM that executes a large subset of the language. (https://nim-lang.org/docs/nims.html https://nim-lang.org/docs/nims.html) Really cool stuff, the only other language that I’ve seen doing this approach is Jai, which is sadly still in closed development. (Jai’s CTE is much grander in scope, in that it would be able to do anything normal code can do.)
C++ (gcc and clang) and D’s compile-time evaluation is a bit more limited in scope, and still uses a tree-walk interpreter, although D might also migrate to a bytecode VM someday (https://dlang.org/blog/2017/04/10/the-new-ctfe-engine/ https://dlang.org/blog/2017/04/10/the-new-ctfe-engine/)
- alecthomas 5y agoZig does this too I believe: https://kristoff.it/blog/what-is-zig-comptime/ https://kristoff.it/blog/what-is-zig-comptime/
- cyber_kinetist 5y agoGood catch, forgot about Zig. Although it doesn’t have a fast bytecode VM dedicated for running comptime, Andrew has stated that it will achieve CPython-level performance once the self-hosted compiler is finished, so things might become better. https://github.com/ziglang/zig/issues/4055 https://github.com/ziglang/zig/issues/4055
- WalterBright 5y agoD's doesn't allow global variables or pointer arithmetic or monkey business like converting integers to pointers, the rest is supported.
- d110af5ccf 5y ago> the only other language that I’ve seen doing this approach is Jai Many (most?) Lisps have had full arbitrary CTE since ... I'm not sure, probably before C existed?
- xigoi 5y agoI thought most Lisps were interpreted…
- mtreis86 5y agoCommon myth. Many have an interpreter but compile to ASM. A couple compile to JVM - Clojure and ABCL. One, ECL, compiles to C. Writing a Lisp compiler in Lisp isn't much different than writing an interpreter. See the compiler in Paradigms of Artificial Intelligence Programming for an example.
- moonchild 5y ago> Writing a Lisp compiler in Lisp isn't much different than writing an interpreter Writing a lisp compiler in lisp is a very different task from writing an interpreter. Especially if one expects to bootstrap.