Home

Awesome

<div align="center"> <img src="https://github.com/user-attachments/assets/291c4d80-e255-4c17-8543-8528e1a4ddda" /> </br> <img src="https://custom-icon-badges.demolab.com/badge/written_on-rust-blue?style=for-the-badge&logoColor=white" /> <img src="https://custom-icon-badges.demolab.com/badge/based_on-llvm-blue?style=for-the-badge&logoColor=white" /> <img src="https://custom-icon-badges.demolab.com/badge/version-0.3.2-blue?style=for-the-badge&logoColor=white" /> </div>

🧐 What is this?

Toy Programming Language - is a simple compiling language, based on LLVM. </br> Project created to learn and show other people how to create compilers in Rust 🦀

Code separated to 4 modules:

  1. tpl-lexer - lexical analyzer, which turns code into tokens.
  2. tpl-parser - tool for parsing tokens and creating AST.
  3. tpl-ir - codegen module with simple task: translate AST to LLVM Module.
  4. tplc - main part of all project, which contains such things like: cli tool, config parser, llvm module compiler, object linker and etc.

🤖 Tools Used

🦛 Building

  1. Download or clone this repository to your computer.
  2. Install Rust language.
  3. Install LLVM for your system.
  4. Type build command at the main directory:
cargo build --release
  1. Binary file of compiler will be at target/release directory under the name: tplc (or tplc.exe on Windows)

👾 Example

  1. Create file example.tpl and open in any code editor
  2. Write code:
int a = 2; // annotation
int b = a * 2; // annotation using other variables
int c = 2 + 2 * 2; // binary operations priority

print(a); // 2
print(b); // 4
print(c); // 6

a = 2 + 2; // assignment
print(a); // 4

bool flag = true; // boolean type
print(flag); // will print "true"

str greeting = "Hello World!"; // string type
print(greeting); // "Hello World!"

// if-else construction

if 1 < 2 {
    print("1 is less than 2");
};

if 2 != 2 {
    // code
} else {
    print("2 = 2");
};

// loops
int a = 0;

while a < 5 {
    a += 1;
    // or
    a++;
    print(a);
};

for i in 5 {
    print(i);
};

// tests in variables
bool test = 1 + 1 == 2;

// defining functions
define int foo(int a, int b) {
    print("hello from foo function!");
    return a + b;
};

// calling functions
foo(4, 2);

// calling functions in variables annotation or assignment
int a = foo(4, 2);
a = foo(5, 5);
  1. Compile it by command:
tplc example.tpl output
  1. And run like binary file:
./output
<details> <summary><h2>😵 Errors Examples</h2></summary>

image </br> image </br> image </br> image </br> image </br> image </br> image </br>

</details>

💀 License

Project licensed under the BSD-3 License. More information in LICENSE file