A compiler for the Klang programming language.
Table of Contents
There is also a VSCode Syntax Highlighting Extension available.
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:
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.
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.
The following steps will guide you through the process of compiling a Klang source code file.
≥23
gcc
to compile generated helpers and link with assembly code- WSL or Linux
Provided bash scripts and the generated assembly only support Linux
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
.
-
If not done yet, get a local copy of this repo
git clone https://github.com/crochethk/klang-compiler.git cd klang-compiler
-
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. -
Link and run the program
cd out gcc -Wall examples.point.s examples.point.c ./a.out
You can also package the compiler into a self contained jar
using a provided
script.
-
If not done yet, get a local copy of this repo
git clone https://github.com/crochethk/klang-compiler.git cd klang-compiler
-
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 runjar
.
-
Compile a Klang file
To compile the example source code
examples/point.k
follow these steps:- Run
./build/klangc.sh examples/point.k
- Run
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
- 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
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:
-
ANTLR4 Runtime and ANTLR4 Complete are licensed under the BSD License. The full license text can be found in
antlr4-LICENSE.txt
. -
JUnit Platform Console Standalone is licensed under the Eclipse Public License 2.0.