Klaus is a simple stack based language with a compiler implemented in OCaml. The language itself does not rely on any dependencies, it is currently developed on and for x86-64 GNU/Linux, but exploring other architectures and operating systems is something I would like to do.
Its name is a reference to Staplerfahrer Klaus (Stapel being Stack in german).
This project is build using my noob build system on Linux. You will have to build the noob.c file using your systems C compiler, then run the executable to build the main compiler using ocamlc.
Being a stack based language means there are no variables, all data is kept on a stack. Klaus provides you with a basic set of input/output, algebraic and branching/looping instructions. Currently the syntax is still in a very simple, pseudo assembly state, I am planning on expanding the synatx and allowing for more complex, abstract syntax.
# Example program that compares a user input to 10 and prints the result
Read
Push 10
Cmp < :bigger
Push 0
Puts
End
:bigger
Push 1
Puts
All data handled by klaus is signed 64-bit integers.
# Fibonacci Sequence
Read
Push 1
Push 1
# Loop compare
:loop
Get 2
Push 2
Cmp > :end
# Loop body
Pop
Push 1
Sub
Get 1
Get 3
Get 3
Add
Jmp :loop
# Loop End
:end
Pop
Pop
Puts
End
Many more examples can be found in the examples/
folder
Push <imm>
: Pushes an immediate value onto the stackPop
: Pops the top value of the stackPuts
: Prints the top value of the stack (without popping it)Read
: Reads a user input (as int) and pushes it on top of the stackSwap
: Swaps the top 2 valuesDup
: Duplicates the top valueGet (<imm>)
: Gets a value from the stack (indexed by either and immediate value or the value on top of the stack)Add
: Replaces the top 2 values by their sumSub
: Replaces the top 2 values by their differenceMul
: Replaces the top 2 values by their productDiv
: Replaces the top 2 values by their quotient:<label>
: Marks a label to jump toJmp :<label>
: Jumps the a labelCmp <comparison> :<label>
: Compares the top 2 values on the stack by a given comparison (<,>,<=,>=,=), if true jump to the label
- Replace libc
- Explore Reverse Polish Notation
- Expand documentation and example programs
- Char, String and float datatypes
Further information about the language will follow