Simple Script engine for C++

General overview

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:

  • Virtual Machine
  • Language syntax close to Javascript
  • Dynamic Garbage collector
  • Straightforward way to expose C++ functions and types
  • Operator overriding through metamethods

Tested compilers:

  • MSVC++ 32/64 bits MSVC 2015/2017 or build tools v141
  • Linux/MinGW 32/64 bits, gnu compiler 4.8 or above

Language overview

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();

API overview

ZetScript API interop with C++ by registering C functions or binding script functions.

Call C++ code from ZetScript

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;
}

Call ZetScript from c++

To call ZetScript from C++ code is done by binding script function after compile script function.


							

Exposing C++ types to ZetScript

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.


							




Downloads

You can download source code from: