Learning Lua - Intro

This article is published as unfinished draft. I plan to work on this notes continously.

Did you have some experience with some programming language that you always wish to play with? One of that languages for me is Lua. I've played with it many times. I've enjoy it and I've always thinking why this language is not more mainstream.

Lua is pretty light, whole source files of language has aroung 1MB, and can be integrated (embedded) into other programs written mainly in C, C++. You can find it especially as scripting language in gaming industry.

First steps

If you want to learn Lua language. I guess that first step is to install it in your local environment.

For this case I have no other other ambitions that redirect you to official documentations.

Luarocks

If you plan to tweak some specific topic as I do - you will need to find way how to install library dependecies for your master piece.

Lua has amazing package manager called Luarocks and here is how you can install it.

What I did?

I am playing with on my BSD (GhostBSD) machine where I install it with as root:

> # Install Lua
> pkg install lua

and LuaRocks:

> wget https://luarocks.org/releases/luarocks-3.8.0.tar.gz
> tar zxpf luarocks-3.8.0.tar.gz
> cd luarocks-3.8.0
> make && make install

More or less it's the same as in documentation. I guess that better for you is simply follow official documentation.

Language primitives

Rest of this article are my notes from reading official tutorials and experimenting with language a bit. I'll be glad if you find it useful, but think about that this notes are just hints to help me jump into language as I play with it just time to time.

Reserved keywords

The following words are reserved. You cannot use them as identifiers:

and       break     do        else      elseif
end       false     for       function  if
in        local     nil       not       or
repeat    return    then      true      until
while

This is pretty cool, right? Language is very small and you can do quite a lot with that.

Variable initialization

> a = 2; b = 4
> print(a^b)
16
> print(b^3)
64

Types and values

print(type("Hello world"))  --> string
print(type(10.4*3))         --> number
print(type(print))          --> function
print(type(type))           --> function
print(type(true))           --> boolean
print(type(nil))            --> nil
print(type(type(X)))        --> string

Dynamic types:

print(type(a))   --> nil   (`a' is not initialized)
a = 10
print(type(a))   --> number
a = "a string!!"
print(type(a))   --> string
a = print        -- yes, this is valid!
a(type(a))       --> function

Numbers

Examples of valid numeric constants are:

4     0.4     4.57e-3     0.3e12     5e+20
Tags: #languages #lua , Created: 14. 8. 2022
Created by Martin Krizan (2024)