embeddable script suggestions?

Started by
3 comments, last by thre3dee 17 years, 6 months ago
Hi, I've been toying with the idea of designing a scripting language and developing a virtual machine that uses a bit of template meta programming to help bind a Cpp application (including classes etc) to it. The three main constraints for the scripting language and interpreter I wanted were: * Type strict (theoretically if done right should go faster than dynamic typed, easier to develop and optimize etc) * API designed for use in Cpp applications (games especially), which separates scripts for use in event systems and other such things * Provide an easy binding system because I dislike having boost for libraries such as luabind (because its just so damn bulky and annoying to carry around) and also because only one or two binding libraries allow you to bind to native functions with ease not the int func (vm *) kind which is annoying especially for binding existing, unchangable code. In this respect I have named my scripting language e for embeddable. While its designed to be a lot like Cpp there are a few major differences. Oh and what I have designed so far is not completed. I've started documentation (a Lua like documentation style), but have to change most of it to suite the new syntax style as I recently did a big overhaul. I'll post a few examples of the code so you can see the syntax. One of the main difference is that variable declarations are mixed up to be "identifier1[, identifier2...n]:type" instead of "type identifier1[, identifier2...n]" like in Cpp. Scripts will be run via an entry point as well. a few syntax examples
math:group -- this is a namespace in Cpp
{
	vector3:def -- this defines a user-type (class/struct in Cpp)
	{
	public:
		-- this declares three member variables
		-- real is 32-bit float, for double use real8
		x, y, z:real;

		-*
			declares a function that returns the length of the vector
			'nil' is the same as void in Cpp
		-*
		length:real (nil)
		{
			-*
				the sqrt function in the math group is accessed by a period symbol '.'
				self member variables and functions require the preceding period symbol '.'
			*-
			return math.sqrt (.x * .x + .y * .y + .z * .z);
		}

		-*
			declares a function that normalizes the vector
			a function that returns nothing must specify 'nil'
			nil is not actually required in a parameter list if there
			are no parameters
		-*
		normalise:nil ()
		{
			len:real = .length ();
			if (len == 0.0) len = 1.0;
			.x /= len;
			.y /= len;
			.z /= len;
		}
	}
}

main:int (msg:string)
{
	-* declares a reference (counted) to a string (which is new'd (gc keyword)).
	not having the reference symbol ^ will only copy the string and the new'd copy will be garbage collected
	*-
	hello:^string = gc:string ("Hello");
	console.print (hello + ", world! " + "OMG" + 1337); -* primitives are converted and added (or whatever other arithmetic) based on the l-value's type automatically *-
	-- output is "Hello, world! OMG1337"

	-- arrays are declared like the following
	-- variable length arrays must be declared as a reference and be gc'd like Cpp
	names:^ string [] = gc:string[10];
	names_fixed:string[10] = { "My Name!" };
	return false;
}
the api might look like this
eVirtualMachine * evm = eCreateMachine (eAPI_VERSION);
eScript * script = 0;
if (evm->compileFile ("examples.e", &script) == eOk) // script pointer address can be NULL
{
	// script can be retrieved like this as well
	// eScript * script = evm->getScript ("examples.e");

	// parameters in stack will be treated using the script function decl
	script->call ("main", "Hello, world!"); // main:int (msg:string)
}





the native types are:
char
short
int
byte
word
dword
real
real8
string
A few libraries will be implemented with the following: console, math, system, io, string What do you think? Any suggestions? There's a fair amount that can be assumed to be exactly like Cpp like logic etc [Edited by - thre3dee on September 21, 2006 2:40:20 AM]
Advertisement
you should use source tags see faq
i didn't want the lang's to make some of my tokens light up a wrong colour so i just made my own (painstakingly)
here's an update. I've figured the slightly unique feature of my scripting system.

Because its going to be designed to quickly embed in games or event driven applications, that's exactly what I intend to make mine for while keeping all the other functionality.

-- defines the enter frame event for the game:actor class-- but only if its an NPC characterevent onEnterFrame for game:actor (real deltaT) : on (this.type == "npc"){	this.move (this.direction, deltaT * 2)	if (this.health < 20) this.pathTo (game:instance.findNearestObject ("health_pickup"))}-- defines the enter frame event for the game:actor class-- but only if its an PLAYER characterevent onEnterFrame for game:actor (real deltaT) : on (this.type == "player"){	this.move (this.direction, deltaT * 2)	if (this.health < 20) this.flash (game:instance.hud.getElement ("healthbar"))}-- defines an implementation of the global onEnterFrame (deltaTime) event-- this implementation will only trigger on odd framesevent onEnterFrame (real deltaTime) : on (isOddFrame == true), priority (0){	-- code	int count = game:instance.getActorCount ()	for (int i = 0; i < count; ++i)	{		game:actor ^ a = game:instance.getActor (i)		trigger a.onEnterFrame (deltaTime)	}}


and the application code would be as follows
// C++ codeevm->getGlobal ()->linkVariable ("isOddFrame", &isFrameOdd);isFrameOdd = frame % 2;evm->triggerEvent ("onEnterFrame", deltaT);	// fires off every global onEnterFrame event implemention in order of priorityevm->triggerEvent ("onEnterFrame"); // returns invalid parameter list error because there are no onEnterFrame events that take no parameters
And the good thing about the event system is that they are completely unattached from the internal C++ classes and environment.

This topic is closed to new replies.

Advertisement