第一章 概览
1、chunks
chunks是一系列语句, lua执行的每一块语句,比如一个文件或者交互模式下的每一行都是一个chunks。
2、全局变量
全局变量不需要声明,给一个变量赋值后即创建了这个全局变量,全局变量的初始值为
print(b) -->nil
b = 10
print(b) -->10
3、词法约定
Lua保留字
and break do else elseif
end false for function if
in local nil not or
repeat return then true until
while
Lua是大小写敏感的。
注释: 单行注释: --
多行注释: –[[ –]]
--[[
-- comment
--]]
4、命令行方式
第二章 类型和值
Lua是动态类型语言,变量不要类型定义。 Lua中有8个基本类型:nil、boolean、 number、
string、userdata、function、thread、和table,函数type可以测试变量或者值的类型
1、Nil lua的特殊类型, 只有一个值 nil
2、Booleans 取值false true
3、Numbers 表示实数,Lua中没有整数。
4、Strings 指字符的序列。lua是8位字节,所以字符串可以包含任何数值字符,包括嵌入的0。
string和其他对象一样,Lua自动进行内存分配和释放
可以使用单引号或者双引号表示字符串
a = "a line"
b = 'another line‘
转义
\a bell
\b back space -- 后退
\f form feed -- 换页
\n newline -- 换行
\r carriage return -- 回车
\t horizontal tab -- 制表
\v vertical tab
\\ backslash -- "\"
\" double quote -- 双引号
\' single quote -- 单引号
\[ left square bracket -- 左中括号
\] right square bracket -- 右中括号
例子:
> print("one line\nnext line\n\"in quotes\", 'in quotes'")
one line
next line
"in quotes", 'in quotes'
> print('a backslash inside quotes: \'\\\'')
a backslash inside quotes: '\'
> print("a simpler way: '\\'")
a simpler way: '\'
5、Functions
函数是第一类值(和其他变量相同),意味着函数可以存储在变量中,可以作为函数
的参数,也可以作为函数的返回值。
6、Userdata and Threads
userdata可以将C数据存放在Lua变量中,userdata在Lua中除了赋值和相等比较外
没有预定义的操作。userdata用来描述应用程序或者使用C实现的库创建的新类型。
第三章 表达式
Lua中的表达式包括数字常量、字符串常量、变量、一元和二元运算符、函数调用。
还可以是非传统的函数定义和表构造。
1、算数运算符
二元运算 + – * / ^ (幂)
一元运算 –(负值)
2、关系运算
< > <= >= == ~=
3、逻辑运算
and or not
4、连接运算
.. --两个点
print("Hello ".. "World") --> Hello World
print(0 .. 1) --> 01
5、优先级
从高到低的顺序:
^
not - (unary)
* /
+ -
..
< > <= >= ~= ==
and
or
6、表的构造
构造器是创建和初始化表的表达式,{}构建一个空表,
可以直接初始化数组
构造函数可以使用任何表达式初始化
构造结构体 record
days = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"} tab = {math.sin(1), math.sin(2), math.sin(3), math.sin(4),
math.sin(5),math.sin(6), math.sin(7), math.sin(8)} for k, v in pairs(days) do
print(v)
end for k, v in pairs(tab) do
print(v)
end --record
a ={x = 23,y= 39} -- a ={} a.x = 0 a.y=0
print(a.x,a.y)
第四章 基本语法
1、赋值语句
赋值是改变一个变量的值和改变表域的最基本方法
a = "hello".."world"
t ={n=2,m=0}
t.n = t.n + 1 x = 3
a,b = 10, 2 * x
print(a,b)
2、局部变量与代码块(block)
使用local创建一个局部变量,与全局变量不同,局部变量只在被声明的那个代码块
内有效。代码块:指一个控制结构内,一个函数体,或者一个chunk(变量被声明的那
个文件或者文本串)。
x = 10
locali = 1 -- local to the chunk
whilei<=x do
localx = i*2 -- local to the while body
print(x) --> 2, 4, 6, 8, ...
i = i + 1
end
ifi > 20 then
localx -- local to the "then" body
x = 20
print(x + 2)
else
print(x) --> 10 (the global one)
end
print(x) --> 10 (the global one)
3、控制结构语句
条件控制
print("enter a number")
n = io.read("*number")
if n > 0 then
print(n.." bigger than zero")
elseif n > 100 then
print(n.." bigger than 100")
else
print(n.." smaller than zero")
end -- remember the end
循环控制
n = 3 while n > 0 do
print(n)
n = n - 1
end n = 3
repeat
print(n)
n = n -1
until n < 0 m = 0
for i = 1, 10, 1 do
m = m + i
print(i * m)
end
范型for循环
month ={"Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec"}
for k,v in pairs(month) do
print(k,v)
end for k,y in ipairs(month) do
print(k,month[k])
end
break和return
break语句用来退出当前循环(for,repeat,while)。在循环外部不可以使用。
return 用来从函数返回结果,当一个函数自然结束结尾会有一个默认的return。(这
种函数类似pascal的过程)
n = 3
while 1 > 0 do
print(n)
n = n - 1
if n < 0 then
break
end
end function max(x,y)
if x > y then
return x
else
return y
end
end print(max(23,0))