====== Introduction to Lua ====== ---- ===== Lua ===== * Creator: Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, Pontifical Catholic University of Rio de Janeiro, Brazil * Introduced: 1993 * Open source * Installable package on many UNIX/Linux systems * Windows installer at http://code.google.com/p/luaforwindows * Can be used for tasks that other dynamic programming languages are used for. * Has gained its most prominent footholds in the areas of embedded programming because of its relatively small size and as a scripting language for large games * UI modification in World of Warcraft, Rift, Age of Conan, Lord of the Rings Online, Warhammer Online, Runes of Magic and others * Also used to extend applications such as Wireshark (http://wiki.wireshark.org/Lua) and Adobe Lightroom (http://www.sauria.com/blog/2008/10/09/lua-in-lightroom/). * Wikipedia [[http://developers.slashdot.org/story/12/02/01/1429229/wikipedia-chooses-lua-as-its-new-template-language|to move to Lua]] for its templating system (~5/2012). * Several Lua-based game development platforms, including commercially-licensed * LOVE 2D - https://love2d.org/ * Corona SDK - http://www.anscamobile.com/corona/ (mobile) * MOAI SDK - http://getmoai.com/ (mobile, cloud) * See more [[http://en.wikipedia.org/wiki/Lua_%28programming_language%29#Applications|Lua applications]] ---- ===== Lua Online Resources ===== * http://www.lua.org - Official site * http://www.lua.org/manual/5.1 - Reference manual * http://lua-users.org/wiki - User-created wiki * http://lua-users.org/wiki/TutorialDirectory - Directory of Lua tutorials * http://www.lua.org/cgi-bin/demo - Web-based demo * Books: * http://www.lua.org/pil/ - "Programming in Lua" (1st Ed.) ---- ===== Lua Execution ===== * Programs typically given .lua extension * Can be executed with //lua prog.lua// * Interactive shell by running //lua// * Web-based demo at http://www.lua.org/cgi-bin/demo ---- ===== Lua Syntax ===== * syntax very similar to Python and even more similar to Ruby * strength is in processing strings and tables * uses [[http://en.wikipedia.org/wiki/Type_conversion|coercion]] for every integer and number type to convert it into a single type * can add a float, long integer or a double to any other type of integer or number without a hitch * Tables (like dictionaries or hashes) are the only data structuring mechanism that Lua has. * Lua tables can emulate other data structures as needed. * Object-oriented programming implementation is minimalistic. * Lua uses tables and functions rather than classes. ===== Data Types ===== * Because it is small, Lua has only eight basic data types: * nil (denotes no data type) * booleans * numbers * strings * functions * userdata (a type that allows arbitrary C data to be stored in Lua variables) * threads * tables ===== Variables and Identifiers ===== * Because any value can represent a condition, booleans in Lua differ from those in many other languages. * Both false and nil are considered false in Lua, but Lua considers everything else true (including zero and an empty string). ==== Global and local variables ==== * Unlike Python, global variables do not need to be declared. * To create one, assign a value to it. To delete it, give it the nil value. * A global variable exists only if it has a non-nil value. * Most variables in Lua are global by default, and you must declare the variable "local" to make it a local variable. ==== No integer types ==== * Because most CPUs perform floating-point arithmetic just as fast as integer arithmetic, numbers in Lua represent real, double-precision, and floating-point numbers rather than common integers. * Lua doesn't need integer types, so it doesn't have them. ==== Rename anything ==== * You can basically rename anything in Lua, even to the point of making a function un-callable: x = io x.read() io = "Hello world!" x = "Let's make io uncallable!" io.read() * The second line--x.read()--gets keyboard input through the io module. * Because io is essentially a variable with a function as a value, you can give it a different value so that io does not relate to the input/output functions anymore. (line 3) * In line 5, when you try to get keyboard input from the io module again, Lua returns an error. * The program is unable to call the input/output functions now that io's value has been reassigned. * In order to use io again, you must restart the Lua program. ===== Operators and Assignment ===== ==== Concatenate strings with .. ==== * print("Hello".."World!") is valid. * print("I've said 'Hello World'"..5.."or more times.") is not valid. * print("I've said 'Hello World' " ..5 .. " or more times.")is valid. ==== Logical not operators ==== * Lua uses ~= for the negation of equality. ==== Strings and integers ==== * Lua treats strings and integers differently. * Although print("1" * 2) prints 2, "1" < 2 is always false. ===== Lua and OOP ===== * Lua supports OOP, but due to Lua's size, its implementation of OOP lacks a few features. * Lua uses tables and functions for OOP rather than classes. * In the same way that Python accesses a member function or member variable of a class, Lua accesses it with Table.function or Table.variable. * Because Lua does not have the class concept, each object defines its own behavior and shape: -- This is a Lua comment. -- Definition of the Earth "class" -- starts with a table called Earth: Earth = {martians = 5389} function Earth:casualties (survivors) Earth.martians = Earth.martians - survivors print("Earth is free! "..Earth.martians.." martians survived!") end -- Call the Earth:casualties method: Earth:casualties(5380) * Second implemention of the above code: -- The casualties function is part of the table this time -- so that it can be called via the dot or the colon syntax. Earth = {martians = 5389, casualties = function (self, survivors) self.martians = self.martians - survivors print("Earth is free! "..self.martians.." martians survived!") end } Earth.casualties(Earth, 5380) Earth.martians = 5389 Earth:casualties(5380) ---- ===== Lua vs. Python ===== * Simple trivia game in Lua code that uses a table as a dictionary to store both the questions and the answers: print("What's your name?") name = io.read() questions = { ["Which came first? Minix or Unix?"] = "Unix", ["Who created Linux?"] = "Linus Torvalds", ["In what year was Linux created?"] = "1991" } correct_answers = 0 for key,value in pairs(questions) do print(key) answer = io.read() if answer == value then correct_answers = correct_answers + 1 end end if correct_answers == 0 then print("You need to browse Wikipedia!") else print("\nGood job, "..name.."!") print("Correct answers: "..correct_answers..") end * Equivalent Python code name = raw_input("What's your name?\n") questions = {"Which came first? Minix or Unix?":"Unix", "Who created Linux?":"Linus Torvalds", "In what year was Linux created?":"1991"} correct_answers = 0 for key in questions: print key answer = raw_input() if answer == questions[key]: correct_answers += 1 if correct_answers == 0: print "You need to browse Wikipedia!" else: print "\nGood job, " + name + "!" print "Correct answers: ", correct_answers ---- ===== Lua Tables ===== See the [[http://lua-users.org/wiki/TablesTutorial|Tables Tutorial]] at lua-users.org. For an example of a two-dimensional array implemented through tables, see the LOVE game framework [[https://love2d.org/wiki/Tutorial:Gridlocked_Player|Gridlocked Player]] example. ---- ===== Patterns (Regular Expressions) ===== See the [[http://lua-users.org/wiki/PatternsTutorial|Patterns Tutorial]] at lua-users.org. ---- ===== Performance ===== See some [[http://trac.caspring.org/wiki/LuaPerformance|notes on Lua performance]] at trac.caspring.org ---- ===== Coroutines (Threading) ===== See the [[http://lua-users.org/wiki/CoroutinesTutorial|Coroutines Tutorial]] at lua-users.org. ----