Luart
Comprehensive Windows framework to develop in Lua

Tutorial : Learn programming with Luart (Part 1)

By Samir Tine, published on December 2021

With Luart, you can easily program in Windows using the Lua language. Still, you'll need to know coding in Lua! During this tutorial, you will be able to learn the fundamentals of programming with Lua, which will then enable you to then become familiar with LuaRT and start creating your own applications. Let's go !

Let's start with some prehistory...

Programming has always been about solving problems. As the Cro-Magnon man used stones to make fire, modern man uses a computer to facilitate his daily activities. Using tools by humans always follows the same pattern, the same sequence of steps to achieve (or not achieve, if there is an error!) the desired result.

To cook meat, prehistoric people prepared leaves and twigs first, then rubbed flints together to make a spark and blew on them to make embers. Today, to write an email, the modern man starts by turning on his computer, logging into his email account, and then writes it, before sending it.

Here are two diagrams, one representing cooking meat and one representing sending an email (can we speak of humanity's progress here?). Essentially, these are instruction sequences since the instructions must be followed one after another. That's what programming is all about!

A program consists of a series of instructions executed one after another.

A modern approach to complexity

Using voice assistants nowadays makes it easier to program your smartphone or computer to do certain tasks: "send Lisa an email" or "wake me up at 6 am tomorrow". It's sort of like programming, but you cannot express the inventiveness that makes you a man (or woman).

With the voice assistant, you can only program what was previously defined as being programmable (send an email, set an alarm clock, etc.).

Programming languages, such as Lua, provide more flexibility by allowing you to define instruction sequences in much more precise manners, while also being particularly simple. Just as voice assistants translate your words into actions, Lua translates your instructions into actions our computers can understand (don't tell me you thought you were really talking with your phone!). To accomplish this, LuaRT uses an interpreter.

An interpreter translates your instructions into actions that are executed by the computer.
Luart uses an interpreter named luart.exe. In its hands, your instructions will be executed one after another. Wait a minute, how am I going to give it my instructions ?

A voice assistant speaks with the voice of his master (hence modern slavery), a Luart interpreter will use a text file in which you will specify the instructions he must perform.

You can run your program with LuaRT by entering luart.exe myprogram.lua in a console window or by double-clicking the file in Windows Explorer.

Your first program

Come on, you will finally be able to write your first program !

"Hey Google, how do we write a program" ? You guess right, not like that ! Ok ok I will not use the voice assistant example in my programming tutorials anymore...

The first step involves creating a text file (for example, using the famous Notepad) and saving it as "myprogram.lua". Write the following instructions in this file :


print("Listen !")
sys.beep()

Go to the directory where you saved your program, then run it with in a console window with the following command :


luart.exe myprogram.lua 
            

You should see a message "Listen !" on the screen and then hear a beep. Great, your program was executed sequentially as we requested !

The first line: print ("Listen !")

In the first line of the program, the computer is asked to perform an action called print on the screen. Why are there parentheses around the command? Through parentheses, the interpreter is instructed not to print just anything, but rather to print "Listen!".

Just like when you go shopping, you must open your wallet in order to pay. You can then choose whichever method of payment you want, cash or credit card. Well, in Lua it is the same: opening parentheses tells what the action will be working with.

With the same analogy, Lua calls the means of payments arguments. The action with parentheses will then be executed depending on these arguments. The action in question is actually called a function in Lua (reminds us of our math lessons without a doubt !).

A function allows to perform an action based on arguments enclosed in parentheses.
In summary, your program's first line instructs the interpreter:

Instructions

Interpretation

printHere is the name of a function
(Execute the function with the following arguments
"Listen !"The first argument is a quoted sentence
)No more arguments, execute the function now !

Programming languages have a very specific grammar with words and expressions that are different from our own language, and it is for this reason that we need to learn them in order to program. The language we are describing is Lua. Regardless of what language is used, it cannot be translated to other languages like javascript, C, or assembler, whose syntax has to be learned before using them. Hopefully, the concepts remain the same, such as the concept of function.

One can ask at this point: where is the function print ? Indeed your program does not indicate to the interpreter where the function is, to be able to run it. It is actually a basic function available throughout your program (it is called a library function part of a whole arsenal of functions already integrated into the interpreter and therefore immediately available to make your life easier. This arsenal is actually called the runtime Library). This library does not store books but functions ready to be executed by your programs.

Luart provides a much larger library of functions than the standard version of Lua, allowing users to do many more actions in your programs without additional effort.

Second line: sys.beep()

Good! The first line of the program covered so many concepts! I assure you, the few concepts you have just learned will help you understand the rest better. According to the first instruction's syntax, and by analogy, we can describe this second line as follows:

Instructions

Interpretation

sys.beepHere is the name of a function
(Execute the function with the following arguments
)No arguments, execute the function now !

I think this is rather clear if you have followed along. Not quite yet, though.

Perhaps you are wondering why there is a period in the middle of a function name. The period is usually used to separate two sentences in human language. Good point! In Lua language, a dot is also used to separate instructions, not sentences. What are sys and beep in this case? Since it isfollowed by parentheses, beep is the function that the interpreter will execute. But then what is it?

The dot in Lua language is used to separate, not sentences but instructions. In this case, you will tell me, what are sys and beep?

Let's answer this question! Change the second line in your program:

print("Listen !")
beep()       

Run your program by typing luart.exe myprogram.lua in the console window ... and a disaster occurs! Rather than hearing a beep, you just saw a strange error message instead!


myprogram.lua:2: attempt to call a nil value (global 'beep')

Let's see. The message indicates that the interpreter tried to execute the beep function on line 2 but ended up executing a null value instead! That is to say, the interpreter found nothing but absolute nothing, the interstellar void !

Do you remember the print function? It has been executed, however, because the interpreter stores it in the overall library space (a sort of central aisle), which makes it accessible anywhere in your program. In contrast, there is a beep function in the interpreter, but it is stored in a separate library shelf.

Yes, it is in the sys aisle, as you probably guessed. Without this primordial indication, the interpreter couldn't find the beep function.

The dot in sys.beep tells you that the beep function is located in the sys aisle of the Luart library.

The point, in Lua, is used to classify elements, for example functions, in the standard library.

Keeping all this in mind, here is how the interpreter interprets your first program:

Instructions

Interpretation

print(I order you to execute that function from the central aisle of the library, using the following arguments
"Listen !"The first argument is a quoted sentence
)No more arguments, execute the function now !
sys.Here is the name of a shelf in the library
beep(Here is the name of a function from this shelf that I order you to execute, using the following arguments
)No arguments, execute the function now !

A punctuation symbol, which follows a word in Lua, offers information to the interpreter: an open parenthesis indicates a function to be executed. The dot indicates that the word preceding the dot is a shelf, a storage area.

In conclusion

What a wide range of concepts this tutorial covers ! If you have followed along correctly, you have already grasped some important Lua concepts:

After reading the first part of this tutorial, it is time to take a short break and go back to your real life, to enjoy your loved ones for example, before diving into the second part with curiosity !