2024-08-25 15:37:39 +02:00
# zlox, a clox rewrite in Zig.
While reading [Crafting Interpreters ](https://craftinginterpreters.com/ ), after having rewrote the interpreter part in Rust (see [rs-lox ](https://github.com/mycroft/rs-lox )), I'm now attempting to write the bytecode compiler in Zig.
## Progression
- [x] 14 - Chunks of Bytecode
- [x] 15 - A Virtual Machine
- [x] 16 - Scanning on Demand
- [x] 17 - Compiling Expressions
2024-08-25 18:15:49 +02:00
- [x] 18 - Types of Values
2024-08-25 18:21:04 +02:00
- [x] 19 - Strings
2024-08-26 16:49:06 +02:00
- [x] 20 - Hash Tables
2024-08-26 22:54:25 +02:00
- [x] 21 - Global Variables
2024-08-27 10:23:07 +02:00
- [x] 22 - Local Variables
2024-08-27 15:13:21 +02:00
- [x] 23 - Jumping Back and Forth
2024-08-29 08:28:08 +02:00
- [x] 24 - Calls and Functions
2024-08-29 12:59:28 +02:00
- [x] 25 - Closures
2024-08-30 09:53:32 +02:00
- [x] 26 - Garbage Collection
2024-08-25 15:37:39 +02:00
- [ ] 27 - Classes and Instances
- [ ] 28 - Method and Initializers
- [ ] 29 - Superclasses
- [ ] 30 - Optimization
## Build & run
There is a `justfile` to help building & running:
```sh
$ just build
$ ls -l zig-out/bin/zlox
-rwxr-xr-x 1 mycroft mycroft 2823296 Aug 25 15:36 zig-out/bin/zlox
2024-08-27 12:21:44 +02:00
$ set -x TRACE
2024-08-25 15:37:39 +02:00
$ just run
zig build run
2024-08-27 12:21:44 +02:00
== Hash table count:0 capacity:0 ==
== End of hash table ==
2024-08-25 15:37:39 +02:00
2024-08-27 12:21:44 +02:00
zlox> print !(5 - 4 > 3 * 2 == !nil);
2024-08-25 16:23:16 +02:00
== code ==
0000 1 OP_CONSTANT 0 '5'
0002 | OP_CONSTANT 1 '4'
0004 | OP_SUBSTRACT
0005 | OP_CONSTANT 2 '3'
0007 | OP_CONSTANT 3 '2'
0009 | OP_MULTIPLY
000a | OP_GREATER
000b | OP_NIL
000c | OP_NOT
000d | OP_EQUAL
000e | OP_NOT
2024-08-27 12:21:44 +02:00
000f | OP_PRINT
2024-08-25 16:23:16 +02:00
== end of code ==
2024-08-25 15:37:39 +02:00
2024-08-25 16:23:16 +02:00
0000 1 OP_CONSTANT 0 '5'
[ 5 ]
0002 | OP_CONSTANT 1 '4'
[ 5 ][ 4 ]
0004 | OP_SUBSTRACT
[ 1 ]
0005 | OP_CONSTANT 2 '3'
[ 1 ][ 3 ]
0007 | OP_CONSTANT 3 '2'
[ 1 ][ 3 ][ 2 ]
0009 | OP_MULTIPLY
[ 1 ][ 6 ]
000a | OP_GREATER
[ false ]
000b | OP_NIL
[ false ][ nil ]
000c | OP_NOT
[ false ][ true ]
000d | OP_EQUAL
[ false ]
000e | OP_NOT
[ true ]
2024-08-27 12:21:44 +02:00
000f | OP_PRINT
2024-08-25 16:23:16 +02:00
true
2024-08-27 12:21:44 +02:00
0010 2 OP_RETURN
== Hash table count:0 capacity:0 ==
== End of hash table ==
zlox>
2024-08-25 15:37:39 +02:00
```