This is an implementation of my programming language, CBLang using the LLVM compiler infrastructure. This means that the language can be compiled to native machine code. It contrasts with the tree-walk interpreter I made in C++ for the language. The compiler is written in Python, using llvmlite, which is the only dependency of this project.
Below is an example CBLang script for calculating one billion fibonacci numbers, although because of floating point they quickly become invalid after about 50 numbers:
scope fibonacci(int n) -> int = {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
int a = 0;
int b = 1;
int i = 2;
while (i <= n) {
int c = a + b;
a = b;
b = c;
i = i + 1;
}
return b;
}
scope main = {
fibonacci(1000000000);
}
On my Core i5-13600KF, this process finished in about 0.7s, as opposed to my tree-walk implementation which could do 1000 numbers in about 0.2s, meaning the same calculation would take about 55 hours. More extensive performance testing will follow once the implementation is more complete.
This is a list of features that are currently implemented:
- Types: basic int, float, char, bool, string, and array types
- Control flow statements: 'if' and 'while' are currently supported
- Global function definitions, calling functions, etc
- Custom class definitions, with member variables and functions. Although
this is a compiled language, the user doesn't have to worry about memory
management. All user types and
stringandarrayare reference counted automatically at runtime. - Implicit casting between types, for example passing an int to 'print' will work even though 'print' accepts a string argument
- Complex expressions
This is a list of features that I want to implement in the future. It is not ordered in terms of importance:
- Variadic arguments
- Generics
- For statements
- Types can define other types they can be casted from
- Types can define custom operators
- Fixing the obvious self-referencing problem
Because this is very early stages, obviously you should not use this for any
project, but if you just want to try it out, it's pretty simple. Just clone
this repository, create a python venv
and install the requirements.txt file. Run the main.py file, which takes
two arguments:
- The file to compile, the example (my testing file) is located in
example/test.cblang. - The folder to build to. Inside the folder the program will put the generated LLVM IR, the object file generated by LLVM, and the executable program.
Documentation exists in the docs folder, which can be generated using
sphinx.