Lua Basic Syntax _lua

Source: Internet
Author: User
Tags numeric lua

1 Introduction

Implemented by clean C. Need to be called by the host program to inject the C function.

2 Grammatical conventions

Lua's syntax is based on BNF syntax rules.

Lua is sensitive to case sensitivity.

2.1 Reserved Keywords

The key words that are not in the C language are:

Copy Code code as follows:

and ElseIf function
In nil the local not OR
Repeat then until

Spec: global variable begins with an underscore.

2.2 operator

C language does not have operators:

^
~=
--Rounding down
Operators that are not in Lua:

+=
-=

2.3 String Definition

Take an escape character: express those ambiguous characters by means of an escape character

Character representation

A--representing character a
\97--representing character a
\049--Represents the numeric character 1
Other escape characters represent

\\n--Represents a string \ n
\ n--on behalf of line wrapping
Note that the numeric character must be three digits. Other characters cannot exceed three digits.

With long brackets: all the contents of the long brackets are treated as ordinary characters.

[[]]--0-level long bracket
[==[]==]--2-level long bracket

3 Values and types

Lua is a dynamic language, with variables that have no type and values. Values carry type information themselves.

Lua has eight basic types of data: Nil, Boolean, number, string, function, UserData, thread, table.

Only nil and false cause the condition to be false, others are true.

The UserData type variable is used to save C data. Lua can only use this type of data, not create or modify it, to ensure that the host program is fully in control of the data.

Thread is used to implement the coprocessor (Coroutine).

Table is used to implement an associative array. Table allows any type of data to be indexed, and also allows any type to do the value in the table field (previously described
Any type does not contain nil). The table is the only data structure in Lua.
Because a function is also a value, a function can be stored in a table.

function, UserData, thread, table values of these types are objects. These types of variables simply hold the reference to the variable and do not perform any copy of the nature of the assignment, parameter passing, function return, and so on.

The library function type () returns the type description information for the variable.

3.1 Forced conversions

Lua provides an automatic conversion between numbers and strings.
You can use the Format function to control the conversion of numbers to strings.

4 variables

There are three types of variables: global variables, local variables, and fields in a table.

Variables outside the function default to global variables unless the declaration is displayed with local. The variables in the function and the parameters of the function default to local variables.

The scope of a local variable starts at the end of the statement block (or until the declaration of the next local variable with the same name).

The default values for variables are nil.

Copy Code code as follows:

A = 5-global variable
Locals B = 5--Local variables
function joke ()
c = 5--local variable
Locals d = 6--Local variables
End
Print (c,d)--> nil Nil
Todo
Locals a = 6--Local variables
b = 6-Global variables
Print (A,B); --> 6 6
End
Print (A,B)--> 5 6

Convenient marking,--> represents the result of the preceding expression.

4.1 Index

Use square brackets [] for the index of the table. Lua is provided using syntactic sugars. Operation.

T[i]
T.I--A simplified way of writing when an index is a string type
Gettable_event (t,i)--using indexed access is essentially a function call like this

4.2 Environment Table

All global variables are placed in an environment table with a variable named _env. The access to a global variable A is _ENV.A (_env_ for convenience only).

Each function holds a reference to the environment table as a variable that contains all the variables that the function can call.
The child function inherits the environment table from the parent function.
You can read and write environment tables through function getfenv/setfenv.

The above mentioned is the entire content of this article, I hope you can enjoy.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.