Klyvos is a programming language built in Java with a custom compiler, bytecode format, and stack-based virtual machine.
source (.klvs) -> ANTLR -> parse tree -> AST -> bytecode (.klvb) -> VM
- Source files:
.klvs - Bytecode files:
.klvb - Stack-based VM
- Functions
varandconstif,while,forbreak,continue,return- Lists, indexing, and slicing
- Bytecode Disassembly
const a = 1
var b = 0
Klyvos uses var for mutable values and const for values that should not be reassigned.
Klyvos follows a compile-then-run workflow:
- Write source code in a
.klvsfile - Parse it with ANTLR
- Build the AST
- Compile the AST into bytecode
- Run the bytecode on the VM
The VM is stack-based, so instructions operate on values pushed to and popped from a stack.
usevarconstfn- expression statements
ifwhileforbreakcontinue- blocks
return
- assignment
- logical operators:
and,or,not - comparisons:
==,!=,<,<=,>,>=,in - arithmetic:
+,-,*,/,%,** - unary operators
- function calls
- indexing and slicing
- list literals
- literals and variables
const a = 10
const b = 5
println(a + b)
var i = 0
while (i < 5) {
i = i + 1
}
for (i in [1, 2, 3, 4, 5]) {
println(i)
}
Klyvos bytecode is stored in .klvb files.
Klyvos compiles high-level code into a compact instruction set executed by a stack-based virtual machine.
| Mnemonic | Code |
|---|---|
LOAD_CONST |
0x01 |
LOAD_VAR |
0x02 |
STORE_VAR |
0x03 |
LOAD_GLOBAL |
0x04 |
STORE_GLOBAL |
0x05 |
| Mnemonic | Code |
|---|---|
ADD |
0x10 |
SUB |
0x11 |
MUL |
0x12 |
DIV |
0x13 |
MOD |
0x14 |
NEG |
0x15 |
| Mnemonic | Code |
|---|---|
EQ |
0x20 |
NEQ |
0x21 |
LT |
0x22 |
LTE |
0x23 |
GT |
0x24 |
GTE |
0x25 |
AND |
0x26 |
OR |
0x27 |
NOT |
0x28 |
| Mnemonic | Code |
|---|---|
JUMP |
0x30 |
JUMP_IF_FALSE |
0x31 |
BREAK |
0x32 |
CONTINUE |
0x33 |
CALL |
0x34 |
RETURN |
0x35 |
POP |
0x36 |
| Mnemonic | Code |
|---|---|
MAKE_LIST |
0x40 |
GET_INDEX |
0x41 |
SET_INDEX |
0x42 |
SLICE |
0x43 |
| Mnemonic | Code |
|---|---|
NOOP |
0x60 |
DUP |
0x61 |
SWAP |
0x62 |
.klvs— source code.klvb— compiled bytecode
klyvos run <file.klvs> Compile and run a source file
klyvos compile <file.klvs> Compile source to bytecode only
klyvos <file.klvb> Run a bytecode file
klyvos dis <file.klvb> Disassemble bytecode
klyvos -v | -version Show version
klyvos -h | --help Show help
klyvos compile myscript.klvs
klyvos run test.klvs
klyvos dis output.klvbast/— AST nodes and visitorsparser/— AST buildercompiler/— compiler and scope logicvm/— VM and runtime valuesbin/— bytecode I/O helperstokens/— tokens and token typesvalidator/— validation layermodules/— module registryerror/— compile and runtime errors
Klyvos requires a stdlib directory at runtime.
When running the executable or CLI, place the stdlib folder in the same directory as the program:
klyvos.exe
stdlib/
mvn clean package