Simple Script engine for C++
ZetScript is a programming language with an API that allows you to interop easily with your C++ code.
A Hello World sample is shown below,
#include "zetscript.h" int main(){ zetscript::ScriptEngine script_engine; script_engine.compileAndRun("Console::outln(\"Hello World from ZetScript!\")"); return 0; }
Features:
Tested compilers:
Zetscript language syntax is similar to Javascript
var object={ i: 10, f: 0.5, s: "a string", b: true, v: [1,0.5, "a string", true] }; // for-in loop for(var k,v in object){ Console::outln("key:{0} value: {1}",k,v); } // A class example class Test{ function1(a){ this.data1 =a; Console::outln("calling from Test. Data1: {0}",this.data1); } }; // post declaration Test::function2 function Test::function2(){ this.data2="a string"; } // TestExtended extends from Test class TestExtended extends Test{ function1(a){ super(2); // it calls Test::function1(2) this.data1+=5; Console::outln("calling from TestExtended. Data1: {0}",this.data1); } function3(){ // this.data3=6; Console::outln("data3 is "+this.data3); } }; // instances TestExtended class var t=new TestExtended();
ZetScript API interop with C++ by registering C functions or binding script functions.
To call C++ code from ZetScript is done by defining and registering a C function.
#include "zetscript.h" // C function to register void sayHelloWorld(zetscript::ScriptEngine *_script_engine){ printf("Hello world\n"); } int main(){ zetscript::ScriptEngine script_engine; // Registers 'sayHelloWorld' function script_engine.registerFunction("sayHelloWorld",sayHelloWorld); // Compiles and run a script where it calls 'sayHelloWorld' function script_engine.compileAndRun( "sayHelloWorld();" ); return 0; }
To call ZetScript from C++ code is done by binding script function after compile script function.
To expose C++ type to ZetScript is done by registering C++ type. To expose members functions or properties is done by by defining and registering a C function.
You can download source code from: