Lua is a high-level, multi-paradigm[^1] scripting language that compiles to C. It uses very few reserved keywords. The design philosophy that went into Lua's construction was to provide **one** way to skin any given cat. Introducing the smallest number of constructs feasible to perform a huge variety of tasks. This is a stark contrast to most other languages I'm familiar with (see: [[JavaScript]]'s Arrays, Sets, and Maps or [[Python]]'s Lists, Tuples, Maps, Sets, etc).
The sum total of all reserved keywords in Lua:
```plaintext
and break do else else if end false
for function goto if in local nil not
or repeat return then true until while
```
My favorite quote from the wikipedia article for Lua:
> [!cite] Providing a small set of general features that can be extended to fit different problem types.
It doesn't _directly_ support classes, inheritance, namespaces, and many other commonplace features, but they are easy to implement using Lua's **tables**.
Lua is used for:
- Game Development
- Love2D - the engine behind Balatro
- Playdate - the little yellow crank-laden game console that could
- Roblox & whatnot - apparently Lua is used for creating games inside of games
# Lua Syntax & Oddities
Presented in Lua.
```lua
-- Comments start with double-hyphens
global_var = "Variables are global by default..."
local local_var = "..unless they're prefixed with 'local'."
-- String concatenation
concatenated = global_var .. local_var
-- Control flow
my_var = 1
if my_var > 0 then
print("my_var is positive")
elseif my_var == 0 then
print("my_var is neither positive nor negative)
else
print("my_var is negative")
end
-- Loops
while my_var > 0 do
my_var = my_var - 1
end
repeat
my_var = my_var + 1
until my_var == 10
for i = 0, 10, 2 do
-- type coercion and concatenation
print(i .. " is an even number")
end
```
# Tables
Lua's most important data structure. A table is a set of key/value pairs, where the key may be named. If no name is supplied, the key will automatically adopt a monotonically increasing numeric value starting at **1** (as opposed to most languages, where the first position of an array is at index = **0**, in Lua it's index = **1**).
Keys can even be functions. I've not yet run across a use for this, but I"m sure there are plenty.
Tables, then, function very similar to [[Interface Object-Oriented Definition|Objects]], where the methods are stored as functions keyed on their name.
```lua
-- Creating a pseudo class
Rect = {}
-- Factory method
Rect.new = function(x, y)
return {x = x, y = y}
end
```
# LuaRocks
LuaRocks is the `pip` or `npm` of Lua. It's the package manager. It's a separate install from Lua, much like the other two examples are separate installs from [[Python]] and [[JavaScript]].
****
## Source
- Wikipedia
- Official Lua documentation
- Fireship Lua in 100 seconds video
- Some experimentation, mostly Playdate-related
## Related
[^1]: Multi-paradigm meaning it supports [[Functions|Functional Programming]], [[Interface Object-Oriented Definition|Object-Oriented Programming]], and other styles.