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

ଉଇକିପିଡ଼ିଆ ରୁ
Lua
Lua-logo-nolabel.svg
ପାରାଡ଼ିଜିମ୍‌ Multi-paradigm: scripting, imperative (procedural, prototype-based object-oriented), functional
ପ୍ରଥମେ ଦେଖାଯାଇଥିଲା 1993; 21 years ago (1993)
ଡିଜାଇନ କରିଥିଲେ Roberto Ierusalimschy
Waldemar Celes
Luiz Henrique de Figueiredo
ସ୍ଥାୟୀ ରିଲିଜ 5.2.3 (ଡିସେମ୍ବର ୭, ୨୦୧୩ (୨୦୧୩-12-07))
ଟାଇପିଂ ପଦ୍ଧତି dynamic, strong, duck
ପ୍ରମୁଖ ବ୍ୟବହାର Lua, LuaJIT, LLVM-Lua, Lua Alchemy
ଉପଭାଷା Metalua, Idle, GSL Shell
ଏହାକୁ ପ୍ରଭାବିତ କରିଛନ୍ତି C++, CLU, Modula, Scheme, SNOBOL
ଏହା ଦ୍ଵାରା ପ୍ରଭାବିତ ହୋଇଛନ୍ତି Io, GameMonkey, Squirrel, Falcon, MiniD
Implementation language C
ଅପେରେଟିଂ ସିଷ୍ଟମ Cross-platform
ସତ୍ତ୍ଵ MIT License
ୱେବସାଇଟ lua.org

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


ଇତିହାସ[ସମ୍ପାଦନା]

ଫିଚର[ସମ୍ପାଦନା]

କିଛି କୋଡ ଉଦାହରଣ[ସମ୍ପାଦନା]

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

print('Hello World!')

Comments use the following syntax, similar to that of Ada, Eiffel, Haskell, SQL and VHDL:

-- A comment in Lua starts with a double-hyphen and runs to the end of the line.
 
--[[ Multi-line strings & comments
     are adorned with double square brackets. ]]
 
--[=[ Comments like this can have other --[[comments]] nested. ]=]

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:

function addto(x)
  -- 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
fourplus = addto(4)
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 t is defined to be any integer index n such that t[n] is not nil and t[n+1] is nil; moreover, if t[1] is nil, n can be zero. For a regular array, with non-nil values from 1 to a given n, its length is exactly that 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 n, fibs[n] will give the nth 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

ବ୍ୟବହାର[ସମ୍ପାଦନା]

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

ଆହୁରି ଦେଖନ୍ତୁ[ସମ୍ପାଦନା]

ଆଧାର[ସମ୍ପାଦନା]

ଆହୁରି ପଢନ୍ତୁ[ସମ୍ପାଦନା]

ବହି[ସମ୍ପାଦନା]

ପ୍ରବନ୍ଧ[ସମ୍ପାଦନା]

ବାହାର ଲିଙ୍କ[ସମ୍ପାଦନା]