Lua Programming Language
Total Page:16
File Type:pdf, Size:1020Kb
Lua Programming Language An Introduction Dr. Christian Storm TNG Big Techday 7 23rd May 2014 About Lua ■ invented as configuration and data description language ■ first version released 1993, current version is 5.2.3 ■ Lua is interpreted, dynamically typed, garbage collected, has closures, coroutines, <insert fancy stuff here>, … ■ Lua is ■ clean & simple: a designed, not evolved language ■ fast: even faster with LuaJIT see, e.g., Computer Language Benchmarks Game, Hash benchmark ■ small: liblua.so.5.2.3 is 200K, 60 source files, 14,728 lines code (C,C++,make) ■ portable: written in ANSI C/C++ ■ embeddable & extensible: C/C++, Java, C#, Perl, Python, Ruby, … ■ Lua complements C’s low level power (e.g., via inline Assembler) I high(er) level language expressibility without having to use C++ :) Lua Programming Language – An Introduction 1 awesome X11 window manager vim editor VLC Media Player http://awesome.naquadah.org http://www.vim.org http://www.videolan.org/vlc/ LuaTEX Angry Birds Nginx HTTP Server http://www.luatex.org http://www.angrybirds.com http://nginx.org Wireshark NetBSD’s Kernel Havok Engine http://www.wireshark.org http://www.netbsd.org http://www.havok.com I …scriptingalmostand and in may everywhereSnakeeven theCrytek’s other UITetris! subsystem places CryEngine-based in Adobe’s you of probably Blizzard’s Photoshop games, wouldn’t World Lightroom e.g., of expect WarcraftFarCry it … and Crysis Lua … so what? I empowers your Texas Instruments EK-LM3S to play Pong •◦◦◦◦◦◦ Lua Programming Language – An Introduction 2 awesome X11 window manager vim editor VLC Media Player http://awesome.naquadah.org http://www.vim.org http://www.videolan.org/vlc/ LuaTEX Angry Birds Nginx HTTP Server http://www.luatex.org http://www.angrybirds.com http://nginx.org Wireshark NetBSD’s Kernel Havok Engine http://www.wireshark.org http://www.netbsd.org http://www.havok.com I empowersscriptingalmostand… and in may everywhereeven theCrytek’s your other UITetris! subsystem Texas places CryEngine-based in Adobe’s Instruments you of probably Blizzard’s Photoshop EK-LM3S games, wouldn’t World Lightroom e.g., to of expect play WarcraftFarCry Pong it … and Crysis Lua … so what? I … and Snake ••◦◦◦◦◦ Lua Programming Language – An Introduction 2 awesome X11 window manager vim editor VLC Media Player http://awesome.naquadah.org http://www.vim.org http://www.videolan.org/vlc/ LuaTEX Angry Birds Nginx HTTP Server http://www.luatex.org http://www.angrybirds.com http://nginx.org Wireshark NetBSD’s Kernel Havok Engine http://www.wireshark.org http://www.netbsd.org http://www.havok.com I empowers…scriptingalmostand and in may everywhereSnake theCrytek’s your other UI subsystem Texas places CryEngine-based in Adobe’s Instruments you of probably Blizzard’s Photoshop EK-LM3S games, wouldn’t World Lightroom e.g., to of expect play WarcraftFarCry Pong it … and Crysis Lua … so what? I … and even Tetris! •••◦◦◦◦ Lua Programming Language – An Introduction 2 awesome X11 window manager vim editor VLC Media Player http://awesome.naquadah.org http://www.vim.org http://www.videolan.org/vlc/ LuaTEX Angry Birds Nginx HTTP Server http://www.luatex.org http://www.angrybirds.com http://nginx.org Wireshark NetBSD’s Kernel Havok Engine http://www.wireshark.org http://www.netbsd.org http://www.havok.com I empowers…scriptingalmostand and in may everywhereSnakeeven Crytek’s your other Tetris! Texas places CryEngine-based in Adobe’s Instruments you probably Photoshop EK-LM3S games, wouldn’t Lightroom e.g., to expect play FarCry Pong it … and Crysis Lua … so what? I scripting the UI subsystem of Blizzard’s World of Warcraft ••••◦◦◦ Lua Programming Language – An Introduction 2 awesome X11 window manager vim editor VLC Media Player http://awesome.naquadah.org http://www.vim.org http://www.videolan.org/vlc/ LuaTEX Angry Birds Nginx HTTP Server http://www.luatex.org http://www.angrybirds.com http://nginx.org Wireshark NetBSD’s Kernel Havok Engine http://www.wireshark.org http://www.netbsd.org http://www.havok.com I empowers…scriptingalmostand and in may everywhereSnakeeven the your other UITetris! subsystem Texas places in Adobe’s Instruments you of probably Blizzard’s Photoshop EK-LM3S wouldn’t World Lightroom to of expect play Warcraft Pong it … Lua … so what? I scripting Crytek’s CryEngine-based games, e.g., FarCry and Crysis •••••◦◦ Lua Programming Language – An Introduction 2 awesome X11 window manager vim editor VLC Media Player http://awesome.naquadah.org http://www.vim.org http://www.videolan.org/vlc/ LuaTEX Angry Birds Nginx HTTP Server http://www.luatex.org http://www.angrybirds.com http://nginx.org Wireshark NetBSD’s Kernel Havok Engine http://www.wireshark.org http://www.netbsd.org http://www.havok.com I empowers…scriptingand and in may Snakeeven theCrytek’s your other UITetris! subsystem Texas places CryEngine-based Instruments you of probably Blizzard’s EK-LM3S games, wouldn’t World e.g., to of expect play WarcraftFarCry Pong it … and Crysis Lua … so what? I almost everywhere in Adobe’s Photoshop Lightroom ••••••◦ Lua Programming Language – An Introduction 2 I empowers…scriptingalmost and everywhereSnakeeven theCrytek’s your UITetris! subsystem Texas CryEngine-based in Adobe’s Instruments of Blizzard’s Photoshop EK-LM3S games, World Lightroom e.g., to of play WarcraftFarCry Pong and Crysis Lua … so what? awesome X11 window manager vim editor VLC Media Player http://awesome.naquadah.org http://www.vim.org http://www.videolan.org/vlc/ LuaTEX Angry Birds Nginx HTTP Server http://www.luatex.org http://www.angrybirds.com http://nginx.org Wireshark NetBSD’s Kernel Havok Engine http://www.wireshark.org http://www.netbsd.org http://www.havok.com I and in may other places you probably wouldn’t expect it … ••••••• Lua Programming Language – An Introduction 2 Outline Lua Language Basics Syntax Data Types Statements and Control Structures Functions Closures More Advanced Lua Modules Coroutines Metatables and Metamethods OOP in Lua Lua Programming Language – An Introduction 3 Lua Language Basics – Syntax What does Lua look like? function factorial(n, ans) ans = ans and ans or 1 if ans == math.huge then print(”E: overflow”) return nil end if n ~= 0 then return factorial(n-1, n*ans) end return ans end fact = factorial(arg[1] and tonumber(arg[1]) or 0) print(fact) > lua propertailrecursionfactorial.lua 5 120 > _ Lua Programming Language – An Introduction 4 Lua Language Basics – Syntax Lua’s Syntax chunk ::= block block ::= {stat}[retstat] stat ::= ’;’ | varlist ’=’ explist | functioncall | label | break | goto Name | do block end | while exp do block end | repeat block until exp | if exp then block {elseif exp then block}[else block] end | for Name ’=’ exp ’,’ exp [’,’ exp] do block end | for namelist in explist do block end | function funcname funcbody | local function Name funcbody | local namelist [’=’ explist] retstat ::= return [explist] [’;’] label ::= ’::’ Name ’::’ funcname ::= Name {’.’ Name} [’:’ Name] varlist ::= var {’,’ var} var ::= Name | prefixexp ’[’ exp ’]’ | prefixexp ’.’ Name namelist ::= Name {’,’ Name} explist ::= exp {’,’ exp} exp ::= nil | false | true | Number | String | ’...’ | functiondef | prefixexp | tableconstructor | exp binop exp | unop exp prefixexp ::= var | functioncall | ’(’ exp ’)’ functioncall ::= prefixexp args | prefixexp ’:’ Name args args ::= ’(’ [explist] ’)’ | tableconstructor | String functiondef ::= function funcbody funcbody ::= ’(’ [parlist] ’)’ block end parlist ::= namelist [’,’ ’...’] | ’...’ tableconstructor ::= ’{’ [fieldlist] ’}’ fieldlist ::= field {fieldsep field}[fieldsep] field ::= ’[’ exp ’]’ ’=’ exp | Name ’=’ exp | exp fieldsep ::= ’,’ | ’;’ binop ::= ’+’ | ’-’ | ’*’ | ’/’ | ’^’ | ’%’ | ’..’ | ’<’ | ’<=’ | ’>’ | ’>=’ | ’==’ | ’~=’ | and | or (**) unop ::= ’-’ | not | ’#’ (**) operator precedence is missing http://www.lua.org/manual/5.2/manual.html#9 Lua Programming Language – An Introduction 5 Lua Language Basics – Syntax … compared to Python 3.4’s Syntax single_input: NEWLINE | simple_stmt | compound_stmt NEWLINE try_stmt: (’try’ ’:’ suite ((except_clause ’:’ suite)+ [’else’ ’:’ suite] file_input:(NEWLINE | stmt)* ENDMARKER [’finally’ ’:’ suite] | ’finally’ ’:’ suite)) eval_input: testlist NEWLINE* ENDMARKER with_stmt:’with’ with_item (’,’ with_item)* ’:’ suite decorator:’@’ dotted_name [ ’(’ [arglist] ’)’ ] NEWLINE with_item: test [’as’ expr] decorators: decorator+ except_clause:’except’[test [’as’ NAME]] decorated: decorators (classdef | funcdef) suite: simple_stmt | NEWLINE INDENT stmt+ DEDENT funcdef:’def’ NAME parameters [’->’ test] ’:’ suite test: or_test [’if’ or_test ’else’ test] | lambdef parameters: ’(’ [typedargslist] ’)’ test_nocond: or_test | lambdef_nocond typedargslist:(tfpdef [’=’ test] (’,’ tfpdef [’=’ test])* [’,’ lambdef:’lambda’[varargslist] ’:’ test [’*’ [tfpdef] (’,’ tfpdef [’=’ test])* [’,’ ’**’ tfpdef] | ’**’ tfpdef]] lambdef_nocond:’lambda’[varargslist] ’:’ test_nocond | ’*’ [tfpdef] (’,’ tfpdef [’=’ test])* [’,’ ’**’ tfpdef] | ’**’ tfpdef) or_test: and_test (’or’ and_test)* tfpdef: NAME [’:’ test] and_test: not_test (’and’ not_test)* varargslist:(vfpdef [’=’ test] (’,’ vfpdef [’=’ test])* [’,’ not_test:’not’ not_test | comparison [’*’ [vfpdef] (’,’ vfpdef [’=’ test])* [’,’ ’**’ vfpdef] | ’**’ vfpdef]] comparison: expr (comp_op expr)* | ’*’ [vfpdef] (’,’ vfpdef [’=’ test])* [’,’ ’**’ vfpdef] | ’**’ vfpdef) comp_op: ’<’|’>’|’==’|’>=’|’<=’|’<>’|’!=’|’in’|’not’’in’|’is’|’is’’not’ vfpdef: NAME star_expr: ’*’ expr stmt: simple_stmt | compound_stmt expr: xor_expr (’|’ xor_expr)* simple_stmt: small_stmt