Lua
Lua 巴西里约热内卢天主教大学 1993年 脚本语言 标准 C 语言
官网: www.lua.org
搭建环境
- 编译官方源码(windews 编译相对较繁琐,要有编译程序,比如 vs,windows 建议使用 LuaForWindows 安装包;linux 和 macos 相对简单,在下载的源源文件的 doc 中有编译步骤)
http://www.lua.org/download.html
-
下载编译好的文件直接安装(可能版本比较旧)
-
使用第三方的 IDE
解释器和编译器
个人理解,解释器类似于 Java 中的 java 命令,可以直接使用 java className 差不多,只不过 java 得先编译,而 Lua 不用先编译(边编译边执行),直接执行 lua fileName 。Lua 的编译器就和 Java 的编译器类一样了,都是编译成二进制文件。
约定
- Chunks 和 Blocks
Block: A block is a list of statements, which are executed sequentially
Chunk: The unit of compilation of Lua is called a chunk. Syntactically, a chunk is simply a block
Block 是一个或多个顺序执行的语句,Chunk 是 Lua 的一个编译单元(可以是一个 Lua 文件,一系列语法集合等),在语法结构上 Chunk 仅仅是一个 Block。
chunk ::= {stat [';']} -- chunk 可以是一个语句,可以是多个语句集合,还可以是函数,比如一个文件(文件中可能有比较复杂的操作),或者交互模式下的一行命令(一行命令也可能有很多的操作)
stat ::= do black end -- do ... end 之间的部分被称为一个 block
chunk ::= block -- Syntactically, a chunk is simply a block
- 命名
Names (also called identifiers ) in Lua can be any string of letters, digits, and underscores, not beginning with a digit and not being a reserved word. Identifiers are used to name variables, table fields, and labels.
Lua is a case-sensitive language.
Lua 大小写敏感
保留字:
and break do else elseif end
false for function goto if in
local nil not or repeat return
then true until while
尽量不要声明以下滑线开头,有一个或多个大写字母的变量名称,类似 _VERSION 等,这些 Lua 可能内置了全局变量。一切皆变量,没有赋值前,所有的变量都是 nil,包括不存在的变量。
- 变量
Variables are places that store values. There are three kinds of variables in Lua: global variables, local variables, and table fields.
全局变量,局部变量,表域
-- 不使用 local 声明的变量都是全局变量(函数的形式参数是特殊的局部变量)
a = 5 -- global
local b = 5 -- local
function t()
c = 5 -- global
local d = 5 -- local
end
print(c, d) --> 5, nil
do
e = 5 -- global
local f = 5 -- local
end
print(e, f) --> 5, nil
- 注释
单行:--
多行:--[[ --]]
-- 单行注释
--[[
多行注释
--]]
- 命令行操作
lua [options] [script [args]]
-e:直接执行命令 lua -e "print(_VERSION)"
-l:加载文件
-i:进入交互模式