lua presentation for the chicago glug 5-5-2007

Upload: mikhail-miguel

Post on 06-Apr-2018

221 views

Category:

Documents


1 download

TRANSCRIPT

  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    1/17

    LuaPresentation for the Chicago GLUG 5/5/07

    Tristan Sloughter ([email protected])

    May 5, 2007

    Tristan Sloughter ([email protected]) Lua Presentation for the Chicago GLUG 5/5/07

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    2/17

    Quote

    When I am working on a problem, I never think about beauty. I

    think only of how to solve the problem. But when I have

    finished, if the solution is not beautiful, I know it is wrong.

    Richard Buckminister Fuller

    Tristan Sloughter ([email protected]) Lua Presentation for the Chicago GLUG 5/5/07

    http://goforward/http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    3/17

    The Classics

    Dynamically Typed

    String, Math, I/0, etc LibrariesREPL

    Tristan Sloughter ([email protected]) Lua Presentation for the Chicago GLUG 5/5/07

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    4/17

    The Important Stuff

    Size 150K

    First class functions

    Tables and MetaTables the only data structure, mold toyour needs

    Coroutines

    C Interface

    Tristan Sloughter ([email protected]) Lua Presentation for the Chicago GLUG 5/5/07

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    5/17

    Whats it Good For?

    Any script

    Interface with C code, used by a lot of video games (WoW,

    Far Cry, Ion WM, Adobe Photoshop Lightroom, nmap)Portable, only need ANSI C

    Simple and Beautiful scripts!

    Tristan Sloughter ([email protected]) Lua Presentation for the Chicago GLUG 5/5/07

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    6/17

    Functions and Variables

    Local and Global Variables

    First class functions

    Lexical ScopeVariables number of arguments, ...

    Multiple return values

    Tristan Sloughter ([email protected]) Lua Presentation for the Chicago GLUG 5/5/07

    F ti

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    7/17

    Functions

    Multiple Return Values Example

    s,e = string.find(hello Lua users, Lua)

    Results

    s -> 7

    e -> 9

    Another Multiple Return Values Example

    a,b = unpack{10, 20, 30}

    Results

    a -> 10

    b -> 20

    F ti

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    8/17

    Functions

    Closures

    function counter()

    local i = 0

    return

    function ()

    i = i+1

    return i

    end

    end

    Usec1 = count()

    c1() -> 1

    c2 = count()

    c2() -> 1

    c1() -> 2

    T bl

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    9/17

    Tables

    Does Everything

    Namespaces and OOP created through tables

    All other data structures created with tables

    Even the evironment is a table

    Tristan Sloughter ([email protected]) Lua Presentation for the Chicago GLUG 5/5/07

    Tables

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    10/17

    Tables

    Table, Array, Lista = {x=10, y=20}

    a.w = 30

    seq= {"First", "Second", "Third"}

    print(seq[1]) -> "First"

    list=nil

    for line in io.line() do

    list = {next=list, value=line}end

    Tables

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    11/17

    Tables

    Setting the Index

    > optable = {[+]="plus", [-]="minus"}

    > print(optable[-])

    minus

    > optable = {[+]="plus", ["x"]="minus"}

    > print(optable.x)

    minus

    Tables

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    12/17

    Tables

    Overwrite/Store Functions

    > math.sin = print

    > math.sin("hello")

    hello

    Iterators

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    13/17

    Iterators

    Example

    > t = {10, 20, 30}

    > for i,v in ipairs(t) doprint(i, v)

    end

    1 10

    2 20

    3 30

    > t = {S={"AB"}, A={"CB","a"}}

    > t.s="s"

    > for k,v in pairs(t) do

    print(k, v)

    end

    A table: 0x62cc60

    S table: 0x62cc10

    s s

    MetaTables

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    14/17

    MetaTables

    Reset certain builtins

    add is +

    eq is ==

    etc.

    index creates a function that is called each time you

    access a tables field

    newindex is called each time you create a new field in a

    table

    Change them for G (The Global Environment)

    MetaTables

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    15/17

    MetaTables

    Example

    Set = {}

    Set.new (list)

    local set = {}

    setmetatable(set, mt)

    for _, v in ipairs(list)

    do set [v] = true end

    return set

    end

    mt.__add = Set.union

    s1 = Set.new{10, 20, 30, 50}

    s2 = Set.new{30, 1}

    Set.print (s1 + s2) ->{1, 10, 20, 30, 50}

    OOP

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    16/17

    OOP

    Example

    Account = {balance=0}

    function Account:new(o)

    o = o o r { }

    setmetatable(o, self)

    self.__index = self

    return o

    end

    function Account:withdraw(v)

    self.balance = self.balance - v

    end

    a = Account:new{balance = 10}

    a:withdraw(5)

    print(a.balance)

    Links

    http://find/http://goback/
  • 8/3/2019 Lua Presentation for the Chicago Glug 5-5-2007

    17/17

    Links

    Lua - http://www.lua.org/

    Programming Lua -

    http://www.amazon.com/exec/obidos/ASIN/8590379825/lua-home-20

    These Slides - https://tristan.diginux.net/

    Tristan Sloughter ([email protected]) Lua Presentation for the Chicago GLUG 5/5/07

    http://find/http://goback/