Welcome to the SPL to WebAssembly Compiler project! This compiler is designed to compile code written in the Simple Programming Language (SPL) to WebAssembly, JavaScript, and x86 assembly, with the goal of enabling efficient portable execution of SPL programs across different platforms.
Sierpinski on JavaScript/WebAssembly target | Sierpinski on x86 target |
---|---|
The following targets are supported:
- WebAssembly
- JavaScript (Browser and Node.js)
- x86
This project is written in Java 21 and relies on the Gradle build tool for packaging and library management. It is recommended to use IntelliJ when working with the source code. To get started, simply clone this repository, launch IntelliJ and open the cloned directory as a project. The IDE should automatically detect that a Gradle project is present.
To generate a jar file containing all libraries, run the shadowJar
task. This can also be done outside
an IDE by executing gradlew shadowJar
(make sure the build script for your respective operating system
is marked as executable).
Usage: splc [-hV] [--headless] [-o=<output>] -t=<target> <input>
<input> Source code file to compile.
-h, --help Show this help message and exit.
--headless Disable rendering procedures.
-o, --output=<output> File to write output to.
-t, --target=<target> The target to generate code for (JS, NODEJS, WASM,
X86).
-V, --version Print version information and exit.
Example: jar -jar splc.jar myprogram.spl --target wasm
The WebAssembly code generator emits code in its textual representation. Before it can be executed by the
browser, it has to be translated to its binary representation. This can easily be done by using the wat2wasm
tool from the WebAssembly binary toolkit. Simply download the latest release
for your operating system. wat2wasm
can be found in the bin
directory. It is recommended to add it to your
system's path variable.
To compile the example from above, use the following command wat2wasm --enable-threads myprogram.wat
.
The x86 code generator emits 64-bit assembly for the NASM assembler. Programs generated
by the compiler depend on libc
and the SPL standard library written in C (This compiler implements
this System V AMD64 ABI
calling convention to allow interoperability with C code). SDL2 is used to implement SPL's
rendering procedures. Linking against libc
and the standard library can, for
example, be accomplished using GCC.
Use the following commands to assemble and link a program:
- Install SDL2:
sudo apt install libsdl2-dev
(Ubuntu/Debian) - Compile the standard library:
gcc -c stdlib.c
- Compile the program assembly:
nasm -f elf64 mypogram.asm -o myprogram.o
- Link program and standard library:
gcc myprogram.o stdlib.o -o myprogram -lSDL2 -no-pie
Run it using chmod +x myprogram && ./myprogram
.
When targeting Node.js and headless mode being disabled, JavaScript programs generated by the compiler will depend
on @kmamal/sdl and canvas. These
libraries can be installed using npm install @kmamal/sdl canvas
.
When the --headless
option is passed to the compiler, rendering procedures will be disabled. Dependencies on the
respective rendering libraries will be dropped. Additionally, the x86 standard library has to be compiled with
the HEADLESS
define present (gcc -c stdlib.c -DHEADLESS
).
This project includes a playground that can be used to run both WebAssembly and JavaScript files generated by the compiler, which can be found in the web directory. It requires Node.js to be installed on your system. To run it, follow the steps below.
- Make sure Node.js is installed. Both latest and LTS should work.
- CD into the static directory (
cd web/static
). - Install required libraries and compile the website (
npm i && npm run build
). - CD back into the web directory (
cd ..
). - Install required libraries for the server and run it (
npm i && node .
) - A webserver should now be available on http://localhost:3000.
- On the website, select either a JavaScript or a WebAssembly file generated by the compiler.