Awesome
<div align="center"> <img src="https://github.com/user-attachments/assets/291c4d80-e255-4c17-8543-8528e1a4ddda" /> </br> <img src="https://tokei.rs/b1/github/mealet/tpl-lang?branch=main&style=for-the-badge&color=%230389f5" /> <img src="https://img.shields.io/badge/dynamic/toml?url=https%3A%2F%2Fraw.githubusercontent.com%2Fmealet%2Ftpl-lang%2Frefs%2Fheads%2Fmain%2FCargo.toml%3Fraw%3Dtrue&query=workspace.package.version&style=for-the-badge&label=Version&color=%230389f5" /> </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:
tpl-lexer
- lexical analyzer, which turns code into tokens.tpl-parser
- tool for parsing tokens and creating AST.tpl-ir
- codegen module with simple task: translate AST to LLVM Module.tplc
- main part of all project, which contains such things like: cli tool, config parser, llvm module compiler, object linker and etc.
🤖 Tools Used
- Programming Language: Rust
- Code Generator: LLVM
- LLVM Library: Inkwell
- Colored Terminal Library: Colored
💡 Installation
- Install any of these C compilers:
clang
,gcc
,cc
. - Download archive for you'r system from Releases
- Unpack it anywhere
- Use the binary file (
tplc
on Linux/Mac,tplc.exe
on Windows)
🦛 Building
- Download or clone this repository to your computer.
- Install Rust language.
- Install LLVM for your system.
- Type build command at the main directory:
cargo build --release
- Binary file of compiler will be at
target/release
directory under the name: tplc (or tplc.exe on Windows)
Types
int8 // - 8 bit integer number
int16 // - 16 bit integer number
int32 // - 32 bit integer number
int64 // - 64 bit integer number
str // - string type
bool // - boolean type (true, false)
void // - void type (better for functions)
Boolean Operations
int32 a = 10;
int32 b = 2;
print(a + b); // 12
print(a - b); // 8
print(a * b); // 20
Defining Functions
define int32 foo(int32 a, int32 b) {
return a * b;
};
print(foo(5, 10)) // 50
Compares
int32 a = 5;
int32 b = 10;
if a < b {
print("less!");
} else {
print("bigger");
};
// "less"
Loops
int32 counter = 0;
// while
while counter < 10 {
print(counter);
counter += 1;
};
// for
for count in 10 {
print(count);
};
Strings
str a = "Hello";
str b = ", World!";
print(concat(a, b)); // Hello, World!
print(a, b); // Same as previous
Lambda functions
fn<int64> fib = int64 ( int64 index ) {
int64 left = 0;
int64 right = 1;
int64 result = 0;
for i in index {
result = left + right;
left = right;
right = result;
};
return result;
};
int64 result = fib(1000);
print(result) // 9079565065540428013
Boolean types
bool a = true;
bool b = false;
print(a, b) // true false
Pointers
int32 a = 5;
int32* b = &a;
print(a); // 5
*b = 100;
print(a); // 100
Sub-functions
define int32 foo(int32 a) {
return a * 2;
};
int32 value = 5;
print(value.foo()); // 10
Arrays
int32[5] a = [1, 2, 3, 4, 5];
// or
auto a = [1, 2, 3, 4, 5];
print(a); // [1, 2, 3, 4, 5];
Type function
int32 a = 5;
int8 b;
bool c = false;
print(a.type()); // int32
print(b.type()); // int8
print(c.type()); // bool
Conversions
int32 a = 5;
int8 b = a.to_int8();
str c = b.to_str();
print(a.type()); // int32
print(b.type()); // int8
print(c.type()); // str
</details>
🤔 Others
Contributing
Check out our Contribution Guide
License
Project licensed under the BSD-3 License. More information in LICENSE file