presently 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/bin/presently +13 -0
- data/lib/presently/application.rb +104 -0
- data/lib/presently/clock.rb +77 -0
- data/lib/presently/display_view.rb +73 -0
- data/lib/presently/environment/application.rb +62 -0
- data/lib/presently/page.rb +38 -0
- data/lib/presently/page.xrb +31 -0
- data/lib/presently/presentation.rb +72 -0
- data/lib/presently/presentation_controller.rb +181 -0
- data/lib/presently/presenter_view.rb +264 -0
- data/lib/presently/slide.rb +148 -0
- data/lib/presently/slide_view.rb +92 -0
- data/lib/presently/state.rb +66 -0
- data/lib/presently/version.rb +9 -0
- data/lib/presently.rb +9 -0
- data/license.md +21 -0
- data/public/_components/@socketry/syntax/Syntax/CodeElement.js +337 -0
- data/public/_components/@socketry/syntax/Syntax/Errors.js +52 -0
- data/public/_components/@socketry/syntax/Syntax/Language/apache.js +49 -0
- data/public/_components/@socketry/syntax/Syntax/Language/applescript.js +157 -0
- data/public/_components/@socketry/syntax/Syntax/Language/assembly.js +42 -0
- data/public/_components/@socketry/syntax/Syntax/Language/bash-script.js +108 -0
- data/public/_components/@socketry/syntax/Syntax/Language/bash.js +32 -0
- data/public/_components/@socketry/syntax/Syntax/Language/basic.js +232 -0
- data/public/_components/@socketry/syntax/Syntax/Language/c++.js +1 -0
- data/public/_components/@socketry/syntax/Syntax/Language/c.js +1 -0
- data/public/_components/@socketry/syntax/Syntax/Language/clang.js +201 -0
- data/public/_components/@socketry/syntax/Syntax/Language/cpp.js +1 -0
- data/public/_components/@socketry/syntax/Syntax/Language/csharp.js +166 -0
- data/public/_components/@socketry/syntax/Syntax/Language/css.js +244 -0
- data/public/_components/@socketry/syntax/Syntax/Language/diff.js +24 -0
- data/public/_components/@socketry/syntax/Syntax/Language/go.js +135 -0
- data/public/_components/@socketry/syntax/Syntax/Language/haskell.js +110 -0
- data/public/_components/@socketry/syntax/Syntax/Language/html.js +69 -0
- data/public/_components/@socketry/syntax/Syntax/Language/io.js +68 -0
- data/public/_components/@socketry/syntax/Syntax/Language/java.js +134 -0
- data/public/_components/@socketry/syntax/Syntax/Language/javascript.js +89 -0
- data/public/_components/@socketry/syntax/Syntax/Language/json.js +36 -0
- data/public/_components/@socketry/syntax/Syntax/Language/lisp.js +38 -0
- data/public/_components/@socketry/syntax/Syntax/Language/lua.js +87 -0
- data/public/_components/@socketry/syntax/Syntax/Language/markdown.js +112 -0
- data/public/_components/@socketry/syntax/Syntax/Language/nginx.js +37 -0
- data/public/_components/@socketry/syntax/Syntax/Language/objective-c.js +1 -0
- data/public/_components/@socketry/syntax/Syntax/Language/ocaml.js +225 -0
- data/public/_components/@socketry/syntax/Syntax/Language/pascal.js +166 -0
- data/public/_components/@socketry/syntax/Syntax/Language/patch.js +2 -0
- data/public/_components/@socketry/syntax/Syntax/Language/perl5.js +317 -0
- data/public/_components/@socketry/syntax/Syntax/Language/php-script.js +112 -0
- data/public/_components/@socketry/syntax/Syntax/Language/php.js +18 -0
- data/public/_components/@socketry/syntax/Syntax/Language/plain.js +20 -0
- data/public/_components/@socketry/syntax/Syntax/Language/protobuf.js +77 -0
- data/public/_components/@socketry/syntax/Syntax/Language/python.js +208 -0
- data/public/_components/@socketry/syntax/Syntax/Language/ruby.js +124 -0
- data/public/_components/@socketry/syntax/Syntax/Language/scala.js +81 -0
- data/public/_components/@socketry/syntax/Syntax/Language/smalltalk.js +30 -0
- data/public/_components/@socketry/syntax/Syntax/Language/sql.js +865 -0
- data/public/_components/@socketry/syntax/Syntax/Language/super-collider.js +70 -0
- data/public/_components/@socketry/syntax/Syntax/Language/swift.js +176 -0
- data/public/_components/@socketry/syntax/Syntax/Language/xml.js +76 -0
- data/public/_components/@socketry/syntax/Syntax/Language/xrb.js +33 -0
- data/public/_components/@socketry/syntax/Syntax/Language/yaml.js +29 -0
- data/public/_components/@socketry/syntax/Syntax/Language.js +276 -0
- data/public/_components/@socketry/syntax/Syntax/Loader.js +78 -0
- data/public/_components/@socketry/syntax/Syntax/Match.js +546 -0
- data/public/_components/@socketry/syntax/Syntax/Rule.js +306 -0
- data/public/_components/@socketry/syntax/Syntax.js +356 -0
- data/public/_components/@socketry/syntax/bin/syntax-ast.js +42 -0
- data/public/_components/@socketry/syntax/examples/_template.html +53 -0
- data/public/_components/@socketry/syntax/examples/apache.html +72 -0
- data/public/_components/@socketry/syntax/examples/applescript.html +72 -0
- data/public/_components/@socketry/syntax/examples/assembly.html +74 -0
- data/public/_components/@socketry/syntax/examples/bash.html +90 -0
- data/public/_components/@socketry/syntax/examples/basic.html +87 -0
- data/public/_components/@socketry/syntax/examples/c.html +141 -0
- data/public/_components/@socketry/syntax/examples/clang.html +202 -0
- data/public/_components/@socketry/syntax/examples/csharp.html +110 -0
- data/public/_components/@socketry/syntax/examples/css-colors.html +179 -0
- data/public/_components/@socketry/syntax/examples/custom-theme.html +155 -0
- data/public/_components/@socketry/syntax/examples/diff.html +142 -0
- data/public/_components/@socketry/syntax/examples/examples.css +216 -0
- data/public/_components/@socketry/syntax/examples/go.html +413 -0
- data/public/_components/@socketry/syntax/examples/haskell.html +373 -0
- data/public/_components/@socketry/syntax/examples/html.html +316 -0
- data/public/_components/@socketry/syntax/examples/index.html +97 -0
- data/public/_components/@socketry/syntax/examples/io.html +552 -0
- data/public/_components/@socketry/syntax/examples/java.html +786 -0
- data/public/_components/@socketry/syntax/examples/javascript.html +199 -0
- data/public/_components/@socketry/syntax/examples/json.html +150 -0
- data/public/_components/@socketry/syntax/examples/lisp.html +476 -0
- data/public/_components/@socketry/syntax/examples/lua.html +737 -0
- data/public/_components/@socketry/syntax/examples/markdown.html +121 -0
- data/public/_components/@socketry/syntax/examples/mixed.html +306 -0
- data/public/_components/@socketry/syntax/examples/nginx.html +554 -0
- data/public/_components/@socketry/syntax/examples/ocaml.html +596 -0
- data/public/_components/@socketry/syntax/examples/pascal.html +762 -0
- data/public/_components/@socketry/syntax/examples/perl5.html +488 -0
- data/public/_components/@socketry/syntax/examples/php-script.html +142 -0
- data/public/_components/@socketry/syntax/examples/php.html +95 -0
- data/public/_components/@socketry/syntax/examples/plain.html +222 -0
- data/public/_components/@socketry/syntax/examples/protobuf.html +405 -0
- data/public/_components/@socketry/syntax/examples/python.html +82 -0
- data/public/_components/@socketry/syntax/examples/readme.md +79 -0
- data/public/_components/@socketry/syntax/examples/ruby.html +58 -0
- data/public/_components/@socketry/syntax/examples/scala.html +41 -0
- data/public/_components/@socketry/syntax/examples/smalltalk.html +436 -0
- data/public/_components/@socketry/syntax/examples/sql.html +373 -0
- data/public/_components/@socketry/syntax/examples/super-collider.html +55 -0
- data/public/_components/@socketry/syntax/examples/swift.html +176 -0
- data/public/_components/@socketry/syntax/examples/wrap-demo.html +103 -0
- data/public/_components/@socketry/syntax/examples/xml.html +112 -0
- data/public/_components/@socketry/syntax/examples/xrb.html +37 -0
- data/public/_components/@socketry/syntax/examples/yaml.html +72 -0
- data/public/_components/@socketry/syntax/license.md +21 -0
- data/public/_components/@socketry/syntax/package-lock.json +834 -0
- data/public/_components/@socketry/syntax/package.json +43 -0
- data/public/_components/@socketry/syntax/readme.md +162 -0
- data/public/_components/@socketry/syntax/test/Syntax/CodeElement.js +306 -0
- data/public/_components/@socketry/syntax/test/Syntax/ErrorHandling.js +85 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/apache.js +153 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/applescript.js +198 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/assembly.js +209 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/bash-script.js +225 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/bash.js +162 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/basic.js +265 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/clang.js +390 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/csharp.js +436 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/css.js +431 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/diff.js +206 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/go.js +386 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/haskell.js +454 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/html.js +111 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/io.js +229 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/java.js +362 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/javascript.js +101 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/json.js +101 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/lisp.js +224 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/lua.js +307 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/markdown.js +163 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/nginx.js +267 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/ocaml.js +299 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/pascal.js +311 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/perl5.js +333 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/php-script.js +197 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/php.js +92 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/plain.js +327 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/protobuf.js +294 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/python.js +213 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/ruby.js +70 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/scala.js +75 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/smalltalk.js +223 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/sql.js +281 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/super-collider.js +66 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/swift.js +71 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/xml.js +170 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/xrb.js +57 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language/yaml.js +123 -0
- data/public/_components/@socketry/syntax/test/Syntax/Language.js +62 -0
- data/public/_components/@socketry/syntax/test/Syntax/Match.js +40 -0
- data/public/_components/@socketry/syntax/test/Syntax/Rule.js +251 -0
- data/public/_components/@socketry/syntax/test/Syntax.js +38 -0
- data/public/_components/@socketry/syntax/test/helpers/ast-matcher.js +90 -0
- data/public/_components/@socketry/syntax/themes/base/apache.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/applescript.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/assembly.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/bash.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/basic.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/c.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/clang.css +0 -0
- data/public/_components/@socketry/syntax/themes/base/csharp.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/css.css +22 -0
- data/public/_components/@socketry/syntax/themes/base/diff.css +48 -0
- data/public/_components/@socketry/syntax/themes/base/go.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/haskell.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/html.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/io.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/java.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/javascript.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/json.css +41 -0
- data/public/_components/@socketry/syntax/themes/base/lisp.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/lua.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/markdown.css +16 -0
- data/public/_components/@socketry/syntax/themes/base/nginx.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/ocaml.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/pascal.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/perl5.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/php-script.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/php.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/plain.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/protobuf.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/python.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/ruby.css +23 -0
- data/public/_components/@socketry/syntax/themes/base/scala.css +3 -0
- data/public/_components/@socketry/syntax/themes/base/smalltalk.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/sql.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/super-collider.css +33 -0
- data/public/_components/@socketry/syntax/themes/base/swift.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/syntax.css +63 -0
- data/public/_components/@socketry/syntax/themes/base/xml.css +1 -0
- data/public/_components/@socketry/syntax/themes/base/xrb.css +29 -0
- data/public/_components/@socketry/syntax/themes/base/yaml.css +1 -0
- data/public/_components/@socketry/syntax/themes/theming.md +233 -0
- data/public/_components/@socketry/syntax/update-examples.js +135 -0
- data/public/_static/index.css +593 -0
- data/public/application.js +147 -0
- data/readme.md +69 -0
- data/releases.md +3 -0
- data/templates/code.xrb +12 -0
- data/templates/default.xrb +5 -0
- data/templates/image.xrb +8 -0
- data/templates/section.xrb +5 -0
- data/templates/title.xrb +8 -0
- data/templates/translation.xrb +8 -0
- data/templates/two_column.xrb +8 -0
- metadata +280 -0
|
@@ -0,0 +1,737 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<html>
|
|
3
|
+
<head>
|
|
4
|
+
<meta charset="UTF-8">
|
|
5
|
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
6
|
+
<title>Lua Examples - @socketry/syntax</title>
|
|
7
|
+
<link rel="stylesheet" href="examples.css">
|
|
8
|
+
</head>
|
|
9
|
+
<body>
|
|
10
|
+
<header>
|
|
11
|
+
<h1>Lua Examples</h1>
|
|
12
|
+
<p class="subtitle">Lua syntax highlighting</p>
|
|
13
|
+
</header>
|
|
14
|
+
|
|
15
|
+
<nav>
|
|
16
|
+
<a href="index.html">Back to Examples</a>
|
|
17
|
+
</nav>
|
|
18
|
+
|
|
19
|
+
<h1>Lua Code Example</h1>
|
|
20
|
+
|
|
21
|
+
<p>Lua is a lightweight, embeddable scripting language with simple syntax and powerful features.</p>
|
|
22
|
+
|
|
23
|
+
<div class="example">
|
|
24
|
+
<h2>Basic Syntax</h2>
|
|
25
|
+
<p class="description">Example:</p>
|
|
26
|
+
|
|
27
|
+
<pre><code class="language-lua">-- Single-line comment
|
|
28
|
+
|
|
29
|
+
--[[
|
|
30
|
+
Multi-line comment
|
|
31
|
+
can span multiple lines
|
|
32
|
+
]]--
|
|
33
|
+
|
|
34
|
+
-- Variables (global by default)
|
|
35
|
+
x = 10
|
|
36
|
+
name = "Alice"
|
|
37
|
+
|
|
38
|
+
-- Local variables
|
|
39
|
+
local y = 20
|
|
40
|
+
local greeting = "Hello"
|
|
41
|
+
|
|
42
|
+
-- Print to console
|
|
43
|
+
print("Hello, World!")
|
|
44
|
+
print(x, y, name)
|
|
45
|
+
</code></pre>
|
|
46
|
+
|
|
47
|
+
<div class="example">
|
|
48
|
+
<h2>Data Types</h2>
|
|
49
|
+
<p class="description">Example:</p>
|
|
50
|
+
|
|
51
|
+
<pre><code class="language-lua">-- nil (represents no value)
|
|
52
|
+
local nothing = nil
|
|
53
|
+
|
|
54
|
+
-- Boolean
|
|
55
|
+
local flag = true
|
|
56
|
+
local check = false
|
|
57
|
+
|
|
58
|
+
-- Numbers (all numbers are floats)
|
|
59
|
+
local integer = 42
|
|
60
|
+
local decimal = 3.14
|
|
61
|
+
local scientific = 1.5e10
|
|
62
|
+
local hex = 0xFF
|
|
63
|
+
|
|
64
|
+
-- Strings
|
|
65
|
+
local str1 = "double quotes"
|
|
66
|
+
local str2 = 'single quotes'
|
|
67
|
+
local multiline = [[
|
|
68
|
+
Multi-line string
|
|
69
|
+
preserves whitespace
|
|
70
|
+
]]
|
|
71
|
+
|
|
72
|
+
-- Check type
|
|
73
|
+
print(type(42)) -- number
|
|
74
|
+
print(type("hello")) -- string
|
|
75
|
+
print(type(true)) -- boolean
|
|
76
|
+
print(type(nil)) -- nil
|
|
77
|
+
</code></pre>
|
|
78
|
+
|
|
79
|
+
<div class="example">
|
|
80
|
+
<h2>Operators</h2>
|
|
81
|
+
<p class="description">Example:</p>
|
|
82
|
+
|
|
83
|
+
<pre><code class="language-lua">-- Arithmetic
|
|
84
|
+
a = 10 + 5 -- 15
|
|
85
|
+
b = 10 - 5 -- 5
|
|
86
|
+
c = 10 * 5 -- 50
|
|
87
|
+
d = 10 / 5 -- 2
|
|
88
|
+
e = 10 % 3 -- 1 (modulo)
|
|
89
|
+
f = 2 ^ 8 -- 256 (exponentiation)
|
|
90
|
+
g = -5 -- unary minus
|
|
91
|
+
|
|
92
|
+
-- Comparison
|
|
93
|
+
10 == 10 -- true (equal)
|
|
94
|
+
10 ~= 5 -- true (not equal)
|
|
95
|
+
10 < 20 -- true
|
|
96
|
+
10 > 5 -- true
|
|
97
|
+
10 <= 10 -- true
|
|
98
|
+
10 >= 5 -- true
|
|
99
|
+
|
|
100
|
+
-- Logical
|
|
101
|
+
true and false -- false
|
|
102
|
+
true or false -- true
|
|
103
|
+
not true -- false
|
|
104
|
+
|
|
105
|
+
-- String concatenation
|
|
106
|
+
s = "Hello" .. " " .. "World" -- "Hello World"
|
|
107
|
+
|
|
108
|
+
-- Length operator
|
|
109
|
+
len = #"Hello" -- 5
|
|
110
|
+
len = #{1, 2, 3} -- 3
|
|
111
|
+
</code></pre>
|
|
112
|
+
|
|
113
|
+
<div class="example">
|
|
114
|
+
<h2>Control Structures</h2>
|
|
115
|
+
<p class="description">Example:</p>
|
|
116
|
+
|
|
117
|
+
<pre><code class="language-lua">-- if-then-else
|
|
118
|
+
x = 10
|
|
119
|
+
if x > 0 then
|
|
120
|
+
print("positive")
|
|
121
|
+
elseif x < 0 then
|
|
122
|
+
print("negative")
|
|
123
|
+
else
|
|
124
|
+
print("zero")
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
-- while loop
|
|
128
|
+
i = 0
|
|
129
|
+
while i < 5 do
|
|
130
|
+
print(i)
|
|
131
|
+
i = i + 1
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
-- repeat-until loop (like do-while)
|
|
135
|
+
i = 0
|
|
136
|
+
repeat
|
|
137
|
+
print(i)
|
|
138
|
+
i = i + 1
|
|
139
|
+
until i >= 5
|
|
140
|
+
|
|
141
|
+
-- for loop (numeric)
|
|
142
|
+
for i = 1, 10 do
|
|
143
|
+
print(i)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
-- for loop with step
|
|
147
|
+
for i = 0, 100, 10 do
|
|
148
|
+
print(i) -- 0, 10, 20, ..., 100
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
-- for loop (generic - iterate over collection)
|
|
152
|
+
for i, v in ipairs({10, 20, 30}) do
|
|
153
|
+
print(i, v)
|
|
154
|
+
end
|
|
155
|
+
|
|
156
|
+
-- break
|
|
157
|
+
for i = 1, 10 do
|
|
158
|
+
if i == 5 then
|
|
159
|
+
break
|
|
160
|
+
end
|
|
161
|
+
print(i)
|
|
162
|
+
end
|
|
163
|
+
</code></pre>
|
|
164
|
+
|
|
165
|
+
<div class="example">
|
|
166
|
+
<h2>Functions</h2>
|
|
167
|
+
<p class="description">Example:</p>
|
|
168
|
+
|
|
169
|
+
<pre><code class="language-lua">-- Function definition
|
|
170
|
+
function greet(name)
|
|
171
|
+
print("Hello, " .. name .. "!")
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
greet("Alice")
|
|
175
|
+
|
|
176
|
+
-- Function with return value
|
|
177
|
+
function add(a, b)
|
|
178
|
+
return a + b
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
result = add(3, 4) -- 7
|
|
182
|
+
|
|
183
|
+
-- Multiple return values
|
|
184
|
+
function minmax(a, b)
|
|
185
|
+
if a < b then
|
|
186
|
+
return a, b
|
|
187
|
+
else
|
|
188
|
+
return b, a
|
|
189
|
+
end
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
min, max = minmax(10, 5) -- min=5, max=10
|
|
193
|
+
|
|
194
|
+
-- Variable number of arguments
|
|
195
|
+
function sum(...)
|
|
196
|
+
local total = 0
|
|
197
|
+
for _, v in ipairs({...}) do
|
|
198
|
+
total = total + v
|
|
199
|
+
end
|
|
200
|
+
return total
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
print(sum(1, 2, 3, 4, 5)) -- 15
|
|
204
|
+
|
|
205
|
+
-- Anonymous functions
|
|
206
|
+
square = function(x)
|
|
207
|
+
return x * x
|
|
208
|
+
end
|
|
209
|
+
|
|
210
|
+
-- Local function
|
|
211
|
+
local function factorial(n)
|
|
212
|
+
if n <= 1 then
|
|
213
|
+
return 1
|
|
214
|
+
else
|
|
215
|
+
return n * factorial(n - 1)
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
|
|
219
|
+
print(factorial(5)) -- 120
|
|
220
|
+
</code></pre>
|
|
221
|
+
|
|
222
|
+
<div class="example">
|
|
223
|
+
<h2>Tables (Arrays and Dictionaries)</h2>
|
|
224
|
+
<p class="description">Example:</p>
|
|
225
|
+
|
|
226
|
+
<pre><code class="language-lua">-- Array (1-indexed)
|
|
227
|
+
local fruits = {"apple", "banana", "cherry"}
|
|
228
|
+
print(fruits[1]) -- "apple"
|
|
229
|
+
print(fruits[2]) -- "banana"
|
|
230
|
+
|
|
231
|
+
-- Table length
|
|
232
|
+
print(#fruits) -- 3
|
|
233
|
+
|
|
234
|
+
-- Add element
|
|
235
|
+
fruits[4] = "date"
|
|
236
|
+
table.insert(fruits, "elderberry")
|
|
237
|
+
|
|
238
|
+
-- Remove element
|
|
239
|
+
table.remove(fruits, 2) -- removes "banana"
|
|
240
|
+
|
|
241
|
+
-- Iterate over array
|
|
242
|
+
for i, fruit in ipairs(fruits) do
|
|
243
|
+
print(i, fruit)
|
|
244
|
+
end
|
|
245
|
+
|
|
246
|
+
-- Dictionary (key-value pairs)
|
|
247
|
+
local person = {
|
|
248
|
+
name = "Alice",
|
|
249
|
+
age = 30,
|
|
250
|
+
city = "NYC"
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
print(person.name) -- "Alice"
|
|
254
|
+
print(person["age"]) -- 30
|
|
255
|
+
|
|
256
|
+
-- Add/modify field
|
|
257
|
+
person.email = "alice@example.com"
|
|
258
|
+
person["phone"] = "555-1234"
|
|
259
|
+
|
|
260
|
+
-- Iterate over dictionary
|
|
261
|
+
for key, value in pairs(person) do
|
|
262
|
+
print(key, value)
|
|
263
|
+
end
|
|
264
|
+
|
|
265
|
+
-- Mixed table
|
|
266
|
+
local mixed = {
|
|
267
|
+
"first", -- [1]
|
|
268
|
+
"second", -- [2]
|
|
269
|
+
name = "Alice",
|
|
270
|
+
age = 30
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
print(mixed[1]) -- "first"
|
|
274
|
+
print(mixed.name) -- "Alice"
|
|
275
|
+
|
|
276
|
+
-- Nested tables
|
|
277
|
+
local matrix = {
|
|
278
|
+
{1, 2, 3},
|
|
279
|
+
{4, 5, 6},
|
|
280
|
+
{7, 8, 9}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
print(matrix[2][3]) -- 6
|
|
284
|
+
</code></pre>
|
|
285
|
+
|
|
286
|
+
<div class="example">
|
|
287
|
+
<h2>Table Library Functions</h2>
|
|
288
|
+
<p class="description">Example:</p>
|
|
289
|
+
|
|
290
|
+
<pre><code class="language-lua">local numbers = {3, 1, 4, 1, 5, 9, 2, 6}
|
|
291
|
+
|
|
292
|
+
-- Insert element
|
|
293
|
+
table.insert(numbers, 7) -- append to end
|
|
294
|
+
table.insert(numbers, 1, 0) -- insert at position 1
|
|
295
|
+
|
|
296
|
+
-- Remove element
|
|
297
|
+
table.remove(numbers) -- remove last element
|
|
298
|
+
table.remove(numbers, 1) -- remove at position 1
|
|
299
|
+
|
|
300
|
+
-- Sort
|
|
301
|
+
table.sort(numbers)
|
|
302
|
+
for i, v in ipairs(numbers) do
|
|
303
|
+
print(v)
|
|
304
|
+
end
|
|
305
|
+
|
|
306
|
+
-- Sort with custom comparator
|
|
307
|
+
table.sort(numbers, function(a, b)
|
|
308
|
+
return a > b -- descending order
|
|
309
|
+
end)
|
|
310
|
+
|
|
311
|
+
-- Concatenate array elements
|
|
312
|
+
local words = {"Hello", "World", "from", "Lua"}
|
|
313
|
+
local sentence = table.concat(words, " ")
|
|
314
|
+
print(sentence) -- "Hello World from Lua"
|
|
315
|
+
</code></pre>
|
|
316
|
+
|
|
317
|
+
<div class="example">
|
|
318
|
+
<h2>String Library</h2>
|
|
319
|
+
<p class="description">Example:</p>
|
|
320
|
+
|
|
321
|
+
<pre><code class="language-lua">local text = "Hello, World!"
|
|
322
|
+
|
|
323
|
+
-- Length
|
|
324
|
+
print(#text) -- 13
|
|
325
|
+
print(string.len(text)) -- 13
|
|
326
|
+
|
|
327
|
+
-- Uppercase/lowercase
|
|
328
|
+
print(string.upper(text)) -- "HELLO, WORLD!"
|
|
329
|
+
print(string.lower(text)) -- "hello, world!"
|
|
330
|
+
|
|
331
|
+
-- Substring
|
|
332
|
+
print(string.sub(text, 1, 5)) -- "Hello"
|
|
333
|
+
print(string.sub(text, 8)) -- "World!"
|
|
334
|
+
|
|
335
|
+
-- Find pattern
|
|
336
|
+
start, finish = string.find(text, "World")
|
|
337
|
+
print(start, finish) -- 8, 12
|
|
338
|
+
|
|
339
|
+
-- Replace
|
|
340
|
+
result = string.gsub(text, "World", "Lua")
|
|
341
|
+
print(result) -- "Hello, Lua!"
|
|
342
|
+
|
|
343
|
+
-- Split string (manual)
|
|
344
|
+
function split(str, delimiter)
|
|
345
|
+
local result = {}
|
|
346
|
+
for match in (str..delimiter):gmatch("(.-)"..delimiter) do
|
|
347
|
+
table.insert(result, match)
|
|
348
|
+
end
|
|
349
|
+
return result
|
|
350
|
+
end
|
|
351
|
+
|
|
352
|
+
parts = split("one,two,three", ",")
|
|
353
|
+
|
|
354
|
+
-- Format string
|
|
355
|
+
formatted = string.format("Name: %s, Age: %d", "Alice", 30)
|
|
356
|
+
print(formatted)
|
|
357
|
+
|
|
358
|
+
-- Repeat
|
|
359
|
+
print(string.rep("Ha", 3)) -- "HaHaHa"
|
|
360
|
+
|
|
361
|
+
-- Reverse
|
|
362
|
+
print(string.reverse("Hello")) -- "olleH"
|
|
363
|
+
|
|
364
|
+
-- Character code
|
|
365
|
+
print(string.byte("A")) -- 65
|
|
366
|
+
print(string.char(65)) -- "A"
|
|
367
|
+
</code></pre>
|
|
368
|
+
|
|
369
|
+
<div class="example">
|
|
370
|
+
<h2>Pattern Matching</h2>
|
|
371
|
+
<p class="description">Example:</p>
|
|
372
|
+
|
|
373
|
+
<pre><code class="language-lua">-- Find pattern
|
|
374
|
+
text = "The price is $42.99"
|
|
375
|
+
number = string.match(text, "%d+%.%d+")
|
|
376
|
+
print(number) -- "42.99"
|
|
377
|
+
|
|
378
|
+
-- Match all occurrences
|
|
379
|
+
text = "one two three"
|
|
380
|
+
for word in string.gmatch(text, "%w+") do
|
|
381
|
+
print(word)
|
|
382
|
+
end
|
|
383
|
+
|
|
384
|
+
-- Pattern classes
|
|
385
|
+
-- %a - letters
|
|
386
|
+
-- %d - digits
|
|
387
|
+
-- %w - alphanumeric
|
|
388
|
+
-- %s - whitespace
|
|
389
|
+
-- %p - punctuation
|
|
390
|
+
|
|
391
|
+
-- Replace with pattern
|
|
392
|
+
text = "Phone: 123-456-7890"
|
|
393
|
+
result = string.gsub(text, "(%d+)-(%d+)-(%d+)", "(%1) %2-%3")
|
|
394
|
+
print(result) -- "Phone: (123) 456-7890"
|
|
395
|
+
</code></pre>
|
|
396
|
+
|
|
397
|
+
<div class="example">
|
|
398
|
+
<h2>Metatables and Metamethods</h2>
|
|
399
|
+
<p class="description">Example:</p>
|
|
400
|
+
|
|
401
|
+
<pre><code class="language-lua">-- Create a table
|
|
402
|
+
local vector = {x = 10, y = 20}
|
|
403
|
+
|
|
404
|
+
-- Create metatable
|
|
405
|
+
local mt = {
|
|
406
|
+
__add = function(v1, v2)
|
|
407
|
+
return {x = v1.x + v2.x, y = v1.y + v2.y}
|
|
408
|
+
end,
|
|
409
|
+
__tostring = function(v)
|
|
410
|
+
return "(" .. v.x .. ", " .. v.y .. ")"
|
|
411
|
+
end
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
-- Set metatable
|
|
415
|
+
setmetatable(vector, mt)
|
|
416
|
+
|
|
417
|
+
-- Use metamethods
|
|
418
|
+
local v1 = {x = 1, y = 2}
|
|
419
|
+
local v2 = {x = 3, y = 4}
|
|
420
|
+
setmetatable(v1, mt)
|
|
421
|
+
setmetatable(v2, mt)
|
|
422
|
+
|
|
423
|
+
local v3 = v1 + v2 -- calls __add
|
|
424
|
+
print(v3.x, v3.y) -- 4, 6
|
|
425
|
+
|
|
426
|
+
-- __index metamethod
|
|
427
|
+
local defaults = {color = "red", size = 10}
|
|
428
|
+
local mt = {__index = defaults}
|
|
429
|
+
|
|
430
|
+
local obj = {}
|
|
431
|
+
setmetatable(obj, mt)
|
|
432
|
+
|
|
433
|
+
print(obj.color) -- "red" (from defaults)
|
|
434
|
+
obj.color = "blue"
|
|
435
|
+
print(obj.color) -- "blue" (from obj)
|
|
436
|
+
</code></pre>
|
|
437
|
+
|
|
438
|
+
<div class="example">
|
|
439
|
+
<h2>Object-Oriented Programming</h2>
|
|
440
|
+
<p class="description">Example:</p>
|
|
441
|
+
|
|
442
|
+
<pre><code class="language-lua">-- Define a class
|
|
443
|
+
Person = {}
|
|
444
|
+
Person.__index = Person
|
|
445
|
+
|
|
446
|
+
-- Constructor
|
|
447
|
+
function Person:new(name, age)
|
|
448
|
+
local obj = {
|
|
449
|
+
name = name,
|
|
450
|
+
age = age
|
|
451
|
+
}
|
|
452
|
+
setmetatable(obj, self)
|
|
453
|
+
return obj
|
|
454
|
+
end
|
|
455
|
+
|
|
456
|
+
-- Methods
|
|
457
|
+
function Person:greet()
|
|
458
|
+
print("Hello, my name is " .. self.name)
|
|
459
|
+
end
|
|
460
|
+
|
|
461
|
+
function Person:getAge()
|
|
462
|
+
return self.age
|
|
463
|
+
end
|
|
464
|
+
|
|
465
|
+
function Person:haveBirthday()
|
|
466
|
+
self.age = self.age + 1
|
|
467
|
+
end
|
|
468
|
+
|
|
469
|
+
-- Create instance
|
|
470
|
+
local alice = Person:new("Alice", 30)
|
|
471
|
+
alice:greet() -- "Hello, my name is Alice"
|
|
472
|
+
print(alice:getAge()) -- 30
|
|
473
|
+
alice:haveBirthday()
|
|
474
|
+
print(alice:getAge()) -- 31
|
|
475
|
+
|
|
476
|
+
-- Inheritance
|
|
477
|
+
Student = Person:new() -- Student inherits from Person
|
|
478
|
+
|
|
479
|
+
function Student:new(name, age, grade)
|
|
480
|
+
local obj = Person:new(name, age)
|
|
481
|
+
obj.grade = grade
|
|
482
|
+
setmetatable(obj, self)
|
|
483
|
+
self.__index = self
|
|
484
|
+
return obj
|
|
485
|
+
end
|
|
486
|
+
|
|
487
|
+
function Student:study()
|
|
488
|
+
print(self.name .. " is studying")
|
|
489
|
+
end
|
|
490
|
+
|
|
491
|
+
local bob = Student:new("Bob", 20, "A")
|
|
492
|
+
bob:greet() -- Inherited method
|
|
493
|
+
bob:study() -- Student-specific method
|
|
494
|
+
</code></pre>
|
|
495
|
+
|
|
496
|
+
<div class="example">
|
|
497
|
+
<h2>Modules</h2>
|
|
498
|
+
<p class="description">Example:</p>
|
|
499
|
+
|
|
500
|
+
<pre><code class="language-lua">-- Define a module (in mymodule.lua)
|
|
501
|
+
local M = {}
|
|
502
|
+
|
|
503
|
+
function M.greet(name)
|
|
504
|
+
print("Hello, " .. name)
|
|
505
|
+
end
|
|
506
|
+
|
|
507
|
+
function M.add(a, b)
|
|
508
|
+
return a + b
|
|
509
|
+
end
|
|
510
|
+
|
|
511
|
+
return M
|
|
512
|
+
|
|
513
|
+
-- Use the module (in another file)
|
|
514
|
+
local mymodule = require("mymodule")
|
|
515
|
+
mymodule.greet("Alice")
|
|
516
|
+
local sum = mymodule.add(3, 4)
|
|
517
|
+
|
|
518
|
+
-- Alternative: import specific functions
|
|
519
|
+
local greet = require("mymodule").greet
|
|
520
|
+
greet("Bob")
|
|
521
|
+
</code></pre>
|
|
522
|
+
|
|
523
|
+
<div class="example">
|
|
524
|
+
<h2>Coroutines</h2>
|
|
525
|
+
<p class="description">Example:</p>
|
|
526
|
+
|
|
527
|
+
<pre><code class="language-lua">-- Create a coroutine
|
|
528
|
+
co = coroutine.create(function()
|
|
529
|
+
for i = 1, 3 do
|
|
530
|
+
print("Coroutine step " .. i)
|
|
531
|
+
coroutine.yield()
|
|
532
|
+
end
|
|
533
|
+
end)
|
|
534
|
+
|
|
535
|
+
-- Resume coroutine
|
|
536
|
+
coroutine.resume(co) -- prints "Coroutine step 1"
|
|
537
|
+
coroutine.resume(co) -- prints "Coroutine step 2"
|
|
538
|
+
coroutine.resume(co) -- prints "Coroutine step 3"
|
|
539
|
+
|
|
540
|
+
-- Check status
|
|
541
|
+
print(coroutine.status(co)) -- "dead"
|
|
542
|
+
|
|
543
|
+
-- Producer-consumer pattern
|
|
544
|
+
function producer()
|
|
545
|
+
return coroutine.create(function()
|
|
546
|
+
for i = 1, 5 do
|
|
547
|
+
coroutine.yield(i)
|
|
548
|
+
end
|
|
549
|
+
end)
|
|
550
|
+
end
|
|
551
|
+
|
|
552
|
+
function consumer(prod)
|
|
553
|
+
while true do
|
|
554
|
+
local status, value = coroutine.resume(prod)
|
|
555
|
+
if not status then break end
|
|
556
|
+
print("Received: " .. value)
|
|
557
|
+
end
|
|
558
|
+
end
|
|
559
|
+
|
|
560
|
+
consumer(producer())
|
|
561
|
+
</code></pre>
|
|
562
|
+
|
|
563
|
+
<div class="example">
|
|
564
|
+
<h2>Error Handling</h2>
|
|
565
|
+
<p class="description">Example:</p>
|
|
566
|
+
|
|
567
|
+
<pre><code class="language-lua">-- pcall (protected call)
|
|
568
|
+
function divide(a, b)
|
|
569
|
+
if b == 0 then
|
|
570
|
+
error("Cannot divide by zero")
|
|
571
|
+
end
|
|
572
|
+
return a / b
|
|
573
|
+
end
|
|
574
|
+
|
|
575
|
+
local status, result = pcall(divide, 10, 2)
|
|
576
|
+
if status then
|
|
577
|
+
print("Result: " .. result) -- 5
|
|
578
|
+
else
|
|
579
|
+
print("Error: " .. result)
|
|
580
|
+
end
|
|
581
|
+
|
|
582
|
+
local status, result = pcall(divide, 10, 0)
|
|
583
|
+
if status then
|
|
584
|
+
print("Result: " .. result)
|
|
585
|
+
else
|
|
586
|
+
print("Error: " .. result) -- "Cannot divide by zero"
|
|
587
|
+
end
|
|
588
|
+
|
|
589
|
+
-- assert
|
|
590
|
+
function validateAge(age)
|
|
591
|
+
assert(age >= 0 and age <= 150, "Invalid age")
|
|
592
|
+
return age
|
|
593
|
+
end
|
|
594
|
+
|
|
595
|
+
age = validateAge(30) -- OK
|
|
596
|
+
-- age = validateAge(-5) -- Error: Invalid age
|
|
597
|
+
</code></pre>
|
|
598
|
+
|
|
599
|
+
<div class="example">
|
|
600
|
+
<h2>File I/O</h2>
|
|
601
|
+
<p class="description">Example:</p>
|
|
602
|
+
|
|
603
|
+
<pre><code class="language-lua">-- Write to file
|
|
604
|
+
file = io.open("output.txt", "w")
|
|
605
|
+
file:write("Hello, World!\n")
|
|
606
|
+
file:write("Line 2\n")
|
|
607
|
+
file:close()
|
|
608
|
+
|
|
609
|
+
-- Read entire file
|
|
610
|
+
file = io.open("input.txt", "r")
|
|
611
|
+
if file then
|
|
612
|
+
local content = file:read("*all")
|
|
613
|
+
print(content)
|
|
614
|
+
file:close()
|
|
615
|
+
end
|
|
616
|
+
|
|
617
|
+
-- Read line by line
|
|
618
|
+
file = io.open("input.txt", "r")
|
|
619
|
+
if file then
|
|
620
|
+
for line in file:lines() do
|
|
621
|
+
print(line)
|
|
622
|
+
end
|
|
623
|
+
file:close()
|
|
624
|
+
end
|
|
625
|
+
|
|
626
|
+
-- Append to file
|
|
627
|
+
file = io.open("log.txt", "a")
|
|
628
|
+
file:write("Log entry\n")
|
|
629
|
+
file:close()
|
|
630
|
+
|
|
631
|
+
-- Check if file exists
|
|
632
|
+
function fileExists(filename)
|
|
633
|
+
local file = io.open(filename, "r")
|
|
634
|
+
if file then
|
|
635
|
+
file:close()
|
|
636
|
+
return true
|
|
637
|
+
else
|
|
638
|
+
return false
|
|
639
|
+
end
|
|
640
|
+
end
|
|
641
|
+
</code></pre>
|
|
642
|
+
|
|
643
|
+
<div class="example">
|
|
644
|
+
<h2>Advanced Examples</h2>
|
|
645
|
+
<p class="description">Example:</p>
|
|
646
|
+
|
|
647
|
+
<pre><code class="language-lua">-- Fibonacci with memoization
|
|
648
|
+
local fib_cache = {}
|
|
649
|
+
|
|
650
|
+
function fibonacci(n)
|
|
651
|
+
if fib_cache[n] then
|
|
652
|
+
return fib_cache[n]
|
|
653
|
+
end
|
|
654
|
+
|
|
655
|
+
local result
|
|
656
|
+
if n <= 1 then
|
|
657
|
+
result = n
|
|
658
|
+
else
|
|
659
|
+
result = fibonacci(n - 1) + fibonacci(n - 2)
|
|
660
|
+
end
|
|
661
|
+
|
|
662
|
+
fib_cache[n] = result
|
|
663
|
+
return result
|
|
664
|
+
end
|
|
665
|
+
|
|
666
|
+
print(fibonacci(10)) -- 55
|
|
667
|
+
|
|
668
|
+
-- Quicksort
|
|
669
|
+
function quicksort(t)
|
|
670
|
+
if #t < 2 then
|
|
671
|
+
return t
|
|
672
|
+
end
|
|
673
|
+
|
|
674
|
+
local pivot = t[1]
|
|
675
|
+
local less, greater = {}, {}
|
|
676
|
+
|
|
677
|
+
for i = 2, #t do
|
|
678
|
+
if t[i] <= pivot then
|
|
679
|
+
table.insert(less, t[i])
|
|
680
|
+
else
|
|
681
|
+
table.insert(greater, t[i])
|
|
682
|
+
end
|
|
683
|
+
end
|
|
684
|
+
|
|
685
|
+
local result = {}
|
|
686
|
+
for _, v in ipairs(quicksort(less)) do
|
|
687
|
+
table.insert(result, v)
|
|
688
|
+
end
|
|
689
|
+
table.insert(result, pivot)
|
|
690
|
+
for _, v in ipairs(quicksort(greater)) do
|
|
691
|
+
table.insert(result, v)
|
|
692
|
+
end
|
|
693
|
+
|
|
694
|
+
return result
|
|
695
|
+
end
|
|
696
|
+
|
|
697
|
+
local unsorted = {3, 1, 4, 1, 5, 9, 2, 6}
|
|
698
|
+
local sorted = quicksort(unsorted)
|
|
699
|
+
for _, v in ipairs(sorted) do
|
|
700
|
+
print(v)
|
|
701
|
+
end
|
|
702
|
+
|
|
703
|
+
-- Map, filter, reduce
|
|
704
|
+
function map(t, fn)
|
|
705
|
+
local result = {}
|
|
706
|
+
for i, v in ipairs(t) do
|
|
707
|
+
result[i] = fn(v)
|
|
708
|
+
end
|
|
709
|
+
return result
|
|
710
|
+
end
|
|
711
|
+
|
|
712
|
+
function filter(t, predicate)
|
|
713
|
+
local result = {}
|
|
714
|
+
for _, v in ipairs(t) do
|
|
715
|
+
if predicate(v) then
|
|
716
|
+
table.insert(result, v)
|
|
717
|
+
end
|
|
718
|
+
end
|
|
719
|
+
return result
|
|
720
|
+
end
|
|
721
|
+
|
|
722
|
+
function reduce(t, fn, initial)
|
|
723
|
+
local acc = initial
|
|
724
|
+
for _, v in ipairs(t) do
|
|
725
|
+
acc = fn(acc, v)
|
|
726
|
+
end
|
|
727
|
+
return acc
|
|
728
|
+
end
|
|
729
|
+
|
|
730
|
+
numbers = {1, 2, 3, 4, 5}
|
|
731
|
+
squared = map(numbers, function(x) return x * x end)
|
|
732
|
+
evens = filter(numbers, function(x) return x % 2 == 0 end)
|
|
733
|
+
sum = reduce(numbers, function(a, b) return a + b end, 0)
|
|
734
|
+
</code></pre>
|
|
735
|
+
|
|
736
|
+
</body>
|
|
737
|
+
</html>
|