Lua - RIP Tutorial · PDF file from: lua It is an unofficial and free Lua ebook created for...

Click here to load reader

  • date post

    04-Oct-2020
  • Category

    Documents

  • view

    0
  • download

    0

Embed Size (px)

Transcript of Lua - RIP Tutorial · PDF file from: lua It is an unofficial and free Lua ebook created for...

  • Lua

    #lua

  • 1

    1: Lua 2

    2

    2

    Examples 3

    3

    4

    Lua 5

    7

    7

    7

    nil 7

    8

    8

    8

    8

    8

    9

    for 9

    9

    9

    PITFALL 9

    10

    10

    2: Lua C API 12

    12

    12

    Examples 12

    Lua 12

    Lua 13

    APILuaLua 13

  • 15

    15

    15

    15

    3: Lua 17

    17

    Examples 17

    17

    17

    17

    17

    18

    18

    18

    18

    19

    19

    ''' 20

    20

    / 20

    20

    20

    return 21

    21

    4: PICO-8 22

    22

    Examples 22

    22

    22

    23

    5: 25

  • 25

    25

    25

    Examples 25

    metatables 25

    25

    - __gc 26

    26

    27

    27

    27

    28

    28

    OOP 29

    6: 31

    31

    31

    Examples 31

    31

    32

    32

    32

    32

    32

    33

    33

    34

    34

    35

    36

    36

    36

  • 7: 38

    38

    38

    Examples 38

    38

    8: 41

    41

    41

    Examples 41

    41

    9: 42

    42

    42

    Examples 42

    Lua 42

    string.find 44

    find 44

    44

    `gmatch` 45

    45

    45

    gsub 46

    46

    46

    46

    46

    10: 47

    47

    Examples 47

    47

    48

  • 11: 49

    49

    49

    Examples 49

    49

    50

    50

    52

    52

    53

    53

    12: 55

    55

    55

    55

    Examples 55

    55

    56

    13: 58

    Examples 58

    58

    58

    58

    58

    Ipairs 59

    59

    Prime Number 59

    60

    60

    60

    61

  • 14: 62

    Examples 62

    pcall 62

    Lua 63

    15: 64

    Examples 64

    64

    64

    64

    64

    64

    64

    64

    16: 66

    66

    66

    Examples 66

    66

    metamethods 66

    68

  • You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: lua

    It is an unofficial and free Lua ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official Lua.

    The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners.

    Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected]

    https://riptutorial.com/zh-TW/home 1

    http://riptutorial.com/ebook/lua https://archive.org/details/documentation-dump.7z mailto:[email protected]

  • 1: Lua

    Lua。PUC-Rio。 。

    Lua。

    LuaJIT。

    1.0 。 1993728

    1.1 。 。 199478

    2.1 Lua 2.1Lua。 。 199527

    2.2 19951128

    2.4 luac 1996514

    2.5 vararg。 1996-11-19

    3.0 auxlibLua 1997-07-01

    3.1 “upvalues”。 1998711

    3.2 199978

    3.2.2 2000222

    4 “for”API。 2000116

    4.0.1 200274

    5 。 2003-04-11

    5.0.3 2006-06-26

    5.1 luaconf.h。 2006-02-21

    5.1.5 2012-02-17

    https://riptutorial.com/zh-TW/home 2

    https://i.stack.imgur.com/46fGZ.gif https://www.lua.org/ http://www.puc-rio.br/ http://www.puc-rio.br/ http://www.puc-rio.br/ http://www.puc-rio.br/ http://www.puc-rio.br/ http://www.puc-rio.br/ http://www.puc-rio.br/ http://www.puc-rio.br/ https://www.lua.org/authors.html https://www.lua.org/authors.html https://www.lua.org/lua-l.html https://www.lua.org/lua-l.html https://www.lua.org/lua-l.html https://www.lua.org/lua-l.html http://luajit.org/ https://www.lua.org/semish94.html https://www.lua.org/semish94.html https://www.lua.org/semish94.html https://www.lua.org/semish94.html https://www.lua.org/spe.html https://www.lua.org/spe.html https://www.lua.org/spe.html https://www.lua.org/spe.html

  • 5.2 goto。 2011-12-16

    5.2.4 201537

    5.3 UTF-832/64。 2015-01-12

    5.3.4 。 2017112

    Examples

    LuaGNU / Linux。

    DebianUbuntu

    sudo apt-get install lua50

    sudo apt-get install lua51

    sudo apt-get install lua52

    WindowsMacOSSourceForge。

    AppleHomebrewLua

    brew install lua

    Homebrew5.2.4,5.3Homebrew / 。

    。。Linux

    $ wget http://lua.org/ftp/lua-5.3.3.tar.gz $ echo "a0341bc3d1415b814cc738b2ec01ae56045d64ef ./lua-5.3.3.tar.gz" | sha1sum -c - $ tar -xvf ./lua-5.3.3.tar.gz $ make -C ./lua-5.3.3/ linux

    tarball make 。 。

    。linux 。solaris aix bsd freebsd macosx mingw。doc/readme.html 。 README 。

    coroutine - • debug - • io - IO• package - • string - Lua• table - Lua - • os - OS• utf8 - UTF-8Lua 5.3•

    https://riptutorial.com/zh-TW/home 3

    http://luabinaries.sourceforge.net/ http://luabinaries.sourceforge.net/ http://brew.sh/ https://github.com/Homebrew/homebrew-versions https://github.com/Homebrew/homebrew-versions https://github.com/Homebrew/homebrew-versions https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html https://www.lua.org/download.html

  • Lua。LuaRocks Lua ToolboxLuaDist。Lua Wiki。

    Lua--

    -- this is single line comment -- need another line -- huh?

    --[[]]

    --[[ This is block comment. So, it can go on... and on... and on.... ]]

    ;

    --[=[ This is also a block comment We can include "]]" inside this comment --]=] --[==[ This is also a block comment We can include "]=]" inside this comment --]==]

    --[[--]]

    --[[ print'Lua is lovely' --]]

    -

    ---[[ print'Lua is lovely' --]]

    --print。

    ---[[ print 'Lua is love' --[=[]] print 'Lua is life' --]=]

    -

    https://riptutorial.com/zh-TW/home 4

    https://luarocks.org/ https://lua-toolbox.com/ http://luadist.org/ http://luadist.org/ http://luadist.org/ http://luadist.org/ http://luadist.org/ http://luadist.org/ http://lua-users.org/wiki/

  • --[[ print 'Lua is love' --[=[]] print 'Lua is life' --]=]

    Lua

    Lua

    lua - shell• luac - •

    bottles_of_mate.lua

    local string = require "string" function bottle_take(bottles_available) local count_str = "%d bottles of mate on the wall." local take_str = "Take one down, pass it around, " .. count_str local end_str = "Oh noes, " .. count_str local buy_str = "Get some from the store, " .. count_str local bottles_left = 0 if bottles_available > 0 then print(string.format(count_str, bottles_available)) bottles_left = bottles_available - 1 print(string.format(take_str, bottles_left)) else print(string.format(end_str, bottles_available)) bottles_left = 99 print(string.format(buy_str, bottles_left)) end return bottles_left end local bottle_count = 99 while true do bottle_count = bottle_take(bottle_count) end

    shell

    $ lua bottles_of_mate.lua

    Get some from the store, 99 bottles of mate on the wall. 99 bottles of mate on the wall. Take one down, pass it around, 98 bottles of mate on the wall. 98 bottles of mate on the wall. Take one down, pass it around, 97 bottles of mate on the wall. 97 bottles of mate on the wall. ... ...

    https://riptutorial.com/zh-TW/home 5

  • 3 bottles of mate on the wall. Take one down, pass it around, 2 bottles of mate on the wall. 2 bottles of mate on the wall. Take one down, pass it around, 1 bottles of mate on the wall. 1 bottles of mate on the wall. Take one down, pass it around, 0 bottles of mate on the wall. Oh noes, 0 bottles of mate on the wall. Get some from the store, 99 bottles of mate on the wall. 99 bottles of mate on the wall. Take one down, pass it around, 98 bottles of mate on the wall. ...

    shellLua

    $ luac bottles_of_mate.lua -o bottles_of_mate.luac

    $ luac -l bottles_of_mate.lua main (13 instructions, 52 bytes at 0x101d530) 0+ params, 4 slots, 0 upvalues, 2 locals, 4 constants, 1 function 1 [1] GETGLOBAL 0 -1 ; require 2 [1] LOADK 1 -2 ; "string" 3 [1] CALL 0 2 2 4 [22] CLOSURE 1 0 ; 0x101d710 5 [22] MOVE 0 0 6 [3] SETGLO