Lua脚本之语法基础快速入门

         Lua的语法基础超级简单,非常易于上手,下面总结一些学习过程中的Lua语法基础:

       在开始学习之前,先介绍一些最基本的概念,在Lua中具有一个代码块的概念,每个函数或者for循环等都是一个代码块。在Lua中,用 “- - ”来标记该行的注释,使用“- - [ [” 和   “ - - ] ] ”之间括起来的部分进行块注释。如下所示:

-- 行注释,仅仅注释当前行
for idx = 1, 10 do  --在代码之后进行行注释
     print("idx=",idx);  
end
--[[
块注释,上边的for循环结构跟end结合起来就是一个Lua中常见的代码块结构。
--]]
         另外, Lua中支持的算术运算符有:+、-、*、/,即加、减、乘、除;支持的关系运算符有:==、~=(不等于)、<、>、<=、>=;支持的逻辑运算符有:and、or、not。需要注意的是,在Lua中,and逻辑运算符如果第一个参数是false,则返回false,不会执行第二个参数的代码(即使第二个参数是一个错误的表达式,也能顺利运行);如果第一个参数是true,返回第二个参数的值。 同理,or逻辑运算符如果第一个参数不是false,则返回第一个参数的值(不会执行第二个参数的代码);否则返回第二个参数的值。这就是所谓的 逻辑运算符短路求值
result = true
if result and an_donot_defined_method() then
  print("no erro occured!")
end
--[[
上述代码输出的错误如下:
stdin:1: attempt to call global 'an_donot_defined_method' (a nil value)
stack traceback:
	stdin:1: in main chunk
	[C]: ?
--]]
result =false 
if (result and an_donot_defined_method())==false  then
  print("no erro occured!")
end
--上述代码顺利通过编译,即使有一个没定义的方法,打印结果:no erro occured!


一、基本数据类型

          Lua中具有5种基本的数据类型, nil、Boolean、string、Number和table在Lua中使用变量不需要提前声明,变量的类型决定于用户赋值的类型可以使用 type()函数判断变量的类型。其中,nil、Boolean、Number都是用法比较简单的类型,string、table类型用法稍微复杂点。给一个变量赋值为nil,表示释放该变量。Boolean跟其他语言一样,只有true和false两种值。Number是双精度浮点数,Lua中没有整数类型。table类型可以当作数组使用。
         在Lua中,变量默认是全局的,这通常导致一些调试困难,最好尽量显式的在变量名之前加上 local 关键字声明该变量为局部变量。
gNumber = 10  --这是一个默认全局的变量
print(type(gNumber))
--输出结果为number
gNumber = nil --之前的number类型gNumber = 10变量被释放
print(type(gNumber))
--输出结果为nil

function LocalVarFunction ()
  local pTable = {} --用local关键字声明一个局部变量,这个变量将在执行LocalVarFunction方法后销毁
     for idx = 1, 5 do 
           local result = true  --这个result将在每个for循环执行之后销毁
           if result then 
              local pString = "这个字符串将在if代码块之后销毁"
              pTable[idx] = pString
              print(pTable[idx])
           end
     end
end

      下面详细介绍string以及table两种类型的详细用法。
1、string类型的用法
       Lua中的字符串操作非常出色。下表是一些特殊意义的字符:
特殊的Lua字符串
 字符意义字符意义
     \a响铃    \v 垂直制表符 
    \b退格    \\反斜杠
    \f换页符    \“双引号
    \n换行符    \'单引号
    \r换行符   \[左方括号
   \t制表符   \]右方括号
      a、类型转换
     Lua会根据上下文在合理合法的情况下隐式进行数字和字符之间的转换。另外,也可以使用 tonumber()函数和tostring()函数显式地进行字符与数字的转换。 见代码实例:
--字符与数字的隐式转换
print("10" + 7)
--输出结果为:17,将字符10隐私转化为Number类型计算
print("hello" + 7)
--无法进行运算,即不能隐式将"hello"字符转化为Number计算
--[[
   系统错误如下:
stdin:1: attempt to perform arithmetic on a string value  
stack traceback:  
    stdin:1: in main chunk  
    [C]: ?  
--]]



--字符与数字的显式转换
print(tonumber("100")+11)
--输出结果为:111
print(type(tostring(100)))
--输出结果为:string
       b、常用的字符处理函数介绍    string.char()函数根据传入的ASCII编码返回该编码对应的字符。如:string.char(10),表示字符换行符,10是换行符的ASCII编码。    
    string.len()函数求字符串的长度。如:
print(string.len("hello"))
--输出结果为:5
   string.sub(aString, start, end) 函数返回指定字符串的子串。如:
gString = "hello Lua"
print(string.sub(gString, 7,9))
--输出结果为:Lua
     string.format()函数格式化输出指定字符串。 %s表示字符串,%d表示所有数字,%D表示非数字,%a表示字母,%c表示控制字符,%l小写字母,%p标点符号,%s空格符号,%u大写字母,%w字母数字,%x十六进制数,%z用0表示的字符。加%前缀可以让特殊符号也能用在格式化中(如:().%+_*?[ ^ $ ]),如%%代表百分比符号。%.4f表示小数点后有4位的浮点数,%02d.表示至少有两个数字的整数,如果不足两个数字则用0补足。如:
aString = "哈哈,你是"
bString = "一头猪"
print(string.format("%s%s", aString, bString))
--输出结果为:哈哈,你是一头猪
      sting.find(sourceString, targetString) 函数在sourceString字符串中查找第一个符合targetString字符串的位置,如果找到则返回开始和结束的位置,没找到则返回nil。
      string.gsub(sourceString, pattern, replacementString) 函数返回一个字符串,sourceString字符中满足pattern格式的字符都会被替换成replacementString参数的值。

     string.gfind(sourceString, pattern) 函数遍历一个字符串,一旦查找到符合指定格式的字符串就返回该子串。

2、table类型的用法

     一般table可以当做数组使用,可以通过table[n]的索引形式访问任意数组中的某个成员。在Lua中,table还能被当做字典dictionary数据使用,并且数组跟字典的用法还能混合使用(实质上还是数组,只不过索引从数字变成其他属性值)。
      a、使用其他值作为table的索引以及多维table
       table还可以使用其他的值作为索引值,并且能用数字跟其他值同时作为同一个table的索引。如:
gTable = {}
gTable.name = "eric"
gTable.gender = "man"
gTable.phonenumber = "0000000000"
gTable[1] = "公司"
gTable[2] = "部门"
for index, value in pairs(gTable) do 
  print(index, value)
end
--[[
输出结果如下:
1	公司
2	部门
phonenumber	0000000000
gender	man
name	eric
--]]
      注意,上述循环中的pairs()函数可以遍历table中的每一对值(索引以及索引对应的value,有点类似字典,不是吗?)
      事实上, table的索引还可以是table本身,这样就组成了一个多维table或多维字典。跟其他语言的多维数组或字典比起来,使用真是超级方便,非常非常的灵活。如:
gTable = {}
gTable.name = "eric"
gTable.gender = "man"
gTable.phonenumber = "0000000000"
gTable[1] = "公司"
gTable[2] = "部门"
gTable.hobby = {"跑步", "读书", "游戏", "动漫"}  -- 多维table,可以通过gTable.hobby[1]的方式访问.即gTable.hobby本身也是一个table
gTable.secTable = {}
gTable.secTable.job = "程序员"
gTable.secTable.label = "写代码的"
gTable.secTable.description = "职责是实现产品的逻辑"

for index, value in pairs(gTable) do 
  print(index, value)
  if ("table" == type(value)) then
     for idx, var in pairs(value) do 
         print("二维table:", idx, var)
     end
   end
end
--[[
输出结果如下:
1	公司
2	部门
hobby	table: 0x7fdceac14bc0
二维table:	1	跑步
二维table:	2	读书
二维table:	3	游戏
二维table:	4	动漫
phonenumber	0000000000
gender	man
secTable	table: 0x7fdceac15100
二维table:	label	写代码的
二维table:	description	职责是实现产品的逻辑
二维table:	job	程序员
name	eric
--]]

      b、table 的常用函数
       table.getn()函数,返回table中元素的个数。如:
gStringTable = {"a", "b","c","d","e"}
for i = 1, table.getn(gStringTable) do
     print(gStringTable[i])
end
      table.sort()函数,将table中的元素从小到大排列。如:
gNumberTable = {10, 5, 7, 2,3, 2}
table.sort(gNumberTable)
for i = 1, table.getn(gNumberTable) do 
   print(gNumberTable[i])
end
--输出结果如下:
2
2
3
5
7
10
    table.insert(pTable, position, value) 函数在table中插入一个新值,位置参数如果没指定,则默认将新值插入到table的末尾。
     table.remove(pTable, position) 函数从指定table中删除指定位置的元素并返回该元素,如果没有指定删除的位置,则默认删除table的最后一个元素。
    
   介绍到这里, Lua中基本的数据类型诸位应该都能掌握,休息一下,下面接着开始简单介绍Lua的基本语句以及函数。

二、Lua中的常用语句结构以及函数

 1、Lua中的常用语句结构介绍

--if 语句结构,如下实例:
gTable = {"hello", 10}
if nil ~= gTable[1] and "hello" == gTable[1] then
  print("gTable[1] is" , gStringTable[1])
elseif  10 == gTable[2] then
  print("gTable[2] is", gTable[2])
else 
  print("unkown gTable element")
end

--while 和repeat循环语句结构,while先判断条件,如果true才执行代码块(有可能跳过该代码块);repeat则是在最后判断条件,保证代码块至少执行一次。
gTable = {1,2,3,4,5,6,7,8,9,10}
index = 1
while gTable[index] < 10 do 
   print("while gTable[",index,"] is ",gTable[index])
   index = index + 1 -- 注意,Lua不支持index++或者index += 1形式的运算符。
end
--[[
while循环输出结果如下:
while gTable[	1	] is 	1
while gTable[	2	] is 	2
while gTable[	3	] is 	3
while gTable[	4	] is 	4
while gTable[	5	] is 	5
while gTable[	6	] is 	6
while gTable[	7	] is 	7
while gTable[	8	] is 	8
while gTable[	9	] is 	9
--]]

--上一个循环结束后,index = 10
repeat
    print("repeat gTable[",index,"] is ",gTable[index])
   index = index - 2
until index < 1
--[[
输出结果如下:
repeat gTable[	10	] is 	10
repeat gTable[	8	] is 	8
repeat gTable[	6	] is 	6
repeat gTable[	4	] is 	4
repeat gTable[	2	] is 	2
--]]

--for循环结构,for循环结构具有三个参数,初始值,结束值,每个循环增加值。
for index = 1, 5 do --不设置第三个参数的话,默认缺省第三个参数是1,即每个循环 index 增加1
   print("for cycle index =",index)
end
--[[
输出结果为:
for cycle index =	1
for cycle index =	2
for cycle index =	3
for cycle index =	4
for cycle index =	5
--]]

for index = 20 , 0, -5 do --设定第三个参数为-5
 print("for cycle index:",index)
end
--[[
输出结果:
for cycle index:	20
for cycle index:	15
for cycle index:	10
for cycle index:	5
for cycle index:	0
--]]

--break关键字可以使循环强制退出,Lua中没有continue关键字,需要通过其他方式实现continue关键字,比如if-else语句。或者通过网络下载Lua的continue关键字补丁安装来解决该问题

for index = 1, 100, 5 do 
  if index > 10 and index < 25 then  --用if-else语句实现continue关键字的功能
     print("continue!!!!! index=",index)
  else 
    if index > 15 and index < 35 then 
       print("break~~~~~index=",index)
       break
    end
    print("At end index=",index)
  end
end

--[[
输出结果如下:
At end index=	1
At end index=	6
continue!!!!! index=	11
continue!!!!! index=	16
continue!!!!! index=	21
break~~~~~index=	26
--]]

--最后还要提的一点是,Lua中switch语句的缺失,用if-elseif-else语句代替的话,显得非常臃肿,还有其他的一些实现方案。笔者在网上麦子加菲童鞋的博客中找到一种Lua中代替switch语句非常优雅的方案。下面贴出麦子加菲原代码:
--Switch语句的替代语法(所有替代方案中觉得最好,最简洁,最高效,最能体现Lua特点的一种方案)
action = {  
  [1] = function (x) print(x) end,  
  [2] = function (x) print( 2 * x ) end,  
  ["nop"] = function (x) print(math.random()) end,  
  ["my name"] = function (x) print("fred") end,  
}  
 
while true do  
    key = getChar()  
    x = math.ramdon()  
    action[key](x)  
end 

2、Lua中的函数
     在Lua脚本中, 函数是以function关键字开始,然后是函数名称,参数列表,最后以end关键字表示函数结束。需要注意的是, 函数中的参数是局部变量,如果参数列表中存在(...)时,Lua内部将创建一个类型为table的局部变量arg,用来保存所有调用时传递的参数以及参数的个数(arg.n)
function PrintTable (pTable)
  for index = 1, table.getn(pTable) do 
      print("pTable[",index,"] =",pTable[index])
  end
end

gStringTable = {"hello","how","are","you"}
PrintTable(gStringTable)
--[[
输出结果为:
pTable[	1	] =	hello
pTable[	2	] =	how
pTable[	3	] =	are
pTable[	4	] =	you
--]]

function PrintFriendInfo (name, gender, ...) 
  local friendInfoString = string.format("name:%s  gender:%d",name,gender)
  if 0 < arg.n then
     for index = 1, arg.n do 
        friendInfoString = string.format("%s otherInfo:%s",friendInfoString, arg[index])
     end
   end
   print(friendInfoString)
end


PrintFriendInfo ("eric", 1, "程序员","2b", 50)

 --输出结果为:
-- name:eric  gender:1 otherInfo:程序员 otherInfo:2b otherInfo:50

       Lua函数的返回值跟其他语言比较的话,特殊的是能够返回多个返回值。return之后,该Lua函数从Lua的堆栈里被清理
function GetUserInfo ()
    local name = "eric"
    local gender = 1
    local hobby = "动漫"
  return name, gender, hobby
end

print(GetUserInfo())

--输出结果:eric	1	动漫


三、Lua中的库函数

          在本文的最后,介绍一些Lua中常用的库函数。
          1.数学库
          math库的常用函数: 三角函数math.sin、math.cos、取整函数math.floor、math.ceil、math.max、math.min、随机函数math.random、math.randomseed(os.time())、变量pi和huge。
          2、I/O库
         进行I/O操作前,必须先用io.open()函数打开一个文件。io.open()函数存在两个参数,一个是要打开的文件名,另一个是模式字符,类似"r"表示读取、“w”表示写入并同时删除文件原来内容,“a”表示追加,“b”表示打开二进制文件。该函数会返回一个表示文件的返回值,如果打开出错则返回nil,写入之前需要判断是否出错,比如: local file = assert(io.open(filename, “w”))..使用完毕后,调用io.close(file).或file:close()。
       几个常用I/O函数:io.input ()、io.output ()、 io.read()、 io.write()
local file = assert(io.open(filename, “w”))
if file ~= nil then
  file:write("hello lua!!!!")  --注意,等同于io.write("hello lua!!!!")
  file:close()  --等同于io.close(file)
end

         3、调试库

         debug.getinfo()函数,他的第一个参数 可以是一个函数或一个栈层。返回结果是一个table,其中包含了函数的定义位置、行号、函数类型、函数名称等信息。

         debug.getlocal()函数检查函数任意局部变量,有两个参数,第一个是希望查询的函数栈层,另一个是变量的索引。

         assert(trunk)() 函数,执行参数中代码块并在出错时提供报错功能

a = "hello world"
b = "print(a)"
assert(loadstring(b))()
--输出结果:
hello world

        4、几个处理Lua码块的函数

        loadstring(pString)()函数可以直接执行pString字符串组成的Lua码,但不提供报错功能

loadstring("for index = 1, 4 do print(\"for cycle index =\",index) end")()
--[[
输出结果
for cycle index =	1
for cycle index =	2
for cycle index =	3
for cycle index =	4
--]]

      dofile(filename) 函数的功能是载入并立刻执行Lua 脚本文件。可以用来载入定义函数的文件或者数据文件、或立即执行的 Lua 码。 dofile 函数会将程序的执行目录作为当前目录。如果要载入程序执行目录的子目录里的文件,需要加上子目录的路径。

dofile("/Users/ericli/WorkSpace/Lua语言/hellolua.lua")
--输出结果:Hello Lua!
         本篇总结完毕,本篇只是总结了 Lua 的一些最基本的语法。至于 Lua 的更高级的内容,比如:协同程序、模块与包、 Lua调用 C 码、 C++ Lua 的整合等,还需要在以后的学习过程中深入。


参考资料:

解决Lua语法缺失及替代措施

书籍:《Lua程序设计》、《Lua戏开发实践指南》


  • 5
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
VR(Virtual Reality)即虚拟现实,是一种可以创建和体验虚拟世界的计算机技术。它利用计算机生成一种模拟环境,是一种多源信息融合的、交互式的三维动态视景和实体行为的系统仿真,使用户沉浸到该环境中。VR技术通过模拟人的视觉、听觉、触觉等感觉器官功能,使人能够沉浸在计算机生成的虚拟境界中,并能够通过语言、手势等自然的方式与之进行实时交互,创建了一种适人化的多维信息空间。 VR技术具有以下主要特点: 沉浸感:用户感到作为主角存在于模拟环境中的真实程度。理想的模拟环境应该使用户难以分辨真假,使用户全身心地投入到计算机创建的三维虚拟环境中,该环境中的一切看上去是真的,听上去是真的,动起来是真的,甚至闻起来、尝起来等一切感觉都是真的,如同在现实世界中的感觉一样。 交互性:用户对模拟环境内物体的可操作程度和从环境得到反馈的自然程度(包括实时性)。例如,用户可以用手去直接抓取模拟环境中虚拟的物体,这时手有握着东西的感觉,并可以感觉物体的重量,视野中被抓的物体也能立刻随着手的移动而移动。 构想性:也称想象性,指用户沉浸在多维信息空间中,依靠自己的感知和认知能力获取知识,发挥主观能动性,寻求解答,形成新的概念。此概念不仅是指观念上或语言上的创意,而且可以是指对某些客观存在事物的创造性设想和安排。 VR技术可以应用于各个领域,如游戏、娱乐、教育、医疗、军事、房地产、工业仿真等。随着VR技术的不断发展,它正在改变人们的生活和工作方式,为人们带来全新的体验。
Lua 是一种轻量级的脚本语言,它具有简洁、高效、易于扩展等特点。Lua 可以被嵌入到 C/C++ 应用程序中,也可以作为独立的解释器运行。下面简单介绍一下 Lua 脚本的基本语法和用法。 ## 1. 基本语法 Lua 语言的基本语法与众多编程语言类似。下面是一个简单的 Lua 脚本示例: ```lua -- 定义变量 a = 10 b = 20 -- 条件语句 if a < b then print("a is less than b") else print("a is greater than or equal to b") end -- 循环语句 for i = 1, 10 do print(i) end -- 函数定义 function add(a, b) return a + b end -- 函数调用 sum = add(a, b) print("sum =", sum) ``` ## 2. 常用函数 Lua 中内置了一些常用的函数,比如字符串处理函数、数学函数、文件操作函数等。下面是一些常用函数的示例: ```lua -- 字符串处理函数 str = "hello world" print(string.upper(str)) -- 将字符串转换为大写 print(string.sub(str, 1, 5)) -- 截取字符串 -- 数学函数 print(math.pow(2, 3)) -- 求幂 print(math.sqrt(16)) -- 求平方根 -- 文件操作函数 file = io.open("test.txt", "w") file:write("Hello World") file:close() file = io.open("test.txt", "r") print(file:read("*all")) file:close() ``` ## 3. Lua 与 C/C++ 的交互 Lua 可以被嵌入到 C/C++ 应用程序中,实现 Lua 脚本与 C/C++ 代码的交互。在 C/C++ 中,可以使用 Lua API 调用 Lua 脚本中的函数、读写变量等。下面是一个简单的示例: ```c #include "lua.hpp" int main() { lua_State* L = luaL_newstate(); luaL_openlibs(L); luaL_dofile(L, "test.lua"); // 执行 Lua 脚本 lua_getglobal(L, "add"); // 获取 Lua 函数 lua_pushnumber(L, 10); // 压入参数 lua_pushnumber(L, 20); lua_call(L, 2, 1); // 调用函数,2 表示有两个参数,1 表示有一个返回值 double result = lua_tonumber(L, -1); // 获取返回值 printf("result = %lf\n", result); lua_close(L); return 0; } ``` 上面的示例中,C/C++ 代码使用 Lua API 调用了 Lua 脚本中的 add 函数,并获取了该函数的返回值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值