Introduction to Lua programming language
Lua programming language is written in C and started as a project in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes. it is a robust, fast, lightweight, and embeddable scripting language that supports multiple programming paradigms such as procedural, object-oriented, and functional programming. Mainly, Lua is not used as a standalone scripting language that can integrate into other programs written in mainly C, C++, and .NET/C# because of its speed and smaller footprint with only 21 keywords. Lua is used as a plugin language for many applications such as Redis, nvim, and many games.
Some of the Usecases of Lua are :
- Lua is a popular component in game engine development. Some game engines allow users to add additional functionality by making plugins written in Lua
- Some popular tools such as nvim allow use to write configurations in Lua programming language.
- In Redis, Lua behaves like stored procedures and will enable us to run our application logic inside Redis itself.
- For installing Lua in Linux/ubuntu
sudo apt install lua5.4:i386
- For installing Lua in MacOS
brew update && brew install lua
How Lua is structured and its works
Lua has two main components:
- Lua interpreter
- Lua virtual machine (VM)
Lua interpreter is written in ANSI C, making its interpreter highly efficient and portable to run on multiple devices. Lua works differently from other interpreter languages such as python instead of being directly interpreted from a Lua file, the Lua interpreter first compiles the Lua file into bytecode. This compilation is done during runtime and Then loaded into Lua virtual machine that will run this compiled Lua bytecode.
Advantages of Lua programming language
- Compact and Simple syntax: With a simple syntax structure containing only 21 keywords and a single data structure called a table, it's a highly compact and simple programming language with few concepts to learn.
- Highly extendable: Lua doesn't have many standard libraries, which allows us to customize it according to our needs.
- Free to use: Lua is open source under the MIT License. which allows it to be free to use and to have free software distributed in source code.
- Cross-platform support: As Lua is written in "clean C", it allows you to run Lua virtually anywhere.
- Easy to embed: Lua can be embedded in many programming languages such as C/C++, Java, Fortran, Ruby, OPL (EPOC), C#, and tools like Redis, nvim, and so on.
Drawbacks of Lua
- Debugging Support: Lua has Limited error handling support which makes it difficult to identify the exact errors in the Lua program.
- Variables Scope: In Lua, all variables are created as global variables which can lead to bugs in Lua code.
- Small Community: Lua being simple and compact has a small community, as it is mostly used as a plugin language rather than the main programming language.
First Lua Program
All Lua files will have the extension .lua and will be invoked by the Lua interpreter by passing the file name as a parameter or we can directly write in the Lua shell, To write the program in the Lua shell, open the Lua shell by typing "Lua" command.
print("Hello Geeks", _VERSION);
Now, let us write our first Lua program, to do different mathematical calculations on given values and print some values. To run this Lua script we will write lua name_of_file.lua as shown below.
function sum(a, b) return a + b end function diff(a, b) return a - b end function printGeeks(a, b) return "Hi, Geeks!" end local a = 1 local b = 2 local c = sum(a, b) print(c) local d = diff(a, b) print(d) local e = printGeeks(a, b) print(e)