This is my Python implementation of an interpreter for the Lox programming language from Robert Nystrom's Crafting Interpreters.
While the text is implemented in Java and C as its high & low-level implementations, I have no idea how to write either of them! Instead, I'll be using Python for the high-level implementation & eventually Rust for the low-level imeplementation.
For the sake of fitting within a decently sized text, the fully implemented Lox spec omits features that users of other programming languages may miss. Often these are discussed as notes within a chapter, or presented as challenges at the end of a chapter. Significant difference in this implementation from the text reference are noted below.
- (Chapter 4): Arbitrarily nested block comments (
/* ... */) - (Chapter 9):
breakstatements are available forforandwhileloops
- Division by zero returns
NaN(Python'sfloat('nan')) - Strings may be defined using either
"or' - Modulo operator (
%) - Power operator (
^) - Integer division operator (
\) - Both floats and integers are represented
- Return type from operations follows Python3's semantics
- Containers
array()
- A basic
includeheader system- Supports "stdlib" imports (
<header_name>) and path imports ("path/to/file") - Recursive
includenot supported - Imported source assumed to be valid code
- Supports "stdlib" imports (
Unless otherwise noted, behavior mirrors the similarly named Python function.
inputlenordread_text(viapathlib.Path.read_text)str2numstring_array- Gives a
LoxArraywhose contents are equivalent tocollections.deque(<some string>)
- Gives a
absceildivmodfloormaxmin
For methods whose Python equivalent returns Match objects, a LoxArray is returned. The first value in the array will always correspond to match.group(0); if the pattern contains one or more groups then the array will match the output of match.groups()
re_findallre_matchre_searchre_sub
meanmedianmodestd
<array_sum><hello_world><map><split_on>