← Back to list

Lua : Lightweight Programming Language

Lua is a lightweight, efficient, and embeddable scripting language designed for flexibility and ease of integration, especially with C and…

Guruprasad C. Naik · 2025-04-23 04:06 · 5 claps · 2.0 min read
#lua #programming-languages #programming #scripting-language
Open on Medium ↗
Wiki topics: 💻 · Programming

Lua : Lightweight Programming Language

Lua is a lightweight, efficient, and embeddable scripting language designed for flexibility and ease of integration, especially with C and C++ applications. It supports multiple programming paradigms, including procedural, object-oriented, and functional programming36. Lua is widely used in game development, embedded systems, and as a scripting language in various applications.

Key Features

  • Simple and readable syntax
  • Dynamically typed variables
  • Powerful table data structure (used for arrays, dictionaries, and objects)
  • Easy integration with other languages
  • Small footprint and fast execution

To run Lua programs on your computer, you’ll need a standalone Lua interpreter and perhaps some additional Lua libraries. Pre-compiled Lua libraries and executables are available at LuaBinaries. Use your favorite text editor to write your Lua programs.

Basic Concepts

Variables and Data Types

Lua is a dynamically typed language. This means that variables do not have types; only values do. There are no type definitions in the language. All values carry their own type.

  • Variables are declared using the local keyword (optional).
  • Common data types: nil, boolean, number, string, function, table (core structure), userdata, and thread .
local num = 42        -- number
local str = "Hello"   -- string
local bool = true     -- boolean

Operators

  • Arithmetic: +, -, *, /, ^ (power), % (modulus).
  • Comparison: ==, ~=, <, >, <=, >= .
  • Logical: and, or, not .

Control Structure

  • Conditional: if, elseif, else, end
  • Loops: while, for, repeat ... until .
if num > 5 then
  print("num greater than 5")
else
  print("num less than 5 or equals")
end

for i = 1, 5 do
  print(i)
end

Functions

A function definition is an executable expression, whose value has type function.

  • Defined with the function keyword.
  • Can return values and be assigned to variables.
  • Support closures and higher-order functions.
function greet(name)
  print("Hello, " .. name)
end

greet("Jack")

Tables

  • The primary data structure in Lua.
  • Can act as arrays, dictionaries, or objects.
  • Used for object-oriented programming patterns.
local fruits = {"apple", "banana", "mango"}
local person = {name = "Jack", age = 30}
print(person.name)  -- Output: Jack

Modules and Libraries

Modules in Lua are a way to organize and reuse code by grouping related functions and variables into a single file (module), which can then be loaded and used in other scripts. This promotes code reusability, maintainability, and clarity.

-- mymodule.lua
local M = {}
function M.greet(name)
  print("Hello, " .. name)
end
return M

To use your module in another Lua script, use the require function with the module’s name (without the .lua extension):

local mymodule = require("mymodule")
local name = "Jack"
mymodule.greet(name)          -- Output: Hello, Jack

Lua provides standard libraries for tasks like string manipulation, math, and file I/O.

Lua’s simplicity, flexibility, and efficiency make it a popular choice for scripting and rapid prototyping in a wide range of software projects. If you want to explore more read Lua 5.4 Reference Manual.


메타데이터
post_id
b4e790adeebc
slug
lua-lightweight-programming-language-b4e790adeebc
url
https://medium.com/@gprazad/lua-lightweight-programming-language-b4e790adeebc
canonical_url
https://medium.com/@gprazad/lua-lightweight-programming-language-b4e790adeebc
author_url
https://medium.com/@gprazad
status
ok
fetched_at
2026-08-24 16:46:10