An implementation of a Scheme/Lisp interpreter Based on the original Lispy by Peter Norvig, written in Python 3.13.
- Python 3.13 Syntax: Uses pattern matching, union types (
|), and type hints - Core Lisp Features: Variables, functions, conditionals, recursion, and list operations
- Built-in Functions: Arithmetic, comparison, list manipulation, and mathematical functions
- Interactive REPL: Command-line interface for interactive programming
- Tests: Full test suite using pytest with 20+ test cases
- Error Handling: Proper error messages and exception handling
lis.py- The main interpreter implementationtest_lis.py- Test suitedemo_lis.py- Interactive demo with examplesrequirements.txt- Project dependencies
# Create virtual environment
python3 -m venv venv
# Activate virtual environment
source venv/bin/activate # On macOS/Linux
# or
venv\Scripts\activate # On Windows
# Install dependencies
pip install -r requirements.txt# Activate virtual environment first
source venv/bin/activate
# Interactive REPL
python3 lis.py
# Run tests with pytest
pytest test_lis.py -v
# Or run tests directly
python3 test_lis.py
# Run demo with examples
python3 demo_lis.py; Arithmetic
(+ 1 2 3) ; => 6
(* 4 5) ; => 20
(- 10 3) ; => 7
(/ 15 3) ; => 5.0
; Variables
(define x 10)
(define y (* x 2))
y ; => 20
; Functions
(define square (lambda (x) (* x x)))
(square 5) ; => 25
; Conditionals
(if (> 5 3) 'yes 'no) ; => yes
; Lists
(define my-list '(1 2 3 4))
(car my-list) ; => 1
(cdr my-list) ; => (2 3 4)
(length my-list) ; => 4
; Recursion
(define factorial
(lambda (n)
(if (<= n 1)
1
(* n (factorial (- n 1))))))
(factorial 5) ; => 120+,-,*,/- Basic arithmetic (variadic for + and *)abs,max,min,round- Mathematical functions
>,<,>=,<=,=- Comparison operatorseq?,equal?- Equality testing
car- First element of listcdr- Rest of list (all but first)cons- Construct listlist- Create list from argumentsappend- Concatenate listslength- List lengthnull?- Test for empty list
number?- Test if numbersymbol?- Test if symbollist?- Test if listprocedure?- Test if function
if- Conditional expressionquote- Literal datadefine- Variable/function definitionset!- Variable assignmentlambda- Function creationbegin- Sequential execution
apply- Apply function to list of argumentsmap- Apply function to each element of list
The test suite uses pytest and includes:
- Basic arithmetic operations
- Variable definition and assignment
- Lambda functions and recursion
- List operations and predicates
- Error handling and edge cases
- Performance benchmarks
# Activate virtual environment
source venv/bin/activate
# Run all tests with verbose output
pytest test_lis.py -v
# Run tests with short traceback on failures
pytest test_lis.py -v --tb=short
# Run specific test
pytest test_lis.py::test_basic_arithmetic -v
# Run tests and show performance
python3 test_lis.pyThe demo script showcases various features:
python3 demo_lis.pyThis will show examples of all major features and optionally start an interactive session.
- Python 3.10+ (for pattern matching)
- pytest 8.4.1+ (for testing)
Install requirements:
pip install -r requirements.txtThe project uses minimal dependencies:
pytest: Testing framework with fixtures and advanced features- No runtime dependencies for the interpreter itself
The interpreter consists of four main components:
- Lexer/Tokenizer (
tokenize): Converts source code into tokens - Parser (
parse,read_from_tokens): Builds abstract syntax tree - Environment (
Env,standard_env): Manages variable scoping - Evaluator (
eval): Executes the parsed expressions