lua是一门用来支持通用程序设计的扩展语言,同时,它也为面向对象编程,函数式编程,数据驱动式编程,提供提供了一个很好的支持。特别地,作为可嵌入脚本语言提供任何需要的程序使用。lua是C语言实现的一个库,是C和C++标准的通用子集。
需要注意的是:Tables, functions, threads, and (full) userdata values are objects,variables do not actually contain these values, only references to them
Every value in Lua can have a metatable,This metatable is an ordinary Lua table that defines the behavior of the original value under certain special operations.
Lua supports coroutines, also called collaborative multithreading.a coroutine only suspends its execution by explicitly calling a yield function
The following keywords are reserved and cannot be used as names(也就是我们所说的关键字):
and break do else elseif end
false for function goto if in
local nil not or repeat return
then true until while
The following strings denote other tokens:
+ - * / % ^ #
== ~= <= >= < > =
( ) { } [ ] ::
; : , . .. ...
Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as ‘\0’.
Variables are places that store values. There are three kinds of variables in Lua.
需要注意事是:形参也是一种特殊的局部变量。
Before the first assignment to a variable, its value is nil.
lua中两个变量互换: x, y = y, x
x = val 相当于 _ENV.x = val
lua中的0 和 “” 是true的,只有nil 和false 是false
The basic expressions in Lua are the following:
exp ::= prefixexp
exp ::= nil | false | true
exp ::= Number
exp ::= String
exp ::= functiondef
exp ::= tableconstructor
exp ::= ‘...’
exp ::= exp binop exp
exp ::= unop exp
prefixexp ::= var | functioncall | ‘(’ exp ‘)’
“0”==0 evaluates to false
t[0] and t[“0”] denote different entries in a table.
Here are some examples of logical operators
10 or 20 --> 10
10 or error() --> 10
nil or "a" --> "a"
nil and 10 --> nil
false and error() --> false
false and nil --> false
false or nil --> nil
10 and 20 --> 20
Operator precedence in Lua follows the table below, from lower to higher priority:
or
and
< > <= >= ~= ==
..
+ -
* / %
not # - (unary)
^
a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }
is equivalent to
do
local t = {}
t[f(1)] = g
t[1] = "x" -- 1st exp
t[2] = "y" -- 2nd exp
t.x = 1 -- t["x"] = 1
t[3] = f(x) -- 3rd exp
t[30] = 23
t[4] = 45 -- 4th exp
a = t
end
The statement
local function f () body end
translates to
local f; f = function () body end
not to
local f = function () body end
As an example, consider the following definitions:
function f(a, b) end
function g(a, b, ...) end
function r() return 1,2,3 end
Then, we have the following mapping from arguments to parameters and to the vararg expression:
CALL PARAMETERS
f(3) a=3, b=nil
f(3, 4) a=3, b=4
f(3, 4, 5) a=3, b=4
f(r(), 10) a=1, b=10
f(r()) a=1, b=2
g(3) a=3, b=nil, ... --> (nothing)
g(3, 4) a=3, b=4, ... --> (nothing)
g(3, 4, 5, 8) a=3, b=4, ... --> 5 8
g(5, r()) a=5, b=1, ... --> 2 3
The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter ‘self’.
function t.a.b.c:f (params) body end
is syntactic sugar for
t.a.b.c.f = function (self, params) body end
Lua is a lexically scoped language. The scope of a local variable begins at the first statement after its declaration and lasts until the last non-void statement of the innermost block that includes the declaration
x = 10 -- global variable
do -- new block
local x = x -- new 'x', with value 10
print(x) --> 10
x = x+1
do -- another block
local x = x+1 -- another 'x'
print(x) --> 12
end
print(x) --> 11
end
print(x) --> 10 (the global one)
Notice that each execution of a local statement defines new local variables. Consider the following example:
a = {}
local x = 20
for i=1,10 do
local y = 0
a[i] = function () y=y+1; return x+y end
end