# 1 mathematical operation operator

## 1.1 `%`

Operator

In Lua`%`

Although operators and operators in C language are the meaning of modulus, the mode of taking module is different.

In C language, modulus operation is to add the sign of the first operands after taking the absolute value of two operands.

In Lua, it is simply the remainder after rounding down the quotient relative negative infinity.

+++

In C,

```
a1 = abs(a);
b1 = abs(b);
c = a1 % b1 = a1 - floor(a1/b1)*b1;
a % b = (a >= 0) ? c : -c;
```

In Lua,

```
a % b == a - math.floor(a/b)*b
```

Lua operates directly according to the definition of modulus. C does a little bit of processing for modular operation.

+++

**give an example:**

In C

```
int a = 5 % 6;
int b = 5 % -6;
int c = -5 % 6;
int d = -5 % -6;
printf("a,b,c,d");--5,5,-5,-5
```

In Lua

```
a = 5 % 6
b = 5 % -6
c = -5 % 6
d = -5 % -6
x = {a,b,c,d}
for i,v in ipairs(x) do
print(i,v)
end
--> 5
--> -1
--> 1
--> -5
```

It can be seen that only when the operands are the same, the two languages have the same modulus results. When the sign is different, the sign and the value of the modulus result are not equal.

The modular operation in Lua is summarized as follows: a% B, if a and B are the same sign, the result is the modulus of absolute value of a and B; if the sign is different, the result is the difference between the absolute value of B and the modulus of absolute value. The sign of the value after taking module is the same as that of B.

# 2 comparison operator

The result of the comparison operation is`boolean`

Type, not`true`

Namely`false`

。

The supported operators are:

```
< <= ~= == > >=
```

I won’t support it`!`

Operator.

+++

about`==`

Operation. The type of the two operands is compared first. If they are inconsistent, the result is false. There is no automatic conversion between the value and the string.

When comparing whether two objects are equal, only when they point to the same memory area, it is determined as`true`

。·

```
a = 123
b = 233
c = "123"
d = "123"
e = {1,2,3}
f = e
g = {1,2,3}
print(a == b) --> false
Print (a = = C) -- > false -- Compare numbers and strings as different types
print(c == d) --> true
Print (E = = f) -- > true -- the reference points to the same object
Print (E = = g) -- > false -- although the contents are the same, they are different objects
Print (false = = Nil) - > false -- false is Boolean, nil is nil type
```

Convenient marking,`-->`

Represents the result of the previous expression.

+++

`userdata`

And`table`

Can be compared by meta method`eq`

Make changes.

In size comparison, the comparison of numbers and strings is consistent with C language. If it’s a different type of value, Lua tries to call the meta method`lt`

and`le`

。

# 3 logical operator

`and,or,not`

Only think that`false`

And`nil`

False.

## 3.1 `not`

Reverse operation`not`

The result is`boolean`

Type. (`and`

and`or`

The result is not necessarily`boolean`

)

```
B = not a -- A is nil, B is true
C = not not not a -- C is false
```

## 3.2 `and`

`a and b`

, if`a`

False, return`a`

, if`a`

True, return`b`

。

Attention, why`a`

Time to return`a`

What about it? What’s the point? that is because`a`

It could be`false`

perhaps`nil`

The two values are false, but they are different.

## 3.3 `or`

`a or b`

, if`a`

False, return`b`

, if`a`

True, return`a`

。 And`and`

contrary.

+++

**Tips:**When logical operators are used to derive a`boolean`

It is unnecessary to consider who is returned after the logical operation, because the operation result of the logical operator conforms to the original logical meaning.

give an example

```
If (not (a > min and a < max)) then
error()
end
```

+++

## 3.4 others

`and`

And`or`

Following the short-circuit principle, the second operands are evaluated only when needed.

example

```
a = 5
X = a or jjjj () -- although the latter function is not defined, it will not report an error because it will not be executed.
print(a) -->5
print(x) -->5
```

From the example above, we should be alert to logical operations, which may introduce errors that are not expected in time.

# 4 connector

`..`

Concatenates two strings (or numbers) into a new string. For other types, call the meta method`concat`

。

# 5 take length operator

`#`

For strings, the length is the number of characters in the string.

For a table, the length of the table is determined by finding the subscript n satisfying that t [n] is not nil and T [n + 1] is nil.

~~What about other types? ~ ~

## 5.1 examples

```
--String length
print(#"abc
``````
--String length
print(#"abc\0") --> 4
--Watch length
print(#{[1]=1,[2]=2,[3]=3,x=5,y=6}) --> 3
print(#{[1]=1,[2]=nil,[3]=3,x=5,y=6}) --> 1
```

") --> 4
--Watch length
print(#{[1]=1,[2]=2,[3]=3,x=5,y=6}) --> 3
print(#{[1]=1,[2]=nil,[3]=3,x=5,y=6}) --> 1

# 6 priority

From low to high:

```
or
and
< > <= >= ~= ==
..
+ -
* / %
not # - (unary)
^
```

**Power operation > monocular operation > four rule operation > connector > comparison operator > and > or**

# 7 table construction

BNF definition of table construction

```
tableconstructor ::= `{´ [fieldlist] `}´
fieldlist ::= field {fieldsep field} [fieldsep]
field ::= `[´ exp `]´ `=´ exp | Name `=´ exp | exp
fieldsep ::= `,´ | `;´
```

BNF definition referenceBrief introduction to BNF paradigm 。

give an example:

```
a = {}
b = {["price"] = 5; cost = 4; 2+5}
C = {[1] = 2 + 5, [2] = 2, 8, price = ABC ", [" cost "] = 4} -- tables constructed by B and C are equivalent
print(b["price"]) --> 5
print(b.cost) --> 4
Print (B [1]) - > 7 -- if the key value is not given, the subscript is allocated in order, and the subscript starts from 1
print(c["price"]) --> abc
print(c.cost) --> 4
print(c[1]) --> 8
print(c[2]) --> 2
```

**be careful:**

- If the key value is not given, the subscript is allocated in order, and the subscript starts from 1
- If there are the same keys in the table, the value at the bottom is used as the value for the key

The existence of the above two items makes C in the above example C1The output value of is 8.

+++

If there are the same keys in the table, the value at the bottom is used as the value for the key.

`A = {[1] = 5, [1] = 6} -- then a [1] = 6`

+++

If the last field of the table is in the form of an expression and is a function, all the return values of that function are added to the table.

```
a = 1
function order()
a = a + 1
return 1,2,3,4
end
b = {order(); a; order(); }
c = {order(); a; (order());}
print(b[1]) --> 1
Print (B [2]) - > 2 -- the values in the table are not assigned after all expressions are evaluated at once
print(b[3]) --> 1
Print (B [4]) - > 2 -- multi return value function in the form of expression
Print (ා b) -- > 6 -- the length of the table is 6
Print (# C) -- > 3 -- the length of the table after adding brackets is 3
```

# 8 function

A function is an expression whose value is an object of type function. The function is instantiated each time it is executed.

## 8.1 function definition

There are three forms of implementing a function in Lua.

```
f = function() [block] end
local f; f = function() [block] end
a.f = function() [block] end
```

Lua provides syntax sugar to handle these three function definitions separately.

```
function f() [block] end
local function f() [block] end
function a.f() [block] end
```

+++

above`local`

The definition of function is not`local f = function() [block] end`

To avoid the following errors:

```
local f = function()
print("local fun")
if i==0 then
F () -- compilation error: attempt to call global 'f' (a nil value)
i = i + 1
end
end
```

## 8.2 parameters of function

Formal parameters are initialized as local variables through arguments.

Add at the end of the parameter list`...`

Indicates that the function can accept indefinite length parameters. If the tail is not added, the parameter list length of the function is fixed.

```
f(a,b)
g(a,b,...)
H (a,..., b) -- compilation error
```

```
f(1) --> a = 1, b = nil
f(1,2) --> a = 1, b = 2
f(1,2,3) --> a = 1, b = 2
g(1,2) --> a = 1, b = 2, (nothing)
g(1,2,3) --> a = 1, b = 2, (3)
g(1,f(4,5),3) --> a = 1, b = 4, (3)
g(1,f(4,5)) --> a = 1, b = 4, (5)
```

+++

There is also a way to define a function whose formal parameter is self

```
a.f = function (self, params) [block] end
```

The grammatical sugar forms are as follows:

```
function a:f(params) [block] end
```

Examples of use:

```
A = {name = "Tangyi Kejun"}
function a:f()
print(self.name)
end
a: If you use a.f(), then self.name The error attempt to index local 'self' will be reported; in this case, it should be written as a.f (a)
```

`:`

The function of function definition and call can be less than one`self`

Parameter. This form is right`method`

Simulation of

## 8.3 function call

The BNF syntax of function calls in Lua is as follows:

```
functioncall ::= prefixexp args
```

If the value of prefixexp is of type function, the function is called with the given parameters. Otherwise, the meta method “call” of prefixexp is called. The first parameter of call is the value of prefixexp, followed by the args parameter list (see2.8 meta table）。

The function call depends on whether it is passed in or not`self`

The parameters are divided into`.`

Calling and`:`

Call.

Function calls can be divided into**Parameter list call, table call, string call**。

**[to be improved]**

## 8.4 function closure

**If a function accesses its external variables, it is a closure.**

Because the variables inside the function are local variables, it can not be accessed by the outside world. In this case, if the outside world wants to change the value of the local variable, the closure can be used to achieve this purpose.

The specific implementation process is roughly like this: there is a sub function inside the function that can change the local variable. If the function returns this sub function, the external world can operate the local variable by using this sub function.

**example:**Using closure to change local variables

```
--Implement an iterator
function begin(i)
local cnt = i
Return function() -- this is an anonymous function that implements the auto increment function; it is also a closure because it accesses the external variable CNT
cnt = cnt + 1
return cnt
end
end
Iterator = begin (2) -- set the initial value of iterator to 2, and return an iterator function
Print (iterator ()) -- performs iteration
print(iterator())
```

**Tips:**For more information on closures, please refer toHow does JavaScript closure work

#### Reference link

Brief introduction to BNF paradigm(brief introduction to BNF)

How do JavaScript closures work?——StackOverflow(introduces the concept of closure in JavaScript in detail)