DEV Community

vinayak
vinayak

Posted on • Originally published at thebackendblog.com on

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. Mostly, 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.

luaa.gif

Some of the Usecases of Lua are :

  • Lua is a popular component in game engine development. Some game engines allow users to add additional functionally by making plugins written in Lua
  • Some popular tools such as nvim allow use to write configurations in Lua programming language.
  • In Redis, Lua works by behaving like stored procedures and allows us to run our application logic inside Redis itself.

Installing Lua

  • For installing Lua in Linux/ubuntu
sudo apt install lua5.4:i386

Enter fullscreen mode Exit fullscreen mode

Screenshotfrom20220819004134.png

To run the Lua Shell, open the CMD/power shell on Windows and terminal on mac and Linux, write Lua, and press enter. A Lua Prompt comprising of greater-than symbols > will appear, as shown below.

Screenshotfrom20220819004850.png

For checking the version we can use _VERSION and for quitting the Lua shell, we can use os.exit()

Screenshotfrom20220819005153.png

  • For installing Lua in MacOS
brew update && brew install lua

Enter fullscreen mode Exit fullscreen mode

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 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);

Enter fullscreen mode Exit fullscreen mode

Screenshotfrom20220820170522.png

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)

Enter fullscreen mode Exit fullscreen mode

Screenshotfrom20220820183154.png

Conclusion

Lua has limitless potential and is a very Compact and powerful scripting language. That is used to add functionality to any programming language or tools on multiple platforms according to needs and use case.

Top comments (0)