Lua is a lightweight ProgrammingLanguage designed for extending applications, but also frequently used as a stand-alone language. It is implemented as a small library of ANSI C functions that compiles unmodified on all known platforms. The API that allows an application to exchange data with Lua programs and also to extend Lua with C functions. In this sense, Lua can be regarded as a language framework for building domain-specific languages. Lua programs are compiled to ByteCode. Lua's implementation goals are simplicity, efficiency, portability, and low embedding cost. The result is a fast language engine with small footprint, making it ideal in embedded systems too. It is FreeSoftware, designed and implemented by a team at Tecgraf, the Computer Graphics Technology Group of PUC-Rio (the Pontifical Catholic University of Rio de Janeiro in Brazil). Tecgraf is a laboratory of theDepartment of ComputerScience.
The language combines simple procedural syntax with powerful data description constructs based on associative arrays, is dynamically typed, and has automatic memory management with GarbageCollection, making it ideal for configuration, scripting, and rapid prototyping. A fundamental concept in the design of Lua is to provide meta-mechanisms for implementing features, instead of providing a host of features directly in the language. For example, although Lua does not contain ObjectOrientation, it does provide mechanisms for implementing classes and inheritance. This philosophy brings an economy of concepts and keeps the language small, while allowing unconventional extensions to its semantics.
-- simple program that reads a config file in the format "a = b" and -- prints them to the output as "a is b" function fileexists( file ) local f = io.open( file, "r" ) if f then io.close( f ) return true else return false end end function loadconfig( file ) print( "+ reading config file " .. file ) if fileexists( file ) then local cline = 1 for line in io.lines( file ) do if string.sub( line, 0, 2 ) ~= "--" and line ~= "" then local _,_,var, val = string.find( line, "(%S+)%s?=%s?(.*)" ) ; if var == nil or val == nil then print( "! malformed line on line " .. cline .. ", ignoring" ) else print( var .. " is ".. val ) end end cline = cline + 1 end else print( "! " .. file .. " does not exist !" ) print( "! exiting.." ) os.exit() end end loadconfig( "blah.conf" )
$ lua blah.lua + reading config file blah.conf a is b var is val ! malformed line on line 3, ignoring nick is scope