-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
542 lines (514 loc) · 26.2 KB
/
index.html
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
<script src="https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js"></script>
<!Doctype html>
<html>
<head>
<title></title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" href="css/style.css">
<link href="https://fonts.googleapis.com/css?family=Roboto&display=swap" rel="stylesheet">
</head>
<body>
<nav id="navbar">
<header>
<h1>Lua Documentation</h1>
</header>
<ul>
<li><a class="nav-link" href="#introduction" rel="internal">Introduction</a></li>
<li><a class="nav-link" href="#what_you_shold_know_?" rel="internal">What you shold know ?</a></li>
<li><a class="nav-link" href="#history" rel="internal">History</a></li>
<li><a class="nav-link" href="#hello_world" rel="internal">Hello World</a></li>
<li><a class="nav-link" href="#variables" rel="internal">Variables</a></li>
<li><a class="nav-link" href="#variable_declaration" rel="internal">Variable Declaration</a></li>
<li><a class="nav-link" href="#variable_scope" rel="internal">Variable Scope</a></li>
<li><a class="nav-link" href="#data_types" rel="internal">Data Types</a></li>
<li><a class="nav-link" href="#operators">Operators</a></li>
<li><a class="nav-link" href="#conditionals" rel="internal">Conditionals</a></li>
<li><a class="nav-link" href="#loops" rel="internal">Loops</a></li>
<li><a class="nav-link" href="#functions" rel="internal">Functions </a></li>
<li><a class="nav-link" href="#reference" rel="internal">Reference</a></li>
</ul>
</nav>
<!--10 p elementos, 5 code elementos, 5 li elementos-->
<main id="main-doc">
<!--Introduction to Lua-->
<section id="introduction" class="main-section">
<header>Introduction</header>
<p>
Lua is a powerful, efficient, lightweight, embeddable scripting language. It supports procedural programming, object-oriented programming, functional programming, data-driven programming, and data description.
</p>
<p>
Lua combines simple procedural syntax with powerful data description constructs based on associative arrays and extensible semantics. Lua is dynamically typed, runs by interpreting bytecode with a register-based virtual machine, and has automatic memory management with incremental garbage collection, making it ideal for configuration, scripting, and rapid prototyping.
</p>
</section>
<section id="what_you_shold_know_?" class="main-section">
<header>What you shold know ?</header>
<p>
This guide assumes you have the following background:
</p>
<ul>
<li>A general understanding of the programming logic.</li>
<li>Some expirience with the terminal on your Operetion System.</li>
</ul>
</section>
<section id="history" class="main-section">
<header>History</header>
<p>
Lua was created in 1993 by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes, members of the Computer Graphics Technology Group (Tecgraf) at the Pontifical Catholic University of Rio de Janeiro, in Brazil.
</p>
<p>
From 1977 until 1992, Brazil had a policy of strong trade barriers (called a market reserve) for computer hardware and software. In that atmosphere, Tecgraf's clients could not afford, either politically or financially, to buy customized software from abroad. Those reasons led Tecgraf to implement the basic tools it needed from scratch.
</p>
<p>
Lua 1.0 was designed in such a way that its object constructors, being then slightly different from the current light and flexible style, incorporated the data-description syntax of SOL
</p>
<p>
Lua semantics have been increasingly influenced by Scheme over time,especially with the introduction of anonymous functions and full lexical scoping. Several features were added in new Lua versions.
</p>
<p>
Versions of Lua prior to version 5.0 were released under a license similar to the BSD license. From version 5.0 onwards, Lua has been licensed under the MIT License. Both are permissive free software licences and are almost identical.
</p>
</section>
<section id="hello_world" class="main-section">
<header>Hello World</header>
<p>
A program thats prints a "Hello World" will be like this:
</p>
<code>print("Hello World")</code>
</section>
<section id="variables" class="main-section">
<header>Variables</header>
<p>
A variable is nothing but a name given to a storage area that our programs can manipulate. It can hold different types of values including functions and tables.
<br>
The name of a variable can be composed of letters, digits, and the underscore character. It must begin with either a letter or an underscore. Upper and lowercase letters are distinct because Lua is case-sensitive.
</p>
<p>
In Lua, though we don't have variable data types, we have three types based on the scope of the variable.
</p>
<ul>
<li>Global variables − All variables are considered global unless explicitly declared as a local.</li>
<li>Local variables − When the type is specified as local for a variable then its scope is limited with the functions inside their scope.</li>
<li>Table fields − This is a special type of variable that can hold anything except nil including functions.</li>
</ul>
</section>
<section id="variable_declaration" class="main-section">
<header>Variable Declaration</header>
<p>
To declare a variable we will use the follow code:
</p>
<pre>
<code>
--variable declaration':
local a = 10; --a is local
d, f=5; --f is global, d is nill
</code>
</pre>
</section>
<section id="variable_scope" class="main-section">
<header>Variable Scope</header>
<p>
Variables can be optionally local or type specified making it global.
</p>
<pre>
<code>
local i, j --local variable
local i
a, c --global variable
</code>
</pre>
</section>
<section id="data_types" class="main-section">
<header>Data Types</header>
<p>
Lua is a dynamically typed language, so the variables don't have types, only the values have types. Values can be stored in variables, passed as parameters and returned as results.
</p>
<p>The list of data types for values are given below:</p>
<ul>
<li>nil - Used to differentiate the value from having some data or no(nil) data.</li>
<li>boolean - Includes true and false as values. Generally used for condition checking.</li>
<li>number - Represents real(double precision floating point) numbers.</li>
<li>string - Represents array of characters.</li>
<li>function - Represents a method that is written in C or Lua.</li>
<li>userdata - Represents arbitrary C data.</li>
<li>thread - Represents independent threads of execution and it is used to implement coroutines.</li>
<li>table - Represent ordinary arrays, symbol tables, sets, records, graphs, trees, etc., and implements associative arrays. It can hold any value (except nil).</li>
</ul>
<p>To to know the type of the variable we can use the following code:</p>
<code>
type(5.8*t) --return the type of that variable.
</code>
</section>
<section id="operators" class="main-section">
<header>Operators</header>
<p>
An operator is a symbol that tells the interpreter to perform specific mathematical or logical manipulations.
</p>
<p>Arithmetic Operators</p>
<table>
<tr>
<th>Operators</th>
<th>+</th>
<th>-</th>
<th>*</th>
<th>/</th>
<th>%</th>
<th>^</th>
<th>-</th>
</tr>
<tr>
<th>Description</th>
<th>Adds two operands</th>
<th>Subtracts second operand from the first</th>
<th>Multiply both operands</th>
<th>Divide numerator by de-numerator</th>
<th>Modulus Operator and remainder of after an integer division</th>
<th>Exponent Operator takes the exponents</th>
<th>Unary - operator acts as negation</th>
</tr>
</table>
<p>Relational Operators</p>
<table>
<tr>
<th>Operators</th>
<th>==</th>
<th>~=</th>
<th>></th>
<th><</th>
<th>>=</th>
<th><=</th>
</tr>
<tr>
<th>Description</th>
<th>Checks if the value of two operands are equal or not, if yes then condition becomes true.</th>
<th>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.</th>
<th>Checks if the value of two operands are equal or not, if values are not equal then condition becomes true.</th>
<th>Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true.</th>
<th>Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true.</th>
<th>Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true.</th>
</tr>
</table>
<p>Logical Operators</p>
<table>
<tr>
<th>Operators</th>
<th>and</th>
<th>or</th>
<th>not</th>
</tr>
<tr>
<th>Description</th>
<th>Called Logical AND operator. If both the operands are non zero then condition becomes true.</th>
<th>Called Logical OR Operator. If any of the two operands is non zero then condition becomes true.</th>
<th>Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false.</th>
</tr>
</table>
<p>Misc Operators</p>
<table>
<tr>
<th>Operators</th>
<th>..</th>
<th>#</th>
</tr>
<tr>
<th>Description</th>
<th>Concatenates two strings.</th>
<th>An unary operator that return the length of the a string or a table.</th>
</tr>
</table>
</section>
<section id="conditionals" class="main-section">
<header>Conditionals</header>
<p>
An if statement consists of a Boolean expression followed by one or more statements.
Syntax
</p>
<p>
The syntax of an if statement in Lua programming language is:
</p>
<pre>
<code>
if(boolean_expression)
then
--[ statement(s) will execute if the boolean expression is true --]
end
</code>
</pre>
<p>
If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. If Boolean expression evaluates to false, then the first set of code after the end of the if statement(after the closing curly brace) will be executed.
<br>
Lua programming language assumes any combination of Boolean true and non-nil values as true, and if it is either Boolean false or nil, then it is assumed as false value. It is to be noted that in Lua, zero will be considered as true.
</p>
<p>
An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
</p>
<p> The syntax of an if...else statement in Lua programming language is:</p>
<pre>
<code>
if(boolean_expression)
then
--[ statement(s) will execute if the boolean expression is true --]
else
--[ statement(s) will execute if the boolean expression is false --]
end
</code>
</pre>
<p>
If the Boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
<br>
Lua programming language assumes any combination of Boolean true and non-nil values as true, and if it is either Boolean false or nil, then it is assumed as false value. It is to be noted that in Lua, zero will be considered as true.
</p>
<p>
It is always legal in Lua programming to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).
</p>
<p>
The syntax for a nested if statement is as follows:
</p>
<pre>
<code>
if( boolean_expression 1)
then
--[ Executes when the boolean expression 1 is true --]
if(boolean_expression 2)
then
--[ Executes when the boolean expression 2 is true --]
end
end
</code>
</pre>
<p>
You can nest else if...else in the similar way as you have nested if statement.
</p>
</section>
<section id="loops" class="main-section">
<header>Loops</header>
<p>
The while loop statement in Lua programming language repeatedly executes a target statement as long as a given condition is true.
</p>
<p>
The syntax of a while loop in Lua programming language is as follows
</p>
<pre>
<code>
while(condition)
do
statement(s)
end
</code>
</pre>
<p>
Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true.
</p>
<p>
When the condition becomes false, the program control passes to the line immediately following the loop.
</p>
<p>
A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
</p>
<p>
The syntax of a for loop in Lua programming language is as follows:
</p>
<pre>
<code>
for init,max/min value, increment
do
statement(s)
end
</code>
</pre>
<p>
Here is the flow of control in a for loop
</p>
<p>
The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables.
<br>
Next, the max/min. This is the maximum or minimum value till which the loop continues to execute. It creates a condition check internally to compare between the initial value and maximum/minimum value.
<br>
After the body of the for loop executes, the flow of the control jumps back up to the increment/decrement statement. This statement allows you to update any loop control variables.
<br>
The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
</p>
<p>
Unlike the for and while loops, which test the loop condition at the top of the loop, the repeat...until loop in Lua programming language checks its condition at the bottom of the loop.
<br>
A repeat...until loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
</p>
<p>
The syntax of a repeat...until loop in Lua programming language is as follows:
</p>
<pre>
<code>
repeat
statement(s)
until( condition )
</code>
</pre>
<p>
Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute(s) once before the condition is tested.
<br>
If the condition is false, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes true.
<br>
Lua programming language allows to use one loop inside another loop.
</p>
</section>
<section id="functions" class="main-section">
<header>Functions</header>
<p>
A function is a group of statements that together perform a task. You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division usually unique, is so each function performs a specific task.
<br>
The Lua language provides numerous built-in methods that your program can call. For example, method print() to print the argument passed as input in console.
<br>
A function is known with various names like a method or a sub-routine or a procedure etc.
</p>
<p>
The general form of a method definition in Lua programming language is as follows:
</p>
<pre>
<code>
optional_function_scope function function_name(argument1)
function_body
return result_params_comma_separated
end
</code>
</pre>
<p>
A method definition in Lua programming language consists of a method header and a method body. Here are all the parts of a method:
</p>
<ul>
<li>
Optional Function Scope − You can use keyword local to limit the scope of the function or ignore the scope section, which will make it a global function.
</li>
<li>
Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature.
</li>--[[ function returning the max between two numbers --]]
function max(num1, num2)
if (num1 > num2) then
result = num1;
else
result = num2;
end
return result;
<li>
Arguments − An argument is like a placeholder. When a function is invoked, you pass a value to the argument. This value is referred to as the actual parameter or argument. The parameter list refers to the type, order, and number of the arguments of a method. Arguments are optional; that is, a method may contain no argument.
</li>
<li>
Function Body − The method body contains a collection of statements that define what the method does.
</li>
<li>
Return − In Lua, it is possible to return multiple values by following the return keyword with the comma separated return values.
</li>
</ul>
<p>Example:</p>
<pre>
<code>
--[[ function returning the max between two numbers --]]
function max(num1, num2)
if (num1 > num2) then
result = num1;
else
result = num2;
end
return result;
end
</code>
</pre>
<p> Function Arguments</p>
<p>
If a function is to use arguments, it must declare the variables that accept the values of the arguments. These variables are called the formal parameters of the function.
<br>
The formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.
</p>
<p>Calling a Function</p>
<p>
While creating a Lua function, you give a definition of what the function has to do. To use a method, you will have to call that function to perform the defined task.
<br>
When a program calls a function, program control is transferred to the called function. A called function performs the defined task and when its return statement is executed or when its function's end is reached, it returns program control back to the main program.
<br>
To call a method, you simply need to pass the required parameters along with the method name and if the method returns a value, then you can store the returned value.
</p>
<p>Live Demo</p>
<pre>
<code>
function max(num1, num2)
if (num1 > num2) then
result = num1;
else
result = num2;
end
return result;
end
-- calling a function
print("The maximum of the two numbers is ",max(10,4))
print("The maximum of the two numbers is ",max(5,6))
</code>
</pre>
<p>
When we run the above code, we will get the following output.
</p>
<pre>
<code>
The maximum of the two numbers is 10
The maximum of the two numbers is 6
</code>
</pre>
<p>Assigning and Passing Functions</p>
<p>
In Lua, we can assign the function to variables and also can pass them as parameters of another function. Here is a simple example for assigning and passing a function as parameter in Lua.
</p>
<p>Live Demo</p>
<pre>
<code>
myprint = function(param)
print("This is my print function - ##",param,"##")
end
function add(num1,num2,functionPrint)
result = num1 + num2
functionPrint(result)
end
myprint(10)
add(2,5,myprint)
</code>
</pre>
<p>
When we run the above code, we will get the following output.
</p>
<pre>
<code>
This is my print function - ## 10 ##
This is my print function - ## 7 ##
</code>
</pre>
<p>Function with Variable Argument</p>
<p>
It is possible to create functions with variable arguments in Lua using '...' as its parameter. We can get a grasp of this by seeing an example in which the function will return the average and it can take variable arguments.
</p>
<p>Live Demo</p>
<pre>
<code>
function average(...)
result = 0
local arg = {...}
for i,v in ipairs(arg) do
result = result + v
end
return result/#arg
end
print("The average is",average(10,5,3,4,5,6))
</code>
</pre>
<p>
When we run the above code, we will get the following output.
</p>
<code> The average is 5.5</code>
</section>
<section id="reference" class="main-section">
<header>Reference</header>
<p>You can find more informations on <a href="https://www.tutorialspoint.com/lua/index.htm">Tutorials Point</a> and on <a href="https://www.lua.org/docs.html">Lua's original Documentation</a></p>
</section>
</main>
</body>
<script>
const projectName = 'technical-docs-page';
localStorage.setItem('example_project', 'Technical Docs Page');
</script>
</html>