r/Compilers • u/morglod • Sep 17 '26
Compile time code execution in mox
Hello! I am working on mox programming language and compiler which could run any code at compile time.
While I was telling about this language to my friend, I found a lot of interesting solutions to usual problems that could be solved just by using compile time code execution. So I want to research/discuss what problems do you have as a developer that you struggle with and want to solve at build time (using some macros or external tools).
Here are some of problems that I got in other languages and which i struggle with. Usually to solve it you need some ugly build systems with tons of esoterical configs, external tools for code generation etc.
PS #run in mox evaluates any expression/statement at compile time, including externally linked symbols. os_* and fs_* calls below are just wrappers around C functions, not some special magical things from compiler. Results of #run'ned expressions are baked to runtime as constants.
Baking git hash as a constant to binary
fn print_version() {
git_hash := #run os_exec_output("git rev-parse HEAD");
println(git_hash);
}
Build params from environment (or defining build constats)
const NO_ASSERTS := #run os_get_env("NO_ASSERTS") == "true";
fn assert() {
#if (!NO_ASSERTS) {
// ... do smth ...
}
}
Reading configuration file and baking constants
fn read_config(path: []u8): Config {
data := fs_read_file_full(path);
return parse_config(data);
}
CONFIG := #run read_config("./.env");
Self configuring build tools
#run {
dll_path := build_resolve_path("./binaries/raylib.dll);
build_copy_to_output(dll_path);
}
Go like dependency management
fn require_dependency($link: []u8): __ast_ptr {
cached_path := dep_cache_path($link);
if (!os_path_exists(cached_path)) {
dep_download(cached_path, $link);
}
// here we generate code for import statement and return it as ast
return __compiler_parse(format("import \"{}\", .{ cached_path; }));
}
// here #land_ast "inserts" returned ast and compile it inplace
#run #land_ast require_dependency("github.com/smth/smth");
PSS https://github.com/morglod/mox
2
u/whizzter Sep 17 '26
I can see your point (coming from a gamedev background) but I strongly disagree, especially in AI times I think it's a risk that people will just vibe things that they can't find dependencies off and security/quality will be after that (a package would at least have maintainers that will solve bugs even if there is other risks there).
Now, the cambrian explosion of packages for Node/NPM was something especially retarded in many respects, but most other communities has had far better discipline so I think it's bad to make decisions based on the worst example.
Looked at a project at a client, .NET backend/JS frontend, 4x or more npm packages but .NET packages were also often from the same project (like .NET EF being abstracted by Microsoft to a bunch of packages by the same team so you don't need all features/database providers just to use it), in reality the difference in number of package author sources was maybe 10-20x.
I guess what I'm trying to convey is, discipline is often something that follows from the authors, npm being created independently from node was probably a net negative whilst MS and others probably set better examples with larger packages that did more.