(1) Lua introduction to Lua

Source: Internet
Author: User

LuaThe programming language is a simple, lightweight, and scalable scripting language. Lua reads/'lua/(Lu Ah), which means "Luna" (Moon) in Portuguese.

Directory

[Hide]

  • 1 goal
  • 2 features
  • 3 program examples
  • 4 types
  • 5 external links

[Edit] Target

Lua's goal is to become an easyEmbeddedLanguages used in other languages. Most programmers also think that it does.

Many applications use Lua as their embedded scripting language to achieve configurability and scalability. These include the westward journey II, the legend of xianjing, The World of Warcraft, the warehouse hammer 40 k, the bodemen gate, and xuanyuan sword.

[Edit] features

Lua isLightweightLanguage. Its official version only includes a streamlined core and the most basic library. This makes Lua small in size and fast in startup. It is written in standard C language and open in the form of source code. After compilation, it is only one hundred kb, which can be easily embedded into other programs. Unlike many "Big and comprehensive" languages, network communication and graphical interfaces are not provided by default. However, Lua can be easily extended: these functions are provided by the host language (usually C or C ++), and Lua can use them, just as they are originally built in. In fact, many mature extension modules are available.

Lua is a programming language with Multiple Programming paradigms: it only provides a small set of features to meet the needs of different editing paradigms, instead of providing complex feature support for a specific editing paradigm. For example, Lua does not provide inheritance for this feature, but you can use a meta table to simulate it. Concepts such as namespaces and classes are not implemented in BASIC language features, but we can easily simulate them with table structures (the unique complex data structure provided by Lua. Lua can construct a function at any time during runtime and regard it as an object (called the first class function). This feature can well meet the needs of functional programming. This provides these basic meta-features, and we can modify the language as needed.

Lua native supports very few data types. It only provides numbers (double-precision floating point number by default, configurable), Boolean, String, table, subprogram, coroutine) and user-defined data. However, the processing efficiency of tables and strings is very high. With the help of meta-tables, we can efficiently simulate complex data types (such as collections and arrays ).

Lua is a dynamic and weak language that supports incremental garbage collection policies. It has built-in collaborative multi-threading (coroutine) Support unrelated to the operating system.

[Edit] program example

print "Hello, world!"

After the command is executed, "Hello, world!" is displayed on the screen! "(Excluding the front and back ").

A more complex example shows what functions are and Lua's support for closures:

function create_a_counter()    local count = 0    return function()        count = count + 1        return count    endend

Create_a_counter () returns an anonymous function (in Lua, the function can be specified to store variables like other data ). This anonymous function adds count to 1 and returns the result. In an anonymous function, the variable count is neither a global variable nor a regional variable, but a variable called external local variable (an external local variable, also known as upvalue. The value of this variable is always stored in an anonymous function. Therefore, the create_a_counter () function is equivalent to a counter. Each time you call this counter, a value greater than 1 is obtained.

[Edit] Type

Lua is a dynamic type language, and there is no type definition in the language, that is, you do not need to declare the variable type when writing code. Each variable saves its own type.

There are eight basic types: nil, boolean, number, string, userdata, function, thread, table

    print(type("Hello world"))  --> string    print(type(10.4*3))         --> number    print(type(print))          --> function    print(type(type))           --> function    print(type(true))           --> boolean    print(type(nil))            --> nil    print(type(type(X)))        --> string

As you can see, the output result of the last code is string. No matter what X represents, the result is always string, because the return value of the type () function is string type.

[Edit] external link

Official site

  • Lua.org
  • Official English email list, email list archival, or Gmane lua list archival

Developer website

  • Lua-users wiki
  • LuaForge has many LUA-related projects
  • Www.luaer.cn China lua developer

Manual

  • Book: Programming in Lua
  • Manual.luaer.cn lua online manual
  • Book.luaer.cn lua online lua tutorial
  • Lua reference manual Chinese Translation of Lua Reference Manual (cloud wind translation version)

Forum

  • Lua Chinese user group
  • Lua Chinese group
  • Bbs.luaer.cn lua Forum

 

For more information about Lua, see Wikipedia: http://zh.wikipedia.org/wiki/Lua.

 

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.