Skip to content

mealet/tpl-lang

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation


🧐 What is this?

Toy Programming Language - is a simple compiling language, based on LLVM.
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

πŸ’‘ Installation

  1. Install any of these C compilers: clang, gcc, cc.
  2. Download archive for you'r system from Releases
  3. Unpack it anywhere
  4. Use the binary file (tplc on Linux/Mac, tplc.exe on Windows)

πŸ¦› 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)

πŸ‘Ύ Examples

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)

Binary Operations

int32 a = 10;
int32 b = 2;

print(a + b); // 12
print(a - b); // 8
print(a * b); // 20
print(a / b); // 5

Defining Functions

define int32 foo(int32 a, int32 b) {
 return a * b;
};

print(foo(5, 10)) // 50

Boolean Operations

int32 a = 5;
int32 b = 10;

if a < b {
 print("less!");
} else {
 print("bigger");
};

// "less"

// also supported
a < b
a > b
a == b
a != b

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

πŸ€” Others

Contributing

Check out our Contribution Guide

License

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

Documentation

Documentation is avaible on project's github pages: Documentation