12. Hello Lua:控制结构-If
• if Statement
if a <= 0 then
a = a + 1
end
if a < 0 then
a = a + 1
elseif a > 0 then
a = a – 1
else
a = 0
end
Nested if-else statement
Simple if statement
13. Hello Lua:控制结构-While
• While Loop
Lua first tests the while condition, if it is false
then the loop ends, otherwise it executes the
body
a = {1,2,3,4,5}
i = 1
while a[i] do
print(a[i])
i = i + 1
end
Output: 1,2,3,4,5
14. Hello Lua:控制结构-Repeat
• Repeat
Repeat-until statement repeats its body until
the condition is true. This ensures that the body
of the loop is executed at least once.
a = 0
repeat
a = a + 1
print(a)
until a > 10
Output: 1,2,3,4,5,6,7,8,9,10,11
This block always
executes at least once
15. Hello Lua:控制结构-Numeric Loop
• Numeric For Loop
for i=1,10,1 do
print(i)
end
Output: 1,2,3,4,5,6,7,8,9,10
Initial condition
Evaluate to
Increment by
16. Hello Lua:控制结构-Generic Loop
• Generic For Loop
– Allows you to traverse all values returned by an
iterator function
colors = {“red”, “green”, “blue”}
for i,v in ipairs(colors) do
print(i,v)
end
Output: 1 red, 2 green, 3 blue
Iterator Functionindex value
17. Hello Lua:函数
• 函数是第一类型
• 定义函数
• 调用
function add(a, b)
c = a + b
return c
end
x = add(1, 2)
print(x)
输出:3
18. Hello Lua:Closure
• Closures
– A function which returns a function
– Has full access to local variables
– This allows Lua to implement functional programming
function newCounter()
local i = 0
return function()
i = i + 1
return i
end
end
Anonymous
function
c1 = newCounter()
print(c1()) --> 1
print(c1()) --> 2
c2 = newCounter()
print(c2()) --> 1
print(c1()) --> 3
print(c2()) --> 2
* UpValues:lobject.h(231~)
21. Hello Lua:Meta Methods
> x = { value = 3 } -- our object
>
> mt = { __add = function (a, b)
>> return { value = a.value + b.value }
>> end } -- metatable containing event callbacks
>
> a = x + x -- without a metatable this is just a regular table
stdin:1: attempt to perform arithmetic on global `x' (a table value)
stack traceback:
stdin:1: in main chunk
[C]: ?
>
> setmetatable(x, mt) -- attach our metamethods to our object
>
> a = x + x -- try again
> print(a.value)
6
22. Hello Lua:Meta Methods
原型 作用
__index Control 'prototype' inheritance.
if the value is a function, the function is called, passing in the table
and the key; the return value of that function is returned as the result.
__newindex Control property assignment.
When calling myTable[key] = value, if the metatable has a __newindex
key pointing to a function, call that function, passing it the table, key,
and value.
__call Treat a table like a function.
When a table is followed by parenthesis such as myTable( 'foo' ) and
the metatable has a __call key pointing to a function, that function is
invoked (passing any specified arguments) and the return value is
returned.
__add Addition
__sub Subtraction
… …
* 横向比较
23. Hello Lua:Meta Methods
-- create a namespace
Window = {}
-- create the prototype with default values
Window.prototype = {x = 0, y = 0, width = 100, height =
100, }
-- create a metatable
Window.mt = {}
-- declare the constructor function
function Window.new (o)
setmetatable(o, Window.mt)
return o
end
24. Hello Lua:Meta Methods
Now, we define the __index metamethod:
Window.mt.__index = function (table, key)
return Window.prototype[key]
end
After that code, we create a new window and query it for an absent field:
w = Window.new{x = 10, y = 20}
print(w.width) --> 100
When Lua detects that w does not have the requested field, but has a
metatable with an __index field, Lua calls this __index metamethod, with
arguments w (the table) and "width" (the absent key). The metamethod then
indexes the prototype with the given key and returns the result.