If you want to understand how Nim compiler works, start by looking under the hood of the Nim0 compiler, a Nim-compatible toy language with a compiler and an emulator for a RISC CPU. This project is much smaller than the Nim compiler, everything being packed in 5 Nim files. But most important is that all this is explained in Niklaus Wirth's Compiler Construction book that you'll find in the misc directory with cross-references in source code.
Nim0 is a very limited subset of Nim (every Nim0 source is valid Nim syntax with very limited changes). Many features are missing, like functions or the result instruction. But it still can be used to write quite complex programs like a Sudoku solver (look at the examples directory).
Under Linux, you can easily try it and run your first program in 5 commands:
nimble develop nim0 # Install nim0
cd nim0
nim build # Build nim0 compiler
./nim0 comp examples/Factorial # Compile the Factorial example
./nim0 run examples/Factorial # Run Factorial until overflow...
How can you use Nim0?
I don't care moving Nim0 repository from GitLab to GitHub if it can help collaboration on that project. I can even share it with another maintainer if someone is interested. For the current time, I've created a clone at https://github.com/pmetras/nim0.
But I don't think that this project is worth adding an AST. Wirth created Oberon-0 as an illustration for teaching CS and compiler construction. He wanted to keep it as simple as possible, though touching some important parts of compiler design like records or arrays. Nim0 is a direct port of Oberon-0 compiler with a few enhancements. I kept Wirth's coding style in some parts even if using Nim could make the code nicer or I thought that more modern coding would improve it. It's really a pedagogical toy to learn compilation while reading the book Compiler Construction and trying to do some exercises at the end of chapters.
If I wanted to have a more powerful bare compiler with an intermediate representation, I would start from scratch. Nim0 was thought to be a single-pass in-memory compiler with RISC code interpretation (Wirth even provided an FPGA hardware to execute programs). Adding an AST would require changing the whole code generation module and make the whole program more complex for beginners. As it is, it limits the type of language experiments you can do with it. But the goal of Nim0 is not to replace Nim compiler but to make it easier to understand how it works.
If I wanted to have a more powerful bare compiler with an intermediate representation, I would start from scratch.
As it is, it limits the type of language experiments you can do with it.
Fair enough.