Skip to main content

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

Metadata

Release files for moonpie 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for moonpie 0.1.0
File Size Uploaded
moonpie-0.1.0.tar.gz 17.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for moonpie 0.1.0
File Interpreter ABI Platform
moonpie-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 34.7 kB

Release files / moonpie-0.1.0.tar.gz

Download URL moonpie-0.1.0.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
2189722400c143bb159866c5587cc1ec195571d5721e2cb988f73fcf6418b7ef
BLAKE2b-256 checksum
How to use checksums
4fd3b6144c03c5b0be728afa020edc9da38868cca164e3ef3cfed79b3c7750be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release files / moonpie-0.1.0-py3-none-any.whl

Download URL moonpie-0.1.0-py3-none-any.whl
Size 16.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
126028fc870a66aa9b27cd6352a1bf4b2ed4b224fabe4cec3829c792a870dbca
BLAKE2b-256 checksum
How to use checksums
9f52ec145808f563fbc2db470327668463e9e9af7bd6537ab59b55e0d2832075
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page