print("hello world")local function fact(n) if n == 0 then return 1 else return n * fact(n-1) endendlocal a = fact(5)print(a)print(b)--。Lua中有8个基本类型分别为:nil、boolean、number、string、userdata、function、thread和tableprint(type("hello world"))print(type(4.6))print(type(print))print(type(type))print(type(true))print(type(nil))print(type(type(x)))testTable = {}testTable = nilfunction testNil() if testTable == nil then print("nil is false") else print("nil is not false") endendtestNil()--[[这些操作符返回结果为false或者true;==和~=比较两个值,如果两个值类型不同,Lua认为两者不同;nil只和自己相等。Lua通过引用比较tables、userdata、functions。也就是说当且仅当两者表示同一个对象时相等。]]a = {}; a.x = 1; a.y = 0;b = {}; b.x = 1; b.y = 0;c = a;function testEqual() if a == b then print("a == b") elseif c == a then print("a == c") endendtestEqual()--[[逻辑运算符认为false和nil是假(false),其他为真,0也是true. and和or的运算结果不是true和false,而是和它的两个操作数相关]]print(4 and 5)print(4 and false)print(false and nil)print(false or nil)print(true or true)x = x or 10print(x)--三元运算符,前提条件是b不为假a = 1; b = true; c = 2;print((a and b) or c)--not的结果一直返回false或者true print(not false)--..字符串连接,如果操作数为数字,Lua将数字转成字符串。print("hello" .. " world")print(1 .. 2)--对于table usedata,function,lua是作引用比较的;nil只与他自身相等a = {}a.x = 1; a.y = 2b = {}b.x = 1; b.y = 2c = aprint(a == b)print(a ~= b)print(a == c)--表的构造local days = {"sun", "mon", "tue", "wed", "thu", "fri", "sat"}print(days[1])local vec = {x = 1, y = 2, "sun"}print(vec[1])print(vec["x"])print(vec.y)--表索引local a = {["x"]=3, ["y"]=4}a["z"] = 5a.w = 6print(a.x)print(a["y"])print(a.z)print(a.w)--list风格初始化和record风格初始化是这种一般初始化的特例: --{x=0, y=0} <--> {["x"]=0, ["y"]=0} --{"red", "green", "blue"} <--> {[1]="red", [2]="green", [3]="blue"} --链表local single_list = nillocal function make_list(begin, count) for i = 1, count, 1 do single_list = {value = begin, next = single_list} begin = begin + 1 endendmake_list(1, 10)local function print_list() local temp_list = single_list; while temp_list ~= nil do print("list value:" .. temp_list.value) temp_list = temp_list.next; endendprint_list()function test_do() --Lua可以对多个变量同时赋值,变量列表和值列表的各个元素用逗号分开,赋值语句右边的值会依次赋给左边的变量。 --a, b = 10, 2*x <--> a=10; b=2*x --遇到赋值语句Lua会先计算右边所有的值然后再执行赋值操作,所以我们可以这样进行交换变量的值 do x, y = y, x end --if do if true then elseif false then else end end --while do while true do end end --for do for i = 1, 10, 1 do print(i) end endend--泛型forfunction foreach() local list = {"2", "3", "4"} for i,v in ipairs(list) do print("value:" .. v) end for i in pairs(list) do print("kay:" .. i) endendforeach()--Lua语法要求break和return只能出现在block的结尾一句(也就是说:作为chunk的最后一句,或者在end之前,或者else前,或者until前)--[[function foo () return --<< SYNTAX ERROR -- 'return' is the last statement in the next block do return end -- OK ... -- statements not reached end ]]--可变参数:Lua函数可以接受可变数目的参数,和C语言类似在函数参数列表中使用三点(...)表示函数有可变的参数。Lua将函数的参数放在一个叫arg的表中,除了参数以外,arg表中还有一个域n表示参数的个数。function pair_fun(a, b, ...) for i, v in ipairs(arg) do print("arg:" .. i .. " " .. v) endendpair_fun(1,2,3)pair_fun(1,2,3, 4)pair_fun(1,2)print(math.sin(1))--闭包function package() local old_sin = math.sin local k = math.pi/180 math.sin = function(x) return old_sin(x*k) endendpackage()print(math.sin(1))--非全局函数lib = {}lib.foo = function(x, y) return x+ y endprint(lib.foo(1,1))function lib.goo(x , y)--另一种语法方式 return x - yendprint(lib.goo(1,2))--函数定义的两种方式local f = function()endfunction f()end--闭包函数调用function list_iter(t) local i = 0 local n = table.getn(t) return function() i = i + 1 if i <= n then return t[i] end endendfunction use_iter() local t = {10,20,30} iter = list_iter(t) while true do local element = iter() if element == nil then break end print(element) endenduse_iter()--loadstringlocal f = loadstring("local a = 1; return a + 2")print(f())--assertassert(true)--errorlocal function foo() --if false then error() debug.traceback() --endendfunction process_call() foo()end--process_call()--多维数组mt = {}function create_mt() for i = 1, 10 do mt[i] = {} for j = 1, 10 do mt[i][j] = i * j end endendfunction print_mt() for i = 1, table.getn(mt) do for j, v in ipairs(mt[i]) do print(i, j, v) end endendcreate_mt()print_mt()--可变参数function add(...) local s = 0 for i, v in ipairs{...} do s = s + v end return sendprint(add(1,2,3))--元表my_meta={ __add=function(op1, op2) op = {} op.x = op1.x + op2.x; op.y = op1.y + op2.y; return op end}a={x=1, y=2}setmetatable(a,my_meta)b={x=3,y=4}c=a+bprint(c.x,c.y)--输出4,6