Created
January 7, 2016 21:41
-
-
Save anissen/77959a52bb50124e4def to your computer and use it in GitHub Desktop.
hscript revised test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
haxe -main Test -lib hscript -neko build.n && neko build.n script.hxs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
game.say('doing it!' + blah); | |
for (y in 1 ... 3) game.add_hex(1, y); | |
game.say('done :)'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Script { | |
var program :hscript.Expr; | |
var interp :hscript.Interp; | |
public function new(source :haxe.io.Input) { | |
var parser = new hscript.Parser(); | |
interp = new hscript.Interp(); | |
try { | |
program = parser.parse(source); | |
add_context("Math", Math); // share the Math class | |
add_context("angles", [0,1,2,3]); // set the angles list | |
} catch (e :hscript.Expr.Error) { | |
trace('Parse error at line ${parser.line}: $e'); | |
} | |
} | |
public function execute() :Dynamic { | |
if (program == null) { | |
trace('No program to execute.'); | |
return null; | |
} | |
try { | |
return interp.execute(program); | |
} catch (e :hscript.Expr.Error) { | |
trace('Run-time error: $e'); | |
return null; | |
} | |
} | |
public function add_context(name :String, value :Dynamic) { | |
if (interp.variables.exists(name)) trace('Script Context Warning: Overwriting value "$name" (containing ${interp.variables[name]}) with $value!'); | |
interp.variables.set(name, value); | |
} | |
} | |
class ScriptContext { | |
public function new() { | |
} | |
public function add_hex(x :Int, y: Int) { | |
trace('add_hex: $x, $y'); | |
} | |
public function say(text :String) { | |
trace('say: "$text"'); | |
} | |
} | |
class Test { | |
static public function main() { | |
var miniScript = new Script(new haxe.io.StringInput("yell('Hello World!');")); | |
miniScript.add_context('yell', function(s) { trace('HELLO: $s'); }); | |
miniScript.execute(); | |
var levelScript = new Script(sys.io.File.read("script.hxs", false)); | |
levelScript.add_context('game', new ScriptContext()); | |
// levelScript.add_context('Game', ScriptContext); | |
levelScript.add_context('blah', 42); | |
var result = levelScript.execute(); | |
trace('Script terminated with result: $result'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment