Sticky note
A program consists of a series of instructions executed one after another.
Basic notions of Lua programming
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 !
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!
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.
Luart uses an interpreter namedluart.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.
Come on, you will finally be able to write your first program !
"Hey Google, how do we write a program" ?
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 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 the word inside the double quotes"Listen!"
.
Instructions | Interpretation |
---|---|
print | Here 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 this is the reason why 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.
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.beep | Here 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 is followed by parentheses, beep
is the function that the interpreter will execute. But then what is sys
?
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 nil 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 Lua we call it a global function). 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 the interpreter that the beep
function is located in the sys
aisle of the Luart runtime 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 |
sys.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.
What a wide range of concepts this tutorial covers ! If you have followed along correctly, you have already grasped some important Lua concepts:
nil
in Luaprint
function is used to display text on the screensys.beep
function is used to emit a beepAfter 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 !