Skip to content

Instantly share code, notes, and snippets.

@jasononeil
Created March 6, 2012 03:00
Show Gist options
  • Save jasononeil/1983114 to your computer and use it in GitHub Desktop.
Save jasononeil/1983114 to your computer and use it in GitHub Desktop.
A really simple proof of concept for a haxe interactive shell
/**
A super basic interactive console for haxe.
Done because someone on the mailing list asked and I got distracted :)
Consider this code Public Domain.
Jason O'Neil
Run using:
haxe -lib hscript -x Console.hx
(you will need hscript installed via haxelib)
*/
class Console
{
static public function main()
{
// Create the console
var console = new Console();
// Share some stuff with it
console.interpreter.variables.set("Math",Math); // share the Math class
console.interpreter.variables.set("array",[0,1,2,3]); // set an array
// Start the loop
console.run();
}
// The actual class
public var interpreter:hscript.Interp;
public var parser:hscript.Parser;
public function new()
{
// Create the hscript objects
interpreter = new hscript.Interp();
parser = new hscript.Parser();
}
public function run()
{
var input:String;
var keepRunning = true;
// Keep running as long as they haven't typed "exit"
while (keepRunning)
{
neko.Lib.print("haxe> ");
input = neko.io.File.stdin().readLine();
switch (input)
{
case "exit":
keepRunning = false;
case "help":
trace ("I will not help you!");
default:
try
{
// attempt to run a script
var program = parser.parseString(input);
interpreter.execute(program);
} catch (e:Dynamic)
{
trace ("Your command failed!");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment