forked from cursorless-dev/cursorless
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlua.lua
More file actions
131 lines (108 loc) · 2.12 KB
/
lua.lua
File metadata and controls
131 lines (108 loc) · 2.12 KB
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
-- This is a single-line comment
--[[
This is a multi-line comment.
It spans multiple lines.
--]]
-- Variables
local a = 42
local b, c = "Hello", "World"
-- Data Types
local number = 3.14
local boolean = true
local string = "Lua is awesome!"
local table = { 1, 2, 3 }
local nilValue = nil
-- Conditional Constructs
local x = 10
local y = 20
-- if-then-else
if x < y then
print("x is less than y")
elseif x > y then
print("x is greater than y")
else
print("x is equal to y")
end
-- ternary conditional (short if-then-else)
local max = x > y and x or y
print("The maximum value is: " .. max)
-- Functions
function add(x, b)
return x + y
end
local sum = add(5, 7)
print("Sum:", sum)
-- Tables
local person = {
name = "John",
age = 30,
hobbies = { "reading", "gaming", "programming" },
address = {
street = "123 Main St",
city = "Example City",
},
}
-- String manipulation
local concatString = "Hello " .. "World"
-- Metatables and metatable operations
local mt = {
__add = function(a, b)
return a + b
end,
__sub = function(a, b)
return a - b
end,
}
setmetatable(a, mt)
-- Closures
function makeCounter()
local count = 0
return function()
count = count + 1
return count
end
end
local counter = makeCounter()
-- Coroutines
local co = coroutine.create(function()
for i = 1, 3 do
print("Coroutine", i)
coroutine.yield()
end
end)
-- Error handling
local success, result = pcall(function()
error("This is an error")
end)
if not success then
print("Error:", result)
end
-- Loop Constructs
-- while loop
local i = 1
i = 2
while i <= 5 do
print("While loop iteration: " .. i)
i = i + 1
end
-- repeat-until loop
i = 1
repeat
print("Repeat-Until loop iteration: " .. i)
i = i + 1
until i > 5
-- for loop
for j = 1, 5 do
print("For loop iteration: " .. j)
end
-- numeric for loop with step
for k = 10, 1, -1 do
print("Numeric for loop with step: " .. k)
end
-- for-in loop (iterating over a table)
local fruits = { "apple", "banana", "cherry" }
for key, value in pairs(fruits) do
print("For-In loop: " .. key .. " = " .. value)
end
-- ternary
local max = x > y and x or y