Scott Hall
-
Upload
alisa-cash -
Category
Documents
-
view
22 -
download
0
description
Transcript of Scott Hall
![Page 1: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/1.jpg)
Scott Hall
![Page 2: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/2.jpg)
Outline
• Introduction• History• What are Chunks?• Lexical Conventions• Values and Types• Variables• Control Structures• Functions• Coroutines• Object-Oriented Programming• Garbage Collection• Conclusions
![Page 3: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/3.jpg)
Lua Introduction
• Fast interpreted scripting language• Offers concurrency via coroutines• Offers support for object-oriented programming• Small, and therefore easily embeddable• Powerful but simple• Enjoys great popularity in the video game
industry, in particular for the popular role playing game World of Warcraft
• Lua translates to “Moon” in Portuguese
![Page 4: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/4.jpg)
History of Lua (1)
• Designed by Roberto Ierusalimschy, Waldemar Celes, and Luiz Henrique de Figueiredo at the Pontifical Catholic University of Rio de Janeiro, Brazil
• First appeared in 1993• Influenced by Scheme, SNOBOL, Modula, CLU
and C++• Lua was initially developed as a result of strict
trade barriers instituted by the Brazilian government
![Page 5: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/5.jpg)
History of Lua (2)
• The intended audience was not professional programmers, so the designers wanted avoid cryptic syntax and semantics.
• Lua was born from SOL (Simple Object Language) and DEL(Data-Entry Language).– SOL and DEL had a lack of flow control
![Page 6: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/6.jpg)
Chunks
• Each piece of code that Lua executes is called a chunk. A chunk is simply a sequence of statements.
a = 1
b = 2
a = 1; b = 2
Chunks
![Page 7: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/7.jpg)
Lexical Conventions
• Names, like most languages, can be any string of letters, digits and underscores, not beginning with a digit.
• Lua reserved keywords:and break do else elseif
end false for function if in local nil not or repeat return then true until
while
![Page 8: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/8.jpg)
Values and Types (1)
• Since Lua is a dynamically typed language, variables do not have types, only values do.
x = 4 (the value 4 is an number)
x = “hello” (the value “hello” is a string)
• There are eight basic types available:nil, boolean, number, string, function, userdata, thread, and table
![Page 9: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/9.jpg)
Values and Types (2)
Type Descriptionnil Represents the absence of useful data
boolean True or False value
number real, float, or long integer
string An array of characters
function Allows calls to functions in Lua or C
userdata Allows arbitrary C data to be stored in Lua variables
thread Independent threads of execution and is used to implement coroutines
table Implements associative arrays
![Page 10: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/10.jpg)
Values and Types (3)
• Examples:
print(type("Hello world")) --> string
print(type(10.4*3)) --> number
print(type(print)) --> function
print(type(type)) --> function
print(type(true)) --> boolean
print(type(nil)) --> nil
print(type(type(X))) --> string
![Page 11: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/11.jpg)
Variables (1)
• There are three kinds of variables in Lua: global variables, local variables and table fields. Tables can be created as a list or a record.
• Table can be created using table constructorcolors = {“red”, “green”, “blue”}
time = {hour=13, min=45, sec=34}
• Square brackets ‘[ ]’ are used to index a list:print(colors[1]) --> red
• Records are indexed using a dot notationprint(time.hour) --> 13
List
Record
![Page 12: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/12.jpg)
Variables (2)
• Declaring variables is straightforwarda = 10b = 14
• Lua can perform multiple assignmentsa, b = 10, 14 -- a gets 10 and b
gets 14a, b = b, a -- swap a for bprint(c) --> nil since c has not
be declared
• Local variables can be restricted to different scopes by using the keyword local.
![Page 13: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/13.jpg)
Variables (3)
• Variable Examplex = 10local i = 1
while i <= x do local x = i*2 print(x) i = i + 1end
Output: 2, 4, 6, 8 ...
Global
Local instance
![Page 14: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/14.jpg)
Control Structures (1)
• Like most languages, Lua offers small and conventional set of control structures– Conditionals
•if statements
– Iteration •while•repeat•for
![Page 15: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/15.jpg)
Control Structures (2)
• if Statementif a <= 0 then
a = a + 1 end
if a < 0 then a = a + 1 elseif a > 0 then
a = a – 1else
a = 0end
Nested if-else statement
Simple if statement
![Page 16: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/16.jpg)
Conditional Structures (3)
• While Loop Lua first tests the while condition, if it is false then the loop ends, otherwise it executes the body
a = {1,2,3,4,5}i = 1while a[i] do
print(a[i]) i = i + 1 end
Output: 1,2,3,4,5
![Page 17: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/17.jpg)
Control Structures (4)
• RepeatRepeat-until statement repeats its body
until the condition is true. This ensures that the body of the loop is executed at least once.
a = 0repeat a = a + 1 print(a)until a > 10
Output: 1,2,3,4,5,6,7,8,9,10,11
This block alwaysexecutes at least once
![Page 18: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/18.jpg)
Control Structures (5)
• Numeric For Loop
for i=1,10,1 do
print(i)
end
Output: 1,2,3,4,5,6,7,8,9,10
Initial condition
Evaluate to
Increment by
![Page 19: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/19.jpg)
Control Structures (6)
• Generic For Loop– Allows you to traverse all values returned by
an iterator functioncolors = {“red”, “green”, “blue”}
for i,v in ipairs(colors) do print(i,v)end
Output: 1 red, 2 green, 3 blue
Iterator Functionindex value
![Page 20: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/20.jpg)
Functions
• Functions in Lua are similar to functions in other languages.
function square(x) local sqr = 0 sqr = x * x return sqr
end
• The square function is called as we would expectprint(square(2)) --> 4
• If a function only has one argument then it can be called without parenthesis
result = square “2”print(result) --> 4
![Page 21: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/21.jpg)
Functions (2)
• Closures– A function which returns a function– Has full access to local variables– This allows Lua to implement functional programming
function newCounter()local i = 0return function()
i = i + 1 return i
endend
Anonymousfunction
c1 = newCounter()print(c1()) --> 1print(c1()) --> 2
c2 = newCounter()print(c2()) --> 1print(c1()) --> 3print(c2()) --> 2
![Page 22: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/22.jpg)
Coroutines (1)
• Coroutines are similar to threads but not exactly the same– A program with threads can run several
threads concurrently– Coroutines are collaborative.
• A program with coroutines is running only one of its coroutines and this running coroutine only suspends its execution when it explicitly requests to be suspended
![Page 23: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/23.jpg)
Coroutines (2)
• Creating a coroutine is straightforwardco = coroutine.create(function()
print(“hi”)
end)
• Creating a coroutine returns a value type, thread
print(co) --> thread: 0x8071d98
• Coroutines have three states: suspended, running, and dead. Upon creation the coroutine is in suspended state
![Page 24: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/24.jpg)
Coroutines (3)
• To resume a coroutine we use the function coroutine.resume
coroutine.resume(co) --> hi
• When a coroutine terminates it is now in the dead state, from which it cannot return
• Using the yield function we can suspend the coroutine execution so it can be run later
co = coroutine.create(function()for i=1,10 do print(“co”,i) coroutine.yeild()end
end)
Suspends thecoroutine
![Page 25: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/25.jpg)
Coroutines (4)
• If we resume the coroutine and then check its status we will see the yield function at work
coroutine.resume(co) --> co 1
print(coroutine.status(co)) --> suspended
• To finish the execution we have to continuously resume the coroutine
coroutine.resume(co) --> co 2
coroutine.resume(co) --> co 3
...
coroutine.resume(co) --> co 10
coroutine.resume(co) --> prints nothing
Coroutine has runto completion. It isnow in a dead state
![Page 26: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/26.jpg)
CoroutinesProducer-Consumer Example
• Producer– Produce items to be consumed by the consumer
function producer () return coroutine.create(function ()
item = 1while item < 10 do print("Producing item ", item) coroutine.yield(item) item = item + 1end
end)end
Suspend theproducer
Produce anitem
![Page 27: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/27.jpg)
CoroutinesProducer-Consumer Example
• Consumer– Gets items from the producer
function consumer(producer) while true do local status, value = coroutine.resume(producer)
if status == false then return end print("Consumed item ", value) endend
Consume the itemfrom producer
Check if the produceris dead
![Page 28: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/28.jpg)
CoroutinesProducer-Consumer Example
• Launch the producer-consumerp = producer()consumer(p)
Output:Producing item 1Consumed item 1Producing item 2Consumed item 2Producing item 3Consumed item 3...Producing item 9Consumed item 9Consumed item nil
![Page 29: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/29.jpg)
Object-Oriented Programming (1)
• Tables in Lua are objects– They have states like objects– They have an identity, a selfness, like objects
Account = {balance = 0}
function Account.withdraw(amt)
Account.balance = Account.balance – amt
end
Account.withdraw(19.99)
print(Account.balance) --> -19.99
![Page 30: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/30.jpg)
Object-Oriented Programming (2)
• A more flexible and familiar approach is to operate on the receiver itself
function Account.withdraw(self, amt)self.balance = self.balance – amt
end
a1 = Accounta1.withdraw(a1, 19.99)print(a1.balance) --> -19.99
![Page 31: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/31.jpg)
Object-Oriented Programming (3)
• Classes– Lua does not have the concept of class,
however Lua can emulate classes via prototyping
function Account.new(self, object) object = object or { } setmetatable(object, self) self.__index = self return object
end
Makes the new objecta prototype of Account
![Page 32: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/32.jpg)
Object-Oriented Programming (4)
• So now that we created the concept of an Account class we can create multiple instances of Account.
a1 = Account.new(a1, {balance=0})
a2 = Account.new(a2, {balance=100.25})
Account.withdraw(a1, 100.00)
Account.withdraw(a2, 100.25)
print(a1.balance) --> -100.00
print(a2.balance) --> 0.00
![Page 33: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/33.jpg)
Garbage Collection
• Lua performs automatic memory management
• Users do not have to worry about allocating and freeing memory
• This garbage collector will routinely look for dead objects (objects no longer accessible by Lua)
![Page 34: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/34.jpg)
Conclusion
• Lua is an elegant language, providing easy, readable code
• Lua can easily be extended using C or Lua itself• Lua offers many of the features of much larger
languages– Concurrency– Object-Oriented Programming
• Lua manual link– http://www.lua.org/pil/index.html#P1
![Page 35: Scott Hall](https://reader036.fdocuments.in/reader036/viewer/2022062314/5681351d550346895d9c7c1a/html5/thumbnails/35.jpg)
Scott Hall