pythonScript is a Python syntax interpreter written in Haxe that supports intelligent Haxe module imports, complete Python class system, and function support.
- Standard Python syntax:
import ModuleName - Intelligent type detection:
Math(uppercase) → Haxe modulemath(lowercase) → Python modulehaxe.Math→ detects last partMath(uppercase) → Haxe moduleos.path→ detects last partpath(lowercase) → Python module
- Class definition:
class ClassName: - Constructor:
__init__(self, ...) - Instance methods:
def method(self, params): - Property assignment:
self.property = value - Method calls:
instance.method()
- Create interpreter with parent:
new Interpreter(parentObj) - Dynamic parent access and modification:
interpreter.parent
import pyscript.Interpreter;
class Main {
public static function main() {
var interpreter = new Interpreter();
// Execute Python code
interpreter.run("print('Hello, PyScript!')");
}
}# Import Haxe Math module
import Math
print('π =', Math.PI)
print('sqrt(16) =', Math.sqrt(16))
# Import Sys module
import Sys
print('System:', Sys.systemName)# Define class
class Calculator:
def __init__(self, name):
self.name = name
self.result = 0
def add(self, x, y):
self.result = x + y
return self.result
def show_result(self):
print(self.name, 'result:', self.result)
# Use class
calc = Calculator('MyCalc')
result = calc.add(10, 5)
calc.show_result() # Output: MyCalc result: 15# Define function
def greet(name, age):
print('Hello', name, 'you are', age, 'years old')
# Call function
greet('Alice', 25)// Create parent object
var parentObj = { name: "MyApp", version: "1.0" };
// Create interpreter with parent
var interpreter = new Interpreter(parentObj);
// Access parent
trace("Parent: " + interpreter.parent.name);
// Dynamically modify parent
interpreter.parent = newParentObj;pyscript/
├── pyscript/ # Core interpreter code
│ ├── Interpreter.hx # Main interpreter class
│ ├── Parser.hx # Syntax parser
│ ├── Lexer.hx # Lexical analyzer
│ ├── Token.hx # Token definitions
│ └── ASTNode.hx # Abstract syntax tree nodes
├── example/ # Example code
│ ├── FinalTest.hx # Complete functionality demo
│ ├── SmartImportTest.hx # Smart import system
│ ├── ParentTest.hx # Parent object support
│ ├── HaxeModuleExample.hx # Haxe module examples
│ └── FlixelExample.hx # Game development example
└── README.md # This document
# Compile and run complete functionality demo
haxe -main example.FinalTest -cp . -neko final_test.n && neko final_test.n
# Smart import system demo
haxe -main example.SmartImportTest -cp . -neko smart_test.n && neko smart_test.n
# Parent object support demo
haxe -main example.ParentTest -cp . -neko parent_test.n && neko parent_test.n
# Haxe module usage examples
haxe -main example.HaxeModuleExample -cp . -neko haxe_test.n && neko haxe_test.nPyScript uses intelligent detection algorithms to distinguish between Haxe and Python modules:
| Import Statement | Detection Result | Description |
|---|---|---|
import Math |
Haxe module | First letter uppercase |
import math |
Python module | First letter lowercase |
import sys.FileSystem |
Haxe module | Last part FileSystem uppercase |
- Class definition and instantiation
- Function definition and calling
- Variable assignment and access
- Property assignment (
obj.attr = value) - Method calls (
obj.method()) - Module imports (
import module) - Basic data types (int, float, string, bool)
- Arithmetic and comparison operators
- print function
- List and dictionary operations
- Conditional statements (if/elif/else)
- Loop statements (for/while)
- Exception handling (try/except)
- List comprehensions
- Lambda expressions
class Interpreter {
// Constructor
public function new(?parent:Dynamic)
// Execute Python code
public function run(code:String):Dynamic
// Parent object access
public var parent(get, set):Dynamic
// Variable operations
public function getVariable(name:String):Dynamic
public function setVariable(name:String, value:Dynamic):Void
// Function access
public function getFunctions():Map<String, PythonFunction>
}import Math
class MathUtils:
def __init__(self):
self.pi = Math.PI
def circle_area(self, radius):
return self.pi * Math.pow(radius, 2)
def degrees_to_radians(self, degrees):
return degrees * self.pi / 180
utils = MathUtils()
area = utils.circle_area(5)
print('Circle area:', area)Welcome to submit Issues and Pull Requests to improve the PyScript interpreter!
This project is licensed under the MIT License.