This project demonstrates a Python-based Lua interpreter. It allows you to integrate Lua scripting within Python applications and extend Lua with custom Python functions and libraries.
Note This project is primarily a first attempt at building an interpreter and should not be used for serious projects.
from lua.interpreter import Interpreter, Library
def add_five(n):
return n + 5
libraries = [
Library(
name="custom",
attributes={
"version": "1.2.3",
"doc": "A custom Library"
},
methods={
"print_python": print,
"add_five": add_five
}
)
]
interpreter = Interpreter(libraries)The following Lua script uses the Sieve of Eratosthenes algorithm to find all prime numbers up to a given number:
CODE_PRIMES = """
function find_primes_up_to(n)
if n < 2 then
return {}
end
-- Initialise a table to mark numbers
local is_prime = {}
for i = 2, n do
is_prime[i] = true
end
-- Apply Sieve of Eratosthenes
for i = 2, math.sqrt(n) do
if is_prime[i] then
for j = i * i, n, i do
is_prime[j] = false
end
end
end
-- Collect all prime numbers
local primes = {}
for i = 2, n do
if is_prime[i] then
table.insert(primes, i)
end
end
return primes
end
-- Test the function
local n = 50
local primes = find_primes_up_to(n)
print("Prime numbers up to " .. n .. ":")
for _, prime in ipairs(primes) do
print(prime)
end
"""Run custom Lua code using the interpreter:
code = """
local custom = require('custom')
local name = "John"
local number = 32
print("New number is " .. custom.add_five(number))
custom.print_python("The name is " .. name) -- print `name` Python-side
"""
result, success = interpreter.exec(code)Print the logs from the Lua console:
print(
f"""# Lua Console:
```plaintext
{interpreter.logs}
```"""
)You can inspect the Lua environment to list all variables and functions defined:
for key, value in interpreter.environment.items():
print(key, value)Clear logs or reset the environment to start fresh:
interpreter.clear_logs() # Clear the Lua console
interpreter.reset_environment() # Reset all variables and functions defined in Lua context
interpreter.reset() # Reset both Lua console and context