Skip to content

crochethk/klang-compiler

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

The Klang Compiler

A compiler for the Klang programming language.

Table of Contents
  1. About The Project
  2. How it works
  3. Getting Started
  4. Roadmap
  5. License

There is also a VSCode Syntax Highlighting Extension available.

About The Project

This project is a compiler for the "Klang" programming language, developed as part of a Compiler Construction course. The compiler's main focus is to produce linux compatible GNU Assembly Code from a given Klang source code file. It also is capable of generating Java Byte Code (JBC) – though the supported language features are very limited at the moment.

The language's syntax is slightly inspired by Rust and a program may look something like the following screenshot:

Example Klang Program

You can find out more about the structure and how to write a Klang program here. Also there is an examples folder with some basic programs.

How it works

The language is defined using ANTLR 4 grammar which is then used to generate an according Lexer and Parser. The latter creates a Parse Tree which is then transformed into an Abstract Syntax Tree (AST). The AST consists of Nodes implementing the Visitor Pattern which are traversed by several visitors, each performing the respective task on each Node of the tree. In summary there is a PrettyPrinter, a TypeChecker and several Code Generator visitors.

(back to top)

Getting Started

The following steps will guide you through the process of compiling a Klang source code file.

Prerequisites

  • Java ≥23
  • gcc to compile generated helpers and link with assembly code
  • WSL or Linux

    Provided bash scripts and the generated assembly only support Linux

Quick Start

The easiest way to start compiling Klang files is to use the provided bash script scripts/compile_klang.sh. To demonstrate this the following steps will show how to compile the file examples/point.k.

  1. If not done yet, get a local copy of this repo

    git clone https://github.com/crochethk/klang-compiler.git
    cd klang-compiler
  2. Run the script

    ./scripts/compile_klang.sh asm "./out" "examples/point.k"

    This will create a subdirectory ./out with 3 files:

    • examples.point.s - The main assembly code.
    • examples.point.h - Header with definitions for integration with C code.
    • examples.point.c - Helper C code required for some language features.

    The examples part in the file names is the package inferred by the compiler based on the relative path of the provided Klang file. Thus it's important to first navigate to your source code's root folder before compiling.

  3. Link and run the program

    cd out
    gcc -Wall examples.point.s examples.point.c
    ./a.out
    

(back to top)

Building And Using Self Contained jar

You can also package the compiler into a self contained jar using a provided script.

  1. If not done yet, get a local copy of this repo

    git clone https://github.com/crochethk/klang-compiler.git
    cd klang-compiler
  2. Run the bash script (this may take ~30 sec)

    ./scripts/build_compiler_jar.sh

    This will create a subdirectory build with two relevant files:

    • klangc.jar - The actual compiler executable.
    • klangc.sh - A convenience script to run jar.
  3. Compile a Klang file

    To compile the example source code examples/point.k follow these steps:

    • Run
      ./build/klangc.sh examples/point.k

The output and the other steps are similar to Quick Start.

To print further usage info or inspect all available options run

./build/klangc.sh --help

(back to top)

Roadmap

  • Add support for casting of numerical types
  • Full JBC support
  • Add array types
  • Extend string manipulation support
    • Length
    • Concatenation (strA + strB)
    • Substring
  • Add support for builtin struct methods (e.g. to_string())
  • VSCode Syntax Highlight Extension

(back to top)

License

Except for the following third-party libraries, which are redistributed under their respective licenses, all code in this repository is licensed under the MIT License.

The following libraries are included in this repository and are redistributed under their respective open-source licenses:

  1. ANTLR4 Runtime and ANTLR4 Complete are licensed under the BSD License. The full license text can be found in antlr4-LICENSE.txt.

  2. JUnit Platform Console Standalone is licensed under the Eclipse Public License 2.0.

(back to top)

About

A compiler for the Klang programming language.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published