Language from Crafting Interpreters.
Copyright © 2024 Chris Roberts (Krobbizoid).
This is an implementation of the Lox language from the book Crafting Interpreters by Bob Nystrom. The book covers implementing Lox, a dynamically-typed, object-oriented language. Lox is first implemented with a tree-walk interpreter written in Java (jlox), and then with a bytecode interpreter written in C (clox).
I have followed this book before, so I started on the second half with the bytecode interpreter. All of the original chapters have been completed and I am adding some additional features:
- More native functions to improve Lox's I/O capabilities.
- Source merging script for bootstrapping a preprocessor.
- Standard library of useful functions and collections.
- Preprocessor for C-like modularity.
- Implementation of Lox, or another language in Lox.
This implementation of Lox defines several extension functions to make the
language more capable. Extension functions are prefixed with a double
underscore (__
) to distinguish them from normal functions and reduce
namespace pollution.
Lox does not distinguish between int
and float
number types. If an int
parameter is documented, any number will be accepted, but the fractional part
of the number will be truncated. If an int
return type is documented, the
returned value can be assumed to be an integer.
Calling an extension function is considered undefined behavior if:
- The incorrect number of arguments are passed.
- The incorrect argument types are passed.
- A
NaN
orInfinity
number value is passed.
Extension functions may not behave as documented during undefined behavior.
Return the number of command line arguments, starting at and including the
script path. Always returns 0
in REPL mode and at least 1
outside of REPL
mode.
Return the command line argument at index index
, where index 0
is the
script path. Returns nil
if index
is out of bounds.
Return the byte at index index
of text
, where index 0
is the first byte.
Returns nil
if index
is out of bounds.
Exit with the status code status
.
Close the file with the stream stream
and return whether a file was
successfully closed. Files should be closed after opening.
Read and return the next byte from the input stream stream
. Returns nil
if
an error occurred or if the end-of-file was reached.
Open the file at path
for reading and return a stream. Returns nil
if the
file could not be opened.
Open the file at path
for writing and return a stream. Returns nil
if the
file could not be opened.
Write the byte byte
to the output stream stream
and return the written
byte. Returns nil
if an error occurred.
Return a string representing the number number
.
Return a constant representing the standard error stream. Returns a value unique from the other standard streams and any possible file stream.
Return a constant representing the standard input stream. Returns a value unique from the other standard streams and any possible file stream.
Return a constant representing the standard output stream. Returns a value unique from the other standard streams and any possible file stream.
Return the length of text
in bytes.
Return a single-byte string containing the byte byte
. Returns nil
if byte
is less than 1
or greater than 255
.
Return the number number
with the fractional part truncated.
This implementation of Lox is released under the MIT License:
https://krobbi.github.io/license/2024/mit.txt
See LICENSE.txt for a full copy of the license text.
Crafting Interpreters is copyright of Bob Nystrom.