A little bit about Lua
Tags: programming, lua, beginners
Published: 2022-12-03 12:37:40

## Introduction

**Lua** is a lightweight, high-level, multi-paradigm programming language designed primarily for embedded systems and applications. It was first released in 1993 and is used in a wide range of fields, including game development, web development, and scientific computing.

Some of the key features of Lua include:

- **Small size:** Lua has a small footprint, with the entire interpreter fitting in just over 100KB of memory. This makes it well-suited for use in embedded systems and applications with limited resources.
- **Easy to learn:** Lua has a simple and straightforward syntax, making it easy to learn for beginners. It also has a large and active community, with many resources available to help new users get started.
- **Flexibility:** Lua is a multi-paradigm language, meaning it supports different programming styles, such as procedural, object-oriented, and functional. This makes it versatile and allows it to be used in a wide range of applications.
- **Extensibility:** Lua is designed to be easily extended with C libraries, allowing developers to add new functions and features to the language. This makes it a popular choice for game development, where it is often used as a scripting language.

## Hello, World!

Here are some basic concepts and syntax to start with the language. 

First, Lua is a dynamically-typed language, which means that you don't need to specify the type of a variable when you declare it. Instead, the type of a variable is determined at runtime based on the value it is assigned.

There are eight basic types in Lua: `nil`, `boolean`, `number`, `string`, `function`, `userdata`, `thread`, and `table`.

For example, the following code shows how to declare and initialize a variable in Lua:

```
-- Declare a variable named "x"
x = 10

-- Declare a variable named "name"
name = "John"

```

In this code, the `x` variable is assigned the value 10, which is a `number`, and the `name` variable is assigned the value "John", which is a `string`.

Important to mention:

> The type number represents both integer numbers and real (floating-point) numbers, using two subtypes: integer and float. Standard Lua uses 64-bit integers and double-precision (64-bit) floats, but you can also compile Lua so that it uses 32-bit integers and/or single-precision (32-bit) floats. The option with 32 bits for both integers and floats is particularly attractive for small machines and embedded systems. (See macro LUA_32BITS in file luaconf.h.) - https://www.lua.org/manual/5.4/manual.html#2

Next, Lua uses indentation to define code blocks, rather than using braces like many other languages. This means that you should indent your code to show which statements are part of a code block, like this:

```
-- Define a function named "hello"
function hello()
    -- Print a greeting to the console
    print("Hello, world!")
end
```
The indentation of the `print` statement indicates that it is part of the `hello` function.

## Basic syntax

In the Lua programming language, there are several reserved words and tokens that have special meaning and cannot be used for other purposes.

Here is a list of some of the reserved words in Lua:

- `and`: Used to perform a logical AND operation.
- `break`: Used to break out of a loop or switch statement.
- `do`: Used to begin a block of code.
- `else`: Used as part of an if statement to specify the code to be executed if the condition is not true.
- `elseif`: Used as part of an if statement to specify additional conditions to be tested.
- `end`: Used to indicate the end of a block of code.
- `false`: Used to represent the Boolean value false.
- `for`: Used to create a loop.
- `function`: Used to define a new function.
- `goto`: Used to jump to the specified label.
- `if`: Used to create a conditional statement.
- `in`: Used as part of a for loop to iterate over the elements of a table or other data structure.
- `local`: Used to declare a local variable.
- `nil`: Used to represent the absence of a value.
- `not`: Used to perform a logical NOT operation.
- `or`: Used to perform a logical OR operation.
- `repeat`: Used to begin a loop that repeats until a specific condition is met.
- `return`: Used to return a value from a function.
- `then`: Used as part of an if statement to indicate the code to be executed if the condition is true.
- `true`: Used to represent the Boolean value true.
- `until`: Used to end a repeat loop and specify the condition that must be met to exit the loop.
- `while`: Used to create a loop that continues as long as a specific condition is true.

In addition to reserved words, there are also several tokens in Lua that have special meaning and cannot be used as identifier names. These include the following:

- `+`: Used to perform addition.
- `-`: Used to perform subtraction.
- `*`: Used to perform multiplication.
- `/`: Used to perform division.
- `%`: Used to perform modulo division.
- `^`: Used to perform exponentiation.
- `#`: Used to get the length of a string or table.
- `..`: Used to concatenate two strings.
- `<`: Used to compare two values to see if the first is less than the second.
- `<=`: Used to compare two values to see if the first is less than or equal to the second.
- `==`: Used to compare two values to see if they are equal.
- `~=`: Used to compare two values to see if they are not equal.
- `>`: Used to compare two values to see if the first is greater than the second.
- `>=`: Used to compare two values to see if the first is greater than or equal to the second.
- `=`: Used to assign the value.
- `(`: Used to begin a block of code or an expression.
- `)`: Used to end a block of code or an expression.
- `{`: Used to begin a table constructor.
- `}`: Used to end a table constructor.
- `[` and `]`: Used to index a table.
- `;`: Used to separate statements.
- `:`: Used to separate a method name from its arguments.
- `,`: Used to separate items in a list.
- `.`: Used to access a field of a table or object.
- `&`: Used to perform a bitwise AND.
- `~`: Used to perform a bitwise XOR `print(3 ~ 5)` or as unary bitwise NOT `print(~7)`.
- `|`: Used to perform a bitwise OR.
- `<<`: Used to perform left shift.
- `>>`: Used to perform right shift.
- `//`: Used to perform the floor division.
- `::`: Used to define labels for blocks of code.
- `...`: Used to represent varargs, which are a variable number of arguments passed to a function.

More information you can find in [the official manual](https://www.lua.org/manual/5.4/).

## Variables

In Lua, there are three kinds of variables in Lua: global variables, local variables, and table fields. Variable names must begin with a letter or underscore character, and they can contain letters, numbers, and underscores. They are case-sensitive, so `x` and `X` are considered to be different variables.

In the Lua programming language, variables are assumed to be global unless explicitly declared as local. Local variables have lexical scoping, meaning they can be freely accessed by functions defined inside their scope. Before a variable is assigned a value for the first time, its value is `nil`.

## Statements

In the Lua programming language, a statement is a piece of code that performs a specific action or task. There are several different types of statements in Lua, and each has a specific syntax and purpose.

Here are some examples of common statements in Lua:

Assignment statement: Used to assign a value to a variable. For example:

```
x = 10
```

Conditional statement: Used to execute a block of code only if a certain condition is met. For example:

```
if x > 10 then
  print("x is greater than 10")
end
```

Loop statement: Used to repeatedly execute a block of code. For example:

```
while x < 20 do
  x = x + 1
  print(x)
end
```

Function definition statement: Used to define a function. For example:

```
function add(a, b)
  return a + b
end
```

Function call statement: Used to call a function. For example:

```
result = add(10, 20)
```

These are just a few examples of the types of statements that are available in the Lua programming language.

## In Conclusion

The Lua programming language is a powerful and versatile language that is well-suited for a wide range of tasks. It is small, fast, and easy to learn, making it a popular choice for embedded systems and game development.

If you are interested in learning more about the Lua language, there are many resources available to help you get started. Some recommended resources include the [official Lua website](https://www.lua.org/), which contains [documentation](https://www.lua.org/docs.html) and [tutorials](https://www.lua.org/start.html), and the [Lua mailing list](https://www.lua.org/lua-l.html), which is a great place to ask questions and get help from the community. There are also many books and [online courses](https://www.codecademy.com/learn/learn-lua) available that can help you learn more about the language and its features.

Overall, Lua is a great language to learn and use, and it offers many benefits for developers who want to build efficient and powerful applications.