On Programming

A discussion of programming strategies and results

Lua: The One True Scripting Language

| Comments

If you’ve read me previous post An Argument Against the Cascade then you know I don’t like scripting languages. Scripting languages are inherently slow and has caused the market to flood with inept programmers who can’t handle pointers or manage their own memeory. That being said, there are times when scripting lanauges are important.

For example: I plan to add an in-game console window to my current project (like hitting ~ in source games). If I want to allow for any sort of complex behavior then I will need a scripting language. The route that so many people seem to take is writing their own scripting language. This is a plague that has gone on for far too long. As much as I hate scripting languages, I hate having to know the syntax to 50 scripting languages more. Instead, I plan to use Lua.

Don’t get me wrong, I have my issues with Lua. In Lua arrays tend to be indexed starting at 1 instead of 0. Classes don’t really exist, but rather are tables inheriting from meta-tables. And finally, worst of all, its not Lisp! So why then should we be using Lua? Its simple really:

  1. Its under a BSD style license allowing it to be integrated into anything with almost no restrictions
  2. Intgrating it with C/C++ is a breeze through the use of oolua or luabind.
  3. If it can integrate with C/C++ it can integrate with almost anything else because most languages have bindings to C/C++
  4. Lua is tiny, allowing it to be used almost anywhere with no significant cost

To put in perspective how tiny Lua is, heres an excerpt from their website:

Adding Lua to an application does not bloat it. The tarball for Lua
5.2.1, which contains source code and documentation, takes 245K
compressed and 960K uncompressed. The source contains around 20000
lines of C. Under Linux, the Lua interpreter built with all standard
Lua libraries takes 182K and the Lua library takes 243K.

A perfect real-life example of how this change can help is through CMake and Premake. CMake is a build system that has its own scripting language. It is often criticized for its syntax. Here is a mailing list therad to show just how ugly the syntax is.

Premake is instead built on top of lua. This allows me to use any knowledge I already have about Lua (syntax, how to write functions, how to concatenate strings, variable types, …) and apply it to my build system. This allows you to create a make file using a clean syntax without having to learn yet-another-language. Right now premake has not caught up to cmake in terms of features (for example, it is missing CPack functionality), but I am willing to put up with that for a much cleaner syntax and more direct control. I’ve even been toying with the idea of implementing “prepack” to make up for that shortcoming next time I have some time.

One may wonder why I don’t simply advocate using Lisp as the one true language since it is the purest form of elegance and the best scripting language ever made. The simple answer is that it is not embeddedable. There are few projects trying to get it embeddable and those projects either have far too many issues or are under a virus license like the GPL.