Lua: The Modular Language You Already Know
Published: 2024-06-10 06:02:01

## What is Lua?

Lua is a programming language that was invented in Brazil in 1993. It is a scripting language known for it's speed and efficiency. It was created as an in-house solution for a data-entry application. It was needed to prepare input data files for simulations regularly throughout the day.

Lua was originally a slow language and was request to update it's demands on the computer. It was originally loved despite all of it's current issues. So the developers decided to get to work on improving Lua for the computer. It was a very unique language that looked alot like SQL and at the time it was still very nice and easy to work with for that time.

```lua
-- Early Lua Code To Create a Table(i.e. Javascript Array)
  CREATETABLE
   PUSHNUMBER 1                 # index
   PUSHNUMBER 30                # value
   SETTABLE
   PUSHNUMBER 2                 # index
   PUSHNUMBER 40                # value
   SETTABLE
   PUSHNUMBER 3                 # index
   PUSHNUMBER 50                # value
   SETTABLE
```

## Lua Version 2

Lua 2 came out in February of 1995. This is when they made several quality of life changes. This is when Lua made a bold decision that isn't made a lot of other programming languages. They decided that if the team wanted to implement feaetures it was exceptable to have some incompatibilites with earlier versions of Lua. This can be a very bad thing for production code because it can really stop large portions of a companies data entry. Lua decided to go this route while also understanding the risks that they would be taking by doing this.

They had an idea to also also proviide some tools to convert old code to new versions. This allowed companies to use the tools to update their code and they were able to use the new features.

Lua also did really well by understanding where they stood in the programming language landscape.

> Since the beginning, we designed Lua as an extension language, in the sense that C programs can register their own functions to be called from Lua transparently. In this way, it is easy to extend Lua with domain-specific primitives, so that the end user uses a language tailored to her needs.

This is a quote from Lua's [website](https://www.lua.org/history.html#10). They state here that Lua is so that when writing C programs the programmer has access to write functions how they see fit. With this mentatality they decided to also implement a feature in 2.1 called fallbacks which is used when a Lua function doesn't know what to do it falls back onto a predefined function.

```lua
function Index (a,i)
     if i == "parent" then       - to avoid loop
       return nil
     end
     local p = a.parent
     if type(p) == "table" then
       return p[i]               - may trigger Index again
     else
       return nil
     end
   end

   setfallback("index", Index)

-- This code follows a chain of "parents" upwards, until a table has the required field or
-- the chain ends. With the "index" fallback set as above, the code below prints red even
-- though b does not have a color field:

   a=Window{x=100, y=200, color="red"}
   b=Window{x=300, y=400, parent=a}
   print(b.color)
```

## Lua's exposure

Lua laid low on the scene until 1997 when they got some publicity with articles such as 'Lua in Software: Practice & Experience', Getting posted in academic journals got the attention of some developers such as Bret Mogilefsky who wanted to replace the arcaic scripting language that he used in some of the LucasArts Games (yes, Star Wars company).

## What is a scripting language and why do you need one.

A scripting language is any programming language that is used to automate manual processes. The most common example that is used by all programmers today is Bash, Zsh, etc. All shell languages that can be used to automate processes that you do everyday. Most higher-level languages are known as scripting languages. Such as Javascript, Lua, Python, Bash, and Ruby.

These are called higher-level because they deal with more abstraction and are typically used to extend functionality of other functions that may have to do with system architecture. They
are also typically interpreted and used to be short and sweet. This is not always the case like Javascript which has had so much attention that it has been used to create __EVERYTHING!!!__

## Lua Extension

So Lua was very useful to be able to extend C and is used everyday even now with companies like Roblox allowing it's users to code games in Lua because of how well it can be implemented with C. C and Lua can actually run in the same file in the current version of Lua.

## Data Structures

Lua has one Data Structure. A table (that starts at the one index :s ). It is essentially a glorified array, but has some extra features. You can give indexes key value pairs and access them using that key value pair. There are also only two value types. A global and a local variable. A global variable is a variable that is able to be used by all of the other functions and tables in a project. A local variable is only available to the values inside of a code block.

```lua
   globalVar = {
        'Lua can has integers, strings, functions, and booleans!',
        4,
        true,
       'this is how to create a function',
        function()
           -- this is a comment
           -- print is basically console log
           print('imma func')
       end
    }
   patrickTired = true
   -- i will put this local var inside of a if statement and it is not availible outside
   if patrickTired == true then
   local localVar = 'this val can\'t be acccesed outside of this block'
   elseif patrickTired == false then
       print('let\'s have some more fun')
    -- you need end to end the if else chain
   end
```
This is a small code example to get the basic idea. If you want  a bit of a bigger file to play around yourself or ever want to learn about a new language you can use [LearnXinYMinutes](https://learnxinyminutes.com/) which is a great starting point to learn any language you desire.
LearnXinYMinutes for [Lua](https://learnxinyminutes.com/docs/lua/)

## In Conclusion

Lua is a great language that can be used to embed functionality in to many programs. It is fast and easy to learn. I Learned it in a couple ours(Because NeoVim). It is not the most useful language in the world but i still think that it is worth it even if it is just to be able to put on you resume.