Skip to content

carsonetb/cblang-llvm

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CBLang: LLVM

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.

Example

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.

Features

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 string and array are 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

Planned features

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

Usage

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

Documentation exists in the docs folder, which can be generated using sphinx.

About

A compiler for my programming language, CBLang.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages