tim lua 2012
TRANSCRIPT
-
7/29/2019 Tim Lua 2012
1/47
Lua introduction for newprogrammers
Download slides at:http://tstarling.com/presentations
-
7/29/2019 Tim Lua 2012
2/47
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
-
7/29/2019 Tim Lua 2012
3/47
if
if colour == 'black'thencssColour = '#000'
end
-
7/29/2019 Tim Lua 2012
4/47
if
if colour == 'black'thencssColour = '#000'
elsecssColour = colour
end
-
7/29/2019 Tim Lua 2012
5/47
if
if colour == 'black'thencssColour = '#000'
elseif colour == 'white'thencssColour = '#fff'
elsecssColour = colour
end
-
7/29/2019 Tim Lua 2012
6/47
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
-
7/29/2019 Tim Lua 2012
7/47
for
f = 1
for i = 1, 5dof = f * iendreturn'The factorial of 5 is ' .. f
-
7/29/2019 Tim Lua 2012
8/47
Types
String
Number
Boolean (true/false)
nil
A few other things
-
7/29/2019 Tim Lua 2012
9/47
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
ifnot hungry then return'nothing'end
-
7/29/2019 Tim Lua 2012
10/47
Functions
Calling functions
Defining functions
colour = getDivColour()
localfunction getDivColour()
return'blue'end
-
7/29/2019 Tim Lua 2012
11/47
Functions
Functions let you avoid duplication
Functions can have arguments:
localfunction plural(word) return word .. 's'end
German version left as an exercise to thereader
-
7/29/2019 Tim Lua 2012
12/47
Functions
Two types of functions Local functions for private use within the
module
Exported functions
localfunction plural(word) return word .. 's'end
local p = {}function p.hello() return'hello'endreturn p
-
7/29/2019 Tim Lua 2012
13/47
Local variables
function getDivStart()colour = getDivColour()
return''
end
colour = 'Fuschia'
return getDivStart() .. colour .. ''
-
7/29/2019 Tim Lua 2012
14/47
Local variables
function getDivStart() local colour = getDivColour()
return''
end
colour = 'Fuschia'
return getDivStart() .. colour .. ''
-
7/29/2019 Tim Lua 2012
15/47
Local variables
If you don't set a variable to something, it willbe nil by default
local x
if ready thenx = 'GO!'
end-- Now x has "GO!"or nil
-
7/29/2019 Tim Lua 2012
16/47
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
-
7/29/2019 Tim Lua 2012
17/47
Numbered tables
africanFlatbreads = { 'Aish Mehahra', 'Injera',
'Lahoh', 'Ngome'}
return africanFlatbreads[2] -- returns 'Injera'
-
7/29/2019 Tim Lua 2012
18/47
Visiting each table element
pairs: key/value pairs in random order
ipairs: Numeric keys in ascending order
for name, number inpairs(numbers) do...
end
for index, bread inipairs(africanFlatbreads) do...
end
-
7/29/2019 Tim Lua 2012
19/47
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'
-
7/29/2019 Tim Lua 2012
20/47
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
-
7/29/2019 Tim Lua 2012
21/47
Lua introduction for programmers
-
7/29/2019 Tim Lua 2012
22/47
Lexical
Comments reminiscent of SQL
Line breaks ignored
Semicolons to terminate statements optional, discouraged
-- Single line comment
--[[
long comment--]]
-
7/29/2019 Tim Lua 2012
23/47
Data types
nil
Numbers
Single type, floating point
Strings
8-bit clean
boolean
-
7/29/2019 Tim Lua 2012
24/47
Data types
Functions
First class values
Return multiple values
Multiple return values are not bundled into a datatype
Anonymous syntax:
x = function ()...
end
-
7/29/2019 Tim Lua 2012
25/47
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']
-
7/29/2019 Tim Lua 2012
26/47
Operators
Not equals: ~= instead of!=
Concatenation: ..
Length: #
Logical: and, or, not
Exponentiation: ^
Usual meanings: , =, ==, +, -, *, /, %
-
7/29/2019 Tim Lua 2012
27/47
Operator omissions
No assignment operators like +=
Even plain = is not really an operator
No bitwise operators
No ternary ?:
-
7/29/2019 Tim Lua 2012
28/47
Assignment
Like BASIC, assignment is a complete statement, not anexpression
Multiple assignment:
Assignment with local variable declaration:
a, b = c, d
a, b = foo()
Not like this!
local a, b = c, d
local a = b, c = d
-
7/29/2019 Tim Lua 2012
29/47
Control structures Explicit block
Precondition loop
Postcondition loop
do...
end
while cond do...
end
repeat...
until cond
-
7/29/2019 Tim Lua 2012
30/47
Control structures
If
if cond then...
elseif cond then
...else
...end
-
7/29/2019 Tim Lua 2012
31/47
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
-
7/29/2019 Tim Lua 2012
32/47
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
-
7/29/2019 Tim Lua 2012
33/47
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()
-
7/29/2019 Tim Lua 2012
34/47
Objects
Factory function style example
function newObj() local private = 1
local obj = {}
function obj:getPrivate() return private end
return objend
-
7/29/2019 Tim Lua 2012
35/47
Metatables
Each table may have an attached metatable Provides operator overloading
"index" metatable entry is used for object
inheritance and prototype-based OOP
-
7/29/2019 Tim Lua 2012
36/47
MediaWiki/Lua interface
-
7/29/2019 Tim Lua 2012
37/47
Module namespace
All Lua code will be inside the Modulenamespace
Code editor provided
"ace" JavaScript code editor Automatic indenting
Syntax highlighting
-
7/29/2019 Tim Lua 2012
38/47
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
-
7/29/2019 Tim Lua 2012
39/47
Module structure
A module is a Lua chunk that returns an exporttable
require() provided
not isolated package library provided
R l
-
7/29/2019 Tim Lua 2012
40/47
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
F th d
-
7/29/2019 Tim Lua 2012
41/47
Frame methods
Argument access: args
argumentPairs()
getParent()
Provides access to the parent frame, i.e. thearguments to the template which called #invoke
local name1 = frame.args.name1
local t = {}for name, value in frame:argumentPairs() do
t[name] = valueend
F th d
-
7/29/2019 Tim Lua 2012
42/47
Frame methods
Wikitext preprocessing
Structured template invocation
frame:preprocess('{{template}}')
frame:expandTemplate{ title = 'template',
args = {foo = foo}}
A idi d bl i
-
7/29/2019 Tim Lua 2012
43/47
Avoiding double-expansion
Arguments are already expanded Don't construct preprocessor input from
arguments
Use frame:expandTemplate()
F t di ti
-
7/29/2019 Tim Lua 2012
44/47
Future directions
Gabriel Wicke's interface: frame:getArgument()
frame:newParserValue()
frame:newTemplateParserValue() All provided but only with stub functionality
F t di ti
-
7/29/2019 Tim Lua 2012
45/47
Future directions
Interwiki module invocation Languages other than Lua
Date/time functions
Direct access to other core parser functionsand variables
{{PAGENAME}}
{{#ifexist:}} etc.
Further reading
-
7/29/2019 Tim Lua 2012
46/47
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
Try it out
-
7/29/2019 Tim Lua 2012
47/47
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]end
return p