- 1 - 介绍
- 2.1 - 语法约定
- 2.10.1 - 垃圾收集的元方法
- 2.10.2 - Weak Table(弱表)
- 2.10 - 垃圾收集
- 2.11 - Coroutine (协同例程)
- 2.2.1 - 强制转换
- 2.2 - 值与类型
- 2.3 - 变量
- 2.4.1 - Chunk(语句组)
- 2.4.2 - 语句块
- 2.4.3 - 赋值
- 2.4.4 - 控制结构
- 2.4.5 - For 语句
- 2.4.6 - 把函数调用作为语句段
- 2.4.7 - 局部变量声名
- 2.4 - 语句段(Statement)
- 2.5.1 - 数学运算操作符
- 2.5.2 - 比较操作符
- 2.5.3 - 逻辑操作符
- 2.5.4 - 连接符
- 2.5.5 - 取长度操作符
- 2.5.6 - 优先级
- 2.5.7 - Table 构造
- 2.5.8 - 函数调用
- 2.5.9 - 函数定义
- 2.5 - 表达式
- 2.6 - 可视规则
- 2.7 - 错误处理
- 2.8 - Metatable(元表)
- 2.9 - 环境
- 2 - 语言
- 3.1 - 堆栈
- 3.2 - 堆栈尺寸
- 3.3 - 伪索引
- 3.4 - C Closure
- 3.5 - 注册表
- 3.6 - C 中的错误处理
- 3.7 - 函数和类型
- 3.8 - 调试接口
- 3 - 程序接口(API)
- 4.1 - Functions and Types
- 4 - The Auxiliary Library
- 5.1 - Basic Functions
- 5.2 - Coroutine Manipulation
- 5.3 - Modules
- 5.4 - String Manipulation
- 5.5 - Table Manipulation
- 5.6 - Mathematical Functions
- 5.7 - Input and Output Facilities
- 5.8 - Operating System Facilities
- 5.9 - The Debug Library
- 5 - Standard Libraries
- 6 - Lua Stand-alone
- 7.1 - Changes in the Language
- 7.2 - Changes in the Libraries
- 7.3 - Changes in the API
- 7 - Incompatibilities with the Previous Version
- 8 - The Complete Syntax of Lua
- Captures
- Character Class:
- luaL_addchar
- luaL_addlstring
- luaL_addsize
- luaL_addstring
- luaL_addvalue
- luaL_argcheck
- luaL_argerror
- luaL_Buffer
- luaL_buffinit
- luaL_callmeta
- luaL_checkany
- luaL_checkint
- luaL_checkinteger
- luaL_checklong
- luaL_checklstring
- luaL_checknumber
- luaL_checkoption
- luaL_checkstack
- luaL_checkstring
- luaL_checktype
- luaL_checkudata
- luaL_dofile
- luaL_dostring
- luaL_error
- luaL_getmetafield
- luaL_getmetatable
- luaL_gsub
- luaL_loadbuffer
- luaL_loadfile
- luaL_loadstring
- luaL_newmetatable
- luaL_newstate
- luaL_openlibs
- luaL_optint
- luaL_optinteger
- luaL_optlong
- luaL_optlstring
- luaL_optnumber
- luaL_optstring
- luaL_prepbuffer
- luaL_pushresult
- luaL_ref
- luaL_Reg
- luaL_register
- luaL_typename
- luaL_typerror
- luaL_unref
- luaL_where
- lua_Alloc
- lua_atpanic
- lua_call
- lua_CFunction
- lua_checkstack
- lua_close
- lua_concat
- lua_cpcall
- lua_createtable
- lua_Debug
- lua_dump
- lua_equal
- lua_error
- lua_gc
- lua_getallocf
- lua_getfenv
- lua_getfield
- lua_getglobal
- lua_gethook
- lua_gethookcount
- lua_gethookmask
- lua_getinfo
- lua_getlocal
- lua_getmetatable
- lua_getstack
- lua_gettable
- lua_gettop
- lua_getupvalue
- lua_Hook
- lua_insert
- lua_Integer
- lua_isboolean
- lua_iscfunction
- lua_isfunction
- lua_islightuserdata
- lua_isnil
- lua_isnumber
- lua_isstring
- lua_istable
- lua_isthread
- lua_isuserdata
- lua_lessthan
- lua_load
- lua_newstate
- lua_newtable
- lua_newthread
- lua_newuserdata
- lua_next
- lua_Number
- lua_objlen
- lua_pcall
- lua_pop
- lua_pushboolean
- lua_pushcclosure
- lua_pushcfunction
- lua_pushfstring
- lua_pushinteger
- lua_pushlightuserdata
- lua_pushlstring
- lua_pushnil
- lua_pushnumber
- lua_pushstring
- lua_pushthread
- lua_pushvalue
- lua_pushvfstring
- lua_rawequal
- lua_rawget
- lua_rawgeti
- lua_rawset
- lua_rawseti
- lua_Reader
- lua_register
- lua_remove
- lua_replace
- lua_resume
- lua_setallocf
- lua_setfenv
- lua_setfield
- lua_setglobal
- lua_sethook
- lua_setlocal
- lua_setmetatable
- lua_settable
- lua_settop
- lua_setupvalue
- lua_State
- lua_status
- lua_toboolean
- lua_tocfunction
- lua_tointeger
- lua_tolstring
- lua_tonumber
- lua_topointer
- lua_tostring
- lua_tothread
- lua_touserdata
- lua_type
- lua_typename
- lua_Writer
- lua_xmove
- lua_yield
- Pattern:
- Pattern Item:
- assert (v [, message])
- collectgarbage (opt [, arg])
- coroutine.create (f)
- coroutine.resume (co [, val1, ···])
- coroutine.running ()
- coroutine.status (co)
- coroutine.wrap (f)
- coroutine.yield (···)
- debug.debug ()
- debug.getfenv (o)
- debug.gethook ([thread])
- debug.getinfo ([thread,] function [, what])
- debug.getlocal ([thread,] level, local)
- debug.getmetatable (object)
- debug.getregistry ()
- debug.getupvalue (func, up)
- debug.setfenv (object, table)
- debug.sethook ([thread,] hook, mask [, count])
- debug.setlocal ([thread,] level, local, value)
- debug.setmetatable (object, table)
- debug.setupvalue (func, up, value)
- debug.traceback ([thread,] [message])
- dofile (filename)
- error (message [, level])
- getfenv (f)
- getmetatable (object)
- io.close ([file])
- io.flush ()
- io.input ([file])
- io.lines ([filename])
- io.open (filename [, mode])
- io.output ([file])
- io.popen ([prog [, mode]])
- io.read (···)
- io.tmpfile ()
- io.type (obj)
- io.write (···)
- ipairs (t)
- load (func [, chunkname])
- loadfile ([filename])
- loadstring (string [, chunkname])
- math.abs (x)
- math.acos (x)
- math.asin (x)
- math.atan (x)
- math.atan2 (x, y)
- math.ceil (x)
- math.cos (x)
- math.cosh (x)
- math.deg (x)
- math.exp (x)
- math.floor (x)
- math.fmod (x, y)
- math.frexp (x)
- math.huge
- math.ldexp (m, e)
- math.log (x)
- math.log10 (x)
- math.max (x, ···)
- math.min (x, ···)
- math.modf (x)
- math.pi
- math.pow (x, y)
- math.rad (x)
- math.random ([m [, n]])
- math.randomseed (x)
- math.sin (x)
- math.sinh (x)
- math.sqrt (x)
- math.tan (x)
- math.tanh (x)
- module (name [, ···])
- next (table [, index])
- os.clock ()
- os.date ([format [, time]])
- os.difftime (t2, t1)
- os.execute ([command])
- os.exit ([code])
- os.getenv (varname)
- os.remove (filename)
- os.rename (oldname, newname)
- os.setlocale (locale [, category])
- os.time ([table])
- os.tmpname ()
- package.cpath
- package.loaded
- package.loadlib (libname, funcname)
- package.path
- package.preload
- package.seeall (module)
- pairs (t)
- pcall (f, arg1, ···)
- print (···)
- rawequal (v1, v2)
- rawget (table, index)
- rawset (table, index, value)
- require (modname)
- select (index, ···)
- setfenv (f, table)
- setmetatable (table, metatable)
- string.byte (s [, i [, j]])
- string.char (···)
- string.dump (function)
- string.find (s, pattern [, init [, plain]])
- string.format (formatstring, ···)
- string.gmatch (s, pattern)
- string.gsub (s, pattern, repl [, n])
- string.len (s)
- string.lower (s)
- string.match (s, pattern [, init])
- string.rep (s, n)
- string.reverse (s)
- string.sub (s, i [, j])
- string.upper (s)
- table.concat (table [, sep [, i [, j]]])
- table.insert (table, [pos,] value)
- table.maxn (table)
- table.remove (table [, pos])
- table.sort (table [, comp])
- tonumber (e [, base])
- tostring (e)
- type (v)
- unpack (list [, i [, j]])
- xpcall (f, err)
- _G
- _VERSION
- file:close ()
- file:flush ()
- file:lines ()
- file:read (...)
- file:seek ([whence] [, offset])
- file:setvbuf (mode [, size])
- file:write (...)
6 - Lua Stand-alone
Although Lua has been designed as an extension language, to be embedded in a host C program, it is also frequently used as a stand-alone language. An interpreter for Lua as a stand-alone language, called simply lua
, is provided with the standard distribution. The stand-alone interpreter includes all standard libraries, including the debug library. Its usage is:
lua [options] [script [args]]
The options are:
-e stat
: executes string stat;-l mod
: "requires" mod;-i
: enters interactive mode after running script;-v
: prints version information;--
: stops handling options;-
: executesstdin
as a file and stops handling options.
After handling its options, lua
runs the given script, passing to it the given args as string arguments. When called without arguments, lua
behaves as lua -v -i
when the standard input (stdin
) is a terminal, and as lua -
otherwise.
Before running any argument, the interpreter checks for an environment variable LUA_INIT
. If its format is @filename
, then lua
executes the file. Otherwise, lua
executes the string itself.
All options are handled in order, except -i
. For instance, an invocation like
$ lua -e'a=1' -e 'print(a)' script.lua
will first set a
to 1, then print the value of a
(which is '1
'), and finally run the file script.lua
with no arguments. (Here $
is the shell prompt. Your prompt may be different.)
Before starting to run the script, lua
collects all arguments in the command line in a global table called arg
. The script name is stored at index 0, the first argument after the script name goes to index 1, and so on. Any arguments before the script name (that is, the interpreter name plus the options) go to negative indices. For instance, in the call
$ lua -la b.lua t1 t2
the interpreter first runs the file a.lua
, then creates a table
arg = { [-2] = "lua", [-1] = "-la", [0] = "b.lua", [1] = "t1", [2] = "t2" }
and finally runs the file b.lua
. The script is called with arg[1]
, arg[2]
, ··· as arguments; it can also access these arguments with the vararg expression '...
'.
In interactive mode, if you write an incomplete statement, the interpreter waits for its completion by issuing a different prompt.
If the global variable _PROMPT
contains a string, then its value is used as the prompt. Similarly, if the global variable _PROMPT2
contains a string, its value is used as the secondary prompt (issued during incomplete statements). Therefore, both prompts can be changed directly on the command line. For instance,
$ lua -e"_PROMPT='myprompt> '" -i
(the outer pair of quotes is for the shell, the inner pair is for Lua), or in any Lua programs by assigning to _PROMPT
. Note the use of -i
to enter interactive mode; otherwise, the program would just end silently right after the assignment to _PROMPT
.
To allow the use of Lua as a script interpreter in Unix systems, the stand-alone interpreter skips the first line of a chunk if it starts with #
. Therefore, Lua scripts can be made into executable programs by using chmod +x
and the #!
form, as in
#!/usr/local/bin/lua
(Of course, the location of the Lua interpreter may be different in your machine. If lua
is in your PATH
, then
#!/usr/bin/env lua
is a more portable solution.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论