Skip to content

4c656f/go-lox

Repository files navigation

Go-Lox Interpreter 🚀

An implementation of the Lox programming language Interpreter written in Go. This interpreter is based on the book "Crafting Interpreters" by Robert Nystrom and tested via Codecrafters.

About Lox 📖

Lox is a dynamically-typed scripting language that supports object-oriented programming with classes and inheritance. This implementation follows the tree-walk interpreter pattern.

Features ✨

Here's what's currently implemented in this interpreter:

Feature Status
Basic Arithmetic
Variables
Control Flow (if/else)
Loops (while, for)
Functions
Closures
Classes 🚧
Inheritance 🚧
Standard Library 🚧
Error Handling 🚧

Getting Started 🛠️

Prerequisites

  • Go 1.22 or higher
  • Git

Installation

# Clone the repository
git clone https://github.com/yourusername/go-lox.git

# Navigate to project directory
cd go-lox

Running the Interpreter

There are four ways to use the interpreter:

  1. Print tokens of a Lox script:
./go-lox tokenize <filename>.lox
  1. Print AST of a Lox script:
./go-lox parse <filename>.lox
  1. Evaluate basic expression:
./go-lox eval <filename>.lox
  1. Run a Lox script:
./go-lox run <filename>.lox

Example Lox Program 📝

// This is a simple Lox program
fun fib(n) {
  if (n <= 1) return n;
  return fib(n - 2) + fib(n - 1);
}

print fib(10); // Outputs: 55

Project Structure 🏗️

├── parser/       # Abstract Syntax Tree parser
├── lexer/        # Lexical analysis
├── interpreter/  # Interpreter implementation
├── expression/   # Expression definitions e.g., <, ==, +, >, -
├── stmt/         # Statement definitions e.g., var, fun, for, while
├── token/        # Token definition
└── main.go       # Entry point

Contributing 🤝

I will be glad to receive any of your questions/suggestions/contributions to this project! Feel free to open a PR or contact me via:

Twitter

Email

Telegram


For more information about the Lox language, visit Crafting Interpreters.

Note: This implementation is for educational purposes and may not be suitable for production use.

About

lox language interpreter written in go

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published