Skip to main content

A Lua interpreter written in Python

Project description

MoonPie - Lua Interpreter in Python

A Lua interpreter written in Python, implementing core Lua language features.

Installation

From PyPI (when published)

pip install moonpie

From Source

git clone https://github.com/yourusername/moonpie.git
cd moonpie
pip install -e .

Features

Language Support

  • Data Types: numbers, strings, booleans, nil, tables, functions
  • Operators: arithmetic (+, -, *, /, %, ^), comparison (==, ~=, <, <=, >, >=), logical (and, or, not), string concatenation (..)
  • Variables: local and global variables
  • Tables: Lua's primary data structure with array and hash parts
  • Functions: first-class functions with closures
  • Control Flow: if/elseif/else, while, repeat-until, for loops (numeric and generic)
  • Comments: single-line (--) and multi-line (--[[ ]])

Built-in Functions

  • Basic: print, type, tonumber, tostring, assert, error
  • Math: abs, floor, ceil, sqrt, sin, cos, tan, max, min, pi
  • String: len, upper, lower, sub

Architecture

The interpreter follows a classic architecture:

  1. Lexer (lexer.py) - Tokenizes Lua source code
  2. Parser (parser.py) - Builds an Abstract Syntax Tree (AST) from tokens
  3. AST Nodes (ast_nodes.py) - Defines all AST node types
  4. Evaluator (evaluator.py) - Traverses and executes the AST
  5. Object System (lua_object.py) - Implements Lua values and environment
  6. Built-ins (lua_builtins.py) - Provides standard Lua functions

Usage

Command Line (after installation)

moonpie                    # Start interactive REPL
moonpie script.lua         # Execute a Lua file
moonpie examples/examples.lua

Python Module

python -m moonpie          # Start interactive REPL
python -m moonpie script.lua

As a Library

from moonpie import LuaInterpreter

interpreter = LuaInterpreter()
result = interpreter.run("""
    function add(a, b)
        return a + b
    end
    print(add(5, 3))
""")

Example Code

-- Variables and arithmetic
local x = 10
local y = 20
print(x + y)  -- 30

-- Functions
function factorial(n)
    if n <= 1 then
        return 1
    else
        return n * factorial(n - 1)
    end
end
print(factorial(5))  -- 120

-- Tables
local person = {name = "John", age = 30}
print(person.name)  -- John

local array = {10, 20, 30}
print(array[1])  -- 10
print(#array)   -- 3

-- Loops
for i = 1, 5 do
    print(i)
end

-- String operations
local greeting = "Hello" .. " " .. "World"
print(greeting)  -- Hello World

Testing

Run the test suite:

moonpie tests/test_basic.lua
# or
python -m moonpie tests/test_basic.lua

Run examples:

moonpie examples/examples.lua

Implementation Details

  • Lexer: Character-by-character tokenization with support for keywords, operators, strings, numbers, and comments
  • Parser: Recursive descent parser with proper operator precedence
  • Evaluator: Tree-walking interpreter with environment-based scoping
  • Tables: Hybrid array/hash implementation matching Lua's behavior
  • Functions: Support for closures and first-class functions
  • Error Handling: Runtime errors, syntax errors, and type checking

Limitations

This is a Lua interpreter for educational purposes. Some advanced features are not implemented:

  • Metatables and metamethods
  • Coroutines
  • Module system (require/module)
  • File I/O
  • Full standard library
  • Garbage collection optimization
  • Bytecode compilation

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

moonpie-0.1.0.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

moonpie-0.1.0-py3-none-any.whl (16.8 kB view details)

Uploaded Python 3

File details

Details for the file moonpie-0.1.0.tar.gz.

File metadata

  • Download URL: moonpie-0.1.0.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for moonpie-0.1.0.tar.gz
Algorithm Hash digest
SHA256 2189722400c143bb159866c5587cc1ec195571d5721e2cb988f73fcf6418b7ef
MD5 8599fafcd1052a70d72f906833b3477e
BLAKE2b-256 4fd3b6144c03c5b0be728afa020edc9da38868cca164e3ef3cfed79b3c7750be

See more details on using hashes here.

File details

Details for the file moonpie-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: moonpie-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for moonpie-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 126028fc870a66aa9b27cd6352a1bf4b2ed4b224fabe4cec3829c792a870dbca
MD5 58a715e908e3a40afaf258a958fffff7
BLAKE2b-256 9f52ec145808f563fbc2db470327668463e9e9af7bd6537ab59b55e0d2832075

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page