Programming in Lua Translation - EXPRESSIONS

xiaoxiao2021-03-06  37

3.EXPRESSIONS

The expression in Lua includes digital constants, string constants, variables, one yuan and binary operators, function calls. It can also be non-traditional function definitions and table configurations.

3.1 Arithmetic Operator

Binary operator: - * / ^ (Double and subtraction)

One yuan operator: - (negative value)

The operands of these operators are real.

3.2 Relational Operators

<> <=> = == ~ =

These operators return results to false or true; == and ~ = compare two values, if the two value types are different, Lua believes that both; NIL only, etc., Lua compares Tables, UserData, functions. That is to say, when both represent the same object, etc.

A = {}; a.x = 1; a.y = 0

B = {}; B.x = 1; B.Y = 0

C = a

a == c but a ~ = b

The LUA comparison number is performed according to the traditional number size, and the comparison string performs in the order of letters, but alphabetical depends on the local environment.

Pay special attention when comparing different types of values:

"0" == 0 is false

2 <15 IS Obviously True

"2" <"15" is false (alphabetical order!).

In order to avoid inconsistent results, mix comparison numbers and strings, Lua will report an error, such as: 2 <"15

3.3 Logic Operator

And or not

The logical operator thinks that false and nil are false, the other is true, 0 is also TRUE.

The results of the AND and OR are not TRUE and FALSE, but related to its two operands.

A and B: If A is false, then returns a, otherwise returns B

A or B: If A is True, return A, otherwise return B

Print (4 and 5) -> 5

Print (nil and 13) -> nil

Print (False and 13) -> false

Print (4 or 5) -> 4

Print (false or 5) -> 5

A very practical skill: if x is false or nil, the X assignment value V

X = x or v equivalent if not x THEN x = V end

The priority is higher than OR;

The ternary operator in the C language A? B: c can be implemented in Lua: (a and b) or C

The result of NOT has always returned False or True

Print (not nil) -> true

Print (NOT FALSE) -> TRUE

Print (NOT 0) -> False

Print (Not Not Nil) -> False

3.4 Connection Operator

.. String connection, if the operand is a number, Lua converts the number to a string.

Print ("Hello" .. "world") -> Hello World

Print (0 .. 1) -> 01

3.5 priority

From high to low order:

^

Not - (unary)

* /

-

.

<> <=> = ~ ===

and

oral

In addition to ^ and .. All binary operators are all left.

A I (A I) <((b / 2) 1)

5 x ^ 2 * 8 <-> 5 ((x ^ 2) * 8) a (a

-x ^ 2 <-> - (x ^ 2)

X ^ y ^ z <-> x ^ (y ^ z)

3.6 construct

The constructor is an expression of creation and initialization table. Table is a powerful thing that Lua unique features. The simplest constructor is {} to create a empty table. You can initialize array directly:

Days = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "SATURDAY"}

Lua will initialize Days [1] in String "Sunday" (first element index is 1), initialize Days [2] with "Monday" ...

Print (Days [4]) -> Wednesday

The constructor can initialize any expression:

Tab = {sin (1), sin (2), sin (3), sin (4),

sin (5), sin (6), sin (7), sin (8)}

If you want to initialize a table as Record, you can use this:

A = {x = 0, y = 0} and a = {}; A.x = 0; a.y = 0 equivalent

Regardless of how to create table, we can add or remove any type of domain to the table, and the constructor simply affects the initialization of the table.

W = {x = 0, y = 0, label = "console"}

X = {sin (0), sin (1), sin (2)}

w [1] = "Another Field"

X.f = w

Print (w ["x"]) -> 0

Print (W [1]) -> another field

Print (x.f [1]) -> Another Field

w.x = nil - Remove Field "X"

Each time you call the constructor, Lua creates a new table, you can construct a list using table:

List = nil

For line in o.lines () do

List = {next = list, value = line}

end

This code is read from the standard input into each line, and then the link is formed in the order. The following code prints the chain list:

l = list

While L Do

Print (L.Value)

L = L.NEXT

end

In the same constructor, you can mix the list style and the RECORD style, such as:

POLYLINE = {color = "blue", Thickness = 2, npoints = 4,

{x = 0, y = 0},

{x = -10, y = 0},

{x = -10, y = 1},

{x = 0, y = 1}

}

This example also indicates that we can nested constructors to represent complex data structures.

PRINT (POLYLINE [2] .x) -> -10

The initialization method of the above two constructors has restrictions. For example, you cannot use a negative index to initialize an element in a table, and the string index cannot be properly indicated. Here is a more general initialization method, we use [Expression] display The representation will be initialized:

Opnames = {[" "] = "add", ["-"] = "Sub", ["*"] = "mul", ["/"] = "div"}

i = 20; s = "-"

A = {[i 0] = S, [i 1] = S..s, [i 2] = s../....

Print (Opnames [S]) -> SUB

Print (a [22]) -> ---

List style initialization and Record Style Initialization are special examples of this general initialization:

{x = 0, y = 0} <-> {["x"] = 0, ["y"] = 0}

{"Red", "Green", "Blue"} <-> {[1] = "red", [2] = "green", [3] = "blue"}

If you really want an array subscript from 0:

Days = {[0] = "Sunday", "Monday", "Tuesday", "Wednesday",

"Thursday", "Friday", "SATURDAY"}

Note: Not recommended array subscript starts from 0, otherwise many standard libraries cannot be used.

The last ",", "is optional, which can be convenient for future expansion.

A = {[1] = "red", [2] = "Green", [3] = "blue",}

In the construction function, the domain separator comma (",") can be replaced with a semicolon (";"), usually we use a semicolon to segment different types of table elements.

{x = 10, y = 45; "One", "Two", "Three"}

转载请注明原文地址:https://www.9cbs.com/read-69187.html

New Post(0)