• Lua参考手册


    英文原版:

    http://www.lua.org/manual/5.1/

    中文版下面2个地址都有:一样的

    重要部分:

    2.2 – Values and Types

    Lua is a dynamically typed language. This means that variables do not have types; only values do. There are no type definitions in the language. All values carry their own type.

    All values in Lua are first-class values. This means that all values can be stored in variables, passed as arguments to other functions, and returned as results.

    Lua 中的所有值都是一值(first-class) 的。 这意味着所有的值都可以被放在变量里,当作参数传递到另一个函数中,并被函数作为结果返回。

    There are eight basic types in Lua: nilbooleannumberstringfunctionuserdatathread, and tableNil is the type of the value nil, whose main property is to be different from any other value; it usually represents the absence of a useful value.

     Boolean is the type of the values false and true. Both nil and false make a condition false; any other value makes it true. Number represents real (double-precision floating-point) numbers. (It is easy to build Lua interpreters that use other internal representations for numbers, such as single-precision float or long integers; see file luaconf.h.) String represents arrays of characters. Lua is 8-bit clean: strings can contain any 8-bit character, including embedded zeros ('') (see §2.1).

    Lua 中有八种基本类型: nilbooleannumberstringfunctionuserdatathread, and tableNil 类型只有一种值 nil ,它的主要用途用于标表识和别的任何值的差异; 通常,当需要描述一个无意义的值时会用到它。 Boolean 类型只有两种值:false 和 true。 nil 和 false 都能导致条件为假;而另外所有的值都被当作真。 Number 表示实数(双精度浮点数)。 (编译一个其它内部数字类型的 Lua 解释器是件很容易的事;比如把内部数字类型改作 单精度浮点数或长整型。参见文件 luaconf.h 。) String 表示一串字符的数组。 Lua 是 8-bit clean 的: 字符串可以包含任何 8 位字符, 包括零结束符 ('') (参见 §2.1)。

    Lua can call (and manipulate) functions written in Lua and functions written in C (see §2.5.8).

    The type userdata is provided to allow arbitrary C data to be stored in Lua variables. This type corresponds to a block of raw memory and has no pre-defined operations in Lua, except assignment and identity test. However, by using metatables, the programmer can define operations for userdata values (see §2.8). Userdata values cannot be created or modified in Lua, only through the C API. This guarantees the integrity of data owned by the host program.

    The type thread represents independent threads of execution and it is used to implement coroutines (see §2.11). Do not confuse Lua threads with operating-system threads. Lua supports coroutines on all systems, even those that do not support threads.

    The type table implements associative arrays, that is, arrays that can be indexed not only with numbers, but with any value (except nil). Tables can be heterogeneous; that is, they can contain values of all types (except nil). Tables are the sole data structuring mechanism in Lua; they can be used to represent ordinary arrays, symbol tables, sets, records, graphs, trees, etc. To represent records, Lua uses the field name as an index. The language supports this representation by providing a.name as syntactic sugar for a["name"]. There are several convenient ways to create tables in Lua (see §2.5.7).

    Like indices, the value of a table field can be of any type (except nil). In particular, because functions are first-class values, table fields can contain functions. Thus tables can also carry methods (see §2.5.9).

    Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them. Assignment, parameter passing, and function returns always manipulate references to such values; these operations do not imply any kind of copy.

    The library function type returns a string describing the type of a given value.

    userdata 类型用来将任意 C 数据保存在 Lua 变量中。 这个类型相当于一块原生的内存,除了赋值和相同性判断,Lua 没有为之预定义任何操作。 然而,通过使用 metatable (元表) ,程序员可以为 userdata 自定义一组操作 (参见 §2.8)。 userdata 不能在 Lua 中创建出来,也不能在 Lua 中修改。这样的操作只能通过 C API。 这一点保证了宿主程序完全掌管其中的数据。

    thread 类型用来区别独立的执行线程,它被用来实现 coroutine (协同例程)(参见 §2.11)。 不要把 Lua 线程跟操作系统的线程搞混。 Lua 可以在所有的系统上提供对 coroutine 的支持,即使系统并不支持线程。

    table 类型实现了一个关联数组。也就是说, 数组可以用任何东西(除了nil)做索引,而不限于数字。 table 可以以不同类型的值构成;它可以包含所有的类型的值(除 nil 外)。 table 是 lua 中唯一的一种数据结构;它可以用来描述原始的数组、符号表、集合、 记录、图、树、等等。 用于表述记录时,lua 使用域名作为索引。 语言本身采用一种语法糖,支持以 a.name 的形式表示 a["name"]。 有很多形式用于在 lua 中创建一个 table (参见 §2.5.7)。

    跟索引一样, table 每个域中的值也可以是任何类型(除 nil外)。 特别的,因为函数本身也是值,所以 table 的域中也可以放函数。 这样 table 中就可以有一些 methods 了 (参见see §2.5.9)。

    table, function ,thread ,和 (full) userdata 这些类型的值是所谓的对象: 变量本身并不会真正的存放它们的值,而只是放了一个对对象的引用。 赋值,参数传递,函数返回,都是对这些对象的引用进行操作; 这些操作不会做暗地里做任何性质的拷贝。

    库函数 type 可以返回一个描述给定值的类型的字符串。

    强制转换:

      Lua 提供运行时字符串到数字的自动转换。 任何对字符串的数学运算操作都会尝试用一般的转换规则把这个字符串转换成一个数字。 相反,无论何时,一个数字需要作为字符串来使用时,数字都会以合理的格式转换为字符串。 需要完全控制数字怎样转换为字符串,可以使用字符串库中的 format 函数 (参见 string.format)。

    2.3变量:

    写上变量的地方意味着当以其保存的值来替代之。 Lua 中有三类变量:全局变量,局部变量,还有 table 的域。

    一个单一的名字可以表示一个全局变量,也可以表示一个局部变量 (或者是一个函数的参数,这是一种特殊形式的局部变量):

    	var ::= Name
    

    Name 就是 §2.1 中所定义的标识符。

    任何变量都被假定为全局变量,除非显式的以 local 修饰定义 (参见 §2.4.7)。 局部变量有其作用范围: 局部变量可以被定义在它作用范围中的函数自由使用 (参见 §2.6)。

    在变量的首次赋值之前,变量的值均为 nil

    方括号被用来对 table 作索引:

    	var ::= prefixexp `[´ exp `]´
    

    对全局变量以及 table 域之访问的含义可以通过 metatable 来改变。 以取一个变量下标指向的量 t[i] 等价于调用 gettable_event(t,i)。 (参见 §2.8 ,有一份完整的关于 gettable_event 函数的说明。 这个函数并没有在 lua 中定义出来,也不能在 lua 中调用。 这里我们把它列出来只是方便说明。)

    var.Name 这种语法只是一个语法糖,用来表示 var["Name"]

    	var ::= prefixexp `.´ Name
    

    所有的全局变量都是放在一个特定 lua table 的诸个域中,这个特定的 table 叫作 environment (环境)table 或者简称为 环境 (参见 §2.9)。 每个函数都有对一个环境的引用, 所以一个函数中可见的所有全局变量都放在这个函数所引用的环境表(environment table)中。 当一个函数被创建出来,它会从创建它的函数中继承其环境,你可以调用 getfenv 取得其环境。 如果想改变环境,可以调用setfenv。 (对于 C 函数,你只能通过 debug 库来改变其环境; 参见 §5.9)。

    对一个全局变量 x 的访问 等价于 _env.x,而这又可以等价于

         gettable_event(_env, "x")
    

    这里,_env 是当前运行的函数的环境。 (函数 gettable_event 的完整说明参见 §2.8。 这个函数并没有在 lua 中定义出来,也不能调用。 当然,_env 这个变量也同样没有在 Lua 中定义出来。 我们在这里使用它们,仅仅只是方便解释而已。)

    2.4 – Statements

    Lua supports an almost conventional set of statements, similar to those in Pascal or C. This set includes assignments, control structures, function calls, and variable declarations.

    2.4.1 – Chunks

    The unit of execution of Lua is called a chunk. A chunk is simply a sequence of statements, which are executed sequentially. Each statement can be optionally followed by a semicolon:

    	chunk ::= {stat [`;´]}
    

    There are no empty statements and thus ';;' is not legal.

    Lua handles a chunk as the body of an anonymous function with a variable number of arguments (see §2.5.9). As such, chunks can define local variables, receive arguments, and return values.

    A chunk can be stored in a file or in a string inside the host program. To execute a chunk, Lua first pre-compiles the chunk into instructions for a virtual machine, and then it executes the compiled code with an interpreter for the virtual machine.

    Chunks can also be pre-compiled into binary form; see program luac for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly.

    lua 把一个 chunk 当作一个拥有不定参数的匿名函数 (参见 §2.5.9)处理。 正是这样,chunk 内可以定义局部变量,接收参数,并且返回值。

    chunk 可以被保存在一个文件中,也可以保存在宿主程序的一个字符串中。 当一个 chunk 被执行,首先它会被预编译成虚拟机中的指令序列, 然后被虚拟机解释运行这些指令。

    chunk 也可以被预编译成二进制形式;细节参考程序 luac。 用源码形式提供的程序和被编译过的二进制形式的程序是可以相互替换的; Lua 会自动识别文件类型并做正确的处理。

    2.4.2 – Blocks

    A block is a list of statements; syntactically, a block is the same as a chunk:

    	block ::= chunk
    

    A block can be explicitly delimited to produce a single statement:

    	stat ::= do block end
    

    Explicit blocks are useful to control the scope of variable declarations. Explicit blocks are also sometimes used to add a return or break statement in the middle of another block (see §2.4.4).

    语句块是一列语句段;从语法上来说,一个语句块跟一个 chunk 相同:

    	block ::= chunk
    

    一个语句块可以被显式的写成一个单独的语句段:

    	stat ::= do block end
    

     显式的语句块对于控制变量的作用范围很有用。 有时候,显式的语句块被用来在另一个语句块中插入 return 或是 break (参见 §2.4.4)。

    2.5.5 - 取长度操作符

    取长度操作符写作一元操作 #字符串的长度是它的字节数(就是以一个字符一个字节计算的字符串长度)。

    table t 的长度被定义成一个整数下标 n 。 它满足 t[n] 不是 nil 而 t[n+1] 为 nil; 此外,如果 t[1] 为 nil ,n 就可能是零。 对于常规的数组,里面从 1 到 n 放着一些非空的值的时候, 它的长度就精确的为 n,即最后一个值的下标。 如果数组有一个“空洞” (就是说,nil 值被夹在非空值之间), 那么 #t 可能是任何一个是 nil 值的位置的下标 (就是说,任何一个 nil 值都有可能被当成数组的结束)。

    优先级:

    Lua 中操作符的优先级写在下表中,从低到高优先级排序:

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

    通常,你可以用括号来改变运算次序。 连接操作符 ('..') 和幂操作 ('^') 是从右至左的。 其它所有的操作都是从左至右。

  • 相关阅读:
    c3p0连接池c3p0-config.xml配置文件各属性的意义
    MVC案例-架构分析
    jsp中文乱码
    JSP标签
    JSP_include指令
    JavaWeb_请求转发
    JavaWeb_域对象的属性操作
    JavaWeb_JSP语法
    345. Reverse Vowels of a String
    541. Reverse String II
  • 原文地址:https://www.cnblogs.com/youxin/p/3671241.html
Copyright © 2020-2023  润新知