🚧 Experimental language hacking!
This is the home of the slang-lang programming language. Slang-lang is a new general purpose programming language with the following features:
- Staticly typed. no runtime type errors
- Support for plain functions
- Support for plain structs
- Support for classes
- Checked exceptions. Exceptions must be handled.
- Support for generic types, like lists and hashmaps.
- Switch statement, to switch over integer constants
- If, While, Loop statements
- Enums, implemented as tagged unions, as in rust enums.
- Case expressions, to choose based on enum values
- Statements can be used as expressions
- Significant white space, blocks are indented with tabs, as in python
- Labeled function arguments
- Functions and types are module private by default, they can be explicitly made public
- No null pointer, there is an option type
- String interpolation
Hello world example:
from std import print
pub fn main() -> int:
print("Hello world")
0
The slang-lang compiler itself is written in slang-lang. The compiler has several backends:
- ✅ C code. Compiles slang code into C code. This C code can then be compiled with a C compiler like GCC.
- ✅ python. Compiles slang code into python code. This backend is also implemented in the bootstrap compiler.
- 🚧 x86. Compiles to native x86 code, usable on linux. Very limited at the moment.
- 🚧 riscv. Compiles to native riscv code. Under construction, longer term goal.
- 🚧 slang. Compiles slang code into .. slang. Useful for debugging the compiler.
Longer term goal is to develop an OS using the slang-lang language.
Make sure you have the following installed:
maketo build using the Makefilepython3for bootstrapping and testing, with these additional python packages:larkfor parsingnetworkxfor module dependency graphspytestfor running the testsrichfor colorful console output
gccto compile C code
To build the slang-lang compiler and example programs, use make:
$ make
This will perform the bootstrap sequence, and build all example programs.
Run the mandelbrot example:
$ ./build/c/apps/mandel.exe
Run the compiler manually:
$ ./build/compiler5 -h
To run the hello world example with the bytecode backend:
$ ./build/compiler5 --run --backend-bc examples/snippets/hello_world.slang runtime/std.slang -v
To run the test suite:
$ make test
There are two slang lang compilers. A bootstrap compiler (compiler1), implemented in python, and the actual compiler written in slang lang itself.
To bootstrap the compiler, the following sequence is used:
- Compile
compiler-srctocompiler-py1usingcompiler1. This is the bootstrapping. - Compile
compiler-srctocompiler-py2usingcompiler-py1with the backend python. Note thatcompiler-py1!=compiler-py2, due to implementation differences betweencompilerandcompiler1. - Compile
compiler-srctocompiler-py3usingcompiler-py2with the backend python. Now we can check thatcompiler-py2==compiler-py3. - Compile
compiler-srctocompiler-c4usingcompiler-py3with the C backend. Compile the c code to an executable. - Compiler
compiler-srctocompiler-c5usingcompiler-c4with the C backend. Compile to executable with gcc. Now we can assert thatcompiler-c4==compiler-c5.
Now compiler5 should be able to compile itself to identical C code.
The compiler consists of various stages:
- Parsing, consisting of the classic stages
- Lexing: Source code is split into a logical token stream
- Parsing: The token stream is analyzed using a recursive descent parser
- AST: An abstract syntax tree is created
- Type checking
- Transformation
- Transform for-loops into while loops
- Transform classes into structs with functions
- Enums are transformed into tagged unions
- Type checking (again). The transformed AST is type checked again, to ensure the transformations are valid.
- Code generation. Different outputs can be generated from the AST
- python code can be directly generated from the AST
- C code can be generated from the AST
- Custom bytecode (BC) can be generated from the AST
A few libraries are provided in the Libs folder. The goal is a batteries included
idea, like in python.
Available libraries:
- base64, json and xml
- Regular expressions
- Compression, gzip and deflate
- Image formats, such as PNG, JPEG and QOI
- list, vector, hashmap, set and option types
- datetime
This work is MIT licensed.
This project is a recreational coding project. It's main purpose is an extended hobby.