https://github.com/sco1/pylox
A Python implementation of the Lox programming language. Check out https://craftinginterpreters.com/
https://github.com/sco1/pylox
lox lox-interpreter python python3 python310
Last synced: 11 months ago
JSON representation
A Python implementation of the Lox programming language. Check out https://craftinginterpreters.com/
- Host: GitHub
- URL: https://github.com/sco1/pylox
- Owner: sco1
- License: mit
- Created: 2021-10-26T19:26:33.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2025-04-08T14:32:10.000Z (12 months ago)
- Last Synced: 2025-05-07T22:46:51.144Z (11 months ago)
- Topics: lox, lox-interpreter, python, python3, python310
- Language: Python
- Homepage:
- Size: 410 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 14
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# pylox
[](https://pypi.org/project/sco1-pylox/)
[](https://pypi.org/project/sco1-pylox/)
[](https://github.com/sco1/sco1-pylox/blob/main/LICENSE)
[](https://results.pre-commit.ci/latest/github/sco1/pylox/main)
## Introduction
This is my Python implementation of an interpreter for the Lox programming language from Robert Nystrom's *[Crafting Interpreters](https://craftinginterpreters.com/)*.
## Python?
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.
## Differences From Text
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.
### Defined by Challenges
* (Chapter 4): Arbitrarily nested block comments (`/* ... */`)
* (Chapter 9): `break` statements are available for `for` and `while` loops
### User Choice
* Division by zero returns `NaN` (Python's `float('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 `include` header system
* Supports "stdlib" imports (``) and path imports (`"path/to/file"`)
* Recursive `include` not supported
* Imported source assumed to be valid code
### Additional Built-ins:
Unless otherwise noted, behavior mirrors the similarly named Python function.
#### General
* `input`
* `len`
* `ord`
* `read_text` (via `pathlib.Path.read_text`)
* `str2num`
* `string_array`
* Gives a `LoxArray` whose contents are equivalent to `collections.deque()`
#### Math
* `abs`
* `ceil`
* `divmod`
* `floor`
* `max`
* `min`
#### Regex
For methods whose Python equivalent returns [Match objects](https://docs.python.org/3/library/re.html#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_findall`
* `re_match`
* `re_search`
* `re_sub`
#### Stats
* `mean`
* `median`
* `mode`
* `std`
### Pure lox headers
* ``
* ``
* ``
* ``