A bytecode compiler and stack-based VM for a statically-typed language.
Inspired by Inferno OS's Limbo language and Dis virtual machine.
- Static type system:
int,bool,struct,array,slice,pointer - Go/Limbo-style syntax:
fn,:=,for,if - Recursion, slicing, struct literals
- Module system with
package,import,export
make
Compile a single package:
./com ./test/fib
./vm/run obj
Run the full test with package imports:
./com ./test/fib && cp exported obj ./test/fib/
./com ./test/nqueen && cp exported obj ./test/nqueen/
./com ./test && ./vm/run obj
# result 147
// test/fib/fib.yo
package fib
export fn fib(n int) int {
if n <= 1 { return n; };
return fib(n-1) + fib(n-2);
};
// test/main.yo
package main
import "./test/fib"
import "./test/nqueen"
fn main() int {
return fib.fib(10) + nqueen.solve(8);
};
See test/ for full examples including N-Queens solver with slices.
Five-stage compilation pipeline:
- lex.c: tokenizer
- parse.c: recursive descent parser, builds AST
- assert.c, type.c: semantic analysis, type checking
- gen.c, com.c: instruction selection, register allocation
- dis.c: bytecode serialization
VM execution:
lex.c lexical analyzer
parse.c syntax parser
node.c AST node construction
type.c type system
decl.c declaration and scope management
gen.c instruction generation
com.c expression compiler
asm.c assembly output
dis.c bytecode emitter
vm/ virtual machine