OpenWest 2014/Lua

Script up your application with Lua!
 * Ryan Erickson (@ryan_erickson)

"Come learn about Lua, the embeddable language that's used in everything from game scripting to Wikipedia, in mobile to web development. In this presentation, I'll give a short introduction to Lua, how it can be used to extend a C/C++ application, to allow complex scripting without losing performance. I'll also show some interesting Lua language features. Lastly, I'll give a live demonstration of the ease of embedding Lua into a C application."

Script up your application with Lua
 * Ryan Erickson
 * http://www.untestedhacks.com
 * Works at Control4

Ryan's home automation - http://ericksonfamily.com/

The Programming Language Lua - http://www.lua.org/

Lua: getting started - http://www.lua.org/start.html

Installation: yum install lua

curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz tar zxf lua-5.2.3.tar.gz cd lua-5.2.3 make linux test

History:
 * lua.org
 * created in 1993 at PUC-RIO
 * Predecessors: DEL and Sol (merged to Lua)
 * Lua is Portuguese for Moon, not an acronym (play on words from Sol)

Focus:
 * Simplicity for non-programmer audience
 * Portability
 * Designed to be easily embedded, extended
 * Clean ANSI C code
 * Garbage Collected

Why:
 * Size (tiny) < 100kb DLL/LIB
 * Runs on mobile / embedded devices
 * Performance - interpreted faster than Pyuthon, Ruby, Perl, PHP
 * Need faster? LuaJIT
 * LuaJIT is C++ / Java 6 territory (luajit.org)

Momentum:
 * Used a lot in games
 * Adobe Lightroom and Photoshop
 * First interpreted lanugage allowed on iOS
 * Angry Birds / World of Warcraft
 * Control4 uses

Safety:
 * Code runs in sandbox
 * Embedder chooses which modules to expose
 * Host application can provide APIs / primitives to Lua engine

Language:
 * Dynamically typed
 * Whitespace not significant
 * spaces, linebreaks, tabs, what you like
 * Semicolons not required, and discouraged
 * single line comments
 * variables are global by default, 'local' keyword

Types:
 * number, string, boolean, nil, table, function, userdata
 * numbers are double by default
 * can represent floats and integers
 * no i++, i+=2. use i =i +1

Strings:
 * single/double quote
 * backslash to escape
 * string concatenation uses '..' (not +)

nil:
 * empty value
 * evaluates to false
 * frees item for garbage collection

tables: a = {"apple", "banana"} b = {lua = "cool", java = "sucks"} c = a print(c[3], b.fred, b.["java"], a.grape) # orange 3 sucks nul
 * lua's single data structure
 * simultaneous array and hashmap
 * arrays in Lua are 1 based

Comments: -- this is a comment -- this is a multi line comment

Functions: function add(a, b)   print(a + b) end multiply = function(a, b) print(a * b) end plus = add plus(3,5)
 * first class objects

Control: if ... then ... elseif ... else ... End do  print("one") end for i = 1, 10 do print(i) end for k, v in pairs({"}} ???

C Programmer Hangups:
 * no curly braces - uses begin... end
 * No +=, ++
 * Not equals is "~="
 * Not is "not"
 * arrays start at 1
 * #array is not always right (if empty items in the middle)

C interface API
 * set of functions allow C to interact with Lua
 * functions to read/write lua global variables
 * functions to call lua functions
 * functions to register c functions to call within lua
 * stack based parameter passing

Book:
 * "Programming in Lua" - http://lua.org/pil

Reference PDF:
 * http://lua-users.org/wiki/LuaShortReference

Demo: -- comment print("test" .. os.date)

function rirc(user, message) local no_err, errmsg = pcall(parseIRC, user, message) if (no_error == false) then SendIRC("ryan", errmsg) end end

function parseIRC(user, message) message = message:gsub("%s", "") -- trim white space if (message == "|reload") then if (user == "ryan") then dofile("default.lua") end end if (message == "|echo") then SendIRC(user, "hello") end if (message:find("|giflet") ~= nil) then local figstr = message:sub(8) local sd = io.popen("figlet " .. figstr) while(line) do           SendIRC(user, line) end sd:close end

end