项目作者: ZH-Lee

项目描述 :
Based on LLVM
高级语言: C++
项目地址: git://github.com/ZH-Lee/LLVM-L-Language.git
创建时间: 2019-10-30T08:51:30Z
项目社区:https://github.com/ZH-Lee/LLVM-L-Language

开源协议:

下载


LLVM tutorial in C++

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 :)

Table of Contents

Chapter #1 Introduction

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 :)

Basic Procedure of Compiling a program

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.

The Project Structure

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:

  • more data type
  • more straightforward, it combines C++ and Python together.
  • learn to build your own language.

The project structure:

  1. |-- Desktop
  2. |-- CMakeLists.txt
  3. |-- README.md
  4. |-- grammar.txt
  5. |-- examples
  6. |-- source_code.txt
  7. |-- src
  8. |-- AST.cpp
  9. |-- Lexer.cpp
  10. |-- Parser.cpp
  11. |-- main.cpp
  12. |-- LJIT.h
  13. |-- run.sh
  14. |-- Codegen.cpp

Environment

  1. Device: MacOS 10.1
  2. LLVM: 9.0.0

Download and Use the project

  1. $ git clone https://github.com/ZH-Lee/LLVM-L-Language.git
  2. $ cd LLVM-L-Language/src
  3. $ sh run.sh
  4. >>> def foo(x) {var i = x+1; return i;};
  5. >>> Read function definition:
  6. define double @foo(double %x) {
  7. entry:
  8. %Faddtmp = fadd double %x, 1.000000e+00
  9. ret double %Faddtmp
  10. }
  11. >>> foo(5);
  12. >>> 6.000000

TODO List

  • Add For expression
  • Add more data type like int, bool, etc.
  • Add pointers
  • Add array

Chapter #2 Lexer