Hython Interpreter
Hython is a Python-style scripting interpreter written in Haxe. (Think hscript, but with Python syntax and semantics)
It’s designed for: embedding scripting into Haxe projects game modding configuration DSLs runtime logic without recompiling
⚠️ This is not CPython. It’s a Python-like language implemented on top of Haxe.
Usage
Basic Usage
import paopao.hython.Parser;
import paopao.hython.Interp;
// Simple execution
var code = "
def greet(name):
return 'Hello, ' + name + '!'
print(greet('World'))
";
var parser = new Parser();
var expr = parser.parseString(code);
var interp = new Interp();
interp.execute(expr);
Advanced Usage - Calling Specific Functions
// Define your Python-like code
var code = "
def add(a, b):
return a + b
def multiply(a, b):
return a * b
def main(x, y):
sum = add(x, y)
product = multiply(x, y)
print('Sum:', sum)
print('Product:', product)
return {'sum': sum, 'product': product}
";
// Parse and execute to define functions
var parser = new Parser();
var expr = parser.parseString(code);
var interp = new Interp();
interp.execute(expr);
// Call specific functions with arguments
var result = interp.calldef("main", [10, 5]);
// Output:
// Sum: 15
// Product: 50
// Call individual functions
var sum = interp.calldef("add", [7, 3]); // Returns 10
var product = interp.calldef("multiply", [4, 6]); // Returns 24
Setting Variables from Haxe
var interp = new Interp();
// Set variables before executing code
interp.variables.set("myValue", 42);
interp.variables.set("config", {debug: true, version: "1.0"});
var code = "
def process():
print('Value:', myValue)
print('Debug mode:', config['debug'])
return myValue * 2
";
var parser = new Parser();
interp.execute(parser.parseString(code));
var result = interp.calldef("process", []); // Returns 84
Error Handling
var interp = new Interp();
// Set custom error handler
interp.errorHandler = function(error) {
trace("Script error: " + error);
// Handle error gracefully
};
// Configure interpreter
interp.maxDepth = 100; // Maximum recursion depth
interp.allowStaticAccess = true; // Allow static access
interp.allowClassResolve = true; // Allow class instantiation
var code = "
def risky_function():
# This might cause an error
return undefined_variable
";
try {
var parser = new Parser();
interp.execute(parser.parseString(code));
interp.calldef("risky_function", []);
} catch (e:Dynamic) {
trace("Caught error: " + e);
}
Features
Python-Style Syntax
deffunction definitions with default arguments- Python operators:
and,or,not,in,not in,is,is not - List comprehensions:
[x * 2 for x in range(10) if x % 2 == 0] - Dictionary comprehensions:
{x: x**2 for x in range(5)} - Slicing:
my_list[1:5],my_string[::2] - Tuple support:
(1, 2, 3)(it have bug I know, but it's not fixed yet at Tuple Unpacking)
Built-in Functions
Hython includes Python-compatible built-in functions:
Type Conversion:
int(),float(),str(),bool()list(),dict(),type()
Numeric Functions:
abs(),min(),max(),sum()round(),pow(),sqrt()
Sequence Functions:
len(),range(),enumerate()sorted(),reversed(),zip()
Logic Functions:
any(),all(),isinstance()
String Functions:
ord(),chr()
I/O Functions:
print()
Example: Complete Script
var code = "
# Calculate fibonacci numbers
def fibonacci(n):
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
def main(count):
results = [fibonacci(i) for i in range(count)]
print('Fibonacci sequence:', results)
return results
# Helper function
def sum_fibonacci(count):
return sum([fibonacci(i) for i in range(count)])
";
var parser = new Parser();
var interp = new Interp();
interp.execute(parser.parseString(code));
// Call functions
var sequence = interp.calldef("main", [10]);
var total = interp.calldef("sum_fibonacci", [10]);
trace("Sequence: " + sequence); // [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
trace("Sum: " + total); // 88
API Reference
Interp Class
Methods
new()- Create a new interpreter instanceexecute(expr:Expr):Dynamic- Execute parsed expression treecalldef(name:String, args:Array<Dynamic>):Dynamic- Call a defined function by namegetdef(name:String):Dynamic- Get a defined function by namesetVar(name:String, value:Dynamic):Dynamic- Set a variablegetVar(name:String):Dynamic- Get a variable
Properties
variables:Map<String, Dynamic>- Global variable storageerrorHandler:Error->Void- Custom error handlermaxDepth:Int- Maximum recursion depth (default: 100)allowStaticAccess:Bool- Allow static field access (default: true)allowClassResolve:Bool- Allow class instantiation (default: true)
Parser Class
Methods
new()- Create a new parser instanceparseString(code:String):Expr- Parse Python-like code into expression tree
Integration Examples
Game Modding Example
class ModSystem {
var interp:Interp;
public function new() {
interp = new Interp();
// Expose game API to scripts
interp.variables.set("game", GameAPI.instance);
}
public function loadMod(scriptPath:String) {
var code = sys.io.File.getContent(scriptPath);
var parser = new Parser();
interp.execute(parser.parseString(code));
}
public function callModFunction(name:String, args:Array<Dynamic>) {
return interp.calldef(name, args);
}
}
// In your mod script (Python-like syntax):
/*
def on_player_hit(damage, enemy):
game.show_damage(damage)
if game.player.health <= 0:
game.game_over()
return True
def on_level_complete(score):
bonus = score * 1.5
game.add_bonus(bonus)
return bonus
*/
Configuration DSL Example
var configScript = "
def setup():
return {
'window': {
'width': 1280,
'height': 720,
'fullscreen': False
},
'audio': {
'master_volume': 0.8,
'music_volume': 0.6
}
}
";
var parser = new Parser();
var interp = new Interp();
interp.execute(parser.parseString(configScript));
var config = interp.calldef("setup", []);
Origin
This project is a fork of NebulaStellaNova’s pythonscript. I saw it, liked the idea, and decided to push it further. :3