Skip to content

SasankYadati/pylox

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PyLox

A Python implementation of the Lox programming language.

About Lox

Lox is a dynamically-typed, high-level scripting language designed by Robert Nystrom for his book Crafting Interpreters. It features C-style syntax with first-class functions, closures, and object-oriented programming support.

Features

This implementation includes:

  • Lexical Analysis: Tokenization of Lox source code
  • Parsing: Recursive descent parser with precedence handling
  • AST Generation: Abstract Syntax Tree representation
  • Semantic Analysis: Variable resolution and scope checking
  • Interpretation: Tree-walk interpreter with runtime evaluation
  • Data Types: Numbers, strings, booleans, nil
  • Variables: Declaration and assignment
  • Functions: First-class functions with closures
  • Classes: Object-oriented programming with inheritance
  • Control Flow: if/else, while, for loops, break, return
  • Native Functions: Built-in functions like clock()

Example Usage

// src.lox
fun fibonacci(n) {
  if (n <= 1) return n;
  return fibonacci(n - 1) + fibonacci(n - 2);
}

for (var i = 0; i < 10; i = i + 1) {
  print fibonacci(i);
}

Run with:

python3 -m app.main run src.lox

Project Structure

The interpreter follows a traditional compiler pipeline architecture:

Frontend (Lexing & Parsing)

  • scanner.py - Tokenizes source code into tokens
  • token.py - Token data structure and types
  • parser.py - Recursive descent parser producing AST
  • expr.py - Expression AST node definitions
  • stmt.py - Statement AST node definitions

Semantic Analysis

  • resolver.py - Static variable resolution, scope checking etc.

Runtime (Interpreter)

  • interpreter.py - Tree-walk interpreter executing AST
  • environment.py - Scope and variable binding management
  • lox_callable.py - Interface for callable objects
  • lox_function.py - User-defined function implementation
  • lox_class.py - Class and inheritance support
  • lox_instance.py - Object instance representation
  • native_fns.py - Built-in standard library functions

Utilities

  • main.py - Entry point and command-line interface
  • AstPrinter.py - Debug AST visualization
  • errors.py - Error handling and reporting
  • runtime_error.py - Runtime exception types
  • return_error.py - Function return control flow
  • break_error.py - Loop break control flow
  • utils.py - Helper functions

About

Python Implementation of an Interpreter for Lox

Topics

Resources

Stars

Watchers

Forks

Languages