Channels ▼
RSS

Open Source

Lua: an Extensible Embedded Language

Source Code Accompanies This Article. Download It Now.


Example 1: The table definitions in (a) are equivalent to (b).

(a)   t = {}  -- empty table t[1] = i t[2] = i*2 t[3] = i*3 t[4] = i+j
(b)   t = {i, i*2, i*3, i+j} s = {a=x, b=y}

Example 2: The method definition in (a) is equivalent to (b).

(a)   function object:method(params) ... end
(b)   function object.method(self, params) ... end

Example 3: (a) Creating and storing a button; (b) explicitly mapping a window onto the screen; (c) showing a message with two bottom buttons.

(a)   b  = button{label = "Hello world!" command =
"exit(0)" }

(b)   w = toplevel{b} w:show()

(c)   b1 = button{label="Yes", command="yes=1"} b2 =
button{label="No", command="yes=0"} w  =
toplevel{message{text="Overwrite file?"}, frame{b1, b2;
side="left"}; side="top" }

Table 1: Fallback conditions.

String Condition
"arith" Arithmetic on invalid operands.
"order" Order comparison on invalid operands.
"concat" String concatenation on invalid operands.
"getglobal" Reading the value of a global variable that has not been defined.
"index" Retrieving the value of an index not present in a table.
"gettable" Reading the value of an index in a nontable value.
"settable" Writing the value of an index in a nontable value.
"function" Calling a nonfunction value.
"gc" Called during garbage collection for each table being collected.
"error" Called when a fatal error occurs.

Listing One


#include <stdio.h> #include "lua.h"

int main() { char line[BUFSIZ]; while (fgets(line,sizeof(line),stdin)!=0)
lua_dostring(line); return 0; }

Listing Two

function map(list, func) local newlist = {} local i = 1 while list[i] do
newlist[i] = func(list[i]) i = i+1 end return newlist end

Listing Three


list = {} i = 4 while i >= 1 do list = {head=i,tail=list} i = i-1 end

Listing Four

S = Separator{ drawStyle = DrawStyle{style = FILLED}, material =  Material{
ambientColor  = {0.377, 0.377, 0.377}, diffuseColor  = {0.800, 0.771, 0.093},
emissiveColor = {0.102, 0.102, 0.102}, specularColor = {0.0, 0.0, 0.0} },

transform = Transform{ translation = {64.293, 20.206, 0.0}, rotation    = {0.0,
0.0, 0.0, 0.0} }, shape = Sphere{radius = 10.0} }

Listing Five

function Inherit(t,f) if f == "parent" then  -- avoid loops return nil
end local p = t.parent if type(p) == "table" then return p[f] else
return nil end end

setfallback("index", Inherit)

Listing Six

TNumber="number" TPoint={x=TNumber, y=TNumber} TColor={red=TNumber,
blue=TNumber, green=TNumber} TRectangle={topleft=TPoint, botright=TPoint}
TWindow={title="string", bounds=TRectangle, color=TColor}

Listing Seven

function checkType(d, t) if type(t) == "string" then -- t is the name
of a type return (type(d) == t) else -- t is a table, so d must also be a table
if type(d) ~= "table" then return nil else -- d is also a table; check
its fields local i,v = next(t,nil) while i do if not checkType(d[i],v) then
return nil end i,v = next(t,i) end end end return 1 end

Listing Eight

function save() -- create table to hold environment local env = {} -- get first
global var and its value local n, v = nextvar(nil) while n do -- save global
variable in table env[n] = v -- get next global var and its value n, v =
nextvar(n) end return env end

Listing Nine

function runProtected(code) -- save current environment local oldenv = save() --
erase "dangerous" functions readfrom,writeto,execute = nil,nil,nil --
run untrusted code dostring(code) -- restore original environment restore(oldenv)
end

Listing Ten

widgetClass = {} function widgetClass:focus() if self.tkname then
tklua_setFocus(self.tkname) end end buttonClass = { parent = widgetClass,
tkwidget = "button" }

Listing Eleven

function button(self) self.parent = classButton tklua_ID = tklua_ID + 1
tklua_IDtable[tklua_ID] = self return tklua_ID end

Listing Twelve

function setFB(id, f, v) local h = tklua_IDtable[id] if h == nil then return
old_setFB(id,f,v) end if h.tkname and h:isAttrib(f) then
tklua_configure(h.tkname,f,v) end h[f] = v end old_setFB =
setfallback("settable",setFB)

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 

Video