|
| 1 | +-- This is a single-line comment |
| 2 | + |
| 3 | +--[[ |
| 4 | + This is a multi-line comment. |
| 5 | + It spans multiple lines. |
| 6 | +--]] |
| 7 | + |
| 8 | +-- Variables |
| 9 | +local a = 42 |
| 10 | +local b, c = "Hello", "World" |
| 11 | + |
| 12 | +-- Data Types |
| 13 | +local number = 3.14 |
| 14 | +local boolean = true |
| 15 | +local string = "Lua is awesome!" |
| 16 | +local table = { 1, 2, 3 } |
| 17 | +local nilValue = nil |
| 18 | + |
| 19 | +-- Conditional Constructs |
| 20 | +local x = 10 |
| 21 | +local y = 20 |
| 22 | + |
| 23 | +-- if-then-else |
| 24 | +if x < y then |
| 25 | + print("x is less than y") |
| 26 | +elseif x > y then |
| 27 | + print("x is greater than y") |
| 28 | +else |
| 29 | + print("x is equal to y") |
| 30 | +end |
| 31 | + |
| 32 | +-- ternary conditional (short if-then-else) |
| 33 | +local max = x > y and x or y |
| 34 | +print("The maximum value is: " .. max) |
| 35 | + |
| 36 | +-- Functions |
| 37 | +function add(x, b) |
| 38 | + return x + y |
| 39 | +end |
| 40 | + |
| 41 | +local sum = add(5, 7) |
| 42 | +print("Sum:", sum) |
| 43 | + |
| 44 | +-- Tables |
| 45 | +local person = { |
| 46 | + name = "John", |
| 47 | + age = 30, |
| 48 | + hobbies = { "reading", "gaming", "programming" }, |
| 49 | + address = { |
| 50 | + street = "123 Main St", |
| 51 | + city = "Example City", |
| 52 | + }, |
| 53 | +} |
| 54 | + |
| 55 | +-- String manipulation |
| 56 | +local concatString = "Hello " .. "World" |
| 57 | + |
| 58 | +-- Metatables and metatable operations |
| 59 | +local mt = { |
| 60 | + __add = function(a, b) |
| 61 | + return a + b |
| 62 | + end, |
| 63 | + __sub = function(a, b) |
| 64 | + return a - b |
| 65 | + end, |
| 66 | +} |
| 67 | + |
| 68 | +setmetatable(a, mt) |
| 69 | + |
| 70 | +-- Closures |
| 71 | +function makeCounter() |
| 72 | + local count = 0 |
| 73 | + return function() |
| 74 | + count = count + 1 |
| 75 | + return count |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +local counter = makeCounter() |
| 80 | + |
| 81 | +-- Coroutines |
| 82 | +local co = coroutine.create(function() |
| 83 | + for i = 1, 3 do |
| 84 | + print("Coroutine", i) |
| 85 | + coroutine.yield() |
| 86 | + end |
| 87 | +end) |
| 88 | + |
| 89 | +-- Error handling |
| 90 | +local success, result = pcall(function() |
| 91 | + error("This is an error") |
| 92 | +end) |
| 93 | + |
| 94 | +if not success then |
| 95 | + print("Error:", result) |
| 96 | +end |
| 97 | + |
| 98 | +-- Loop Constructs |
| 99 | +-- while loop |
| 100 | +local i = 1 |
| 101 | +i = 2 |
| 102 | +while i <= 5 do |
| 103 | + print("While loop iteration: " .. i) |
| 104 | + i = i + 1 |
| 105 | +end |
| 106 | + |
| 107 | +-- repeat-until loop |
| 108 | +i = 1 |
| 109 | +repeat |
| 110 | + print("Repeat-Until loop iteration: " .. i) |
| 111 | + i = i + 1 |
| 112 | +until i > 5 |
| 113 | + |
| 114 | +-- for loop |
| 115 | +for j = 1, 5 do |
| 116 | + print("For loop iteration: " .. j) |
| 117 | +end |
| 118 | + |
| 119 | +-- numeric for loop with step |
| 120 | +for k = 10, 1, -1 do |
| 121 | + print("Numeric for loop with step: " .. k) |
| 122 | +end |
| 123 | + |
| 124 | +-- for-in loop (iterating over a table) |
| 125 | +local fruits = { "apple", "banana", "cherry" } |
| 126 | +for key, value in pairs(fruits) do |
| 127 | + print("For-In loop: " .. key .. " = " .. value) |
| 128 | +end |
| 129 | + |
| 130 | +-- ternary |
| 131 | +local max = x > y and x or y |
0 commit comments