SlideShare a Scribd company logo
Hands-on Lua
Introductory course
Facts
• Developed at PUC Rio by Roberto
Lerusalimschy et al
• Born as a C/C++ extension language
• Incorporates most innovations of RAD
languages like Python, Ruby…
• Easily embeddable, lightweight and
efficient
• Totally free and open license
Chunks
• “a=1” in interactive mode
– each line in interactive mode is a chunck
• echo “a=1;b=2” > chunk.lua
– file chunk.lua is a chunk
• Running a chunk:
– “lua –lchunk” from command prompt
– “lua chunk.lua arg1 arg2” to pass command-line arguments
– dofile(“chunk.lua”) from within another chunk
Globals
• Define&use anywhere:
– print(b) -- nil
– b=10
– print(b) -- 10
• To undefine:
– b=nil
The interpreter
• Passing chunks:
– lua –lchunk –e “a=math.sqrt(b)”
chunk.lua
• Passing command-line arguments:
– lua –i –e “c=‘Hi !’” chunk.lua 1 2 3
– print(arg[1])
– print(arg[2])
– print(arg[3])
– print(arg[-1]) --???!!!
Types and Values
• From within the interpreter:
– print(type(10))
• You can’t help trying these:
– print(type(nil))
– print(type(print)) --???!!!
Types and Values
• 6 built-in types:
– nil (nil)
– boolean (true | false)
– number (any real number, usual formats
allowed)
– string (any char sequence, 0 allowed!)
– table (more about these later)
– function (first-class values)
• 2 utility types:
– userdata
– thread
Tables
• From within the interpreter:
– t = {}
– t[“key”] = “value”; print(t[“key”])
– print(t[“otherkey”])
– t[1] = “first”; print(t[1]); print(t[“key”])
– print(t.key) --???!!!
– t = { key=“value”, otherkey=“first” }
– t = { key=“value”, 1=“first” } --???!!!
– t = { key=“value”, [1]=“first” }
– print(t.1) --???!!!
Arrays
• From within the interpreter:
– t[1] = ‘a’; t[2] = ‘b’; t[3] = ‘c’
– print(t[1] .. t[2] .. t[3])
– print(t[-1]) (got what you expected?)
– t[-1] = ‘z’
– print(t[-1])
– print(t) --???!!!
– print(unpack(t))
Functions
• From within the interpreter:
– function f ( a ) return a^2 end
– print(f(2))
• Functions are first-class values:
– t = {}
– t[“square”] = f
– print(t.square(2))
Some math…
• Arithmetic operators:
+ - * / -()
• Relational operators:
< > <= >= == ~=
• Logical operators:
and or not
– false and anything == false; true and
anything == anything
– nil and anything == false
– true or anything == true; false or anything
== anything
– nil or anything == anything
Assignments
• From within the interpreter:
– a = “Hi “ .. “there!”
– a, b = 10, a
– print(a, b)
– a, b = b, a
– print(a, b)
– a, b, c = 0
– print(a, b, c) --???!!!
Locals, blocks and scope
• From within the interpreter:
– a = 10
– local b = 2*a
– print(a, b) --???!!!
– do local b = 2*a; print(a,b) end
• What is a block?
– a chunk is a block
– a control sentence is a block
– a function body is a block
if then else
• From within the interpreter:
– if a < 0 then a = -a end
– if a == 1 then
– print(“first”)
– else
– if a < 0 then
– error(“outofrange”)
– else
– print(a)
– end
– end
while and repeat
• From within the interpreter:
– while a > 0 do
– print(a)
– a = a – 1
– end
– repeat
– print(a)
– a = a + 1
– until a > 10
for
• From within the interpreter:
– for i=1,10,2 do print(i) end
– pets = { “cat”, “dog”, “bunny” }
– for i,pet in pairs(pets) do print(i, pet) end
– for i,pet in ipairs(pets) do print(i, pet) end
– pets[“ops”] = “lion”
– for i,pet in pairs(pets) do print(i, pet) end
– for i,pet in ipairs(pets) do print(i, pet) end
break and return
• From within the interpreter:
– for i,pet in pairs(pets) do
– if pet==“lion” then break end
– end
– function findpet(name)
– for i,pet in pairs(pets) do
– if pet==name then return i,pet end
– end
– end
– print(findpet(“lion”))
Formal and actual args
• From within the interpreter:
– function f(a,b) print(a,b) end
– f()
– f(3)
– f(3,4)
– f(3,4,5)
– f{3,4,5} --???!!!
Returning multiple values
• From within the interpreter:
– function f(a,b) return a,b,5 end
– a = f(3,4); print(a)
– a,b = f(3,4); print(a,b)
– a,b,c = f(3,4); print(a,b,c)
– a,b,c,d = f(3,4); print(a,b,c,d)
– a,b,c,d = f(1,2) , f(3,4); print(a,b,c,d)
--???!!!
– t = { 1, 2 }; print(f(unpack(t)))
Variable number of args
• From within the interpreter:
– function f(a, b, …) print(a,b,unpack(arg))
end
– f(1)
– f(1,2)
– f(1,2,”Hi”, “there!”)
Named actual args
• From within the interpreter:
– function p(name, addr, age, gender)
– if age > 120 then error(“U’re kidding me”) end
– record = { name=name, addr=addr, age=age, gender=gender }
– end
– p(“Johnny”, “Cash”, “male”, 90) --???!!!
– function P(r) p(r.name, r.addr, r.age, r.gender) end
– P{name=“Johnny”,addr=“Cash”,gender=“male”,age=90}
Functions as first-class values
• function f(a,b) print(a,b) end
• syntactic sugar for:
– f = function (a,b) print(a,b) end
• some usages:
– table.sort(pets, function (a,b) return (a>b) end)
– square = {
– x = 0; y = 0; size = 20;
– draw = function() some code end
– }
Closures
• From within the interpreter:
– function newCounter ()
– local i = 0
– return function() i=i+1; return i end
– end
– c1 = newCounter()
– c2 = newCounter()
– print(c1(), c1(), c1())
– print(c2(), c2())
Proper tail calls
• Check this C code:
– int f ( char* state ) {
– if (strcmp(state, “exit”))
– return(0);
– return f(state);
– }
– int result = f(“ooops”);
Iterators
• From within the interpreter:
– function words ( filename )
– local file = io.open(filename)
– local line = file:read()
– local i = nil
– return function ()
– local b, e
– while not b do
– if not line then return nil end
– b,e = string.find(line, "[%w_]+", i)
– if b then
– i = e + 1; return string.sub(line, b, e)
– else
– i = nil; line = file:read()
– end
– end
– end
– end
–
– for word in words("chunk.lua") do print(word) end
Loading libraries
• Loading Lua libraries:
– require “mypackage.mylib”
• Loading shared libraries:
– local path =
“/usr/local/lua/lib/libluasocket.so”
– local f = loadlib(path, “luaopen_socket”)
– f() -- opens the library
Errors
• From within the interpreter:
– file = assert(io.open(‘chunk.lua’, ‘r’))
• assert(chunk,msg) == if not chunk then
error(msg)
• From within the interpreter:
– local status, err =
– pcall(P{name=“Johnny”,address=“Cash”,age=150,
gender=“male”})
– if not status then print(err) end
Coroutines
• From within the interpreter:
– co = coroutine.create(function()
– for i=1,10 do
– print("co", i)
– coroutine.yield()
– end
– end)
–
– coroutine.resume(co)
– coroutine.resume(co)
– coroutine.resume(co)
Metatables
• From within the interpreter:
– Complex = {}
– function Complex.new (c)
– setmetatable(c, Complex); return c
– end
– function Complex.__add (a,b)
– return Complex.new{r=a.r+b.r, i=a.i+b.i}
– end
– c = Complex.new{r=1,i=2} +
Complex.new{r=3,i=4}
– print(unpack(c))
Metatables
Key Value
Key Value
__index
__newindex
__tostring
…
setmetatable(t,mt)
t
mt
Polymorphism
• From within the interpreter:
– print(c)
– function Complex.__tostring(c)
– return tostring(c.r) .. ‘+’ .. tostring(c.i)
.. ‘i’
– end
– print(c)
Inheritance
• From within the interpreter:
– Phone = {}
– Phone.keypad = {[1]=“1”, [2]=“2”}
– function Phone.dial ( keys ) print(keys) end
– CarPhone = {}
– CarPhone.mike = io
– CarPhone.dir = { Johnny = “112”, Cash = “211” }
– function CarPhone.voice_dial ()
– CarPhone.dial(CarPhone.dir[CarPhone.mike.read()])
– end
Inheritance
• From within the interpreter:
– CarPhone.voice_dial() ???!!!
– Phone.__index = Phone
– setmetatable(CarPhone, Phone)
– CarPhone.voice_dial()
– Phone.speaker = io
– CarPhone.speaker.write(“Beeeeeeeeep”)
Overloading
• From within the interpreter:
– CellPhone = {
– dial = function ( keys ) error(“no link”)
end
– }
– setmetatable(CellPhone, Phone)
– CellPhone.dial(“112”)
• There is no argument-based
overloading!
Metamethods
• List of metamethods:
– add (+)
– sub (-)
– mul (*)
– div (/)
– mod (%)
– pow (^)
– unm (-)
– concat (..)
– len (#)
– eq (=)
– lt (<)
– le (<=)
– index ([])
– newindex
– call (())
The environment
• From within the interpreter:
– print(_G._G == _G) ???!!!
– for gv in pairs(_G) do print(gv) end
– for obj in pairs(_G) do
– if string.find(obj, “Phone$”) then
– _G[obj].dial(“112”)
– end
– end
– setmetatable(_G, {
– __newindex = function(_,gv) error(“read only!”) end,
– })
– newglobal = “oopss”
More environments
• From within the interpreter:
– ctx = { ignore_case=true }
– function my_find ( string, regexp )
– if ctx and ctx.ignore_case then
– return string.find(string.lower(string), regexp)
– else
– return string.find(string, regexp)
– end
– end
– print(my_find(“Hi there!”, “hi”))
– do
– local myG = { ctx = {} }
– setmetatable(myG, {__index=_G})
– setfenv(my_find, myG)
– end
– print(my_find(“Hi there!”, “hi”))
– setfenv(my_find, _G)
OO Programming
• From within the interpreter:
– function Phone:new (obj)
– obj = obj or {}
– setmetatable(obj, self)
– self.__index = self
– return obj
– end
– my_phone = Phone:new()
– my_phone:dial(“112”) --???!!!
Metatables in OO Programming
Key Value
Key Value
__tostring
__newindex
__index
new
…
obj
Phone
…
setmetatable(obj,self)
…
C API: the stack
• C to Lua:
– void lua_pushnil
– void lua_pushboolean
– void lua_pushnumber
– void lua_pushstring
– void lua_pushlstring
• Lua to C:
– int lua_toboolean
– double lua_tonumber
– const char* lua_tostring
– size_t lua_strlen
– void lua_gettable
– void lua_settable
C running image
Data segment
Code segment
stack
C code Lua lib
C API: other stack functions
– lua_open to create a Lua state
– luaL_loadfile to read a Lua script into memory
– lua_pcall to run a Lua chunk in protected mode
– lua_getglobal to read lua global environment
– lua_setglobal to write lua global environment
– lua_pop to remove a value from the stack
– lua_newtable to create a new table on the stack
– lua_close to end a running state
C API: the stack from the other side
• Pre-C to Lua:
– void
lua_pushcfunction
• C to Lua the right
way™:
– struct luaL_reg
– void luaL_openlib
• Lua to C the right
way™:
– loadlib()
C running image
Data segment
Code segment
stack
C code Lua lib
C API: Example
• #include <dirent.h>
• #include <errno.h>
• static int l_dir (lua_State *L) {
• DIR *dir; struct dirent *entry; int i;
• const char *path = luaL_checkstring(L, 1);
• /* open directory */
• dir = opendir(path);
• if (dir == NULL) { /* error opening the directory? */
• lua_pushnil(L); /* return nil and ... */
• lua_pushstring(L, strerror(errno)); /* error message */
• return 2; /* number of results */
• }
• /* create result table */
• lua_newtable(L);
• i = 1;
• while ((entry = readdir(dir)) != NULL) {
• lua_pushnumber(L, i++); /* push key */
• lua_pushstring(L, entry->d_name); /* push value */
• lua_settable(L, -3);
• }
• closedir(dir);
• return 1; /* table is already on top */
• }
Example: application configuration
• In file “app.conf”:
– Rule{
– name = “HTTP”,
– ports={
– dest={80, 8080},
– },
– signatures = { “GET “ .. URL .. “ HTTP 1.1n” }
– }
– Rule{ name = “FTP”, … }
– Rule{ name = “SSH”, … }
Example: application configuration
• In file “main.c”:
– typedef struct { char*name, int* ports, char** signatures } Rule;
– static Rule rules[256];
– static int numRules = 0;
– static int readRule ( lua_State* L ) {
– if (numRules == sizeof(rules)) return 0;
– Rule rule;
– rule.name = lua_tostring(L, 1);
– rule.ports = …
– rule.signatures = …
– rules[numRules++] = rule;
– return 0;
– }
– …
– int main ( int argc, char** argv ) {
– lua_State* L = lua_open();
– luaL_loadfile(L, “app.conf”);
– lua_pushcfunction(L, readRule);
– lua_setglobal(L, “Rule”);
– lua_pcall(L, 0, 0, 0);
– }
Example: application configuration
• In file “app.conf”:
– function dbIterator ( table )
– local dbConn = lua.sql.connect(db_url)
– local cursor = dbConn.execute(“SELECT * FROM “ .. table)
– return function() return cursor.next() end
– end
–
– for dbRule in dbIterator do Rule(dbRule) end
–
Example: transforms
References
• http://www.lua.org
– http://www.lua.org/manual Reference Manual
– http://www.lua.org/pil Text Book
– http://www.luaforge.net Code Repository
– http://www.keplerproject.org It all begun here

More Related Content

What's hot

Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
g3_nittala
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
Istanbul Tech Talks
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance Python
Ian Ozsvald
 
Python
PythonPython
Python
Wei-Bo Chen
 
Odessapy2013 - Graph databases and Python
Odessapy2013 - Graph databases and PythonOdessapy2013 - Graph databases and Python
Odessapy2013 - Graph databases and Python
Max Klymyshyn
 
Scala introduction
Scala introductionScala introduction
Scala introduction
vito jeng
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
osfameron
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
intelliyole
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & how
lucenerevolution
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
Dmitry Buzdin
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
Ganesh Samarthyam
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
Michiel Borkent
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
Leonardo Borges
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard Library
Nelson Glauber Leal
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the Horizon
Alex Payne
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
Baishampayan Ghose
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
Mario Fusco
 
Lua London Meetup 2013
Lua London Meetup 2013Lua London Meetup 2013
Lua London Meetup 2013
Cloudflare
 
JavaScript Web Development
JavaScript Web DevelopmentJavaScript Web Development
JavaScript Web Development
vito jeng
 

What's hot (20)

Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimization
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
 
Euro python2011 High Performance Python
Euro python2011 High Performance PythonEuro python2011 High Performance Python
Euro python2011 High Performance Python
 
Python
PythonPython
Python
 
Odessapy2013 - Graph databases and Python
Odessapy2013 - Graph databases and PythonOdessapy2013 - Graph databases and Python
Odessapy2013 - Graph databases and Python
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
Haskell in the Real World
Haskell in the Real WorldHaskell in the Real World
Haskell in the Real World
 
Kotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime PerformanceKotlin Bytecode Generation and Runtime Performance
Kotlin Bytecode Generation and Runtime Performance
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & how
 
Poor Man's Functional Programming
Poor Man's Functional ProgrammingPoor Man's Functional Programming
Poor Man's Functional Programming
 
Java Class Design
Java Class DesignJava Class Design
Java Class Design
 
Full Stack Clojure
Full Stack ClojureFull Stack Clojure
Full Stack Clojure
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
 
Mastering Kotlin Standard Library
Mastering Kotlin Standard LibraryMastering Kotlin Standard Library
Mastering Kotlin Standard Library
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the Horizon
 
Pune Clojure Course Outline
Pune Clojure Course OutlinePune Clojure Course Outline
Pune Clojure Course Outline
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Lua London Meetup 2013
Lua London Meetup 2013Lua London Meetup 2013
Lua London Meetup 2013
 
JavaScript Web Development
JavaScript Web DevelopmentJavaScript Web Development
JavaScript Web Development
 

Viewers also liked

What's wrong with web
What's wrong with webWhat's wrong with web
What's wrong with web
Sergey Rubanov
 
Web development with Lua and Sailor @ GeeCon 2015
Web development with Lua and Sailor @ GeeCon 2015Web development with Lua and Sailor @ GeeCon 2015
Web development with Lua and Sailor @ GeeCon 2015
Etiene Dalcol
 
Lua and its Ecosystem
Lua and its EcosystemLua and its Ecosystem
Lua and its Ecosystem
François Perrad
 
Lua as a business logic language in high load application
Lua as a business logic language in high load applicationLua as a business logic language in high load application
Lua as a business logic language in high load application
Ilya Martynov
 
Lua vs python
Lua vs pythonLua vs python
Lua vs python
HoChul Shin
 
igdshare 110220: LuaJIT intro
igdshare 110220: LuaJIT introigdshare 110220: LuaJIT intro
igdshare 110220: LuaJIT intro
igdshare
 
What's New in LuaRocks - Lua Workshop 2014 - Hisham Muhammad
What's New in LuaRocks - Lua Workshop 2014 - Hisham MuhammadWhat's New in LuaRocks - Lua Workshop 2014 - Hisham Muhammad
What's New in LuaRocks - Lua Workshop 2014 - Hisham Muhammad
Hisham Muhammad
 
High Level Application Scripting With EFL and LuaJIT
High Level Application Scripting With EFL and LuaJITHigh Level Application Scripting With EFL and LuaJIT
High Level Application Scripting With EFL and LuaJIT
Samsung Open Source Group
 
Lua by Ong Hean Kuan
Lua by Ong Hean KuanLua by Ong Hean Kuan
Lua by Ong Hean Kuan
fossmy
 
Api Design Anti-Patterns
Api Design Anti-PatternsApi Design Anti-Patterns
Api Design Anti-Patterns
Jason Harmon
 
Roll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and LuaRoll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and Lua
Jon Moore
 
Accounting concepts conventions & principles
Accounting concepts conventions & principlesAccounting concepts conventions & principles
Accounting concepts conventions & principles
Jatin Pancholi
 
LuaConf 2016 - Becoming a Lua Powered Super Hero
LuaConf 2016 - Becoming a Lua Powered Super HeroLuaConf 2016 - Becoming a Lua Powered Super Hero
LuaConf 2016 - Becoming a Lua Powered Super Hero
Charles McKeever
 

Viewers also liked (13)

What's wrong with web
What's wrong with webWhat's wrong with web
What's wrong with web
 
Web development with Lua and Sailor @ GeeCon 2015
Web development with Lua and Sailor @ GeeCon 2015Web development with Lua and Sailor @ GeeCon 2015
Web development with Lua and Sailor @ GeeCon 2015
 
Lua and its Ecosystem
Lua and its EcosystemLua and its Ecosystem
Lua and its Ecosystem
 
Lua as a business logic language in high load application
Lua as a business logic language in high load applicationLua as a business logic language in high load application
Lua as a business logic language in high load application
 
Lua vs python
Lua vs pythonLua vs python
Lua vs python
 
igdshare 110220: LuaJIT intro
igdshare 110220: LuaJIT introigdshare 110220: LuaJIT intro
igdshare 110220: LuaJIT intro
 
What's New in LuaRocks - Lua Workshop 2014 - Hisham Muhammad
What's New in LuaRocks - Lua Workshop 2014 - Hisham MuhammadWhat's New in LuaRocks - Lua Workshop 2014 - Hisham Muhammad
What's New in LuaRocks - Lua Workshop 2014 - Hisham Muhammad
 
High Level Application Scripting With EFL and LuaJIT
High Level Application Scripting With EFL and LuaJITHigh Level Application Scripting With EFL and LuaJIT
High Level Application Scripting With EFL and LuaJIT
 
Lua by Ong Hean Kuan
Lua by Ong Hean KuanLua by Ong Hean Kuan
Lua by Ong Hean Kuan
 
Api Design Anti-Patterns
Api Design Anti-PatternsApi Design Anti-Patterns
Api Design Anti-Patterns
 
Roll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and LuaRoll Your Own API Management Platform with nginx and Lua
Roll Your Own API Management Platform with nginx and Lua
 
Accounting concepts conventions & principles
Accounting concepts conventions & principlesAccounting concepts conventions & principles
Accounting concepts conventions & principles
 
LuaConf 2016 - Becoming a Lua Powered Super Hero
LuaConf 2016 - Becoming a Lua Powered Super HeroLuaConf 2016 - Becoming a Lua Powered Super Hero
LuaConf 2016 - Becoming a Lua Powered Super Hero
 

Similar to Hands on lua

Something about Golang
Something about GolangSomething about Golang
Something about Golang
Anton Arhipov
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
PyData
 
Introduction to python
Introduction to pythonIntroduction to python
Python
PythonPython
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
Vincent Pradeilles
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
Muthu Vinayagam
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
Koen Handekyn
 
Python.pptx
Python.pptxPython.pptx
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
it-people
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap course
Martin Logan
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
Praveen M Jigajinni
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluiton
shin
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
teach4uin
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
romanandreg
 
L4 functions
L4 functionsL4 functions
L4 functions
mondalakash2012
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
Sergio Gil
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?
UFPA
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
jonycse
 
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?
osfameron
 

Similar to Hands on lua (20)

Something about Golang
Something about GolangSomething about Golang
Something about Golang
 
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
Pythran: Static compiler for high performance by Mehdi Amini PyData SV 2014
 
Introduction to python
Introduction to pythonIntroduction to python
Introduction to python
 
Python
PythonPython
Python
 
Monads in Swift
Monads in SwiftMonads in Swift
Monads in Swift
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Functional programming in ruby
Functional programming in rubyFunctional programming in ruby
Functional programming in ruby
 
Python.pptx
Python.pptxPython.pptx
Python.pptx
 
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
«Python на острие бритвы: PyPy project» Александр Кошкин, Positive Technologies
 
Erlang bootstrap course
Erlang bootstrap courseErlang bootstrap course
Erlang bootstrap course
 
Chapter 02 functions -class xii
Chapter 02   functions -class xiiChapter 02   functions -class xii
Chapter 02 functions -class xii
 
Ch01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluitonCh01 basic concepts_nosoluiton
Ch01 basic concepts_nosoluiton
 
Stdlib functions lesson
Stdlib functions lessonStdlib functions lesson
Stdlib functions lesson
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
L4 functions
L4 functionsL4 functions
L4 functions
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
 
Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?Porque aprender haskell me fez um programador python melhor?
Porque aprender haskell me fez um programador python melhor?
 
python beginner talk slide
python beginner talk slidepython beginner talk slide
python beginner talk slide
 
Python basic
Python basicPython basic
Python basic
 
Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?Is Haskell an acceptable Perl?
Is Haskell an acceptable Perl?
 

Recently uploaded

Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Neo4j
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
DianaGray10
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
Javier Junquera
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
Neo4j
 
From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
Sease
 
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptxAI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
Sunil Jagani
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
DanBrown980551
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
zjhamm304
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
UiPathCommunity
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
Ajin Abraham
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Neo4j
 
Discover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched ContentDiscover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched Content
ScyllaDB
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
christinelarrosa
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
DianaGray10
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
christinelarrosa
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
Fwdays
 
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
AlexanderRichford
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
Mydbops
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
UiPathCommunity
 

Recently uploaded (20)

Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and BioinformaticiansBiomedical Knowledge Graphs for Data Scientists and Bioinformaticians
Biomedical Knowledge Graphs for Data Scientists and Bioinformaticians
 
What is an RPA CoE? Session 1 – CoE Vision
What is an RPA CoE?  Session 1 – CoE VisionWhat is an RPA CoE?  Session 1 – CoE Vision
What is an RPA CoE? Session 1 – CoE Vision
 
GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)GNSS spoofing via SDR (Criptored Talks 2024)
GNSS spoofing via SDR (Criptored Talks 2024)
 
Leveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and StandardsLeveraging the Graph for Clinical Trials and Standards
Leveraging the Graph for Clinical Trials and Standards
 
From Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMsFrom Natural Language to Structured Solr Queries using LLMs
From Natural Language to Structured Solr Queries using LLMs
 
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptxAI in the Workplace Reskilling, Upskilling, and Future Work.pptx
AI in the Workplace Reskilling, Upskilling, and Future Work.pptx
 
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance PanelsNorthern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
Northern Engraving | Modern Metal Trim, Nameplates and Appliance Panels
 
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
LF Energy Webinar: Carbon Data Specifications: Mechanisms to Improve Data Acc...
 
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...QA or the Highway - Component Testing: Bridging the gap between frontend appl...
QA or the Highway - Component Testing: Bridging the gap between frontend appl...
 
Session 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdfSession 1 - Intro to Robotic Process Automation.pdf
Session 1 - Intro to Robotic Process Automation.pdf
 
AppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSFAppSec PNW: Android and iOS Application Security with MobSF
AppSec PNW: Android and iOS Application Security with MobSF
 
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid ResearchHarnessing the Power of NLP and Knowledge Graphs for Opioid Research
Harnessing the Power of NLP and Knowledge Graphs for Opioid Research
 
Discover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched ContentDiscover the Unseen: Tailored Recommendation of Unwatched Content
Discover the Unseen: Tailored Recommendation of Unwatched Content
 
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptxPRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
PRODUCT LISTING OPTIMIZATION PRESENTATION.pptx
 
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectorsConnector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
Connector Corner: Seamlessly power UiPath Apps, GenAI with prebuilt connectors
 
Christine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptxChristine's Supplier Sourcing Presentaion.pptx
Christine's Supplier Sourcing Presentaion.pptx
 
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin..."$10 thousand per minute of downtime: architecture, queues, streaming and fin...
"$10 thousand per minute of downtime: architecture, queues, streaming and fin...
 
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
QR Secure: A Hybrid Approach Using Machine Learning and Security Validation F...
 
Must Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during MigrationMust Know Postgres Extension for DBA and Developer during Migration
Must Know Postgres Extension for DBA and Developer during Migration
 
Day 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio FundamentalsDay 2 - Intro to UiPath Studio Fundamentals
Day 2 - Intro to UiPath Studio Fundamentals
 

Hands on lua

  • 2. Facts • Developed at PUC Rio by Roberto Lerusalimschy et al • Born as a C/C++ extension language • Incorporates most innovations of RAD languages like Python, Ruby… • Easily embeddable, lightweight and efficient • Totally free and open license
  • 3. Chunks • “a=1” in interactive mode – each line in interactive mode is a chunck • echo “a=1;b=2” > chunk.lua – file chunk.lua is a chunk • Running a chunk: – “lua –lchunk” from command prompt – “lua chunk.lua arg1 arg2” to pass command-line arguments – dofile(“chunk.lua”) from within another chunk
  • 4. Globals • Define&use anywhere: – print(b) -- nil – b=10 – print(b) -- 10 • To undefine: – b=nil
  • 5. The interpreter • Passing chunks: – lua –lchunk –e “a=math.sqrt(b)” chunk.lua • Passing command-line arguments: – lua –i –e “c=‘Hi !’” chunk.lua 1 2 3 – print(arg[1]) – print(arg[2]) – print(arg[3]) – print(arg[-1]) --???!!!
  • 6. Types and Values • From within the interpreter: – print(type(10)) • You can’t help trying these: – print(type(nil)) – print(type(print)) --???!!!
  • 7. Types and Values • 6 built-in types: – nil (nil) – boolean (true | false) – number (any real number, usual formats allowed) – string (any char sequence, 0 allowed!) – table (more about these later) – function (first-class values) • 2 utility types: – userdata – thread
  • 8. Tables • From within the interpreter: – t = {} – t[“key”] = “value”; print(t[“key”]) – print(t[“otherkey”]) – t[1] = “first”; print(t[1]); print(t[“key”]) – print(t.key) --???!!! – t = { key=“value”, otherkey=“first” } – t = { key=“value”, 1=“first” } --???!!! – t = { key=“value”, [1]=“first” } – print(t.1) --???!!!
  • 9. Arrays • From within the interpreter: – t[1] = ‘a’; t[2] = ‘b’; t[3] = ‘c’ – print(t[1] .. t[2] .. t[3]) – print(t[-1]) (got what you expected?) – t[-1] = ‘z’ – print(t[-1]) – print(t) --???!!! – print(unpack(t))
  • 10. Functions • From within the interpreter: – function f ( a ) return a^2 end – print(f(2)) • Functions are first-class values: – t = {} – t[“square”] = f – print(t.square(2))
  • 11. Some math… • Arithmetic operators: + - * / -() • Relational operators: < > <= >= == ~= • Logical operators: and or not – false and anything == false; true and anything == anything – nil and anything == false – true or anything == true; false or anything == anything – nil or anything == anything
  • 12. Assignments • From within the interpreter: – a = “Hi “ .. “there!” – a, b = 10, a – print(a, b) – a, b = b, a – print(a, b) – a, b, c = 0 – print(a, b, c) --???!!!
  • 13. Locals, blocks and scope • From within the interpreter: – a = 10 – local b = 2*a – print(a, b) --???!!! – do local b = 2*a; print(a,b) end • What is a block? – a chunk is a block – a control sentence is a block – a function body is a block
  • 14. if then else • From within the interpreter: – if a < 0 then a = -a end – if a == 1 then – print(“first”) – else – if a < 0 then – error(“outofrange”) – else – print(a) – end – end
  • 15. while and repeat • From within the interpreter: – while a > 0 do – print(a) – a = a – 1 – end – repeat – print(a) – a = a + 1 – until a > 10
  • 16. for • From within the interpreter: – for i=1,10,2 do print(i) end – pets = { “cat”, “dog”, “bunny” } – for i,pet in pairs(pets) do print(i, pet) end – for i,pet in ipairs(pets) do print(i, pet) end – pets[“ops”] = “lion” – for i,pet in pairs(pets) do print(i, pet) end – for i,pet in ipairs(pets) do print(i, pet) end
  • 17. break and return • From within the interpreter: – for i,pet in pairs(pets) do – if pet==“lion” then break end – end – function findpet(name) – for i,pet in pairs(pets) do – if pet==name then return i,pet end – end – end – print(findpet(“lion”))
  • 18. Formal and actual args • From within the interpreter: – function f(a,b) print(a,b) end – f() – f(3) – f(3,4) – f(3,4,5) – f{3,4,5} --???!!!
  • 19. Returning multiple values • From within the interpreter: – function f(a,b) return a,b,5 end – a = f(3,4); print(a) – a,b = f(3,4); print(a,b) – a,b,c = f(3,4); print(a,b,c) – a,b,c,d = f(3,4); print(a,b,c,d) – a,b,c,d = f(1,2) , f(3,4); print(a,b,c,d) --???!!! – t = { 1, 2 }; print(f(unpack(t)))
  • 20. Variable number of args • From within the interpreter: – function f(a, b, …) print(a,b,unpack(arg)) end – f(1) – f(1,2) – f(1,2,”Hi”, “there!”)
  • 21. Named actual args • From within the interpreter: – function p(name, addr, age, gender) – if age > 120 then error(“U’re kidding me”) end – record = { name=name, addr=addr, age=age, gender=gender } – end – p(“Johnny”, “Cash”, “male”, 90) --???!!! – function P(r) p(r.name, r.addr, r.age, r.gender) end – P{name=“Johnny”,addr=“Cash”,gender=“male”,age=90}
  • 22. Functions as first-class values • function f(a,b) print(a,b) end • syntactic sugar for: – f = function (a,b) print(a,b) end • some usages: – table.sort(pets, function (a,b) return (a>b) end) – square = { – x = 0; y = 0; size = 20; – draw = function() some code end – }
  • 23. Closures • From within the interpreter: – function newCounter () – local i = 0 – return function() i=i+1; return i end – end – c1 = newCounter() – c2 = newCounter() – print(c1(), c1(), c1()) – print(c2(), c2())
  • 24. Proper tail calls • Check this C code: – int f ( char* state ) { – if (strcmp(state, “exit”)) – return(0); – return f(state); – } – int result = f(“ooops”);
  • 25. Iterators • From within the interpreter: – function words ( filename ) – local file = io.open(filename) – local line = file:read() – local i = nil – return function () – local b, e – while not b do – if not line then return nil end – b,e = string.find(line, "[%w_]+", i) – if b then – i = e + 1; return string.sub(line, b, e) – else – i = nil; line = file:read() – end – end – end – end – – for word in words("chunk.lua") do print(word) end
  • 26. Loading libraries • Loading Lua libraries: – require “mypackage.mylib” • Loading shared libraries: – local path = “/usr/local/lua/lib/libluasocket.so” – local f = loadlib(path, “luaopen_socket”) – f() -- opens the library
  • 27. Errors • From within the interpreter: – file = assert(io.open(‘chunk.lua’, ‘r’)) • assert(chunk,msg) == if not chunk then error(msg) • From within the interpreter: – local status, err = – pcall(P{name=“Johnny”,address=“Cash”,age=150, gender=“male”}) – if not status then print(err) end
  • 28. Coroutines • From within the interpreter: – co = coroutine.create(function() – for i=1,10 do – print("co", i) – coroutine.yield() – end – end) – – coroutine.resume(co) – coroutine.resume(co) – coroutine.resume(co)
  • 29. Metatables • From within the interpreter: – Complex = {} – function Complex.new (c) – setmetatable(c, Complex); return c – end – function Complex.__add (a,b) – return Complex.new{r=a.r+b.r, i=a.i+b.i} – end – c = Complex.new{r=1,i=2} + Complex.new{r=3,i=4} – print(unpack(c))
  • 31. Polymorphism • From within the interpreter: – print(c) – function Complex.__tostring(c) – return tostring(c.r) .. ‘+’ .. tostring(c.i) .. ‘i’ – end – print(c)
  • 32. Inheritance • From within the interpreter: – Phone = {} – Phone.keypad = {[1]=“1”, [2]=“2”} – function Phone.dial ( keys ) print(keys) end – CarPhone = {} – CarPhone.mike = io – CarPhone.dir = { Johnny = “112”, Cash = “211” } – function CarPhone.voice_dial () – CarPhone.dial(CarPhone.dir[CarPhone.mike.read()]) – end
  • 33. Inheritance • From within the interpreter: – CarPhone.voice_dial() ???!!! – Phone.__index = Phone – setmetatable(CarPhone, Phone) – CarPhone.voice_dial() – Phone.speaker = io – CarPhone.speaker.write(“Beeeeeeeeep”)
  • 34. Overloading • From within the interpreter: – CellPhone = { – dial = function ( keys ) error(“no link”) end – } – setmetatable(CellPhone, Phone) – CellPhone.dial(“112”) • There is no argument-based overloading!
  • 35. Metamethods • List of metamethods: – add (+) – sub (-) – mul (*) – div (/) – mod (%) – pow (^) – unm (-) – concat (..) – len (#) – eq (=) – lt (<) – le (<=) – index ([]) – newindex – call (())
  • 36. The environment • From within the interpreter: – print(_G._G == _G) ???!!! – for gv in pairs(_G) do print(gv) end – for obj in pairs(_G) do – if string.find(obj, “Phone$”) then – _G[obj].dial(“112”) – end – end – setmetatable(_G, { – __newindex = function(_,gv) error(“read only!”) end, – }) – newglobal = “oopss”
  • 37. More environments • From within the interpreter: – ctx = { ignore_case=true } – function my_find ( string, regexp ) – if ctx and ctx.ignore_case then – return string.find(string.lower(string), regexp) – else – return string.find(string, regexp) – end – end – print(my_find(“Hi there!”, “hi”)) – do – local myG = { ctx = {} } – setmetatable(myG, {__index=_G}) – setfenv(my_find, myG) – end – print(my_find(“Hi there!”, “hi”)) – setfenv(my_find, _G)
  • 38. OO Programming • From within the interpreter: – function Phone:new (obj) – obj = obj or {} – setmetatable(obj, self) – self.__index = self – return obj – end – my_phone = Phone:new() – my_phone:dial(“112”) --???!!!
  • 39. Metatables in OO Programming Key Value Key Value __tostring __newindex __index new … obj Phone … setmetatable(obj,self) …
  • 40. C API: the stack • C to Lua: – void lua_pushnil – void lua_pushboolean – void lua_pushnumber – void lua_pushstring – void lua_pushlstring • Lua to C: – int lua_toboolean – double lua_tonumber – const char* lua_tostring – size_t lua_strlen – void lua_gettable – void lua_settable C running image Data segment Code segment stack C code Lua lib
  • 41. C API: other stack functions – lua_open to create a Lua state – luaL_loadfile to read a Lua script into memory – lua_pcall to run a Lua chunk in protected mode – lua_getglobal to read lua global environment – lua_setglobal to write lua global environment – lua_pop to remove a value from the stack – lua_newtable to create a new table on the stack – lua_close to end a running state
  • 42. C API: the stack from the other side • Pre-C to Lua: – void lua_pushcfunction • C to Lua the right way™: – struct luaL_reg – void luaL_openlib • Lua to C the right way™: – loadlib() C running image Data segment Code segment stack C code Lua lib
  • 43. C API: Example • #include <dirent.h> • #include <errno.h> • static int l_dir (lua_State *L) { • DIR *dir; struct dirent *entry; int i; • const char *path = luaL_checkstring(L, 1); • /* open directory */ • dir = opendir(path); • if (dir == NULL) { /* error opening the directory? */ • lua_pushnil(L); /* return nil and ... */ • lua_pushstring(L, strerror(errno)); /* error message */ • return 2; /* number of results */ • } • /* create result table */ • lua_newtable(L); • i = 1; • while ((entry = readdir(dir)) != NULL) { • lua_pushnumber(L, i++); /* push key */ • lua_pushstring(L, entry->d_name); /* push value */ • lua_settable(L, -3); • } • closedir(dir); • return 1; /* table is already on top */ • }
  • 44. Example: application configuration • In file “app.conf”: – Rule{ – name = “HTTP”, – ports={ – dest={80, 8080}, – }, – signatures = { “GET “ .. URL .. “ HTTP 1.1n” } – } – Rule{ name = “FTP”, … } – Rule{ name = “SSH”, … }
  • 45. Example: application configuration • In file “main.c”: – typedef struct { char*name, int* ports, char** signatures } Rule; – static Rule rules[256]; – static int numRules = 0; – static int readRule ( lua_State* L ) { – if (numRules == sizeof(rules)) return 0; – Rule rule; – rule.name = lua_tostring(L, 1); – rule.ports = … – rule.signatures = … – rules[numRules++] = rule; – return 0; – } – … – int main ( int argc, char** argv ) { – lua_State* L = lua_open(); – luaL_loadfile(L, “app.conf”); – lua_pushcfunction(L, readRule); – lua_setglobal(L, “Rule”); – lua_pcall(L, 0, 0, 0); – }
  • 46. Example: application configuration • In file “app.conf”: – function dbIterator ( table ) – local dbConn = lua.sql.connect(db_url) – local cursor = dbConn.execute(“SELECT * FROM “ .. table) – return function() return cursor.next() end – end – – for dbRule in dbIterator do Rule(dbRule) end –
  • 48. References • http://www.lua.org – http://www.lua.org/manual Reference Manual – http://www.lua.org/pil Text Book – http://www.luaforge.net Code Repository – http://www.keplerproject.org It all begun here