Based on LLVM
This tutorial is a work in process. Beginer will learn to how to build your own
language and i will go deeper into llvm after all basic function have been built.
By the way, i named my language as “L” language.
And i will put more interesting things in L language.
Let’s Start :)
This tutorial showing how to implement your own language step by step. Assumes that you have already know C++, you just need some basic knowledge, go deeper is better :)
At first, you need to know how to compile the compiled language(like C/C++, not Python).
The procedure are showing below:
source code —->Lexer —-> AST(Abstract Syntax Trees) —-> IR —->code generation —-> code optimization —->…
The project does not consider code genration and procedures behind.
I refer the LLVM Documentation. So my project structure is the same as the Documentation. And add more interesting things that you cannot find in that link. Hope that, yeah? Don’t worry, we will talk about that in the future.
What interesting:
The project structure:
|-- Desktop
|-- CMakeLists.txt
|-- README.md
|-- grammar.txt
|-- examples
|-- source_code.txt
|-- src
|-- AST.cpp
|-- Lexer.cpp
|-- Parser.cpp
|-- main.cpp
|-- LJIT.h
|-- run.sh
|-- Codegen.cpp
Device: MacOS 10.1
LLVM: 9.0.0
$ git clone https://github.com/ZH-Lee/LLVM-L-Language.git
$ cd LLVM-L-Language/src
$ sh run.sh
>>> def foo(x) {var i = x+1; return i;};
>>> Read function definition:
define double @foo(double %x) {
entry:
%Faddtmp = fadd double %x, 1.000000e+00
ret double %Faddtmp
}
>>> foo(5);
>>> 6.000000