[Note] LUA Hello World
===
###### tags: `note`, `lua`, `helloworld`
[toc]
# Introduction
## Features
> Lua (/ˈluːə/ LOO-ə; from Portuguese: lua [ˈlu.(w)ɐ] meaning moon) is a
> - cross-platform
> - dynamic language
> - embedable
> - extensible
> - high-level
> - lightweight
> - multi-paradigm programming language
> - open source
> - portable
> - dynamic language
> - weak typing
> - extensible
> - embedable
> - portable
## Interpreter
> Lua's interpreter of compiled bytecode (about 100 KB) is written in ANSI C,and Lua has a relatively simple C API to embed it into applications.
## Application
Lua's application includes
- embedded system such as Android and iOS
- PSP and PS3
- Node NCU
- Wireshark
- Snort
- Games; LucasArts and World of Warcraft
- Game engine; Corona SDK and Moai
# Installation
```shell=
[TomasPC]# curl -R -O http://www.lua.org/ftp/lua-5.3.4.tar.gz
[TomasPC]# tar zxf lua-5.3.4.tar.gz
[TomasPC]# cd lua-5.3.4
[TomasPC]# make linux test
[TomasPC]# lua -v
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
```
# Syntax
## Data Type
There are total 8 kinds of data type including **nil, boolean, number, string, userdata, function, thread and table** as shown below table.
| DataType | Description |
| -------- | ----------------------------------------------- |
| nil | null and is false |
| boolean | ture or false |
| number | double in C |
| string | "a string" or 'a string' |
| userdata | any C structure stored in memory |
| function | any C or Lua function |
| thread | a single thread |
| table | an array and the index can be integer or string |
### Array
```Lua=
arr= {"h", "e", "l", "l", "o"}
for index= 1, #arr do
print(arr[index])
end
for i,v in ipairs(arr) do
print(i,v)
end
```
### Function
It is allowed to return many values of a function.
```Lua=
-- Example 1
function calc(a,b,c)
return a b c
end
result= calc(1,2,3)
print(result)
-- Example 2
function getCalc(a,b,c)
return a,b,c
end
r1,r2,r3= getCalc(1,2,3)
print(r1,r2,r3)
```
### Table
Table is a data structure in Lua like a map in Java or JSON object in Javascript.
The size of Lua table is not flexible.
```Lua=
person ={}
person.name ="jack"
person.age =20
print(person[1])
print(person.name)
print(person["age"])
```
## Comment
### Single line
``` Lua
-- A single comment
```
### Block
``` Lua
--[[Comments can be spread
across several lines ]]
print "Hello World!"
```
### Nested
Nesting of comment blocks will cause a syntax error.
``` Lua
--[[
This will not work. Nesting of comment blocks will cause a syntax error.
--[[
By nesting existing code containing a comment, a syntax error will occur. This is because the nested symbols will confuse the interpreter, so a modification is required to remove the symbols
]]
print "Hello"
]]
```
## Assignment
The default type of variable is nil and global.
```Lua=
a = 10
str = "hello world"
a, b = 10, 15
c = a + b -- plus
d = a - b -- substract
e = a * b -- multiply
f = a / b -- divide
g = a % b -- mod
h = a^2 -- square
i = -a -- negative
```
## Operation
``` Lua=
a , b = 10 , 15
print(a > b) -- greater than
print(a < b) -- less than
print(a == b) -- equal to
print(a ~= b) -- not equal to
print(true and false) -- and
print(true or false) -- or
print(not true) -- not
```
## Flow control
### if condition
``` Lua=
-- if expression
if(true)
then
print("hello")
end
-- if .. else expression
a, b= 10, 15
if(a> b)
then
print(a)
else
print(b)
end
-- nested if
c= 20
if(c >10)
then
if(c <30)
then
print(c)
end
end
```
### loop condition
| loop type |
| ---------------- |
| while |
| for |
| repeat ... until |
| nested loop |
```Lua=
-- while loop
a=10
while( a> 0)
do
print(a)
a =a -1
end
-- for loop
for a=1, 10, 1 do
print(a)
end
-- repeat ... until loop
a=10
repeat
print(a)
a= a- 1
until(a<1)
```
### Module and Package
Establish a file named module.lua.
```Lua=
module={}
module.index=1
function module.sum(a,b)
return a b
end
```
Import from the other file.
```Lua=
-- "module" here is a file name.
require "module"
-- module here is the name of defined in the imported module
print(module.index)
print(module.sum(1,2))
```
# Reference
http://www.lua.org/
http://www2.kimicat.com/lua%E6%95%99%E5%AD%B8
http://yhhuang1966.blogspot.com/2015/07/lua_14.html
https://en.wikipedia.org/wiki/Lua_(programming_language)
https://codertw.com/%E5%89%8D%E7%AB%AF%E9%96%8B%E7%99%BC/390702/
https://en.wikibooks.org/wiki/Lua_Programming