# ଲୁଆ (ପ୍ରୋଗ୍ରାମିଂ ଭାଷା)

ପାରାଡ଼ିଜିମ୍‌ Multi-paradigm: scripting, imperative (procedural, prototype-based object-oriented), functional ୧୯୯୩; ୨୬ ବର୍ଷ ଆଗରୁ Roberto Ierusalimschy Waldemar Celes Luiz Henrique de Figueiredo 5.2.3 (ଡିସେମ୍ବର 7, 2013) dynamic, strong, duck Lua, LuaJIT, LLVM-Lua, Lua Alchemy Metalua, Idle, GSL Shell C++, CLU, Modula, Scheme, SNOBOL Io, GameMonkey, Squirrel, Falcon, MiniD C Cross-platform MIT License lua.org

ଲୁଆ ( LOO, from ଛାଞ୍ଚ:Lang-pt ଛାଞ୍ଚ:IPA-pt meaning moon; explicitly not "LUA"[୧]) ଏକ lightweight ପ୍ରୋଗ୍ରାମିଂ ଭାଷା ଅଟେ । ଏହା ANSI Cରେ ଲେଖା ହୋଇଥିବାରୁ ବିଭିନ୍ନ ପ୍ରକାରର କମ୍ପ୍ୟୁଟରରେ ଚାଲିପାରେ ଏଣୁ ଏହାକୁ Cross-platform ସ୍କ୍ରିପ୍ଟ ଭାଷା କୁହାଯାଇପାରେ । [୧] ଲୁଆର ଆପ୍ଲିକେସନ ପ୍ରୋଗ୍ରାମିଂ ଇଣ୍ଟରଫେସ ବା API ସି ଅପେକ୍ଷା ସରଳ ଅଟେ ।[୨]

## ଫିଚର

### କିଛି କୋଡ ଉଦାହରଣ

ଏକ ପାରମ୍ପରିକ ହେଲୋ ୱାଲ୍ଡ ପ୍ରୋଗ୍ରାମ ନିମ୍ନୋକ୍ତ ପ୍ରକାରରେ ଲେଖା ଯାଇପାରିବ:

print('Hello World!')

-- A comment in Lua starts with a double-hyphen and runs to the end of the line.

are adorned with double square brackets. ]]

The factorial function is implemented as a function in this example:

function factorial(n)
local x = 1
for i = 2,n do
x = x * i
end
return x
end

### ଲୁପ୍

ଲୁଆ ଚାରି ପ୍ରକାରର loops ବ୍ୟବହାର କରିଥାଏ: the while loop, the repeat loop (similar to a do while loop), the for loop, and the generic for loop.

--condition = true

while condition do
--statements
end

repeat
--statements
until condition

for i = first,last,delta do     --delta may be negative, allowing the for loop to count down or up
print(i)
end

The generic for loop:

for key, value in pairs(_G) do
print(key, value)
end

would iterate over the table _G using the standard iterator function pairs, until it returns nil.

### Functions

Lua's treatment of functions as first-class values is shown in the following example, where the print function's behavior is modified:

do
local oldprint = print
-- Store current print function as oldprint
function print(s)
--[[ Redefine print function, the usual print function can still be used
through oldprint. The new one has only one argument.]]
oldprint(s == "foo" and "bar" or s)
end
end

Any future calls to print will now be routed through the new function, and because of Lua's lexical scoping, the old print function will only be accessible by the new, modified print.

Lua also supports closures, as demonstrated below:

-- Return a new function that adds x to the argument
return function(y)
--[[ When we refer to the variable x, which is outside of the current
scope and whose lifetime would be shorter than that of this anonymous
function, Lua creates a closure.]]
return x + y
end
end
print(fourplus(3))  -- Prints 7

A new closure for the variable x is created every time addto is called, so that each new anonymous function returned will always access its own x parameter. The closure is managed by Lua's garbage collector, just like any other object.

### Tables

Tables are the most important data structure (and, by design, the only built-in composite data type) in Lua, and are the foundation of all user-created types. They are conceptually similar to associative arrays in PHP, dictionaries in Python and Hashes in Ruby or Perl.

A table is a collection of key and data pairs, where the data is referenced by key; in other words, it's a hashed heterogeneous associative array. A key (index) can be any value but nil and NaN. A numeric key of 1 is considered distinct from a string key of "1".

Tables are created using the {} constructor syntax:

a_table = {} -- Creates a new, empty table

Tables are always passed by reference:

a_table = {x = 10}  -- Creates a new table, with one entry mapping "x" to the number 10.
print(a_table["x"]) -- Prints the value associated with the string key, in this case 10.
b_table = a_table
b_table["x"] = 20   -- The value in the table has been changed to 20.
print(b_table["x"]) -- Prints 20.
print(a_table["x"]) -- Also prints 20, because a_table and b_table both refer to the same table.

#### As record

A table is often used as structure (or record) by using strings as keys. Because such use is very common, Lua features a special syntax for accessing such fields. Example:

point = { x = 10, y = 20 }   -- Create new table
print(point["x"])            -- Prints 10
print(point.x)               -- Has exactly the same meaning as line above

#### As namespace

By using a table to store related functions, it can act as a namespace.

Point = {}

Point.new = function(x, y)
return {x = x, y = y}
end

Point.set_x = function(point, x)
point.x = x
end

#### As array

By using a numerical key, the table resembles an array data type. Lua arrays are 1-based: the first index is 1 rather than 0 as it is for many other programming languages (though an explicit index of 0 is allowed).

A simple array of strings:

array = { "a", "b", "c", "d" }   -- Indices are assigned automatically.
print(array[2])                  -- Prints "b". Automatic indexing in Lua starts at 1.
print(#array)                    -- Prints 4.  # is the length operator for tables and strings.
array[0] = "z"                   -- Zero is a legal index.
print(#array)                    -- Still prints 4, as Lua arrays are 1-based.

The length of a table ${\displaystyle t}$ is defined to be any integer index ${\displaystyle n}$ such that ${\displaystyle t[n]}$ is not nil and ${\displaystyle t[n+1]}$ is nil; moreover, if ${\displaystyle t[1]}$ is nil, ${\displaystyle n}$ can be zero. For a regular array, with non-nil values from 1 to a given ${\displaystyle n}$, its length is exactly that ${\displaystyle n}$, the index of its last value. If the array has "holes" (that is, nil values between other non-nil values), then #t can be any of the indices that directly precedes a nil value (that is, it may consider any such nil value as the end of the array).[୩]

An array of objects:

function Point(x, y)        -- "Point" object constructor
return { x = x, y = y }   -- Creates and returns a new object (table)
end
array = { Point(10, 20), Point(30, 40), Point(50, 60) }   -- Creates array of points
print(array[2].y)                                         -- Prints 40

Using a hash map to emulate an array normally is slower than using an actual array; however, Lua tables are optimized for use as arrays[୪] to help avoid this issue.

### Metatables

Extensible semantics is a key feature of Lua, and the metatable concept allows Lua's tables to be customized in powerful ways. The following example demonstrates an "infinite" table. For any ${\displaystyle n}$, fibs[n] will give the ${\displaystyle n}$th Fibonacci number using dynamic programming and memoization.

fibs = { 1, 1 }                                -- Initial values for fibs[1] and fibs[2].
setmetatable(fibs, {
__index = function(values, n)                --[[ __index is a function predefined by Lua,
it is called if key "n" does not exist. ]]
values[n] = values[n - 1] + values[n - 2]  -- Calculate and memoize fibs[n].
return values[n]
end
})

### Object-oriented programming

Although Lua does not have a built-in concept of classes, they can be implemented using two language features: first-class functions and tables. By placing functions and related data into a table, an object is formed. Inheritance (both single and multiple) can be implemented via the metatable mechanism, telling the object to look up nonexistent methods and fields in parent object(s).

There is no such concept as "class" with these techniques; rather, prototypes are used, as in the programming languages Self or JavaScript. New objects are created either with a factory method (that constructs new objects from scratch), or by cloning an existing object.

Lua provides some syntactic sugar to facilitate object orientation. To declare member functions inside a prototype table, one can use function table:func(args), which is equivalent to function table.func(self, args). Calling class methods also makes use of the colon: object:func(args) is equivalent to object.func(object, args).

Creating a basic vector object:

local Vector = {}
Vector.__index = Vector

function Vector:new(x, y, z)    -- The constructor
return setmetatable({x = x, y = y, z = z}, Vector)
end

function Vector:magnitude()     -- Another method
-- Reference the implicit object using self
return math.sqrt(self.x^2 + self.y^2 + self.z^2)
end

local vec = Vector:new(0, 1, 0) -- Create a vector
print(vec:magnitude())          -- Call a method (output: 1)
print(vec.x)                    -- Access a member variable (output: 0)

## ସି ଆପ୍ଲିକେସନ ପ୍ରୋଗ୍ରାମିଂ ଇଣ୍ଟରଫେସ

ଲୂଆ ଫଙ୍କ୍‌ସନ୍‌କୁ ସି ପ୍ରୋଗ୍ରାମିଂ ଭାଷା ବ୍ୟବହାର କରି କିପରି କଲ୍ କରିହେବ ତାହାର ଏକ ଛୋଟ ଉଦାହରଣ:

#include <stdio.h>
#include <stdlib.h>
#include <lua.h>
#include <lauxlib.h>

int main(void)
{
lua_State *L = luaL_newstate();
if (luaL_dostring(L, "function foo (x,y) return x+y end")) {
lua_close(L);
exit(EXIT_FAILURE);
}

lua_getglobal(L, "foo");
lua_pushinteger(L, 5);
lua_pushinteger(L, 3);
lua_call(L, 2, 1);
printf("Result: %d\n", lua_tointeger(L, -1));
lua_close(L);
return 0;
}

Running this example gives:

\$ gcc -o example example.c -llua
\$ ./example
Result: 8

## ବ୍ୟବହାର

• ଭିଡିଓ ଗେମ ପ୍ରସ୍ତୁତକାରୀମାନଙ୍କ ପାଇଁ ଏହା ବିପୁଳ ମାତ୍ରାରେ ଆଦୃତ ସ୍କ୍ରିପ୍ଟ ଭାଷା ଅଟେ ।

## ଆଧାର

1. "About Lua". Lua.org. Retrieved 2013-06-19. ଆଧାର ଭୁଲ: Invalid <ref> tag; name "luaabout" defined multiple times with different content
2. Yuri Takhteyev (April 21, 2013). "From Brazil to Wikipedia". Foreign Affairs. Retrieved April 25, 2013.
3. "Lua 5.1 Reference Manual". 2012. Retrieved 2012-10-16.
4. "Lua 5.1 Source Code". 2006. Retrieved 2011-03-24.