Skip to content

bingguo1/compiler_from_scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

Lox Interpreter

A Java implementation of the Lox programming language interpreter, following the design from "Crafting Interpreters" by Robert Nystrom.

Overview

This project implements a tree-walk interpreter for the Lox programming language. Lox is a dynamically-typed scripting language with features like:

  • Variables and basic data types (numbers, strings, booleans)
  • Arithmetic and logical operations
  • Control flow (if/else statements, loops)
  • Functions and closures
  • Object-oriented programming with classes

Project Structure

learncompiler/
├── src/main/java/com/craftingintepreters/lox/
│   ├── Lox.java        # Main interpreter entry point
│   ├── Scanner.java    # Lexical analyzer (tokenizer)
│   ├── Token.java      # Token representation
│   └── TokenType.java  # Token type definitions
├── pom.xml            # Maven build configuration
└── README.md          # This file

Requirements

  • Java 17 or later
  • Maven 3.6+ (for building)

Building the Project

To build the project using Maven:

mvn clean compile

To create a JAR file:

mvn clean package

Running the Interpreter

Interactive Mode (REPL)

Run the interpreter without any arguments to start the interactive prompt:

java -cp target/classes com.craftingintepreters.lox.Lox

This will start a Read-Eval-Print Loop where you can type Lox expressions:

> print "Hello, World!";
Hello, World!
> var a = 5;
> print a + 3;
8

Script Mode

Run a Lox script file:

java -cp target/classes com.craftingintepreters.lox.Lox script.lox

Current Implementation Status

This implementation currently includes:

  • Lexical Analysis: Complete tokenizer that recognizes all Lox tokens
  • Token Types: Support for literals, operators, keywords, and identifiers
  • Basic REPL: Interactive command-line interface
  • File Execution: Ability to run Lox scripts from files
  • Error Reporting: Basic error handling with line numbers

Lox Language Features

The scanner currently recognizes these language constructs:

Operators: +, -, *, /, =, ==, !=, <, <=, >, >=, !

Keywords: and, class, else, false, for, fun, if, nil, or, print, return, super, this, true, var, while

Literals: Numbers, strings, identifiers

Delimiters: (, ), {, }, ,, ., ;

Development

Running Tests

mvn test

Project Goals

This project serves as a learning exercise to understand:

  • Lexical analysis and tokenization
  • Recursive descent parsing
  • Abstract syntax trees (AST)
  • Tree-walk interpretation
  • Programming language design principles

Next Steps

Future development will include:

  • Parser implementation (syntax analysis)
  • Abstract Syntax Tree (AST) generation
  • Expression evaluation
  • Statement execution
  • Variable binding and environments
  • Function calls and closures
  • Object-oriented features

Resources

License

This project is for educational purposes. Please refer to the original "Crafting Interpreters" book for comprehensive explanations of the concepts implemented here.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages