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

ଉଇକିପିଡ଼ିଆ ରୁ
Jump to navigation Jump to search
ପାରାଡ଼ିଜିମ୍‌Multi-paradigm: scripting, imperative (procedural, prototype-based object-oriented), functional
ପ୍ରଥମେ ଦେଖାଯାଇଥିଲା୧୯୯୩; ୨୭ ବର୍ଷ ଆଗରୁ (1993)
ଡିଜାଇନ କରିଥିଲେRoberto Ierusalimschy
Waldemar Celes
Luiz Henrique de Figueiredo
ସ୍ଥାୟୀ ରିଲିଜ5.2.3 (ଡିସେମ୍ବର 7, 2013 (2013-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 languageC
ଅପେରେଟିଂ ସିଷ୍ଟମCross-platform
ସତ୍ତ୍ଵMIT License

ଲୁଆ ( /ˈ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
  return x


ଲୁଆ ଚାରି ପ୍ରକାରର 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

until condition

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

The generic for loop:

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

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


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

  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)

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
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 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}

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

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 is defined to be any integer index such that is not nil and is nil; moreover, if is nil, can be zero. For a regular array, with non-nil values from 1 to a given , its length is exactly that , 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)
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.


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 , fibs[n] will give the 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]

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)

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

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_getglobal(L, "foo");
    lua_pushinteger(L, 5);
    lua_pushinteger(L, 3);
    lua_call(L, 2, 1);
    printf("Result: %d\n", lua_tointeger(L, -1));
    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.

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



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