Programming a runtime: Choosing a scripting language

There are a lot of dif­fer­ent types of run­times, but today we’re talk­ing about the type that acts as a self-​​contained plat­form, such as Java, or Flash.  The goal of such run­times is to sup­port write-​​once, run-​​anywhere pro­grams.  I got it into my head that I should make one.  We’re not going to be start­ing from scratch though, we’ll be using an exist­ing embed­d­a­ble script­ing lan­guage, and a cross-​​platform API for all things that are nor­mally plat­form spe­cific, such as open­ing win­dows, get­ting the time, and read­ing in input.  I have a few pos­si­ble script­ing lan­guages in mind, I could use Lua, AngelScript, or Squir­rel.  Each of these has var­i­ous pros and cons.  My present goal is to cre­ate a run­time that can be used to write 2D games entirely with a script­ing lan­guage, and have them run on any plat­form the run­time sup­ports with­out mod­i­fi­ca­tion of any kind.  This means I’ll need a script­ing lan­guage, and binds to cre­ate a win­dow, play sounds, dis­play sprites, and get user input.  This run­time will be entirely focused on games, so it may or may not be suit­able for nor­mal apps.

 

Lua

Lua is a small, light-​​weight script­ing lan­guage best known for its use in World of War­craft.  Lua has seen many other notable releases, and has quite com­plete doc­u­men­ta­tion, and a wide com­mu­nity that sup­ports it.  This is by far the most pop­u­lar choice on our list.  Lua has a syn­tax much like Basic.  There are no semi­colons, a line is ended with a car­riage return, there are also no brack­ets for begin­ning and end­ing code blocks, you end things like if state­ments, loops, and func­tions with an end state­ment.  Lua has noth­ing like classes out of the gate, but it’s quite sim­ple to cre­ate a way for a table to act like a class.

Sam­ple code: ( from http://​en​.wikipedia​.org/​w​i​k​i​/​L​u​a​_​(​p​r​o​g​r​a​m​m​i​n​g​_​l​a​n​g​u​age) )

function factorial(n)
	local x = 1
	for i = 2,n do
		x = x * i
	end
	return x
end

Pros:

  • Many peo­ple are famil­iar with Lua
  • User-​​friendly syntax
  • Type­less
  • Gen­er­ally fast

Cons:

  • No built-​​in sup­port for classes
  • Garbage col­lec­tion must either be man­aged by the run­time, or it may run at any time caus­ing slowdowns
  • User-​​friendly syn­tax is not nec­es­sar­ily programmer-​​friendly

Squir­rel

Squir­rel was cre­ated by a devel­oper who was work­ing on lim­it­ing some of Lua’s dis­ad­van­tages (such as garbage col­lec­tion caus­ing unpre­dictable slow­downs), at the time there wasn’t any way to con­trol Lua’s garbage col­lect­ing hab­bits, so he began by imple­ment­ing ref­er­ence count­ing instead of mark and sweep col­lec­tion.  At any rate he kept find­ing more and more that he’d like to change about Lua, and the results were a whole new lan­guage by the name of Squir­rel.  Squir­rel has a syn­tax most sim­i­lar to java-​​script, and is the first of our C-​​like lan­guages.  It has strict typ­ing like C, but sup­ports fea­tures like dynam­i­cally adding mem­bers to struc­tures, like java-​​script.  Squir­rel natively sup­ports fully fea­tured classes, includ­ing inheritance.

Sam­ple code: ( from http://​en​.wikipedia​.org/​w​i​k​i​/​S​q​u​i​r​r​e​l​_​(​p​r​o​g​r​a​m​m​i​n​g​_​l​a​n​g​u​age) )

function factorial(x)
{
	if (x == 0)
	{
		return 1;
	}
	else
	{
		return x * factorial(x-1);
	}
}

Pros:

  • Native sup­port for classes
  • Con­sis­tent performance
  • Sim­i­lar under­ly­ing struc­ture to Lua
  • More famil­iar to C pro­gram­mers and web developers
  • I feel there’s some strange affin­ity between myself and this lan­guage >.>

Cons:

  • Not AS fast as Lua
  • More “codey”-looking code
  • Smaller com­mu­nity

Angel Script

Finally at the fur­thest end of the spec­trum we have Angel Script.  Angel Script code can essen­tially be copied and pasted into a C++ file, and it will com­pile and run.  It is strongly typed, and designed to bind extremely closely with native code.  Of all the lan­guages I’m con­sid­er­ing Angel Script is the fastest when it comes to call­ing C++ func­tions from out­side the script, and I believe has com­pa­ra­ble speed to the oth­ers when it comes to run­ning the script itself.  I haven’t tested it, but wouldn’t be sur­prised if it was a touch faster, just from using stan­dard C data types.

Sam­ple code: ( I mod­i­fied the squir­rel exam­ple >.> )

int factorial(int x)
{
	if (x == 0)
	{
		return (1);
	}
	else
	{
		return (x * factorial(x-1));
	}
}

Pros:

  •  EXTREMELY sim­i­lar to C++ in syntax
  • Fast native func­tion calling
  • Con­sis­tent performance
  • Most famil­iar to C/​C++ programmers

Cons:

  • Very strict syn­tax (like C++)
  • Very “codey”
  • Smaller com­mu­nity

 

I’m think­ing I’ll make an inter­face for run­ning scripts, and have my code inter­act with that instead of directly to the script­ing lan­guages.  This way I can change script­ing lan­guages eas­ily at a later date, or even include mul­ti­ple lan­guages in the run­time.  I may also exper­i­ment with using Mono instead of a sin­gle lan­guage.  Mono is a VM that runs its own byte-​​code, but has many lan­guages that can be com­piled to run on it.  It’s quite a bit big­ger, and more com­plex to use than any of my other choices, but the pay­off is sub­stan­tial in terms of both raw speed, and ver­sa­til­ity.  Mono has a sub­stan­tial list of pro­gram­ming lan­guages that it will work with right now as well.  The main rea­son­ing in not choos­ing mono is that embed­ding it, and keep­ing it in-​​line is quite a bit more com­plex, I’d also like to have tight con­trol over any mark and sweep garbage col­lec­tion, as it could prove to be a source of stut­ter­ing, and I’m not quite sure that’s pos­si­ble in Mono.

Leave a Reply

Your email address will not be published.

Connect with Facebook