RSS
 

LUA syntax

23 Sep

I love Lua for scripting… It’s easy to integrate with any C/C++ framework, powerful, fast, takes little memory, good debugging hooks, memory control, and (best of it all) it has coroutines, which is something that I utterly love in game development.

lua

But this is not an article praising Lua… To the contrary, it’s a rant about the stuff that annoys me in it and that I wish someone would fix… I know most of this could be fixed by me, since Lua is Open Source, but to be honest most of it would be a huge endeavor in the guts of Lua itself.

  1. Why 1-based arrays? Every language I know of has 0-based arrays (that is, arrays that start at position 0), but Lua had to be different… This is not a big problem per se, but the amount of times you do it wrong because you’re shifting from C/C++ to Lua is huge… Probably someone that sticks to Lua can do it without messing up a single time, but I’m an indy programmer!
  2. Why THEN and END? This is my main annoyance with Lua… All IFs in Lua have to have the keyword THEN after the condition, and END up when the syntax block ends… I understand being mandatory an instruction block (i.e. not allowing for a single instruction), since that leads to some ambiguities and tricky to spot bugs sometimes, but couldn’t they have used ‘{‘ and ‘}’, or even ‘[‘ and ‘]’, or something else but a clunky keyword that makes your code seem ugly? On the same note, the FOR and WHILE loops also require the DO keyword, which also sucks… is it that complicated to make a parser that doesn’t need those things?
  3. Use of doubles for numbers. This one I understand… numbers in Lua are expressed as doubles (which allows them to be fractional and yet provide a good range of integer numbers without loss of precision). But when you’re making games, you don’t want to keep switching between single and double precision on the FPU, and Lua kind of forces it (since you’re probably using floats all the time for everything else, like meshes). Adding an integer type and going for floating point numbers would help tremendously here (although I believe it wouldn’t be such a simple task as I’m stating, since the typeless nature of Lua makes it complicated, but I wouldn’t mind having some methods to work with floating point numbers, and others for integers for this purpose). The way I have it now, I do a compilation of Lua with numbers as floats for my game projects, and cross my fingers so that the precision doesn’t byte me in the future.
    What I do is when I want to treat a number as an integer (in comparisons and things like that), I round to the nearest integer… Hopefully that’s enough to stop some problems (which might be tricky to debug, since 3.9999999 != 4)
  4. Lack of types. This one isn’t really Lua’s fault (since most languages don’t do this), I would just really really really appreciate if I could have 2, 3 and 4 dimension vectors treated as internal types, so that I could use vector math in Lua (without the performance hit I get when I use arrays as vectors)
  5. Lack of pre-processor. Again, not Lua’s fault, but it would be nice to have a preprocessor in Lua’s parser… I can run stuff through the C preprocessor and feed that result to Lua, but that screws up debugging a bit, which is too bad…
  6. Lack of a class based system. I know I can use meta-methods and tables to mimic a class in Lua, but that’s not the same. Imagine that I return an object from C code. That implies that I “copy” the data from the C++ object to a Lua table, and push also some functions (C/C++ and Lua). This sounds terribly inefficient. It would be better to be able to specify some classes (with the methods associated to them), and just copy the data on top of the table. You might be able to do this with some trickery, but I haven’t found the trick yet, I admit. This one isn’t very serious, since Lua never pretended to be an OO language, and I can live just fine with passing some light user data values back and forth between Lua and C/C++.

 

I’ve looked into several other scripting languages (mainly Javascript (through the V8 engine) and Python), and they have more serious handicaps for my taste than Lua, and that’s why I keep using Lua.

Some of these seem pretty easy to fix for the developers (or someone that really takes the time to delve into the Lua source-code). Of course, changing these, will it still be Lua? I like to think that it would some sort of Lua+, different yet better. Maybe one of these days I’ll take the time to fix 1, 2 and 4 (well, 1 and 2, 4 might be too hard). If anybody knows someone that’s actually done some of this, feel free to give me a poke! Smile

 
 

Leave a Reply

 
*

 
  1. Manabu

    December 14, 2011 at 6:05 pm

    On your points 1 and 3, take a look on luajit’s FFI, that permits you use C structs in Lua.

    If you don’t want/can’t use luajit’s FFI, and don’t mind losing the doubles, you can compile Lua with the LNUM patch to have both floats and integers (and complex numbers): http://luaforge.net/projects/lnum/

    As for point 2, I love the “then” and “do” in Lua. It is much more visible than the “:” in python, and makes reading the code easier. Even one-liners, if you have good syntax highlight. It also avoids the endless brackets positioning wars in languages with c-like syntax.

    I don’t like much the “end” keyword, and would rather have the blocks delimited by indentation, like in python. It is clean and enforces good practices. It would also help to limit one-liners to an reasonable size, like in python.

    But it is all taste, and nothing that an LPEG pre-processor can’t solve if you are really, really bothered with it.

     
    • Covenant

      January 4, 2012 at 1:49 pm

      Hi Manabu,

      I’ve had the idea that luajit was just a way to compile lua, instead of interpreting… Must have understood it wrong when I was looking at it a long time ago… Need to get my facts straight there…
      FFI seems a very nice thing indeed, although I don’t see how that fixes point 1 and 3.

      The other “syntax” points, I agree that it’s a matter of taste… Identation based blocks of code seems like a terrible thing for me, seems too error-prone, since the lack of a space/tab can throw the whole script off, and it might be complicated to debug, specially in an embeded language…

      But yes, you’re right, it’s taste… 🙂
      I’m not too bothered (it just annoys, doesn’t create more bugs) with it when I spend 2 or 3 days straight working with Lua, just the switching between C++ and Lua that throws me off… 🙂

      Thanks for pointing luajit as a possible solution for my main problems (points 1, 3 and 5), from a bug-free development standpoint.