Lua 5.3 ‚考‰‹†Œ

  • View
    272

  • Download
    3

Embed Size (px)

DESCRIPTION

Lua 5.3 参考手册作者 Roberto Ierusalimschy, Luiz Henrique de Figueiredo, Waldemar Celes译者 云风

Text of Lua 5.3 ‚考‰‹†Œ

  • lua

    www.lua.org

  • \0

    luaconf.h LUA_32BITS

  • 0/0

    a.name a["name"]

    i ja[i] a[j]

    1.0 == 1

    a[2.0] = true 22

    type

    var_ENV.var

  • _ENV_ENV

    _ENV_ENV

    _ENV

    _ENV

    _G_G

    _ENVload

    load loadfile

    lua

    errorpcall xpcall

    xpcall lua_pcall

  • __add

    "add"

    getmetatable

    setmetatable

    ____add

    obj

    rawget(getmetatable(obj) or {}, "__" .. event_name)

  • +__add

    -*

    /%^-

    //&

    |~~

    >

    ..

    #

    ==

    // == ~= = < > = ( ) { } [ ] :: ; : , . .. ...

    \a \b \f \n\r \t \v \\

    \" \'\z

  • \0\xXX

    \ddd

    \u{XXX}

    [[ [=[]====]

    a1

    a = 'alo\n123"' a = "alo\n123\"" a = '\97lo\10\04923"' a = [[alo 123"]] a = [==[ alo 123"]==]

    e E 0x0X

    p P

  • 3 345 0xff 0xBEBADA

    3.0 3.1416 314.16e-2 0.31416E1 34e1 0x0.1E 0xA23p-4 0X1.921FB54442D18P+1

    -- --

    var ::= Name

    var ::= prefixexp [ exp ]

    t[i] gettable_event(t,i)gettable_event

    var.Name var["Name"]

  • var ::= prefixexp . Name

    x _ENV.x_ENV

    block ::= {stat}

    stat ::= ;

    a = b + c (print or io.write)('done')

    a = b + c(print or io.write)('done') a = b + c; (print or io.write)('done')

    ;(print or io.write)('done')

    stat ::= do block end

  • chunk ::= block

    _ENV _ENV

    luacstring.dump

    load

    stat ::= varlist = explistvarlist ::= var {, var}explist ::= exp {, exp}

    i = 3 i, a[i] = i+1, 20

    a[3] a[4] a[i] i

  • x, y = y, x

    x y

    x, y, z = y, z, x

    x y z

    t[i]= val settable_event(t,i,val)

    settable_event

    x = val _ENV.x = val

    stat ::= while exp do block endstat ::= repeat block until expstat ::= if exp then block {elseif exp then block} [else block]

    stat ::= goto Namestat ::= labellabel ::= :: Name ::

  • stat ::= break

    stat ::= return [explist] [;]

    doreturn end

    stat ::= for Name = exp , exp [, exp] do block end

    for v = e1, e2, e3 do block end

    do local var, limit, step = tonumber(e1), tonumber(e2), tonumber(e3 if not (var and limit and step) then error() end var = var - step while true do var = var + step if (step >= 0 and var > limit) or (step < 0 and var < limit) then break

  • end local v = var block end end

    var limit step

    v

    stat ::= for namelist in explist do block endnamelist ::= Name {, Name}

    for var_1, , var_n in explist do block end

    do local f, s, var = explist while true do local var_1, , var_n = f(s, var) if var_1 == nil then break end var = var_1 block end end

    explist

    f s var

  • var_i

    stat ::= functioncall

    stat ::= local namelist [= explist]

    exp ::= prefixexpexp ::= nil | false | trueexp ::= Numeralexp ::= LiteralStringexp ::= functiondefexp ::= tableconstructorexp ::= ...exp ::= exp binop expexp ::= unop expprefixexp ::= var | functioncall | ( exp )

  • ...

    f() -- 0 g(f(), x) -- f() g(x, f()) -- g x f() a,b,c = f(), x -- f() 1 c nil a,b = ... -- a -- b -- a b nil a,b,c = x, f() -- f() 2 a,b,c = f() -- f() 3 return f() -- f() return ... -- parameters return x,y,f() -- x, y, f() {f()} -- f() {...} -- {f(), nil} -- f()

    (f(x,y,z)) f(f(x,y,z)) f

    f

    +

  • -*///%^-

    /pow

    //

    &|~>>

  • formatstring.format

    ==~==

    ==

  • "0"==0t[0] t["0"]

    ~= ==

    a > b b < a a >= b b 10 10 or error() --> 10 nil or "a" --> "a" nil and 10 --> nil false and error() --> false false and nil --> false false or nil --> nil 10 and 20 --> 20

    -->

  • ..

    __concat

    #

    __len

    __len t

    {10, 20, nil, 40}

    4 3

    or and < > = ~= == | ~ & > .. + - * / // % unary operators (not # - ~) ^

    ..^

  • tableconstructor ::= { [fieldlist] }fieldlist ::= field {fieldsep field} [fieldsep]field ::= [ exp ] = exp | Name = exp | expfieldsep ::= , | ;

    [exp1] = exp2 exp1exp2 name = exp ["name"] = exp

    exp [i] = exp i

    a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }

    do local t = {} t[f(1)] = g t[1] = "x" -- 1st exp t[2] = "y" -- 2nd exp t.x = 1 -- t["x"] = 1 t[3] = f(x) -- 3rd exp t[30] = 23 t[4] = 45 -- 4th exp a = t end

    exp

    functioncall ::= prefixexp args

  • functioncall ::= prefixexp : Name args

    v:name(args)v.name(v,args) v

    args ::= ( [explist] )args ::= tableconstructorargs ::= LiteralString

    f{fields}f({fields})f'string' f"string" f[[string]]

    f('string')

    return functioncall

    return (f(x)) -- return 2 * f(x) return x, f(x) -- f(x); return -- return x or f(x) --

    functiondef ::= function funcbodyfuncbody ::= ( [parlist] ) block end

  • stat ::= function funcname funcbodystat ::= local function Name funcbodyfuncname ::= Name {. Name} [: Name]

    function f () body end

    f = function () body end

    function t.a.b.c.f () body end

    t.a.b.c.f = function () body end

    local function f () body end

    local f; f = function () body end

    local f = function () body end

    f

    parlist ::= namelist [, ...] | ...

    ...

  • function f(a, b) end function g(a, b, ...) end function r() return 1,2,3 end

    CALL PARAMETERS f(3) a=3, b=nil f(3, 4) a=3, b=4 f(3, 4, 5) a=3, b=4 f(r(), 10) a=1, b=10 f(r()) a=1, b=2 g(3) a=3, b=nil, ... --> (nothing) g(3, 4) a=3, b=4, ... --> (nothing) g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 g(5, r()) a=5, b=1, ... --> 2 3

    self

    function t.a.b.c:f (params) body end

    t.a.b.c.f = function (self, params) body end

  • x = 10 -- do -- local x = x -- 'x', 10 print(x) --> 10 x = x+1 do -- local x = x+1 -- 'x' print(x) --> 12 end print(x) --> 11 end print(x) --> 10

    local x = x xx

    a = {} local x = 20 for i=1,10 do local y = 0 a[i] = function () y=y+1; return x+y end end

    y x

    lua.h

  • LUA_USE_APICHECK

    lua_CFunction

    lua_checkstack

    LUA_MINSTACKLUA_MINSTACK

    lua_call

    lua_checkstack

  • 1 abs(index) top

    LUA_TNONE

    lua_pushcclosure

    lua_upvalueindexlua_upvalueindex(1)

    lua_upvalueindex(n)

  • LUA_REGISTRYINDEX

    luaL_ref

    lua.h

    LUA_RIDX_MAINTHREAD

    LUA_RIDX_GLOBALS

    longjmp

    LUAI_THROW

    setjmp

    lua_atpanic abort

    lua_error

  • longjmpfoo

    longjmpfoo

    lua_yieldk lua_callk lua_pcallkk

    lua_yieldk lua_callk lua_pcallk

    int original_function (lua_State *L) { ... /* code 1 */ status = lua_pcall(L, n, m, h); /* calls Lua */ ... /* code 2 */ }

    lua_pcall

    int k (lua_State *L, int status, lua_KContext ctx) { ... /* code 2 */ } int original_function (lua_State *L) { ... /* code 1 */ return k(L, lua_pcall(L, n, m, h), ctx); }

  • klua_KFunction lua_pcall

    lua_pcallk

    lua_pcall lua_pcallk

    int original_function (lua_State *L) { ... /* code 1 */ return k(L, lua_pcallk(L, n, m, h, ctx2, k), ctx1); }

    lua_pcallklua_callk

    lua_pcallk ctxlua_pcallk

    lua_pcallkLUA_YIELD LUA_OK

    lua_yieldk lua_callkLUA_YIELD

    lua_callkLUA_OK lua_yieldk

    lua_yieldk

    lua_callk

    op

    x|y

  • x y ?

    x -e v

    lua_absindex

    int lua_absindex (lua_State *L, int idx);

    idx

    lua_Alloc

    typedef void * (*lua_Alloc) (void *ud, void *ptr, size_t osize, size_t nsize);

    realloc udlua_newstate ptr

    osizensize

    ptr NULL osize ptr

    ptr NULL osizeosize

    LUA_TSTRING LUA_TTABLE LUA_TFUNCTIONLUA_TUSERDATA LUA_TTHREAD osize

    nsize free NULL

    nsize reallocNULL osize >= nsize

    luaL_newstate

  • static void *l_alloc (void *ud, void *ptr, size_t osize, size_t nsize) { (void)ud; (void)osize; /* not used */ if (nsize == 0) { free(ptr); return NULL; } else return realloc(ptr, nsize); }

    free(NULL) realloc(NULL,size)malloc(size) realloc

    lua_arith

    void lua_arith (lua_State *L, int op);

    op