This is an implementation of the C Lox compiler from Crafting Interpreters. The implementation is mostly the same as in the book but with the following changes:
- Constants use 8 or 16-bit indices.
- New instructions:
OP_ZEROpush 0 onto the stackOP_ONEpush 1 onto the stackOP_MINUS_ONEpush -1 onto the stackOP_INTEGERpushes an 8-bit integer onto the stackOP_INTEGER_16pushes a 16-bit integer onto the stackOP_CONSTANT_16push a 16-bit indexed constant onto the stack
- New native functions:
read()reads a byte fromstdinornilif end of streamutf(byte, byte, byte, byte)converts 1, 2, 3, or 4 bytes into a UTF stringprinterr(string)prints a string tostderrexit(number)exits with the specific exit code
These additions allow running Lox.lox, an Lox interpreter written in Lox. In particular, Lox.lox introduces more constants than the original limit 255 per-chunk in the original clox implementation and requires the 4 native functions.
$ gcc src/*.src -O3 -o clox$ ./clox examples/helloworld.lox