Skip to content

vla5924-practice/compiler-project

Repository files navigation

Compiler

CI Dev Container CodeQL Code coverage

This is a home for Compiler project, a free for use, open source, written from scratch compiler with additional support of LLVM IR code generation and binary executable production.

Language

A compiler supports an imperative Python-flavoured language with static typing and a main entry function. For example, a program that calculates the factorial of aт integer may be written this way:

# Factorial
def fact(n: int) -> int:
    if n <= 1:
        return 1
    return n * fact(n - 1)

# Main function
def main() -> None:
    x: int = input()
    x = fact(x + 0)
    print(x) # output

To learn more about the language, supported types, and constructs, visit the docs.

Compilation flow

The code above can be compiled to LLVM IR or even an executable file by simply running a compiler application:

compiler factorial.py --output factorial.ll

Under the hood, the code from a given input file will be processed in multiple stages using different internal representations:

  • preprocessing,
  • lexical and syntactic analysis (parsing),
  • semantic analysis (verification),
  • optimizing transformations (dead code elimination, constants propagation, etc),

and more. To learn about the overall process, frontend, and backend, visit the docs.

Installation

A compiler can be built from source code using CMake, as well as with the following prerequisites:

  • Ninja (this is a preferred generatior, but Unix Makefiles or MSVC++ are also supported)
  • LLVM 17.0.6 or newer (to enable LLVM IR code generation and binary compilation via clang)

Dev Container

Another way to obtain ready-to-go environment is pull a Docker image, then open repository in Dev Container:

docker pull ghcr.io/vla5924-practice/compiler-project/devcontainer:latest
git clone https://github.com/vla5924-practice/compiler-project.git
devcontainer up --workspace-folder compiler-project

Build with CMake

The following commands can be used to build an application and run the unit testing:

cmake -S compiler -B build -G Ninja
cmake --build build --target cli --parallel
cmake --build build --target run_tests

To learn more about the installation process, build options, and other features, visit the docs.