moonpie 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- moonpie-0.1.0/LICENSE +21 -0
- moonpie-0.1.0/MANIFEST.in +7 -0
- moonpie-0.1.0/PKG-INFO +181 -0
- moonpie-0.1.0/README.md +148 -0
- moonpie-0.1.0/examples/examples.lua +112 -0
- moonpie-0.1.0/moonpie/__init__.py +14 -0
- moonpie-0.1.0/moonpie/__main__.py +122 -0
- moonpie-0.1.0/moonpie/ast_nodes.py +186 -0
- moonpie-0.1.0/moonpie/evaluator.py +339 -0
- moonpie-0.1.0/moonpie/lexer.py +268 -0
- moonpie-0.1.0/moonpie/lua_builtins.py +205 -0
- moonpie-0.1.0/moonpie/lua_interpreter.py +43 -0
- moonpie-0.1.0/moonpie/lua_object.py +183 -0
- moonpie-0.1.0/moonpie/lua_token.py +79 -0
- moonpie-0.1.0/moonpie/parser.py +415 -0
- moonpie-0.1.0/moonpie.egg-info/PKG-INFO +181 -0
- moonpie-0.1.0/moonpie.egg-info/SOURCES.txt +21 -0
- moonpie-0.1.0/moonpie.egg-info/dependency_links.txt +1 -0
- moonpie-0.1.0/moonpie.egg-info/entry_points.txt +2 -0
- moonpie-0.1.0/moonpie.egg-info/requires.txt +5 -0
- moonpie-0.1.0/moonpie.egg-info/top_level.txt +1 -0
- moonpie-0.1.0/pyproject.toml +52 -0
- moonpie-0.1.0/setup.cfg +4 -0
moonpie-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Raymond Hulha
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
moonpie-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: moonpie
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Lua interpreter written in Python
|
|
5
|
+
Author-email: MoonPie <java4life@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/rhulha/MoonPie
|
|
8
|
+
Project-URL: Documentation, https://github.com/rhulha/MoonPie#readme
|
|
9
|
+
Project-URL: Repository, https://github.com/rhulha/MoonPie
|
|
10
|
+
Project-URL: Bug Reports, https://github.com/rhulha/MoonPie/issues
|
|
11
|
+
Keywords: lua,interpreter,scripting,language
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
23
|
+
Classifier: Topic :: Software Development :: Interpreters
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.7
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=6.0; extra == "dev"
|
|
30
|
+
Requires-Dist: black; extra == "dev"
|
|
31
|
+
Requires-Dist: flake8; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+
# MoonPie - Lua Interpreter in Python
|
|
35
|
+
|
|
36
|
+
A Lua interpreter written in Python, implementing core Lua language features.
|
|
37
|
+
|
|
38
|
+
## Installation
|
|
39
|
+
|
|
40
|
+
### From PyPI (when published)
|
|
41
|
+
```bash
|
|
42
|
+
pip install moonpie
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### From Source
|
|
46
|
+
```bash
|
|
47
|
+
git clone https://github.com/yourusername/moonpie.git
|
|
48
|
+
cd moonpie
|
|
49
|
+
pip install -e .
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
## Features
|
|
53
|
+
|
|
54
|
+
### Language Support
|
|
55
|
+
- **Data Types**: numbers, strings, booleans, nil, tables, functions
|
|
56
|
+
- **Operators**: arithmetic (+, -, *, /, %, ^), comparison (==, ~=, <, <=, >, >=), logical (and, or, not), string concatenation (..)
|
|
57
|
+
- **Variables**: local and global variables
|
|
58
|
+
- **Tables**: Lua's primary data structure with array and hash parts
|
|
59
|
+
- **Functions**: first-class functions with closures
|
|
60
|
+
- **Control Flow**: if/elseif/else, while, repeat-until, for loops (numeric and generic)
|
|
61
|
+
- **Comments**: single-line (--) and multi-line (--[[ ]])
|
|
62
|
+
|
|
63
|
+
### Built-in Functions
|
|
64
|
+
- **Basic**: print, type, tonumber, tostring, assert, error
|
|
65
|
+
- **Math**: abs, floor, ceil, sqrt, sin, cos, tan, max, min, pi
|
|
66
|
+
- **String**: len, upper, lower, sub
|
|
67
|
+
|
|
68
|
+
## Architecture
|
|
69
|
+
|
|
70
|
+
The interpreter follows a classic architecture:
|
|
71
|
+
|
|
72
|
+
1. **Lexer** (`lexer.py`) - Tokenizes Lua source code
|
|
73
|
+
2. **Parser** (`parser.py`) - Builds an Abstract Syntax Tree (AST) from tokens
|
|
74
|
+
3. **AST Nodes** (`ast_nodes.py`) - Defines all AST node types
|
|
75
|
+
4. **Evaluator** (`evaluator.py`) - Traverses and executes the AST
|
|
76
|
+
5. **Object System** (`lua_object.py`) - Implements Lua values and environment
|
|
77
|
+
6. **Built-ins** (`lua_builtins.py`) - Provides standard Lua functions
|
|
78
|
+
|
|
79
|
+
## Usage
|
|
80
|
+
|
|
81
|
+
### Command Line (after installation)
|
|
82
|
+
```bash
|
|
83
|
+
moonpie # Start interactive REPL
|
|
84
|
+
moonpie script.lua # Execute a Lua file
|
|
85
|
+
moonpie examples/examples.lua
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Python Module
|
|
89
|
+
```bash
|
|
90
|
+
python -m moonpie # Start interactive REPL
|
|
91
|
+
python -m moonpie script.lua
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### As a Library
|
|
95
|
+
```python
|
|
96
|
+
from moonpie import LuaInterpreter
|
|
97
|
+
|
|
98
|
+
interpreter = LuaInterpreter()
|
|
99
|
+
result = interpreter.run("""
|
|
100
|
+
function add(a, b)
|
|
101
|
+
return a + b
|
|
102
|
+
end
|
|
103
|
+
print(add(5, 3))
|
|
104
|
+
""")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Example Code
|
|
108
|
+
|
|
109
|
+
```lua
|
|
110
|
+
-- Variables and arithmetic
|
|
111
|
+
local x = 10
|
|
112
|
+
local y = 20
|
|
113
|
+
print(x + y) -- 30
|
|
114
|
+
|
|
115
|
+
-- Functions
|
|
116
|
+
function factorial(n)
|
|
117
|
+
if n <= 1 then
|
|
118
|
+
return 1
|
|
119
|
+
else
|
|
120
|
+
return n * factorial(n - 1)
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
print(factorial(5)) -- 120
|
|
124
|
+
|
|
125
|
+
-- Tables
|
|
126
|
+
local person = {name = "John", age = 30}
|
|
127
|
+
print(person.name) -- John
|
|
128
|
+
|
|
129
|
+
local array = {10, 20, 30}
|
|
130
|
+
print(array[1]) -- 10
|
|
131
|
+
print(#array) -- 3
|
|
132
|
+
|
|
133
|
+
-- Loops
|
|
134
|
+
for i = 1, 5 do
|
|
135
|
+
print(i)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
-- String operations
|
|
139
|
+
local greeting = "Hello" .. " " .. "World"
|
|
140
|
+
print(greeting) -- Hello World
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
## Testing
|
|
144
|
+
|
|
145
|
+
Run the test suite:
|
|
146
|
+
```bash
|
|
147
|
+
moonpie tests/test_basic.lua
|
|
148
|
+
# or
|
|
149
|
+
python -m moonpie tests/test_basic.lua
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
Run examples:
|
|
153
|
+
```bash
|
|
154
|
+
moonpie examples/examples.lua
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## Implementation Details
|
|
158
|
+
|
|
159
|
+
- **Lexer**: Character-by-character tokenization with support for keywords, operators, strings, numbers, and comments
|
|
160
|
+
- **Parser**: Recursive descent parser with proper operator precedence
|
|
161
|
+
- **Evaluator**: Tree-walking interpreter with environment-based scoping
|
|
162
|
+
- **Tables**: Hybrid array/hash implementation matching Lua's behavior
|
|
163
|
+
- **Functions**: Support for closures and first-class functions
|
|
164
|
+
- **Error Handling**: Runtime errors, syntax errors, and type checking
|
|
165
|
+
|
|
166
|
+
## Limitations
|
|
167
|
+
|
|
168
|
+
This is a Lua interpreter for educational purposes. Some advanced features are not implemented:
|
|
169
|
+
- Metatables and metamethods
|
|
170
|
+
- Coroutines
|
|
171
|
+
- Module system (require/module)
|
|
172
|
+
- File I/O
|
|
173
|
+
- Full standard library
|
|
174
|
+
- Garbage collection optimization
|
|
175
|
+
- Bytecode compilation
|
|
176
|
+
|
|
177
|
+
## References
|
|
178
|
+
|
|
179
|
+
- [Lua 5.4 Reference Manual](https://www.lua.org/manual/5.4/)
|
|
180
|
+
- [Writing A Compiler In Go](https://compilerbook.com/)
|
|
181
|
+
- [Crafting Interpreters](https://craftinginterpreters.com/)
|
moonpie-0.1.0/README.md
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
# MoonPie - Lua Interpreter in Python
|
|
2
|
+
|
|
3
|
+
A Lua interpreter written in Python, implementing core Lua language features.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
### From PyPI (when published)
|
|
8
|
+
```bash
|
|
9
|
+
pip install moonpie
|
|
10
|
+
```
|
|
11
|
+
|
|
12
|
+
### From Source
|
|
13
|
+
```bash
|
|
14
|
+
git clone https://github.com/yourusername/moonpie.git
|
|
15
|
+
cd moonpie
|
|
16
|
+
pip install -e .
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Features
|
|
20
|
+
|
|
21
|
+
### Language Support
|
|
22
|
+
- **Data Types**: numbers, strings, booleans, nil, tables, functions
|
|
23
|
+
- **Operators**: arithmetic (+, -, *, /, %, ^), comparison (==, ~=, <, <=, >, >=), logical (and, or, not), string concatenation (..)
|
|
24
|
+
- **Variables**: local and global variables
|
|
25
|
+
- **Tables**: Lua's primary data structure with array and hash parts
|
|
26
|
+
- **Functions**: first-class functions with closures
|
|
27
|
+
- **Control Flow**: if/elseif/else, while, repeat-until, for loops (numeric and generic)
|
|
28
|
+
- **Comments**: single-line (--) and multi-line (--[[ ]])
|
|
29
|
+
|
|
30
|
+
### Built-in Functions
|
|
31
|
+
- **Basic**: print, type, tonumber, tostring, assert, error
|
|
32
|
+
- **Math**: abs, floor, ceil, sqrt, sin, cos, tan, max, min, pi
|
|
33
|
+
- **String**: len, upper, lower, sub
|
|
34
|
+
|
|
35
|
+
## Architecture
|
|
36
|
+
|
|
37
|
+
The interpreter follows a classic architecture:
|
|
38
|
+
|
|
39
|
+
1. **Lexer** (`lexer.py`) - Tokenizes Lua source code
|
|
40
|
+
2. **Parser** (`parser.py`) - Builds an Abstract Syntax Tree (AST) from tokens
|
|
41
|
+
3. **AST Nodes** (`ast_nodes.py`) - Defines all AST node types
|
|
42
|
+
4. **Evaluator** (`evaluator.py`) - Traverses and executes the AST
|
|
43
|
+
5. **Object System** (`lua_object.py`) - Implements Lua values and environment
|
|
44
|
+
6. **Built-ins** (`lua_builtins.py`) - Provides standard Lua functions
|
|
45
|
+
|
|
46
|
+
## Usage
|
|
47
|
+
|
|
48
|
+
### Command Line (after installation)
|
|
49
|
+
```bash
|
|
50
|
+
moonpie # Start interactive REPL
|
|
51
|
+
moonpie script.lua # Execute a Lua file
|
|
52
|
+
moonpie examples/examples.lua
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
### Python Module
|
|
56
|
+
```bash
|
|
57
|
+
python -m moonpie # Start interactive REPL
|
|
58
|
+
python -m moonpie script.lua
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### As a Library
|
|
62
|
+
```python
|
|
63
|
+
from moonpie import LuaInterpreter
|
|
64
|
+
|
|
65
|
+
interpreter = LuaInterpreter()
|
|
66
|
+
result = interpreter.run("""
|
|
67
|
+
function add(a, b)
|
|
68
|
+
return a + b
|
|
69
|
+
end
|
|
70
|
+
print(add(5, 3))
|
|
71
|
+
""")
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Example Code
|
|
75
|
+
|
|
76
|
+
```lua
|
|
77
|
+
-- Variables and arithmetic
|
|
78
|
+
local x = 10
|
|
79
|
+
local y = 20
|
|
80
|
+
print(x + y) -- 30
|
|
81
|
+
|
|
82
|
+
-- Functions
|
|
83
|
+
function factorial(n)
|
|
84
|
+
if n <= 1 then
|
|
85
|
+
return 1
|
|
86
|
+
else
|
|
87
|
+
return n * factorial(n - 1)
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
print(factorial(5)) -- 120
|
|
91
|
+
|
|
92
|
+
-- Tables
|
|
93
|
+
local person = {name = "John", age = 30}
|
|
94
|
+
print(person.name) -- John
|
|
95
|
+
|
|
96
|
+
local array = {10, 20, 30}
|
|
97
|
+
print(array[1]) -- 10
|
|
98
|
+
print(#array) -- 3
|
|
99
|
+
|
|
100
|
+
-- Loops
|
|
101
|
+
for i = 1, 5 do
|
|
102
|
+
print(i)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
-- String operations
|
|
106
|
+
local greeting = "Hello" .. " " .. "World"
|
|
107
|
+
print(greeting) -- Hello World
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Testing
|
|
111
|
+
|
|
112
|
+
Run the test suite:
|
|
113
|
+
```bash
|
|
114
|
+
moonpie tests/test_basic.lua
|
|
115
|
+
# or
|
|
116
|
+
python -m moonpie tests/test_basic.lua
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
Run examples:
|
|
120
|
+
```bash
|
|
121
|
+
moonpie examples/examples.lua
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Implementation Details
|
|
125
|
+
|
|
126
|
+
- **Lexer**: Character-by-character tokenization with support for keywords, operators, strings, numbers, and comments
|
|
127
|
+
- **Parser**: Recursive descent parser with proper operator precedence
|
|
128
|
+
- **Evaluator**: Tree-walking interpreter with environment-based scoping
|
|
129
|
+
- **Tables**: Hybrid array/hash implementation matching Lua's behavior
|
|
130
|
+
- **Functions**: Support for closures and first-class functions
|
|
131
|
+
- **Error Handling**: Runtime errors, syntax errors, and type checking
|
|
132
|
+
|
|
133
|
+
## Limitations
|
|
134
|
+
|
|
135
|
+
This is a Lua interpreter for educational purposes. Some advanced features are not implemented:
|
|
136
|
+
- Metatables and metamethods
|
|
137
|
+
- Coroutines
|
|
138
|
+
- Module system (require/module)
|
|
139
|
+
- File I/O
|
|
140
|
+
- Full standard library
|
|
141
|
+
- Garbage collection optimization
|
|
142
|
+
- Bytecode compilation
|
|
143
|
+
|
|
144
|
+
## References
|
|
145
|
+
|
|
146
|
+
- [Lua 5.4 Reference Manual](https://www.lua.org/manual/5.4/)
|
|
147
|
+
- [Writing A Compiler In Go](https://compilerbook.com/)
|
|
148
|
+
- [Crafting Interpreters](https://craftinginterpreters.com/)
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
-- MoonPie Lua Interpreter Examples
|
|
2
|
+
|
|
3
|
+
print("=== Fibonacci Sequence ===")
|
|
4
|
+
function fibonacci(n)
|
|
5
|
+
if n <= 1 then
|
|
6
|
+
return n
|
|
7
|
+
end
|
|
8
|
+
return fibonacci(n - 1) + fibonacci(n - 2)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
for i = 0, 10 do
|
|
12
|
+
print("fib(" .. tostring(i) .. ") = " .. tostring(fibonacci(i)))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
print("\n=== Table Manipulation ===")
|
|
16
|
+
local fruits = {"apple", "banana", "cherry", "date"}
|
|
17
|
+
for i = 1, #fruits do
|
|
18
|
+
print(i .. ": " .. fruits[i])
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
print("\n=== Higher-Order Functions ===")
|
|
22
|
+
function map(arr, fn)
|
|
23
|
+
local result = {}
|
|
24
|
+
for i = 1, #arr do
|
|
25
|
+
result[i] = fn(arr[i])
|
|
26
|
+
end
|
|
27
|
+
return result
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
function double(x)
|
|
31
|
+
return x * 2
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
local numbers = {1, 2, 3, 4, 5}
|
|
35
|
+
local doubled = map(numbers, double)
|
|
36
|
+
for i = 1, #doubled do
|
|
37
|
+
print(doubled[i])
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
print("\n=== Closures ===")
|
|
41
|
+
function makeCounter()
|
|
42
|
+
local count = 0
|
|
43
|
+
return function()
|
|
44
|
+
count = count + 1
|
|
45
|
+
return count
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
local counter = makeCounter()
|
|
50
|
+
print(counter())
|
|
51
|
+
print(counter())
|
|
52
|
+
print(counter())
|
|
53
|
+
|
|
54
|
+
print("\n=== Math Library ===")
|
|
55
|
+
print("pi = " .. tostring(math.pi))
|
|
56
|
+
print("sqrt(16) = " .. tostring(math.sqrt(16)))
|
|
57
|
+
print("max(5, 10, 3) = " .. tostring(math.max(5, 10, 3)))
|
|
58
|
+
print("min(5, 10, 3) = " .. tostring(math.min(5, 10, 3)))
|
|
59
|
+
|
|
60
|
+
print("\n=== String Library ===")
|
|
61
|
+
local text = "Hello World"
|
|
62
|
+
print("Original: " .. text)
|
|
63
|
+
print("Upper: " .. string.upper(text))
|
|
64
|
+
print("Lower: " .. string.lower(text))
|
|
65
|
+
print("Length: " .. tostring(string.len(text)))
|
|
66
|
+
print("Substring (1,5): " .. string.sub(text, 1, 5))
|
|
67
|
+
|
|
68
|
+
print("\n=== Nested Functions ===")
|
|
69
|
+
function outer(x)
|
|
70
|
+
local function inner(y)
|
|
71
|
+
return x + y
|
|
72
|
+
end
|
|
73
|
+
return inner
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
local add5 = outer(5)
|
|
77
|
+
print(add5(3))
|
|
78
|
+
print(add5(7))
|
|
79
|
+
|
|
80
|
+
print("\n=== Complex Table ===")
|
|
81
|
+
local person = {
|
|
82
|
+
name = "Alice",
|
|
83
|
+
age = 30,
|
|
84
|
+
address = {
|
|
85
|
+
city = "New York",
|
|
86
|
+
country = "USA"
|
|
87
|
+
},
|
|
88
|
+
hobbies = {"reading", "coding", "gaming"}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
print("Name: " .. person.name)
|
|
92
|
+
print("Age: " .. tostring(person.age))
|
|
93
|
+
print("City: " .. person.address.city)
|
|
94
|
+
print("First hobby: " .. person.hobbies[1])
|
|
95
|
+
|
|
96
|
+
print("\n=== Boolean Logic ===")
|
|
97
|
+
local function check(value)
|
|
98
|
+
if not value then
|
|
99
|
+
return "None"
|
|
100
|
+
elseif value > 10 then
|
|
101
|
+
return "Large"
|
|
102
|
+
elseif value > 5 then
|
|
103
|
+
return "Medium"
|
|
104
|
+
else
|
|
105
|
+
return "Small"
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
print(check(15))
|
|
110
|
+
print(check(8))
|
|
111
|
+
print(check(3))
|
|
112
|
+
print(check(nil))
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""
|
|
2
|
+
MoonPie - A Lua interpreter written in Python
|
|
3
|
+
|
|
4
|
+
A complete Lua interpreter implementing core Lua language features
|
|
5
|
+
including functions, tables, closures, loops, and standard libraries.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from .lua_interpreter import LuaInterpreter
|
|
9
|
+
|
|
10
|
+
__version__ = "0.1.0"
|
|
11
|
+
__author__ = "MoonPie"
|
|
12
|
+
__email__ = "moonpie@example.com"
|
|
13
|
+
|
|
14
|
+
__all__ = ["LuaInterpreter"]
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""
|
|
3
|
+
MoonPie Command Line Interface
|
|
4
|
+
|
|
5
|
+
This module provides command-line execution capabilities for Lua files
|
|
6
|
+
and an interactive REPL (Read-Eval-Print Loop).
|
|
7
|
+
|
|
8
|
+
Usage:
|
|
9
|
+
python -m moonpie <file.lua> # Execute a Lua file
|
|
10
|
+
python -m moonpie # Start interactive REPL
|
|
11
|
+
moonpie <file.lua> # Execute a Lua file (after installation)
|
|
12
|
+
moonpie # Start interactive REPL (after installation)
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
import sys
|
|
16
|
+
import argparse
|
|
17
|
+
import os
|
|
18
|
+
from pathlib import Path
|
|
19
|
+
from .lua_interpreter import LuaInterpreter
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def start_repl():
|
|
23
|
+
"""Start the interactive REPL."""
|
|
24
|
+
interpreter = LuaInterpreter()
|
|
25
|
+
|
|
26
|
+
print("MoonPie - Lua Interpreter in Python")
|
|
27
|
+
print(f"Version {__package__.__version__ if hasattr(__package__, '__version__') else '0.1.0'}")
|
|
28
|
+
print("Type 'exit' or press Ctrl+C to quit")
|
|
29
|
+
print()
|
|
30
|
+
|
|
31
|
+
while True:
|
|
32
|
+
try:
|
|
33
|
+
line = input("> ")
|
|
34
|
+
|
|
35
|
+
if line.strip() in ('exit', 'quit'):
|
|
36
|
+
print("Goodbye!")
|
|
37
|
+
break
|
|
38
|
+
|
|
39
|
+
if not line.strip():
|
|
40
|
+
continue
|
|
41
|
+
|
|
42
|
+
result = interpreter.run(line)
|
|
43
|
+
|
|
44
|
+
if result is not None:
|
|
45
|
+
print(result.to_str())
|
|
46
|
+
|
|
47
|
+
except KeyboardInterrupt:
|
|
48
|
+
print("\nGoodbye!")
|
|
49
|
+
break
|
|
50
|
+
except EOFError:
|
|
51
|
+
print("\nGoodbye!")
|
|
52
|
+
break
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def main():
|
|
56
|
+
"""Main entry point for command-line execution."""
|
|
57
|
+
parser = argparse.ArgumentParser(
|
|
58
|
+
prog='moonpie',
|
|
59
|
+
description='MoonPie - A Lua interpreter written in Python',
|
|
60
|
+
epilog='Examples:\n python -m moonpie script.lua # Execute file\n python -m moonpie # Start REPL',
|
|
61
|
+
formatter_class=argparse.RawDescriptionHelpFormatter
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
parser.add_argument(
|
|
65
|
+
'file',
|
|
66
|
+
nargs='?',
|
|
67
|
+
help='Lua file to execute (.lua extension recommended). If omitted, starts interactive REPL.'
|
|
68
|
+
)
|
|
69
|
+
|
|
70
|
+
parser.add_argument(
|
|
71
|
+
'--version',
|
|
72
|
+
action='version',
|
|
73
|
+
version='%(prog)s 0.1.0'
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
parser.add_argument(
|
|
77
|
+
'-v', '--verbose',
|
|
78
|
+
action='store_true',
|
|
79
|
+
help='Enable verbose output'
|
|
80
|
+
)
|
|
81
|
+
|
|
82
|
+
try:
|
|
83
|
+
args = parser.parse_args()
|
|
84
|
+
except SystemExit:
|
|
85
|
+
return
|
|
86
|
+
|
|
87
|
+
if not args.file:
|
|
88
|
+
start_repl()
|
|
89
|
+
return
|
|
90
|
+
|
|
91
|
+
if not os.path.isfile(args.file):
|
|
92
|
+
print(f"Error: File '{args.file}' not found.", file=sys.stderr)
|
|
93
|
+
sys.exit(1)
|
|
94
|
+
|
|
95
|
+
try:
|
|
96
|
+
with open(args.file, 'r', encoding='utf-8') as f:
|
|
97
|
+
code = f.read()
|
|
98
|
+
except UnicodeDecodeError:
|
|
99
|
+
print(f"Error: Could not read '{args.file}' as UTF-8 text.", file=sys.stderr)
|
|
100
|
+
sys.exit(1)
|
|
101
|
+
except IOError as e:
|
|
102
|
+
print(f"Error reading file '{args.file}': {e}", file=sys.stderr)
|
|
103
|
+
sys.exit(1)
|
|
104
|
+
|
|
105
|
+
if args.verbose:
|
|
106
|
+
print(f"Executing Lua file: {args.file}")
|
|
107
|
+
print(f"File size: {len(code)} characters")
|
|
108
|
+
|
|
109
|
+
try:
|
|
110
|
+
interpreter = LuaInterpreter()
|
|
111
|
+
result = interpreter.run(code)
|
|
112
|
+
|
|
113
|
+
except Exception as e:
|
|
114
|
+
print(f"Error executing Lua: {e}", file=sys.stderr)
|
|
115
|
+
if args.verbose:
|
|
116
|
+
import traceback
|
|
117
|
+
traceback.print_exc()
|
|
118
|
+
sys.exit(1)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
if __name__ == '__main__':
|
|
122
|
+
main()
|