A Python interpreter implementation written in Luau that runs inside Roblox. PyLua 0.2 compiles Python code to bytecode for efficient execution, featuring comprehensive support for Python language features.
local python = require('path.to.PyLua.python')
-- Simple one-line execution
python.execute('print("Hello, PyLua!")')For better performance when running the same code repeatedly:
local python = require('path.to.PyLua.python')
-- Compile Python code to bytecode
local bytecode, error = python.compile([[
x = 10
y = 20
result = x + y
print("Result:", result)
]])
if error then
print("Compilation error:", error)
return
end
-- Execute the bytecode multiple times
for i = 1, 5 do
print("Execution", i)
local success, variables = python.runBytecode(bytecode)
if success then
print("Variables:", variables)
end
endlocal python = require('path.to.PyLua.python')
local code = [[
# Create and manipulate Python data structures
students = [
{"name": "Alice", "grade": 85},
{"name": "Bob", "grade": 92},
{"name": "Charlie", "grade": 78}
]
print("Student grades:")
total = 0
count = 0
for student in students:
name = student["name"]
grade = student["grade"]
print(name, ":", grade)
total = total + grade
count = count + 1
average = total / count
print("Class average:", average)
if average >= 80:
print("Great class performance!")
else:
print("Room for improvement")
]]
-- Compile and execute
local bytecode, compileError = python.compile(code)
if compileError then
print("Compilation failed:", compileError)
else
local success, variables = python.runBytecode(bytecode)
if not success then
print("Runtime error:", variables)
end
endCompiles and immediately executes Python code.
- Parameters:
code(string) - Python source code - Returns:
success(boolean),result(any) - Execution result or error message
Compiles Python source code to bytecode for later execution.
- Parameters:
code(string) - Python source code - Returns:
bytecode(table),error(string|nil) - Compiled bytecode or error message
Executes pre-compiled bytecode.
- Parameters:
bytecode(table) - Compiled bytecode frompython.compile()options(table, optional) - Execution optionsdebug(boolean) - Enable debug output
- Returns:
success(boolean),variables(table) - Success status and variable state
Bytecode compilation provides several advantages:
- Performance: Compile once, execute many times without re-parsing
- Validation: Catch syntax errors at compile time
- Inspection: Examine bytecode structure for debugging
- Caching: Store compiled bytecode for future use
local bytecode = {
constants = {}, -- Constant values (numbers, strings, etc.)
names = {}, -- Variable and function names
code = {}, -- Bytecode instructions
sourceLines = {} -- Original source lines for error reporting
}Enable debug mode to see detailed execution information:
local python = require('path.to.PyLua.python')
local bytecode = python.compile('x = 42\nprint("x =", x)')
python.runBytecode(bytecode, {debug = true})Debug output includes:
- Stack operations (PUSH/POP)
- Variable assignments
- Function calls
- Instruction execution flow
- Numbers: Integers and floats
- Strings: Text with proper escaping
- Booleans:
True,False - None: Python's null value
- Lists:
[1, 2, 3]with indexing andlen() - Dictionaries:
{"key": "value"}with key access - Tuples:
(1, 2, 3)immutable sequences - Sets:
{1, 2, 3}unique value collections
- Conditionals:
if,elif,elsewith nesting - For Loops:
for item in iterable:over lists, ranges, etc. - While Loops:
while condition:with proper termination
print(*args)- Output valueslen(obj)- Get length of collectionstype(obj)- Get object typerange(start, stop, step)- Generate number sequencesint(value),float(value),str(value),bool(value)- Type conversion
- Arithmetic:
+,-,*,/,%,** - Comparison:
==,!=,<,<=,>,>= - Assignment:
=
PyLua 0.2 uses a three-phase architecture:
- Compiler (
compiler/compiler.luau): Parses Python source and generates bytecode - Virtual Machine (
vm/bytecode_executor.luau): Executes bytecode instructions - Core Libraries (
core/): Built-in functions and Python object implementations
Python Source Code
↓
[Tokenizer]
↓
[Compiler] → Bytecode
↓
[VM Executor] → ResultsCurrent limitations (may be addressed in future versions):
- No custom function definitions (
def) - No classes or inheritance
- Limited exception handling
- No module imports
- No list comprehensions
This repository is under the MIT license.