sudw1n/zlox
A bytecode VM-based interpreter inspired from the Lox language, built using Zig.
A bytecode VM-based interpreter inspired from the Lox language, built using Zig.
print "hello world\n";
let a = 10 * (2 + 3) - 7;
print a;
print "\n";
let b = !(true == false);
let c = ((3 + 4) * 5) - (6 / 2);
print b;
print "\n";
print c;
print "\n";
fn main() {
let a = "outer a\n";
{
let a = "inner a\n";
print a;
}
print a;
}
main();
fn main() {
for (let i = 1; i < 10; i = i + 1) {
for (let j = 1; j <= i; j = j + 1) {
print "*";
}
print "\n";
}
}
main();
fn fib(n) {
if n < 2 { return n; }
return fib(n - 1) + fib(n - 2);
}
// clock() is a native function
let start = clock();
let result = fib(3);
print result; print "\n";
print "took: "; print clock() - start; print " miliseconds\n";
fn outer() {
let x = "local x\n";
fn inner() {
print x;
}
inner();
}
let x = "global x\n";
outer();
The language, tentatively named wlox, is designed to be a dynamically typed programming language with support for garbage collection, functions, and closures. Inspired by the Lox programming language, wlox aims to provide a modern and intuitive syntax while offering powerful features.
Dynamic Typing: In wlox, variables are dynamically typed, meaning their types are determined at runtime rather than compile time. This allows for greater flexibility and ease of use, as variables can hold values of any type without explicit type annotations.
Garbage Collection: To manage memory efficiently and prevent memory leaks, wlox incorporates automatic garbage collection. This feature relieves developers from manual memory management tasks, allowing them to focus on writing clean and concise code.
Functions and Closures: Functions play a central role in wlox, serving as first-class citizens that can be assigned to variables, passed as arguments, and returned from other functions. Closures, a powerful concept in functional programming, are fully supported, enabling the creation of lexically scoped function closures.
The language design of wlox aims to strike a balance between simplicity and expressiveness while also exploring the features of most modern programming languages.
To ensure continuous improvement and adaptability, the following future enhancements are planned for Wlox:
This project uses devbox to manage its development environment.
Install devbox:
curl -fsSL https://get.jetpack.io/devbox | bash
Start the devbox shell:
devbox shell
Run a script in the devbox environment:
devbox run <script>
Scripts are custom commands that can be run using this project's environment. This project has the following scripts:
You can build the autogenerated Zig documentation on your local machine with the docs script.
First, make sure you are inside the devbox shell. Then run:
devbox run docs
Now you can view the documentation in your browser at http://localhost:8000
, just make sure the
8000
port isn't already being used
Install the npm packages required for the REPL. This should be run at least once, before launching the web servers.
pushd www/
npm install
popd
Build the wasm file and start the Python web server. Open the URL http://localhost:8000
in your
browser.
zig build -Dtarget=wasm32-freestanding -Doptimize=ReleaseFast wlox
devbox run www-python
Build the wasm file with verbose output.
zig build -Dverbose -Dprint-bytecode -Dtarget=wasm32-freestanding -Doptimize=ReleaseFast wlox
Run a simple web server in Python to serve the website files. The console output should inform about which port the server is running on.
python -m http.server -d www/
Run the Nginx web server to serve the website files. The port is determined by the NGINX_WEB_PORT
environment variable.
devbox services up
Build the command-line interpreter.
zig build -Doptimize=ReleaseFast
Build the command-line interpreter with verbose output
zig build -Dverbose -Dprint-bytecode -Ddisplay-stack
Build the local documentation and start the web server to view the documentation.
zig build docs
python -m http.server -d ./zig-out/docs/