SlideShare a Scribd company logo
Lua 
A powerful, light-weight scripting language
Introduction 
 Lua name 
 Support for 
◦ Object-oriented programming 
◦ Functional programming 
◦ Data-driven programming 
 Embedding program 
 Interpreted Language
Introduction 
 is implemented in pure ANSI C and 
compiles 
 Strongly integrated with C and C++ 
 Cross-platform 
 Good performance 
◦ To claim to be "as fast as Lua" 
 Multi-thread 
◦ Coroutines 
 Easy learning curve. 
 Free
Uses 
 Angry Bird 
 Simcity 4 
 World of Warcraft 
 Adobe Photoshop Lightroom 
 Other sort of applications.
IDE 
 LuaEdit 
◦ Download 
 Eclipse Plug-in 
◦ Lua Development Tools
Hello World! 
 Download Lua 
 Execute Lua 
 Interpreter 
 Print(‘Hello World’);
Values and Type 
 Lua is dynamically typed language. 
 Types 
◦ Nil 
◦ Boolean 
◦ Number 
◦ String 
◦ Function 
◦ Userdata 
◦ Thread 
◦ Table 
 Values 
◦ Initial : nil 
◦ Boolean => nil/false : false, others : true 
◦ Number : double-precision floating-point. 
◦ String : ‘’, “”, [[]], [==[ ]==]
Comment 
 Single comment 
◦ -- 
 Block comment 
◦ --[[ 
◦ ]]--
Variable 
 3 kinds of Scope 
◦ Global 
 _G 
◦ Local 
 local 
◦ Table 
 self 
 Multiple assignment 
◦ a,b = 1,2 
◦ a,b = b,a
The type “Table” 
 Like 
◦ ActionScript Array 
◦ Php Array 
◦ Perl Hash 
◦ Map 
 Indexed Array 
◦ Index is start from 1 
 Associative Array 
◦ Nil and NaN both cannot be as a key 
 Get length : # operator 
 Constructor 
◦ a = {} 
◦ a = { prop1, prop2 } => a[1] = prop1, a[2] = prop2 
◦ a = { prop = “test” } => a[“prop”] = “test” 
◦ a = { [f(x)] = 2 } => a[f(x)] = 2
Funciton 
 Definition 
function func_name ([args]) 
do something 
end 
method: 
function a.b.c:func_name(self, body) body end 
anonymous: 
function() 
do something 
end 
 call function 
• func_name(args); 
• table.func_name(agrs)
Function 
 Call method 
• table:func_name(args) 
 Result return 
◦ Able to return multiple result 
• return a; 
• return a,b; 
• return a or b
Logical Operator 
 Not 
 And 
 Or 
• not(a==1) 
• return a and b 
• return func1() and func2() 
• return a or b 
• return func1() or func2()
Statements 
 if exp then block {elseif exp then 
block} [else block] end 
 while exp do block end 
 repeat block until exp
For statement 
 numeric 
◦ work with number calculation 
• for I = 1, 10 do block end 
• for I = 1, 10, 2 do block end 
• for I = 1, #(table1) do block end 
 generic 
◦ work with iterators, stop when value is nil 
• for namelist in explist do block end 
• for k, v in pairs(table1) do block end 
• for k, v in ipairs(table1) do block end
Metatables and Metamethods 
 Every value has metatable and 
metamethod 
 The key of metatable => event 
 The value of metatable => 
metamethod 
 getmetatable -> get the metatable of 
any value 
 setmetatable -> set the metatable of 
table
Metatables and Metamethods 
 Add 
 Sub 
 Mul 
 Mod 
 Pow 
 Unm 
 Concat 
 Len 
 Eq 
 Lt 
 Le 
 Index 
 Newindex 
 Call
For OOP purpose 
Person={} 
function Person:new(p) 
local obj = p 
if (obj == nil) then 
obj = {name="ChenHao", age=37, handsome=true} 
end 
self.__index = self 
return setmetatable(obj, self) 
end 
function Person:toString() 
return self.name .." : ".. self.age .." : ".. (self.handsome and "handsome" or "ugly") 
end
Modules 
 require(‘hello’); 
◦ package.loaded to record which module is 
loaded 
 dofile(‘hello.lua’); 
 loadfile(‘hello.lua’);
Standard Libraries 
 Basic library 
 Coroutine library 
 Package library 
 String manipulation 
 Table manipulation 
 Mathematical functions 
 Bitwise operations 
 Input and Output 
 Etc.
Error Handling 
 Throw exception 
◦ The error function 
 Catch exception 
◦ pcall or xpcall
Garbage Collection 
 Auto memory management. 
 2 indexes to determine GC 
◦ The garbage-collector pause 
◦ The garbage-collector step multiplier

More Related Content

PPT
jimmy hacking (at) Microsoft
PDF
Reactive Programming in the Browser feat. Scala.js and PureScript
PDF
Building a Tagless Final DSL for WebGL
PDF
05. haskell streaming io
PPT
Groovy
PPTX
Столпы функционального программирования для адептов ООП, Николай Мозговой
PDF
All Aboard The Scala-to-PureScript Express!
PPT
Overloading
jimmy hacking (at) Microsoft
Reactive Programming in the Browser feat. Scala.js and PureScript
Building a Tagless Final DSL for WebGL
05. haskell streaming io
Groovy
Столпы функционального программирования для адептов ООП, Николай Мозговой
All Aboard The Scala-to-PureScript Express!
Overloading

What's hot (20)

PDF
Introduction to kotlin
PDF
Taking Kotlin to production, Seriously
PPT
Operator overloading
PDF
GR8Conf 2011: GPars
PDF
Kotlin boost yourproductivity
PDF
Intro to Kotlin
PPT
Operator Overloading
PDF
MTL Versus Free
PDF
Orthogonal Functional Architecture
PPT
The Kotlin Programming Language
PPTX
Iron Languages - NYC CodeCamp 2/19/2011
PPTX
Introduction to kotlin + spring boot demo
PDF
Swift and Kotlin Presentation
PDF
Clojure made-simple - John Stevenson
PPTX
Compile time polymorphism
PDF
Pune Clojure Course Outline
PDF
Droidcon Poland - From Kotlin to Machine Code
PDF
Cocoaheads Meetup / Alex Zimin / Swift magic
PDF
Halogen: Past, Present, and Future
PDF
Writing a compiler in go
Introduction to kotlin
Taking Kotlin to production, Seriously
Operator overloading
GR8Conf 2011: GPars
Kotlin boost yourproductivity
Intro to Kotlin
Operator Overloading
MTL Versus Free
Orthogonal Functional Architecture
The Kotlin Programming Language
Iron Languages - NYC CodeCamp 2/19/2011
Introduction to kotlin + spring boot demo
Swift and Kotlin Presentation
Clojure made-simple - John Stevenson
Compile time polymorphism
Pune Clojure Course Outline
Droidcon Poland - From Kotlin to Machine Code
Cocoaheads Meetup / Alex Zimin / Swift magic
Halogen: Past, Present, and Future
Writing a compiler in go
Ad

Similar to Lua Study Share (20)

PPTX
C++ theory
PPT
Groovy presentation
PDF
Scala is java8.next()
PPTX
Functional and code coverage verification using System verilog
PPTX
Java For Automation
PDF
PofEAA and SQLAlchemy
PDF
Swift, functional programming, and the future of Objective-C
PDF
07 control+structures
PPTX
JavaScript (without DOM)
PPTX
Programming in java basics
PDF
JavaScript in 2016
PPTX
JavaScript in 2016 (Codemotion Rome)
PPT
L4 functions
PDF
A Recovering Java Developer Learns to Go
PPTX
Programming picaresque
PPTX
Make BDD great again
PDF
Denis Lebedev, Swift
PPTX
Java gets a closure
PDF
Scala for Java Developers
C++ theory
Groovy presentation
Scala is java8.next()
Functional and code coverage verification using System verilog
Java For Automation
PofEAA and SQLAlchemy
Swift, functional programming, and the future of Objective-C
07 control+structures
JavaScript (without DOM)
Programming in java basics
JavaScript in 2016
JavaScript in 2016 (Codemotion Rome)
L4 functions
A Recovering Java Developer Learns to Go
Programming picaresque
Make BDD great again
Denis Lebedev, Swift
Java gets a closure
Scala for Java Developers
Ad

Recently uploaded (20)

PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Digital Strategies for Manufacturing Companies
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
How Creative Agencies Leverage Project Management Software.pdf
PPTX
history of c programming in notes for students .pptx
PDF
System and Network Administraation Chapter 3
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PDF
System and Network Administration Chapter 2
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
Reimagine Home Health with the Power of Agentic AI​
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Design an Analysis of Algorithms II-SECS-1021-03
2025 Textile ERP Trends: SAP, Odoo & Oracle
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Digital Strategies for Manufacturing Companies
How to Choose the Right IT Partner for Your Business in Malaysia
How Creative Agencies Leverage Project Management Software.pdf
history of c programming in notes for students .pptx
System and Network Administraation Chapter 3
Odoo POS Development Services by CandidRoot Solutions
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
Understanding Forklifts - TECH EHS Solution
System and Network Administration Chapter 2
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Design an Analysis of Algorithms I-SECS-1021-03
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...

Lua Study Share

  • 1. Lua A powerful, light-weight scripting language
  • 2. Introduction  Lua name  Support for ◦ Object-oriented programming ◦ Functional programming ◦ Data-driven programming  Embedding program  Interpreted Language
  • 3. Introduction  is implemented in pure ANSI C and compiles  Strongly integrated with C and C++  Cross-platform  Good performance ◦ To claim to be "as fast as Lua"  Multi-thread ◦ Coroutines  Easy learning curve.  Free
  • 4. Uses  Angry Bird  Simcity 4  World of Warcraft  Adobe Photoshop Lightroom  Other sort of applications.
  • 5. IDE  LuaEdit ◦ Download  Eclipse Plug-in ◦ Lua Development Tools
  • 6. Hello World!  Download Lua  Execute Lua  Interpreter  Print(‘Hello World’);
  • 7. Values and Type  Lua is dynamically typed language.  Types ◦ Nil ◦ Boolean ◦ Number ◦ String ◦ Function ◦ Userdata ◦ Thread ◦ Table  Values ◦ Initial : nil ◦ Boolean => nil/false : false, others : true ◦ Number : double-precision floating-point. ◦ String : ‘’, “”, [[]], [==[ ]==]
  • 8. Comment  Single comment ◦ --  Block comment ◦ --[[ ◦ ]]--
  • 9. Variable  3 kinds of Scope ◦ Global  _G ◦ Local  local ◦ Table  self  Multiple assignment ◦ a,b = 1,2 ◦ a,b = b,a
  • 10. The type “Table”  Like ◦ ActionScript Array ◦ Php Array ◦ Perl Hash ◦ Map  Indexed Array ◦ Index is start from 1  Associative Array ◦ Nil and NaN both cannot be as a key  Get length : # operator  Constructor ◦ a = {} ◦ a = { prop1, prop2 } => a[1] = prop1, a[2] = prop2 ◦ a = { prop = “test” } => a[“prop”] = “test” ◦ a = { [f(x)] = 2 } => a[f(x)] = 2
  • 11. Funciton  Definition function func_name ([args]) do something end method: function a.b.c:func_name(self, body) body end anonymous: function() do something end  call function • func_name(args); • table.func_name(agrs)
  • 12. Function  Call method • table:func_name(args)  Result return ◦ Able to return multiple result • return a; • return a,b; • return a or b
  • 13. Logical Operator  Not  And  Or • not(a==1) • return a and b • return func1() and func2() • return a or b • return func1() or func2()
  • 14. Statements  if exp then block {elseif exp then block} [else block] end  while exp do block end  repeat block until exp
  • 15. For statement  numeric ◦ work with number calculation • for I = 1, 10 do block end • for I = 1, 10, 2 do block end • for I = 1, #(table1) do block end  generic ◦ work with iterators, stop when value is nil • for namelist in explist do block end • for k, v in pairs(table1) do block end • for k, v in ipairs(table1) do block end
  • 16. Metatables and Metamethods  Every value has metatable and metamethod  The key of metatable => event  The value of metatable => metamethod  getmetatable -> get the metatable of any value  setmetatable -> set the metatable of table
  • 17. Metatables and Metamethods  Add  Sub  Mul  Mod  Pow  Unm  Concat  Len  Eq  Lt  Le  Index  Newindex  Call
  • 18. For OOP purpose Person={} function Person:new(p) local obj = p if (obj == nil) then obj = {name="ChenHao", age=37, handsome=true} end self.__index = self return setmetatable(obj, self) end function Person:toString() return self.name .." : ".. self.age .." : ".. (self.handsome and "handsome" or "ugly") end
  • 19. Modules  require(‘hello’); ◦ package.loaded to record which module is loaded  dofile(‘hello.lua’);  loadfile(‘hello.lua’);
  • 20. Standard Libraries  Basic library  Coroutine library  Package library  String manipulation  Table manipulation  Mathematical functions  Bitwise operations  Input and Output  Etc.
  • 21. Error Handling  Throw exception ◦ The error function  Catch exception ◦ pcall or xpcall
  • 22. Garbage Collection  Auto memory management.  2 indexes to determine GC ◦ The garbage-collector pause ◦ The garbage-collector step multiplier