the lua programming language background
play

The Lua Programming Language Background Created in 1993 - PowerPoint PPT Presentation

Pantea Haghighatkhah, Laurens Post, Jelle Mulyadi, Norico Groeneveld, Nina Schoeber January 18, 2018 The Lua Programming Language Background Created in 1993 Petrobras (Brazil) Foreign software restricted In-house


  1. Pantea Haghighatkhah, Laurens Post, Jelle Mulyadi, Norico Groeneveld, Nina Schoeber January 18, 2018 The “Lua” Programming Language

  2. Background

  3. • Created in 1993 • Petrobras (Brazil) • Foreign software restricted • In-house software 1 History of Lua

  4. • Replacement of internal languages • Tcl, Perl & Forth unsuitable • Intentionally minimal and forgiving • Very little original syntax 2 Creation

  5. • Highly portable • High performance due to data visualization • Easy to embed and extend 3 Creation (II)

  6. • Very small codebase • Interpreter written in C • Fully modular 4 Result

  7. • Easy to learn • Extensibility Dynamic associative arrays Reflective facilities Fallbacks • Very minimal, unused features removed 5 Advantages of Lua

  8. • C++ • Scheme • Javascript • Julia • Ruby 6 Influence Influences: Influenced by:

  9. Properties

  10. • Imperative • Dynamically typed • Lexically scoped locals, but global by default • Strict evaluation • The usual call-by-reference / call-by-value distinction 7 Lua properties

  11. • Nil • Number (double) • String • Boolean (since 5.0) • Function • Table All are first-class No classes, no arrays, no structs, no enums, no static / public / private, no integers(ish) 8 Primitives

  12. Semantics

  13. 9 2 a = 1 b = a*2 1 a = 1 ; b = a*2 1 b = a*2; a = 1; 1 b = a*2 2 a = 1 1 Semantics • Chunk: A series of statements. Separation by ‘;’ is optional. • Operations: • Arithmetic: Addition + , subtraction - , multiplication * , unary - and power ̂ . • Relational: < , > , <= , >= , == , ~= Comparison by reference. • Logical: and , or , not , only false and nil are considered false.

  14. 10 2 --> 1 2 print(a,b) 4 a, b = a+1, b+1, b+2 -- b+2 is ignored 3 print(a,b,c) --> 0 1 nil a, b, c = 0, 1 • Multiple assignments: Evaluates first 1 • Adjusts the number of values x, y = y, x --swaps x and y 2 a, b = 10, 2*x 1 Semantics: Assignments

  15. 11 1 a = b 5 end 6 print (x) --> nil • Have local parameters in a chunk. local x , y = 2, 10 -- both x and y are local local x = 10 2 if x < y do 3 x = y -- x becomes 10 4 end 4 3 a = b 1 2 a, b = 1 , 2 1 print (x) --> 10 6 end 5 while a < b do 4 x = 10 3 while a < b do 2 a, b = 1 , 2 Semantics: Assignments • Local variables: Only by using “local” statement

  16. 12 r = 1 end 7 return r 6 end 5 r = r* v 4 for i, v in ipars(...) do 3 2 • Functions can have multiple results: function product (...) 1 • Variable arguments: a , b = foo() 4 end 3 return 1, 2 2 function foo () 1 Semantics: Functions

  17. 13 • First class functions & Lexical scoping print(s2()) --> 8 4 print(s1()) --> 1 3 s2 = startCounting(7) 2 s1 = startCounting(0) -- pass the arguments needed 1 • Make closures an upvalue . end 6 return i end 5 i = i+1 4 return function () 3 local i = t 2 function startCounting (t) 1 Semantics: Closure i is called an external local variable or

  18. • Keeps the state after successive calls. • Two steps: • A factory function. 1 ipairs(...) • The closure. 14 Closure: Iterator • Iterators are closures .

  19. 15 6 --creating the iterator (closure) 3 iter = list_iter(t) 4 while true do 5 -- calling the iterator local element = iter() t = {10, 20, 30} 7 if element == nil then break end 8 print(element) 9 end 2 1 5 2 end 8 end 7 1 if i <= n then 6 i = i + 1 function list_iter (t) return function () 4 local n = table.getn(t) 3 local i = 0 Iterator: Example Closure Factory for lists return t[i] end

  20. More simple way: 1 t = {10, 20, 30} 2 for element in list_iter(t) do 3 print(element) 4 end 16 Iterator: Example

  21. 1 function f(x) 2 return g(x) 3 end 17 Semantics: Tail recursion • Call to another function as last action : • Stores no information from the calling function. ⇒ no stack overflow. • No extra stack space for a tail call =

  22. Error handling

  23. Raising an error 1 n = getInput() 2 if not n then error("invalid input") end 1 n = assert(getInput(), "invalid input") 2 -- returns first argument if it's not false or nil 3 -- otherwise throws error with optional second argument as message Mind the strict evaluation Don’t build an error message with string concatenation or use the explicit error test 18 Error handling: raising

  24. 19 5 end 8 ... 7 -- foo raised an error, value is error message 6 else ... Put code that may raise an error in a function 4 -- no errors while running foo, value is return value of foo 3 if success then 2 success, value = pcall(foo, argument) 1 Catch errors with pcall Error handling: catching

  25. Error because of invalid function call 1 function iNeedAString(str) 2 ... -- do something that needs a string 3 end 4 5 maybeAString = 1 6 iNeedAString(maybeAString) Where should you check, before calling or inside the called function? 20 Error handling: blaming

  26. 21 end Default is 1 2 Is passed as level in the calling hierarchy, to blame the caller end 6 ... 5 4 The error isn’t caused by the called function error("string expected", 2) 3 if type(str) ~= "string" then 2 function foo (str) 1 Error handling: blaming

  27. Metaprogramming

  28. 1 f = load("i = i + 1") 2 i = 1 3 f() 4 print(i) -- 2 Any code can be used But only string representation Not designed for advanced purposes 22 Metaprogramming

  29. Coroutines

  30. No threats in Lua Coroutines are a bit like threads Threads can be run concurrently Program with coroutines can only run one coroutine at a time But a running coroutine is only suspended when requested 23 Coroutines

  31. 24 6 Just a function? Suspended -> running -> dead print(coroutine.status(co)) -- dead 8 -- I'm running coroutine.resume(co) 7 print(coroutine.status(co)) -- suspended -- thread: 0x02670ba8 1 print(co) 5 4 end) 3 print("I'm running") 2 co = coroutine.create(function () Coroutines

  32. 25 9 coroutine.resume(co) -- (error: cannot resume dead coroutine) 14 -- Running # 10 coroutine.resume(co) 13 ... 12 -- Running # 2 coroutine.resume(co) 11 print(coroutine.status(co)) -- suspended 10 -- Running # 1 coroutine.resume(co) print(coroutine.status(co)) -- suspended Suspending execution 8 7 end) 6 end 5 coroutine.yield() 4 print("Running #", i) 3 for i=1,10 do 2 co = coroutine.create(function () 1 Coroutines

  33. Passing data with resume 1 co = coroutine.create(function (arg) 2 print(arg) 3 end) 4 coroutine.resume(co, 1) -- 1 26 Coroutines: exchanging data

  34. 27 1 print(coroutine.resume(co)) -- true 1 2 3 4 end) 3 return 1, 2, 3 2 co = coroutine.create(function () print(coroutine.resume(co)) -- true 1 2 3 Passing data with yield or return 4 end) 3 coroutine.yield(1, 2, 3) 2 co = coroutine.create(function () 1 Coroutines: exchanging data

  35. Similar to generators in Python, but coroutines can be suspended from auxiliary functions Coroutines enable non-preemptive multithreading Explicit synchronization 28 Coroutines

  36. Language structure

  37. Dynamic associative arrays Main data structure No fixed size Constructor expression 1 a = {} -- Empty table 2 x = "y" 3 a.x = 5 -- a["x"] = 5 4 a[x] = 10 -- a["y"] = 10 29 Tables

  38. Dynamic associative arrays Main data structure No fixed size Constructor expression 1 a = {} -- Empty table 2 x = "y" 3 a.x = 5 -- a["x"] = 5 4 a[x] = 10 -- a["y"] = 10 29 Tables

  39. Metatables Fallback methods Inheritance Overriding 30 Object Oriented Programming

  40. 31 9 end 15 return o 14 self._index = self 13 setmetatable(o, self) 12 o = o or {} 11 function Rectangle:new(o) 10 -- Constructor 8 1 end} 7 self.y = self.y * factor 6 self.x = self.x * factor 5 scale = function(self, factor) 4 y = 10, -- default y 3 Rectangle = {x = 5, -- default x 2 -- Prototype Metatables

  41. 31 9 end 15 return o 14 self._index = self 13 setmetatable(o, self) 12 o = o or {} 11 function Rectangle:new(o) 10 -- Constructor 8 1 end} 7 self.y = self.y * factor 6 self.x = self.x * factor 5 scale = function(self, factor) 4 y = 10, -- default y 3 Rectangle = {x = 5, -- default x 2 -- Prototype Metatables

  42. 1 myRectangle = Rectangle:new{x = 20} -- Table {x = 20} with metatable Rectangle 2 print(myRectangle.x) -- 20, overriden x 3 print(myRectangle.y) -- 10, default y, inherited from Rectangle 32 Inheritance and overriding

  43. 33 2 enabled collectgarbage() -- Cannot collect 'val' unless weak values are 5 end 4 t[key] = val 3 local val = {} do Weak tables 1 Example setmetatable(t, {__mode = 'k'}) -- Weak keys 3 setmetatable(t, {__mode = 'v'}) -- Weak values 2 t = {} 1 Do not prevent garbage collection Garbage collection

  44. Embedding

  45. • Allow extension • Simplification • Compiled at run-time • Configuration and customization • Example extension: AddOns in World of Warcraft 34 Why embed Lua?

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend