Simple PoC language that takes a one line arithmetic calculation like ((5*5) + 2)
and outputs the final value in a clang generate binary from LLVM IR. The main learnings here were focused around the high level architecture rather than the intricacies of the language. It uses LALRPOP and llvm-sys for my own learning (and anyone who is starting out with these tools) around LALRPOP and LLVM:
- To parse a grammar, which in this case is a simple one line arithmetic i.e
((5*5) + 2)
using LALRPOP taken from this tutorial - To then compile that to LLVM IR with llvm-sys
- To generate that from LLVM IR to a native executable using clang
flowchart LR;
CalculonLanguage-->Parser;
Parser-->|LALRPOP| AST;
AST-->CalculonCalculations;
CalculonCalculations-->|llvm-sys| LLVM-IR;
LLVM-IR-->|clang| Binary;
Going to extend this as a seperate language to include a more complex grammar. Keep this here with the simple language as it demonstrates high level what architecture is needed for a language that uses LLVM as the backend. And also extend the LLVM IR in the next iteration to do optimizations on the IR code!
Currently set up for arm64 (m1 Mac) with LLVM (13), just change this line in the code to the required architecture
Install LLVM 13
brew install llvm@13
Set LLVM_SYS_130_PREFIX variable before you run cargo run
export LLVM_SYS_130_PREFIX=/PATH/TO/LLVM13/VERSION
Some examples are in the example folder, just run
cargo run example/calculator.calculon