This repository was archived by the owner on Jul 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput.lua
More file actions
164 lines (125 loc) · 2.83 KB
/
input.lua
File metadata and controls
164 lines (125 loc) · 2.83 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
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
-- === Variables and Assignment === --
local x = 10
local y = x + 5
local a, b = 20, 30
local const z = 42
-- === Control Structures === --
while y > 0 do
print("y = " .. y)
y = y - 1
end
repeat
print("This will print at least once")
until false
for i = 1, 5 do
print("i = " .. i)
end
for key, value in pairs({a = 1, b = 2}) do
print(key .. " = " .. value)
end
-- === Functions === --
function greet(name)
print("Hello, " .. name)
end
greet("World")
function add(a, b)
return a + b
end
local sum = add(3, 4)
-- Method call syntax
local obj = {value = 5}
function obj:double()
self.value = self.value * 2
end
obj:double()
-- === Anonymous Functions / Lambdas === --
local square = function(x)
return x * x
end
print(square(5))
-- === Tables and Arrays === --
local person = {
name = "Alice",
age = 30,
["__metatable"] = { protected = true }
}
person.job = "Engineer"
local fruits = {"apple", "banana", "cherry"}
-- Indexed access
print(fruits[1])
-- === Expressions and Operators === --
local mathExpr = (2 + 3) * 4 / 2 - 1 % 2 ^ 3
local boolExpr = (true or false) and not false
local isEqual = (5 == 5)
local isNotEqual = (5 != 6)
local comparison = 10 >= 5 and 3 < 4
-- Concatenation
local greeting = "Hello" .. " " .. "Lua"
-- Prefix and Infix expressions
local negated = -x
local logicalNot = not true
-- Indexing
local val = person["name"]
-- Assignment
person.age = 31
-- === Varargs and Variable Arguments === --
function printAll(...)
for _, v in ipairs({...}) do
print(v)
end
end
printAll(1, 2, 3, 4)
-- === Coroutines === --
coroutine.create(function()
print("Inside coroutine")
end)
coroutine.resume(coroutine.create(function()
print("Resuming this coroutine")
end))
-- === Async/Await === --
async function fetchData()
local result = await somePromise()
print("Data fetched: " .. result)
end
-- === Try-Catch-Finally === --
try
error("Something went wrong!")
catch err
print("Caught error: " .. tostring(err))
finally
print("Finally block executed")
end
-- === Import Statements === --
import "utils.lua"
import "config.lua" as configModule
-- === Return Statement === --
function returnTest()
return 1, 2, 3
end
local r1, r2, r3 = returnTest()
-- === Break Statement === --
for i = 1, 10 do
if i > 5 then break end
print(i)
end
-- === Nil and Literals === --
local nothing = nil
local truth = true
local falsehood = false
local str = "This is a string"
local num = 3.14159
-- === Table Fields and Metaprogramming === --
local metaTable = {
__metatable = { hidden = true },
[1 + 2] = "computed key",
regularKey = "value",
another = {
nested = true
}
}
-- === Function Literal with Receiver === --
local objWithMethod = {}
function objWithMethod:method()
print("Method called on object")
end
objWithMethod:method()