-
Notifications
You must be signed in to change notification settings - Fork 12
/
lua-cs.tex
543 lines (413 loc) · 14 KB
/
lua-cs.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
%% lua-cs.tex
%
% Copyright 2018 Rudy Matela
%
% This text is available under (at your option):
% * Creative Commons Attribution-ShareAlike 3.0 Licence
% * GNU Free Documentation License version 1.3 or Later
%
\documentclass{refcard}
\usepackage[T1]{fontenc}
\renewcommand{\familydefault}{\sfdefault}
\newcommand{\X}{\I{x}}
\newcommand{\Y}{\I{y}}
\newcommand{\Z}{\I{z}}
\newcommand{\W}{\I{w}}
\newcommand{\F}{\I{f}}
\title{Concise Lua Cheat Sheet}
\cright{
Copyright 2018, Rudy Matela --
Compiled on \today{} \\
Upstream: \texttt{https://github.com/rudymatela/concise-cheat-sheets}
}{
This text is available under
the Creative Commons Attribution-ShareAlike 3.0 Licence, \\
\textbf{or} (at your option), the GNU Free Documentation License version 1.3 or Later.
}
\version[~\\]{0.0 (WORK IN PROGRESS)}
% Useful links
% ============
%
% Changes
% -------
%
% 5.2 -> 5.3: http://www.lua.org/manual/5.3/readme.html#changes
% 5.1 -> 5.2: http://www.lua.org/manual/5.2/readme.html#changes
% 5.0 -> 5.1: http://www.lua.org/manual/5.1/manual.html#7.1
%
%
% Book
% ----
%
% Programming in Lua (first edition), Lua 5.0:
% http://www.lua.org/pil/contents.html
\begin{document}
\maketitle
\begin{center}
{\Large \textbf{NOTE:} this is a work in progress}
\end{center}
\section{Structure}
\begin{verbatim}
function funct (n,m)
statements
return value
end
statement
statement
\end{verbatim}
\section{Types}
\begin{ldesc}
\li[nil] nil
\li[boolean] false, true
\li[number] 0, 1, -1, 12.34
\li[string] "double quotes", 'single quotes', [[ \li
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~multi \li
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~line \li
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~]] \li
\li[function] function (x) return x * 2 end
\li[table] \{\}, \{["ab"]="cd", [12]=34\}, \{ab="cd"\}
\end{ldesc}
\section{Tables}
\subsection{Initializing}
\begin{ldesc}
\li[empty] \{\}
\lI[list (indexed by 1)] \{\X, \Y, \Z\}
\li[record] \{\X=\I{value}, \Y=\I{value}\}
\li[mixed record and list] \{\I{length}=2, \X, \Y\}
\lI[general forms] \{[1]=\X, [2]=\Y, [3]=\Z\} \li[~\small (equivalent to the ones above)]
\{["\X"]=\I{value}, ["\Y"]=\I{value}\} \li
\{["\I{length}"]=2, [1]=\X, [2]=\Y\}
\end{ldesc}
\subsection{Acessing}
\begin{ldesc}
\li[\hfill consider:~~] table = \{\I{length}=2, \X, \Y\}
\lI[acessing via string indices] table["length"] = 2 \li
table.length = 2
\lI[accessing integer positions] table[1] = \X \li
table[2] = \Y
\lI[unassigned positions are \I{nil}] table["pos"] = \I{nil} \li
table.pos = \I{nil}
\lI[record syntax only works] table.1*2+asdf ~~~-{-} wrong!
\li[~~for valid identifiers] table["1*2+asdf"] -{-} ok!
\end{ldesc}
\section{Assignments}
\begin{ldesc}
\li[assign variable \I{x} the value \I{expr}] \X~= \I{expr}
\li[assign field \Y~on table \X] \X.\Y~= expr
\li[multiple assignment] \X,~\Y~=~\I{exprX},~\I{exprY}
\li[swap values] \X,~\Y~=~\Y,~\X
\li[missing values (\Z, \W) are nil] \X,~\Y,~\Z,~\W~= \I{exprX},~\I{exprY}
\li[extra values are ignored] \X~= \I{exprX}, \I{ignored}, \I{ignored}
\li[fns. can return multiple vals.] \X,~\Y~=~\F()
\li[local (to fn./chunk/block) var.] local \X~= \I{expr}
\li[local nil var.] local \Y
\end{ldesc}
\section{Control Structures and Blocks}
\begin{ldesc}
\li[do block (for scoping)] do \li
~~\I{statements} \li
end
\lI[if statement] if \I{condition} then \li
~~\I{statements} \li
elseif \I{condition} then \li
~~\I{statements} \li
else \li
~~\I{statements} \li
end
\lI[while statement] while \I{condition} do \li
~~\I{statements} \li
end
\lI[repeat-until statement] repeat \li[~(always executes once)]
~~\I{statements} \li
until \I{condition}
\lI[numeric for (inclusive)] for \I{i}=\I{start},\I{end} do \li[~(\I{i} is local)]
~~\I{statements} \li[~(never change \I{i} manually)]
end
\lI[numeric for (w/step)] for \I{i}=\I{start},\I{end},\I{step} do \li
~~\I{statements} \li
end
\lI[generic for]
for \I{cvars} in \I{iterator(args)} do \li
~~\I{statements} \li
end
\lI[exit loop] break
\end{ldesc}
\section{Operators (grouped by precedence)}
\begin{Ldesc}
\Li[exponentiation] \hfill \^{}
\Li[unary: numeric and bool negation] not -
\Li[multiplication, float division] *, /
\Li[addition, subtraction] +, -
\Li[string concatenation] \hfill ..
\Li[comparisons] ==, \~{}=, <, >, <=, >=~~~~~~
\Li[boolean conjunction] and
\Li[boolean disjunction] or
\end{Ldesc} \\[1ex]
NOTE: \texttt{\^{}} and \texttt{..} are right associative. The rest are left associative.
\section{Functions}
\subsubsection{Function Calls}
\begin{ldesc}
\li[procedure call (action)] \F(\I{args})
\li[call and assign return value] \X~=~\F(\I{args})
\li[call within expression] \Y~=~\X~+~\F(\Z,\W)
\lI[call with numbers] \F(42,1.0)
\li[call with string] \F("\I{string}")
\li[call with single string] \F~"\I{string}
\li[call with table constructor] \F(\{\X=\Z, \Y=\W\})
\li[call with single table constructor] \F\{\X=\Z, \Y=\W\}
\end{ldesc}
\subsubsection{Function Declaration}
\begin{ldesc}
\li[no arguments]
function \F~() \li
~~\I{statements} \li
~~return \I{expression} ~-{-} optional \li
end
\li[single argument]
function \F~(\X)~\I{statements}~end
\li[multiple arguments]
function \F~(\X,\Y)~\I{statements}~end
\li[as assignment $\equiv$ to above]
\F~=~function (\X,\Y)~\I{statements}~end
\end{ldesc}
\subsubsection{Function Arguments}
\begin{ldesc}
\lI[\hfill consider:~~~]
function \F(\I{a},\I{b}) \I{stmts}~end
\li[valid: missing argument are \I{nil}]
\F(\X) ~~~~~-{-}~\I{a}==\X~\I{b}==nil
\li[valid: extra args. are discarded]
\F(\X,\Y,\Z) ~-{-}~\I{a}==\X~\I{b}==\Y
\lI[useful: override nil arguments]
function \F~(\X) \li[~(if \X~is nil it is replaced)]
~~\X~= \X~or \I{defaultX} \li
~~\I{statements} \li
end
\end{ldesc}
\subsubsection{Function Results}
% TODO: Improve formatting:
% - Maybe remove captions
% - Use tabulation instead of ~~~~ to show meaning
% - remove \I{a} and \I{b}, use \U, \V, \W
\begin{ldesc}
\li[\hfill consider:~~~]
function \F() return \I{a},\I{b} end
\lI[assign return values]
\X, \Y~= \F() ~~~~~~-{-} \X=\I{a} \Y=\I{b}
\li[discard extra return values]
\X~= \F() ~~~~~~~~~-{-} \X=\I{a}
\li[extra vars. are set to nil]
\X, \Y, \Z~= \F() ~~~-{-} \X=\I{a} \Z=nil
\li[fn. at end of assign list]
\X, \Y, \Z~= \I{c}, \F() -{-} \X=\I{a} \Z=\I{b}
\li[fn. in assign list]
\X, \Y~= \F(), \I{c} ~~~-{-} \X=\I{a} \Y=\I{c}
\li[fn. at end of arg. list]
\I{g}(\I{c},\F()) ~~~~~~~~-{-} g(\I{c},\I{a},\I{b})
\li[fn. in arg. list]
\I{g}(\F(),\I{c}) ~~~~~~~~-{-} g(\I{a},\I{c})
\li[fn. at end of constr.]
\{\I{c},\F()\} ~~~~~~~~~-{-} \{\I{a}, \I{b}, \I{c}\}
\li[fn. in constr.]
\{\F(),\I{c}\} ~~~~~~~~~-{-} \{\I{a}, \I{c}\}
\li[fn. in expr. (one result)]
\F() + \X ~~~~~~~~~ -{-} \I{a} + \X
\li[force single result]
(\F())
\li[unpack table]
\X, \Y~= unpack\{\I{a}, \I{b}, \I{c}\} -{-} \X=\I{a} \Y=\I{b}
\li[unpack table (in fun arg)]
\F(unpack(\{\X,\Y\})) -{-} \F(\X,\Y)
\end{ldesc}
% Lua gotcha!:
% f(unpack{x,nil,y}) == f(x,nil,y)
% f(unpack{x,nil,y,nil}) == f(x)
% unpack uses only the numbered arguments
%
% This maybe has to deal with the "n" hidden variable
% it only appears on the ones for which lua stores it??
\section{Functions (Advanced)}
\subsubsection{Variable number of arguments}
\begin{ldesc}
\li[assigning directly]
function \F (...) \li
~~\I{arg1}, \I{arg2}, \I{arg3}~= ... \li
~~\I{statements} \li
end
\lI[packing in a table]
function \F (...) \li
~~\I{args} = table.pack(...) \li
~~\I{statements} \li
end
\lI[fixed and rest]
function \F (\X, \Y, ...)~\li
~~\I{statements} \li
end
\lI[select implementation]
function select (n, ...) \li
~~return table.pack(...)[n] \li
end
\end{ldesc} \\[1ex]
{NOTE: The ellipses (\texttt{...}) above are literal}
\subsubsection{Named arguments}
\begin{ldesc}
\li[declaration]
function \F (arg) \li
~~statement ~ -{-} using arg.\I{x}, arg.\I{y}, etc... \li
end
\li[call]
\F\{\I{a}=\X, \I{b}=\Y\}
% Bad example? This could be done by overwriting on arg
% \lI[default args]
% function \_\F(arg) ... end \li
% function \F (arg) \li
% ~~\_\F~(\X~=~arg.\X~or~\I{a}, \li
% ~~~~~ \Y~=~arg.\Y~or~\I{b}) \li
% end
\end{ldesc}
\subsubsection{Tail calls}
\begin{ldesc}
\li[tail call]
function \F(\X,\Y) \li
~~\I{statements} \li
~~return \I{g}(\Z,\W) \li
end
\end{ldesc}
\section{Libraries and Chunks}
\begin{ldesc}
\li[Load library \I{lib}] dofile("lib.lua")
\lI[defining (explictly)]
\I{MyLib} = \{\} \li
\I{MyLib.f}~=~function (\X) ...~end \li
\I{MyLib.g}~=~function (\X,\Y) ...~end
\lI[defining (implicitly)]
\I{MyLib} = \{\} \li
function \I{MyLib.f} (\X) \li
~~... \li
end \li
function \I{MyLib.g} (\X,\Y) \li
~~... \li
end
\lI[local functions (chunk)]
local function \F~(\X) \li
~~... \li
end
\end{ldesc}
% TODO: document the select function:
% select(1,10,20) == 10
% select(2,10,20) == 20
\section{Comments}
\begin{ldesc}
\li[Single line comment] -{-} this is a comment
\li[Multi line comment] -{-}[[ \li
This is a comment \li
-{-}]]
\li[Cancel multi line comment] -{-}-[[ \li
print("not a comment") \li
-{-}]]
\end{ldesc}
\section{Misc}
\begin{itemize}
\item Lua is CaSe SeNsItIvE
\item Tables, userdata and functions are compared by reference:
\begin{itemize}
\item \C{~~~~~~~~~~~~~~~~\{\} ~\~{}=~ \{\}}
\item \C{ ~~function () end ~\~{}=~ function () end}
\end{itemize}
\item In interactive mode, each line is a chunk
\begin{itemize}
\item local declarations expire on next line
\item use do to test local variables
\end{itemize}
\end{itemize}
\section{Input/Output}
\begin{ldesc}
\li[Print line with \I{value}] print(\I{value})
\li[Print line with \I{"string"}] print "string" \li % this looks like syntatic sugar when it is a string
print("string")
\end{ldesc}
\section{Flow of Control}
\begin{ldesc}
\li[newline terminated statements] statement \li
statement
\li[semicolon terminated statements] statement; \li
statement;
\li[semicolon (single line)] statement; statement
\li[double statements on single line (ugly)] statement statement
\end{ldesc}
% TODO: Write about iterators (list most important, and how to create one)
% Section 7.1
\section{Iterators}
\section{Interpreter}
\begin{ldesc}
\li[load interpreter] \$ lua
\li[run \I{file}] \$ lua \I{file}.lua
\li[run then load interpreter] \$ lua -i \I{file}.lua
\li[run then load interpreter] \$ lua -l\I{file}
\li[lua shebang] \#!/usr/bin/env lua
\li[run file (with shebang)] ./\I{file}
\li[run expression] \$ lua -e "\I{expr}"
\end{ldesc}
\pagebreak
\section{Standard Libraries}
\subsubsection{Basic (already imported)}
\begin{ldesc}
\li[Raise error if \I{v} is \C{false}] assert(\I{v}, \I{"message"})
\li[Execute chunk in \I{filename}] dofile(\I{"filename"})
\li[Raise error \C{\I{message}}] error(\I{"message"})
\li[Iterate over table numeric indexes] for \I{i},\I{v} in ipairs(\I{t}) do
\li[Load chunk as fn. (nil on error)] load(\I{"chunk"})
\li[Load chunk on file as a function] loadfile(\I{"filename"})
% \li[Traverse table] \I{newidx}, \I{value} = \li
% ~~next(\I{t}, \I{oldidx})
% \li[Traverse table]
% i = nil \li
% repeat \li
% ~~\I{i}, \I{v} = next(\I{t}, \I{i}) \li
% ~~\I{statement using v} \li
% until i == nil
\li[Iterate over table values] for \I{k},\I{v} in pairs(\I{t}) do
\li[Protected function call] stat, retVal = pcall(\F,\I{args})
\li[Select \I{\C{n}}-th argument] select(\I{n},args)
\li[String to number] tonumber("\I{n}")
\li[String to number (base)] tonumber("\I{n}",b)
\li[Type as a string] type(\I{value})
% from package but exported globally
\li[Loads a given module]
require("\I{modname}")
\end{ldesc}
\subsubsection{String Manipulation}
\begin{ldesc}
\li[Get numeric value of first char] string.byte("\I{c}")
\li[Get chars from numeric values] string.char(97,98,99) ~-{-} "abc"
\li[Find occurrence in string] string.find("\I{string}","\I{pattern}")
\li[??] string.format(??)
\li[??] string.gmatch(??)
\li[??] string.gsub(??)
\li[??] string.len(??)
\li[??] string.lower(??)
\li[??] string.match(??)
\li[??] string.pack(??)
\li[??] string.rep(??)
\li[Return reversed \C{\I{str}}] string.reverse(\I{str})
\li[??] string.sub(??)
\li[??] string.unpack(??)
\li[??] string.upper(??)
\end{ldesc}
\subsubsection{Table}
\begin{ldesc}
\li[??] table.concat(??)
\li[??] table.insert(??)
\li[??] table.move(??)
\li[??] table.pack(??)
\li[??] table.remove(??)
\li[??] table.sort(??)
\li[??] table.unpack(??)
\end{ldesc}
\subsubsection{Mathematical Functions}
\begin{ldesc}
\li[Trigonometry] math.acos math.asin math.atan math.cos
\li[Max and min arg] math.max(\X,\Y,\Z,\W) -{-} or math.min
\li[Max and min int] math.mininteger() -{-} or math.maxinteger
\end{ldesc}
\end{document}