Skip to content
/ klaus Public

simple stack based language with a compiler written in OCaml.

Notifications You must be signed in to change notification settings

nailuj05/klaus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Klaus

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).

Building the compiler

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.


The Klaus language

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

Operators

  • Push <imm>: Pushes an immediate value onto the stack
  • Pop: Pops the top value of the stack
  • Puts: Prints the top value of the stack (without popping it)
  • Read: Reads a user input (as int) and pushes it on top of the stack
  • Swap: Swaps the top 2 values
  • Dup: Duplicates the top value
  • Get (<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 sum
  • Sub: Replaces the top 2 values by their difference
  • Mul: Replaces the top 2 values by their product
  • Div: Replaces the top 2 values by their quotient
  • :<label>: Marks a label to jump to
  • Jmp :<label>: Jumps the a label
  • Cmp <comparison> :<label>: Compares the top 2 values on the stack by a given comparison (<,>,<=,>=,=), if true jump to the label

Future

  • Replace libc
  • Explore Reverse Polish Notation
  • Expand documentation and example programs
  • Char, String and float datatypes

Further information about the language will follow

About

simple stack based language with a compiler written in OCaml.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published