Lua scripting language Overview _lua

Source: Internet
Author: User
Tags arithmetic data structures garbage collection hash logical operators lua

What is LUA?

Lua is an embedded scripting language.

Lua languages can be programmed independently, but this is not the main way to use them. LUA, although dynamic and flexible syntax provides powerful functionality, does not have a perfect library (but there are a number of third third-party) like Java, Python, and it is not a flaw, it's about positioning.

The most typical use of Lua is as a library, embedded in other large languages (called host language) applications, provide the application with parameter configuration or logical description functions, resulting in unprecedented flexibility.

Lua's common host languages are: c/c++,java,.net, and even scripting languages such as Php,ruby. How LUA interacts with host languages: Host languages call variables and functions in LUA scripts through virtual machines; Lua invokes variables and functions in the host language.

Features of the Lua language

-----is 8 Big data types

1. Dynamic language, controllable garbage collection, support for basic operations of numeric, String, Boolean, and nil simple types
2. Build complex data structures based on a hash table, in a prototype manner, and support object-oriented
3. Operations that support custom data types in the host language
4. The function as a common data type, supporting the word legal boundaries, tail recursion
5. Support concurrent programming by means of a coprocessor

The scope of LUA usage

What else can lua do besides writing a World of Warcraft plug-in? You can use Lua to write some commonly used programs, such as manipulating Excel, such as customizing some lookups. Lua can also be used as a configuration (like an INI file or a cfg file), because LUA tables can achieve very complex configuration functions, and Lua is faster to parse than XML. Of course Lua is primarily useful as a scripting support language for games.

The basic content of LUA

Eight types of data: nil,booleans,numbers,strings,table (table), Functions (function), Userdata,threads (CO)

1. Value (number): Inside a double for ===== arithmetic operator (+,-, *,/,^ (Power),%), relational operator (>,<,<=,>=,==,~=) = = = Math Library
2. String: Always end with 0, but can contain any character (including 0), so it is not equivalent to C string, but superset = = = Join operator = = = "String Library
3. Boolean (Boolean): only true,false two values------> logical operators (And,or,not). All other values are true in the condition of the control structure except false and nil. So Lua thinks 0 and empty strings are true.
4. Function: One of the key concepts of LUA. A function or function pointer that is not simple equivalent to C.
5. Table: Heterogeneous hash table. One of the key concepts of LUA.
6.userdata: The C data structure defined by the user (not the scripting user). Script users can only use it, not defined.
7. Thread: The LUA collaboration thread (Coroutine) is not the same as a preemptive thread for a generic operating system.
8.nil: Represents nothing and can be analogous to C's null, but it is not a null pointer.

Copy Code code as follows:

Print (Type ("Hello World")--> string
Print (Type (10.4*3))--> number
Print (type)--> function
Print (Type (true))--> Boolean
Print (type (nil))--> Nil
--The variable does not have a predefined type, and each variable may contain a value of either type.
Print (Type (a))--> nil (' A ' is not initialized)
A = 10
Print (Type (a))--> number
A = "a string!!"
Print (Type (a))--> string
A = print-yes, this is valid!
A (type (a))--> function

Expressions: Arithmetic, relationship, logic, join operators
Process Control: Ifthenelse,while,repeat,for,
Core: Functions (function closures), tables (meta tables), Threads (collaboration threads)
Data structures: arrays, matrices and multidimensional arrays, lists, queues, and bidirectional queues, collections and packages, and string buffering.
Standard library: Math library, string library, IO library, operating system library, debug library

Lua virtual machines

The LUA virtual machine has a stack inside it, and the LUA API provides operations on it, not only on stack operations, but also in arrays, randomly reading and writing stack elements through index values, which is the primary way to exchange data between the two parties.

The host language allows you to write functions for LUA calls, which require that the host language follow the calling convention, take parameters from the stack, and finally put the results into the stack. Registering a host function with Lua_register into the LUA virtual machine (which essentially adds global variables to the LUA language) can be invoked by the Lua language.

The host language can also press the LUA function stack, then press the arguments sequentially, and finally use Lua_call to complete the call to the LUA function.

If there are N elements in the LUA virtual machine stack, you can index up from the bottom of the stack with 1 ~ N, or you can index it from the top of the stack with -1 ~-N, which is generally more common.

Each element of the stack can be any complex LUA data type, and there is no vacancy for elements in the stack, implicitly containing an "empty" type of data.

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.