Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua...

47
Lua introduction for new programmers Download slides at: http://tstarling.com/presentations

Transcript of Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua...

Page 1: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Lua introduction for new programmers

Download slides at:http://tstarling.com/presentations

Page 2: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Hello world

● In [[Module:Hello]] put:

● Then in a wiki page:{{#invoke: Hello | hello }}

● Try it now at http://scribunto.wmflabs.org/

local p = {}function p.hello() return 'Hello, world!'endreturn p

Page 3: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

if

if colour == 'black' then cssColour = '#000'end

Page 4: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

if

if colour == 'black' then cssColour = '#000'else cssColour = colourend

Page 5: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

if

if colour == 'black' then cssColour = '#000'elseif colour == 'white' then cssColour = '#fff'else cssColour = colourend

Page 6: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Equality

if x = y then -- Error: 'then' expected near '=' return 'something'end

if x == y then return 'something'end

● Single equals is only for assignment

● Use double equals for equality

Page 7: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

for

f = 1for i = 1, 5 do f = f * iendreturn 'The factorial of 5 is ' .. f

Page 8: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Types

● String● Number● Boolean (true/false)● nil● A few other things

Page 9: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Logic

● and: both are true

● or: one or the other or both

● not: the following thing is false

if beans and toast then return 'breakfast'end

if chicken or beef then return 'dinner'end

if not hungry then return 'nothing'end

Page 10: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Functions

● Calling functions

● Defining functions

colour = getDivColour()

local function getDivColour() return 'blue'end

Page 11: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Functions

● Functions let you avoid duplication● Functions can have arguments:

local function plural(word) return word .. 's'end

● German version left as an exercise to the reader

Page 12: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Functions● Two types of functions● Local functions for private use within the

module

● Exported functions

local function plural(word) return word .. 's'end

local p = {}function p.hello() return 'hello'endreturn p

Page 13: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Local variables

function getDivStart() colour = getDivColour() return '<div style="background-color: ' .. colour .. '">'end

colour = 'Fuschia'return getDivStart() .. colour .. '</div>'

Page 14: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Local variables

function getDivStart() local colour = getDivColour() return '<div style="background-color: ' .. colour .. '">'end

colour = 'Fuschia'return getDivStart() .. colour .. '</div>'

Page 15: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Local variables

● If you don't set a variable to something, it will be nil by default

local xif ready then x = 'GO!'end-- Now x has "GO!" or nil

Page 16: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Tables

● Creating a table

● Accessing a table element

numbers = { one = 1, two = 2, three = 3}

return numbers.one -- returns 1return numbers['one'] -- also returns 1

Page 17: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Numbered tables

africanFlatbreads = { 'Aish Mehahra', 'Injera', 'Lahoh', 'Ngome'}

return africanFlatbreads[2] -- returns 'Injera'

Page 18: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Visiting each table element

● pairs: key/value pairs in random order

● ipairs: Numeric keys in ascending order

for name, number in pairs(numbers) do ...end

for index, bread in ipairs(africanFlatbreads) do ...end

Page 19: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Strings

● Length

● sub

s = 'hello'return #s -- returns 5

s = 'hello'return s:sub(2, 3) -- returns 'el'return s:sub(2) -- returns 'ello'return s:sub(-2) -- returns 'lo'

Page 20: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Further reading

● Programming in Lua: http://www.lua.org/pil/● Reference manual:

http://www.lua.org/manual/5.2/● Scribunto:

https://www.mediawiki.org/wiki/Extension:Scribunto

● lua-users.org

Page 21: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Lua introduction for programmers

Page 22: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Lexical

● Comments reminiscent of SQL

● Line breaks ignored● Semicolons to terminate statements

– optional, discouraged

-- Single line comment

--[[long comment --]]

Page 23: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Data types

● nil● Numbers

– Single type, floating point

● Strings– 8-bit clean

● boolean

Page 24: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Data types

● Functions– First class values

– Return multiple values

– Multiple return values are not bundled into a data type

– Anonymous syntax:

x = function () ...end

Page 25: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Data types

● Tables– Implemented as a hashtable

– Used for OOP, like JavaScript

– Literal syntax: {name = value} or {a, b, c}

– Access with foo.bar or foo['bar']

Page 26: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Operators

● Not equals: ~= instead of !=● Concatenation: ..● Length: #● Logical: and, or, not● Exponentiation: ^● Usual meanings: <, >, <=, >=, ==, +, -, *, /, %

Page 27: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Operator omissions

● No assignment operators like +=– Even plain = is not really an operator

● No bitwise operators● No ternary ?:

Page 28: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Assignment

● Like BASIC, assignment is a complete statement, not an expression

● Multiple assignment:

● Assignment with local variable declaration:

a, b = c, da, b = foo()

● Not like this!

local a, b = c, d

local a = b, c = d

Page 29: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Control structures● Explicit block

● Precondition loop

● Postcondition loop

do ...end

while cond do ...end

repeat ...until cond

Page 30: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Control structures

● If

if cond then ...elseif cond then ...else ...end

Page 31: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Control structures

● Numeric for

● Generic for

● Index variable is local to the loop

for i = start, stop do ...end

for i in iterator do ...end

Page 32: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Variables

● Lexical scoping, almost identical to JavaScript● An unset variable is identical to a nil variable

– No special syntax for deletion, just x = nil

– No error raised for access to undefined variables

Page 33: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Objects

● Made from tables using a variety of syntaxes, similar to JavaScript

● Private member variables implemented using lexical scoping, as in JavaScript

● Dot for static methods: obj.func()● Colon for non-static methods: obj:func()

Page 34: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Objects

● Factory function style example

function newObj() local private = 1 local obj = {}

function obj:getPrivate() return private end

return objend

Page 35: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Metatables

● Each table may have an attached metatable● Provides operator overloading● "index" metatable entry is used for object

inheritance and prototype-based OOP

Page 36: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

MediaWiki/Lua interface

Page 37: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Module namespace

● All Lua code will be inside the Module namespace

● Code editor provided– "ace" JavaScript code editor

– Automatic indenting

– Syntax highlighting

Page 38: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Invocation

● {{ #invoke: module_name | function_name | arg1 | arg2 | name1 = value1 }}

● #invoke instances are isolated, globals defined in one are not available in another

● Only caches are shared

Page 39: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Module structure

● A module is a Lua chunk that returns an export table

● require() provided– not isolated

● package library provided

Page 40: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Return value

● The exported function returns a wikitext string● Multiple return values are concatenated● Non-string return values are converted to

string– Metatable entry "tostring" supported

Page 41: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Frame methods

● Argument access: args

● argumentPairs()

● getParent()– Provides access to the parent frame, i.e. the

arguments to the template which called #invoke

local name1 = frame.args.name1

local t = {}for name, value in frame:argumentPairs() do t[name] = valueend

Page 42: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Frame methods

● Wikitext preprocessing

● Structured template invocation

frame:preprocess('{{template}}')

frame:expandTemplate{ title = 'template', args = {foo = foo}}

Page 43: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Avoiding double-expansion

● Arguments are already expanded● Don't construct preprocessor input from

arguments● Use frame:expandTemplate()

Page 44: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Future directions

● Gabriel Wicke's interface:– frame:getArgument()

– frame:newParserValue()

– frame:newTemplateParserValue()

● All provided but only with stub functionality

Page 45: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Future directions

● Interwiki module invocation● Languages other than Lua● Date/time functions● Direct access to other core parser functions

and variables– {{PAGENAME}}

– {{#ifexist:}}

– etc.

Page 46: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Further reading

● Programming in Lua: http://www.lua.org/pil/● Reference manual:

http://www.lua.org/manual/5.2/● Scribunto:

https://www.mediawiki.org/wiki/Extension:Scribunto

● lua-users.org

Page 47: Lua introduction for new programmers - Tim Starling's blog Lua 2012.pdf · 2012. 6. 2. · Lua introduction for programmers. Lexical ... A module is a Lua chunk that returns an export

Try it out

● Go to http://scribunto.wmflabs.org/● Create a function that takes several

arguments and does something to them

local p = {}function p.hello(frame) return 'Hello ' .. frame.args[1]endreturn p