Parser and interpreter for Haxe expressions
Haxe Batchfile
Permalink
Failed to load latest commit information.
bin [TravisCI] CI all the things!! Dec 7, 2015
flash [TravisCI] flash-player-debuger has been moved to caskroom/versions Oct 16, 2016
hscript Add filename / line number error reporting with -D hscriptPos (#57) Jan 2, 2017
script exporting classes fully qualified - 2nd attempt Dec 4, 2015
.gitignore 2.0.6 Apr 2, 2016
.travis.yml [TravisCI] use OSX 10.10 Oct 1, 2016
README.md enable AppVeyor and a fix in TravisCI config Mar 26, 2016
Test.hx Add `do ... while` support. (#51) Oct 3, 2016
appveyor.yml enable AppVeyor and a fix in TravisCI config Mar 26, 2016
build-cpp.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-cs.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-each.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-flash.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-interp.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-java.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-js.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-neko.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-php.hxml [TravisCI] CI all the things!! Dec 7, 2015
build-python.hxml [TravisCI] CI all the things!! Dec 7, 2015
extraParams.hxml make sure to keep IntIterator (close #13) May 20, 2015
haxelib.json 2.0.7 (close #47) Apr 3, 2016
hscript.hxml [TravisCI] CI all the things!! Dec 7, 2015
hscript.hxproj added support for --no-inline Sep 13, 2014
release.bat add a line in release.bat to remind Nicolas to create a git tag Aug 21, 2016

README.md

hscript

TravisCI Build Status AppVeyor Build Status

Parse and evalutate Haxe expressions.

In some projects it's sometimes useful to be able to interpret some code dynamically, without recompilation.

Haxe script is a complete subset of the Haxe language.

It is dynamically typed but allows all Haxe expressions apart from type (class,enum,typedef) declarations.

Usage

var expr = "var x = 4; 1 + 2 * x";
var parser = new hscript.Parser();
var ast = parser.parseString(expr);
var interp = new hscript.Interp();
trace(interp.execute(ast));

In case of a parsing error an hscript.Expr.Error is thrown. You can use parser.line to check the line number.

You can set some globaly accessible identifiers by using interp.variables.set("name",value)

Example

Here's a small example of Haxe Script usage :

var script = "
    var sum = 0;
    for( a in angles )
        sum += Math.cos(a);
    sum; 
";
var parser = new hscript.Parser();
var program = parser.parseString(script);
var interp = new hscript.Interp();
interp.variables.set("Math",Math); // share the Math class
interp.variables.set("angles",[0,1,2,3]); // set the angles list
trace( interp.execute(program) ); 

This will calculate the sum of the cosines of the angles given as input.

Haxe Script has not been really optimized, and it's not meant to be very fast. But it's entirely crossplatform since it's pure Haxe code (it doesn't use any platform-specific API).

Advanced Usage

When compiled with -D hscriptPos you will get fine error reporting at parsing time.

You can subclass hscript.Interp to override behaviors for get, set, call, fcall and cnew.

You can add more binary and unary operations to the parser by setting opPriority, opRightAssoc and unops content.

You can use parser.allowJSON to allow JSON data.

You can use parser.allowTypes to parse types for local vars, exceptions, function args and return types. Types are ignored by the interpreter.

You can use new hscript.Macro(pos).convert(ast) to convert an hscript AST to a Haxe macros one.

Limitations

Compared to Haxe, limitations are :

  • no type declarations (classes, enums, typedefs) : only expressions
  • switch construct is supported but not pattern matching (no variable capture, we use strict equality to compare case values and switch value)
  • only one variable declaration is allowed in var
  • the parser supports optional types for var and function if allowTypes is set, but the interpreter ignores them
  • you can enable per-expression position tracking by compiling with -D hscriptPos

Install

In order to install Haxe Script, use haxelib install hscript and compile your program with -lib hscript.

There are only three files in hscript :

  • hscript.Expr : contains enums declarations
  • hscript.Parser : a small parser that turns a string into an expression structure (AST)
  • hscript.Interp : a small interpreter that execute the AST and returns the latest evaluated value