A Mila (Pascal-like, expression-driven language) compiler with LLVM IR backend.
CMakeLists.txt
- Top-level CMake build configurationCMakeModules/
- CMake helper modules (e.g., for testing)external/
- External dependencies (LLVM 19.1)mila-compiler/
- Main compiler source codeast/
- Abstract Syntax Tree (AST) definitions and visitorscodegen/
- LLVM IR code generation logicexception/
- Exception and error handlinglexer/
- Lexical analysis (tokenizer)parser/
- Parsing logic (syntax analysis)main.cpp
- Compiler entry point
mila
- Wrapper script to build and run the compiler, and link outputsamples/
- Example Mila source filestests/
- Input/output test cases for sample programs
The AST is designed using the Visitor pattern, making it easy to extend with new operations such as pretty printers, AST printers, or other analyses without modifying the AST node classes themselves.
Compilation tests and output validation for sample files are implemented using ctest
and defined in CMake modules.
Recommended version: 19.1
A submodule for LLVM 19.1 is provided, and you can build from the submodule if desired.
To build your own LLVM, enable BUILD_LLVM=ON
.
This project uses the standard CMake toolchain. If no CMAKE_BUILD_TYPE
is set, it will automatically build in release mode.
cmake -Bbuild
cd build
cmake --build .
The built compiler outputs LLVM IR, which can be further compiled to a binary.
Install dependencies via Homebrew:
brew install git
brew install cmake
brew install llvm@19
brew install gnu-getopt
Then update your shell rc file (e.g., .bashrc
, .zshrc
):
export PATH="/usr/local/opt/gnu-getopt/bin:$PATH"
The mila
script in the project root is a wrapper that builds and runs the compiler, then uses llc
and clang
to produce an executable. Example usage:
./mila <source.mila> -o <output_binary>
This will:
- Run the compiler to generate LLVM IR (
.ir
file) - Use
llc
to generate assembly (.s
file) - Use
clang
to produce the final executable
From inside the build
directory, you can use the ctest
command to run all tests.
Tests are defined to:
- Compile all example source codes in
samples/
- Run the resulting executables and compare their output with expected output in
tests/
CTest is configured via CMakeModules/tests.cmake
and uses CMakeModules/run_test.cmake
to automate input/output checking.
First, create a source file:
touch test.mila
Then compile it:
./mila test.mila -o test.out