Thought Leadership

Ah! Lua

By Colin Walls

My Portuguese is limited. Well, non-existent actually. Last time I was in Portugal, finding myself in restaurants where no English was spoken, it was a matter of selecting things rather randomly from the menu or pointing at other people’s plates. When I first saw the name Lua, it made me think of the Hawaiian greeting “Aloha!”, but then I found out that its meaning was “moon”.

But really, it is a scripting language …

As I have written about before, I have always been interested in the wide range of available programming languages and have a couple of shelves full of books on the topic. Of course, my focus is particularly on languages that find a place in embedded systems. I was, therefore, intrigued when Andrew Creque contacted me and told me about a language that I had never come across before: Lua.

I have a profound ignorance of many things in the world, but my lack of awareness of Lua is particularly surprising, as it has been around for a couple of decades!

Lua is executed using a virtual machine [like Java] which interprets bytecodes. It has a very straightforward syntax that has much of the simplicity of C, but is more forgiving [IMHO]. This is combined with great flexibility in data representation and automated memory management. If I have any criticism, it might be that the dynamic typing is just a bit too flexible and might lead to very sloppy coding.

Lua has been used in many applications, but why might it be useful to the embedded developer? Several reasons:

  • the Lua runtime is quite compact
  • execution is fast
  • the runtime code is very portable
  • it is designed to integrate with code written in other languages – typically C/C++

In addition, embedded developers are quite a conservative bunch, only adopting a technology that is well proven. Lua has been shown to be robust and has been applied very widely for both embedded and non-embedded [like games design – heard of Angry Birds?] applications. The open source licensing is also an attraction.

As Lua is very easy to get running and get to grips with – I got it installed on my Windows PC and was busy programming within the hour. So, apart from being embedded in systems that need a scripting language, it seems to me that fast prototyping of algorithms is another application of the language. To give you a feel for the language, here is some Lua code:

values = { 3, 6, 9, 2, 4, 8 }

 

local function pvalues ()
for i = 1, #values do
print(values[i])
end
end

 

local function bubble ()
swapflag = true
while swapflag do
swapflag = false
for i = 1, #values-1 do
if values[i] > values[i+1] then
temp = values[i]
values[i] = values[i+1]
values[i+1] = temp
swapflag = true
end
end
end
end

 

bubble()
pvalues()

 

If like me, you have never heard of Lua, you may be surprised to find that you already know how to use it. How come? It is the scripting language built into a number of software products and systems. An example is one of my favorite desktop applications – Adobe Lightroom – which uses Lua for plug-in implementation.

Are you using Lua [knowingly]? If so, I would be interested to hear how you are applying it by comment or email.

Comments

0 thoughts about “Ah! Lua
  • Hi,Colin Walls,I am a student from China,I have downloaded a arm-none-eabi-gnu toolchain for my cortex-m3 system few days ago,but the help-manual dosen’t have a chinese release,and I can’t find any from the internet also.It’s too hard for me to read them line by line,where can I get a helps? Apologize for my bad English.

Leave a Reply

This article first appeared on the Siemens Digital Industries Software blog at https://blogs.sw.siemens.com/embedded-software/2013/09/09/ah-lua/