Code Generation
Code Generation
Code Generation
Created: | Tuesday, August 23, 2022 |
---|---|
Author: | David Delassus |
Category: | Compiler Architecture |
Status: | FINAL |
This LEP specifies the techniques used by the compiler to generate code in the target language.
After parsing the source code into an Abstract Syntax Tree, the compiler needs to generate valid Rust code that can be compiled.
Multiple solutions can achieve this goal:
rustc
Generated code is located in a folder named .llbuild
with the following
structure:
The Cargo.toml
file in this folder describe a Cargo workspace, whose members
are the crates located in the modules/
and executable/
folders.
Each Letlang module will be translated to a library crate.
For example, the following module:
module example::main;
# module code
Will produce a crate with the following structure:
One of the modules is defined as a main module. This module must provide a
public main
function.
The compiler will generate an executable crate which depends on that module. Its role is to start the node and spawn the main process:
Every time a module use the import
statement, the imported module is added as
a dependency to the library crate (in the Cargo.toml
file).
For external dependencies (standard library, external projects, …), the
toolchain will fetch the dependency and compile the library crates in the
.llbuild/modules/
folder accordingly.
Using the Rust crate askama
[2], the compiler uses
templates to transform the Letlang AST into source-code.
Finally, the rustfmt
binary should be executed on the output, to facilitate
debugging.
The Rust crate rustc_ast
[3] is unstable and subject to
change.
There are too few ressources at the moment to target the AST directly.
rustc
directlyCargo simplifies the crate dependency management, avoiding re-inventing the wheel.
Reference | Title | Link |
---|---|---|
1 | Cargo | https://doc.rust-lang.org/cargo/ |
2 | askama Rust crate | https://docs.rs/askama/latest/askama/ |
3 | rustc_ast Rust crate | https://doc.rust-lang.org/beta/nightly-rustc/rustc_ast/index.html |