Advertisement
karobloxYT

lua 1

Sep 9th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.29 KB | None | 0 0
  1. -- Two dashes start a one-line comment.
  2.  
  3. --[[
  4. Adding two ['s and ]'s makes it a
  5. multi-line comment.
  6. --]]
  7.  
  8. ----------------------------------------------------
  9. -- 1. Variables and flow control.
  10. ----------------------------------------------------
  11.  
  12. num = 42 -- All numbers are doubles.
  13. -- Don't freak out, 64-bit doubles have 52 bits for
  14. -- storing exact int values; machine precision is
  15. -- not a problem for ints that need < 52 bits.
  16.  
  17. s = 'walternate' -- Immutable strings like Python.
  18. t = "double-quotes are also fine"
  19. u = [[ Double brackets
  20. start and end
  21. multi-line strings.]]
  22. t = nil -- Undefines t; Lua has garbage collection.
  23.  
  24. -- Blocks are denoted with keywords like do/end:
  25. while num < 50 do
  26. num = num + 1 -- No ++ or += type operators.
  27. end
  28.  
  29. -- If clauses:
  30. if num > 40 then
  31. print('over 40')
  32. elseif s ~= 'walternate' then -- ~= is not equals.
  33. -- Equality check is == like Python; ok for strs.
  34. io.write('not over 40\n') -- Defaults to stdout.
  35. else
  36. -- Variables are global by default.
  37. thisIsGlobal = 5 -- Camel case is common.
  38.  
  39. -- How to make a variable local:
  40. local line = io.read() -- Reads next stdin line.
  41.  
  42. -- String concatenation uses the .. operator:
  43. print('Winter is coming, ' .. line)
  44. end
  45.  
  46. -- Undefined variables return nil.
  47. -- This is not an error:
  48. foo = anUnknownVariable -- Now foo = nil.
  49.  
  50. aBoolValue = false
  51.  
  52. -- Only nil and false are falsy; 0 and '' are true!
  53. if not aBoolValue then print('twas false') end
  54.  
  55. -- 'or' and 'and' are short-circuited.
  56. -- This is similar to the a?b:c operator in C/js:
  57. ans = aBoolValue and 'yes' or 'no' --> 'no'
  58.  
  59. karlSum = 0
  60. for i = 1, 100 do -- The range includes both ends.
  61. karlSum = karlSum + i
  62. end
  63.  
  64. -- Use "100, 1, -1" as the range to count down:
  65. fredSum = 0
  66. for j = 100, 1, -1 do fredSum = fredSum + j end
  67.  
  68. -- In general, the range is begin, end[, step].
  69.  
  70. -- Another loop construct:
  71. repeat
  72. print('the way of the future')
  73. num = num - 1
  74. until num == 0
  75.  
  76.  
  77. ----------------------------------------------------
  78. -- 2. Functions.
  79. ----------------------------------------------------
  80.  
  81. function fib(n)
  82. if n < 2 then return 1 end
  83. return fib(n - 2) + fib(n - 1)
  84. end
  85.  
  86. -- Closures and anonymous functions are ok:
  87. function adder(x)
  88. -- The returned function is created when adder is
  89. -- called, and remembers the value of x:
  90. return function (y) return x + y end
  91. end
  92. a1 = adder(9)
  93. a2 = adder(36)
  94. print(a1(16)) --> 25
  95. print(a2(64)) --> 100
  96.  
  97. -- Returns, func calls, and assignments all work
  98. -- with lists that may be mismatched in length.
  99. -- Unmatched receivers are nil;
  100. -- unmatched senders are discarded.
  101.  
  102. x, y, z = 1, 2, 3, 4
  103. -- Now x = 1, y = 2, z = 3, and 4 is thrown away.
  104.  
  105. function bar(a, b, c)
  106. print(a, b, c)
  107. return 4, 8, 15, 16, 23, 42
  108. end
  109.  
  110. x, y = bar('zaphod') --> prints "zaphod nil nil"
  111. -- Now x = 4, y = 8, values 15..42 are discarded.
  112.  
  113. -- Functions are first-class, may be local/global.
  114. -- These are the same:
  115. function f(x) return x * x end
  116. f = function (x) return x * x end
  117.  
  118. -- And so are these:
  119. local function g(x) return math.sin(x) end
  120. local g; g = function (x) return math.sin(x) end
  121. -- the 'local g' decl makes g-self-references ok.
  122.  
  123. -- Trig funcs work in radians, by the way.
  124.  
  125. -- Calls with one string param don't need parens:
  126. print 'hello' -- Works fine.
  127.  
  128.  
  129. ----------------------------------------------------
  130. -- 3. Tables.
  131. ----------------------------------------------------
  132.  
  133. -- Tables = Lua's only compound data structure;
  134. -- they are associative arrays.
  135. -- Similar to php arrays or js objects, they are
  136. -- hash-lookup dicts that can also be used as lists.
  137.  
  138. -- Using tables as dictionaries / maps:
  139.  
  140. -- Dict literals have string keys by default:
  141. t = {key1 = 'value1', key2 = false}
  142.  
  143. -- String keys can use js-like dot notation:
  144. print(t.key1) -- Prints 'value1'.
  145. t.newKey = {} -- Adds a new key/value pair.
  146. t.key2 = nil -- Removes key2 from the table.
  147.  
  148. -- Literal notation for any (non-nil) value as key:
  149. u = {['@!#'] = 'qbert', [{}] = 1729, [6.28] = 'tau'}
  150. print(u[6.28]) -- prints "tau"
  151.  
  152. -- Key matching is basically by value for numbers
  153. -- and strings, but by identity for tables.
  154. a = u['@!#'] -- Now a = 'qbert'.
  155. b = u[{}] -- We might expect 1729, but it's nil:
  156. -- b = nil since the lookup fails. It fails
  157. -- because the key we used is not the same object
  158. -- as the one used to store the original value. So
  159. -- strings & numbers are more portable keys.
  160.  
  161. -- A one-table-param function call needs no parens:
  162. function h(x) print(x.key1) end
  163. h{key1 = 'Sonmi~451'} -- Prints 'Sonmi~451'.
  164.  
  165. for key, val in pairs(u) do -- Table iteration.
  166. print(key, val)
  167. end
  168.  
  169. -- _G is a special table of all globals.
  170. print(_G['_G'] == _G) -- Prints 'true'.
  171.  
  172. -- Using tables as lists / arrays:
  173.  
  174. -- List literals implicitly set up int keys:
  175. v = {'value1', 'value2', 1.21, 'gigawatts'}
  176. for i = 1, #v do -- #v is the size of v for lists.
  177. print(v[i]) -- Indices start at 1 !! SO CRAZY!
  178. end
  179. -- A 'list' is not a real type. v is just a table
  180. -- with consecutive integer keys, treated as a list.
  181.  
  182. ----------------------------------------------------
  183. -- 3.1 Metatables and metamethods.
  184. ----------------------------------------------------
  185.  
  186. -- A table can have a metatable that gives the table
  187. -- operator-overloadish behavior. Later we'll see
  188. -- how metatables support js-prototypey behavior.
  189.  
  190. f1 = {a = 1, b = 2} -- Represents the fraction a/b.
  191. f2 = {a = 2, b = 3}
  192.  
  193. -- This would fail:
  194. -- s = f1 + f2
  195.  
  196. metafraction = {}
  197. function metafraction.__add(f1, f2)
  198. sum = {}
  199. sum.b = f1.b * f2.b
  200. sum.a = f1.a * f2.b + f2.a * f1.b
  201. return sum
  202. end
  203.  
  204. setmetatable(f1, metafraction)
  205. setmetatable(f2, metafraction)
  206.  
  207. s = f1 + f2 -- call __add(f1, f2) on f1's metatable
  208.  
  209. -- f1, f2 have no key for their metatable, unlike
  210. -- prototypes in js, so you must retrieve it as in
  211. -- getmetatable(f1). The metatable is a normal table
  212. -- with keys that Lua knows about, like __add.
  213.  
  214. -- But the next line fails since s has no metatable:
  215. -- t = s + s
  216. -- Class-like patterns given below would fix this.
  217.  
  218. -- An __index on a metatable overloads dot lookups:
  219. defaultFavs = {animal = 'gru', food = 'donuts'}
  220. myFavs = {food = 'pizza'}
  221. setmetatable(myFavs, {__index = defaultFavs})
  222. eatenBy = myFavs.animal -- works! thanks, metatable
  223.  
  224. -- Direct table lookups that fail will retry using
  225. -- the metatable's __index value, and this recurses.
  226.  
  227. -- An __index value can also be a function(tbl, key)
  228. -- for more customized lookups.
  229.  
  230. -- Values of __index,add, .. are called metamethods.
  231. -- Full list. Here a is a table with the metamethod.
  232.  
  233. -- __add(a, b) for a + b
  234. -- __sub(a, b) for a - b
  235. -- __mul(a, b) for a * b
  236. -- __div(a, b) for a / b
  237. -- __mod(a, b) for a % b
  238. -- __pow(a, b) for a ^ b
  239. -- __unm(a) for -a
  240. -- __concat(a, b) for a .. b
  241. -- __len(a) for #a
  242. -- __eq(a, b) for a == b
  243. -- __lt(a, b) for a < b
  244. -- __le(a, b) for a <= b
  245. -- __index(a, b) <fn or a table> for a.b
  246. -- __newindex(a, b, c) for a.b = c
  247. -- __call(a, ...) for a(...)
  248.  
  249. ----------------------------------------------------
  250. -- 3.2 Class-like tables and inheritance.
  251. ----------------------------------------------------
  252.  
  253. -- Classes aren't built in; there are different ways
  254. -- to make them using tables and metatables.
  255.  
  256. -- Explanation for this example is below it.
  257.  
  258. Dog = {} -- 1.
  259.  
  260. function Dog:new() -- 2.
  261. newObj = {sound = 'woof'} -- 3.
  262. self.__index = self -- 4.
  263. return setmetatable(newObj, self) -- 5.
  264. end
  265.  
  266. function Dog:makeSound() -- 6.
  267. print('I say ' .. self.sound)
  268. end
  269.  
  270. mrDog = Dog:new() -- 7.
  271. mrDog:makeSound() -- 'I say woof' -- 8.
  272.  
  273. -- 1. Dog acts like a class; it's really a table.
  274. -- 2. function tablename:fn(...) is the same as
  275. -- function tablename.fn(self, ...)
  276. -- The : just adds a first arg called self.
  277. -- Read 7 & 8 below for how self gets its value.
  278. -- 3. newObj will be an instance of class Dog.
  279. -- 4. self = the class being instantiated. Often
  280. -- self = Dog, but inheritance can change it.
  281. -- newObj gets self's functions when we set both
  282. -- newObj's metatable and self's __index to self.
  283. -- 5. Reminder: setmetatable returns its first arg.
  284. -- 6. The : works as in 2, but this time we expect
  285. -- self to be an instance instead of a class.
  286. -- 7. Same as Dog.new(Dog), so self = Dog in new().
  287. -- 8. Same as mrDog.makeSound(mrDog); self = mrDog.
  288.  
  289. ----------------------------------------------------
  290.  
  291. -- Inheritance example:
  292.  
  293. LoudDog = Dog:new() -- 1.
  294.  
  295. function LoudDog:makeSound()
  296. s = self.sound .. ' ' -- 2.
  297. print(s .. s .. s)
  298. end
  299.  
  300. seymour = LoudDog:new() -- 3.
  301. seymour:makeSound() -- 'woof woof woof' -- 4.
  302.  
  303. -- 1. LoudDog gets Dog's methods and variables.
  304. -- 2. self has a 'sound' key from new(), see 3.
  305. -- 3. Same as LoudDog.new(LoudDog), and converted to
  306. -- Dog.new(LoudDog) as LoudDog has no 'new' key,
  307. -- but does have __index = Dog on its metatable.
  308. -- Result: seymour's metatable is LoudDog, and
  309. -- LoudDog.__index = LoudDog. So seymour.key will
  310. -- = seymour.key, LoudDog.key, Dog.key, whichever
  311. -- table is the first with the given key.
  312. -- 4. The 'makeSound' key is found in LoudDog; this
  313. -- is the same as LoudDog.makeSound(seymour).
  314.  
  315. -- If needed, a subclass's new() is like the base's:
  316. function LoudDog:new()
  317. newObj = {}
  318. -- set up newObj
  319. self.__index = self
  320. return setmetatable(newObj, self)
  321. end
  322.  
  323. ----------------------------------------------------
  324. -- 4. Modules.
  325. ----------------------------------------------------
  326.  
  327.  
  328. --[[ I'm commenting out this section so the rest of
  329. -- this script remains runnable.
  330. -- Suppose the file mod.lua looks like this:
  331. local M = {}
  332.  
  333. local function sayMyName()
  334. print('Hrunkner')
  335. end
  336.  
  337. function M.sayHello()
  338. print('Why hello there')
  339. sayMyName()
  340. end
  341.  
  342. return M
  343.  
  344. -- Another file can use mod.lua's functionality:
  345. local mod = require('mod') -- Run the file mod.lua.
  346.  
  347. -- require is the standard way to include modules.
  348. -- require acts like: (if not cached; see below)
  349. local mod = (function ()
  350. <contents of mod.lua>
  351. end)()
  352. -- It's like mod.lua is a function body, so that
  353. -- locals inside mod.lua are invisible outside it.
  354.  
  355. -- This works because mod here = M in mod.lua:
  356. mod.sayHello() -- Says hello to Hrunkner.
  357.  
  358. -- This is wrong; sayMyName only exists in mod.lua:
  359. mod.sayMyName() -- error
  360.  
  361. -- require's return values are cached so a file is
  362. -- run at most once, even when require'd many times.
  363.  
  364. -- Suppose mod2.lua contains "print('Hi!')".
  365. local a = require('mod2') -- Prints Hi!
  366. local b = require('mod2') -- Doesn't print; a=b.
  367.  
  368. -- dofile is like require without caching:
  369. dofile('mod2.lua') --> Hi!
  370. dofile('mod2.lua') --> Hi! (runs it again)
  371.  
  372. -- loadfile loads a lua file but doesn't run it yet.
  373. f = loadfile('mod2.lua') -- Call f() to run it.
  374.  
  375. -- loadstring is loadfile for strings.
  376. g = loadstring('print(343)') -- Returns a function.
  377. g() -- Prints out 343; nothing printed before now.
  378.  
  379. --]]
  380.  
  381. ----------------------------------------------------
  382. -- 5. References.
  383. ----------------------------------------------------
  384.  
  385. --[[
  386.  
  387. I was excited to learn Lua so I could make games
  388. with the Löve 2D game engine. That's the why.
  389.  
  390. I started with BlackBulletIV's Lua for programmers.
  391. Next I read the official Programming in Lua book.
  392. That's the how.
  393.  
  394. It might be helpful to check out the Lua short
  395. reference on lua-users.org.
  396.  
  397. The main topics not covered are standard libraries:
  398. * string library
  399. * table library
  400. * math library
  401. * io library
  402. * os library
  403.  
  404. By the way, this entire file is valid Lua; save it
  405. as learn.lua and run it with "lua learn.lua" !
  406.  
  407. This was first written for tylerneylon.com. It's
  408. also available as a github gist. Tutorials for other
  409. languages, in the same style as this one, are here:
  410.  
  411. http://learnxinyminutes.com/
  412.  
  413. Have fun with Lua!
  414.  
  415. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement