All Projects → HaxeFoundation → Hscript

HaxeFoundation / Hscript

Licence: mit
Parser and interpreter for Haxe expressions

Programming Languages

haxe
709 projects

Projects that are alternatives of or similar to Hscript

Eval
Eval is a lightweight interpreter framework written in Swift, evaluating expressions at runtime
Stars: ✭ 157 (-11.8%)
Mutual labels:  interpreter
Boa
Boa is an embeddable and experimental Javascript engine written in Rust. Currently, it has support for some of the language.
Stars: ✭ 2,509 (+1309.55%)
Mutual labels:  interpreter
Symja android library
☕️ Symja - computer algebra language & symbolic math library. A collection of popular algorithms implemented in pure Java.
Stars: ✭ 170 (-4.49%)
Mutual labels:  interpreter
Metalang99
A functional language for C99 preprocessor metaprogramming
Stars: ✭ 152 (-14.61%)
Mutual labels:  interpreter
Pfp
pfp - Python Format Parser - a python-based 010 Editor template interpreter
Stars: ✭ 163 (-8.43%)
Mutual labels:  interpreter
Wasm Micro Runtime
WebAssembly Micro Runtime (WAMR)
Stars: ✭ 2,440 (+1270.79%)
Mutual labels:  interpreter
Lioness
The Lioness Programming Language
Stars: ✭ 155 (-12.92%)
Mutual labels:  interpreter
Alchemyvm
WebAssembly Virtual Machine Built In Elixir
Stars: ✭ 176 (-1.12%)
Mutual labels:  interpreter
Cosmos
A new logic programming language.
Stars: ✭ 164 (-7.87%)
Mutual labels:  interpreter
Duckscript
Simple, extendable and embeddable scripting language.
Stars: ✭ 169 (-5.06%)
Mutual labels:  interpreter
Cling
The cling C++ interpreter
Stars: ✭ 2,322 (+1204.49%)
Mutual labels:  interpreter
Monkey
An Interpreter In Go
Stars: ✭ 162 (-8.99%)
Mutual labels:  interpreter
Pcgr
Personal Cancer Genome Reporter (PCGR)
Stars: ✭ 168 (-5.62%)
Mutual labels:  interpreter
S
the s shell
Stars: ✭ 158 (-11.24%)
Mutual labels:  interpreter
Fake
嵌入式脚本语言 Lightweight embedded scripting language
Stars: ✭ 172 (-3.37%)
Mutual labels:  interpreter
Shentong
A Haskell implementation of the Shen programming language.
Stars: ✭ 155 (-12.92%)
Mutual labels:  interpreter
Nf Interpreter
⚙️ nanoFramework Interpreter, CLR, HAL, PAL and reference target boards
Stars: ✭ 168 (-5.62%)
Mutual labels:  interpreter
Go.vm
A simple virtual machine - compiler & interpreter - written in golang
Stars: ✭ 178 (+0%)
Mutual labels:  interpreter
Monkey Rust
An interpreter for the Monkey programming language written in Rust
Stars: ✭ 174 (-2.25%)
Mutual labels:  interpreter
Covscript
Covariant Script Interpreter
Stars: ✭ 169 (-5.06%)
Mutual labels:  interpreter

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 parser.allowMetadata to parse metadata before expressions on in anonymous types. Metadata are ignored by the interpreter.

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

You can use hscript.Checker in order to type check and even get completion, using haxe -xml output for type information.

Limitations

Compared to Haxe, limitations are :

  • 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
  • you can parse some type declarations (import, class, typedef, etc.) with parseModule

Install

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

These are the main required 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

Some other optional files :

  • hscript.Async : converts Expr into asynchronous version
  • hscript.Bytes : Expr serializer/unserializer
  • hscript.Checker : type checking and completion for hscript Expr
  • hscript.Macro : convert Haxe macro into hscript Expr
  • hscript.Printer : convert hscript Expr to String
  • hscript.Tools : utility functions (map/iter)
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].