Slides

bricklayerbelchedInternet and Web Development

Feb 5, 2013 (4 years and 6 months ago)

119 views

Integrating Lua into CLI

Roberto Ierusalimschy

Renato Cerqueira


PUC
-
Rio, Brazil

PUC
-
Rio


Lua & CLI

What is Lua?


Yet Another Scripting Language


Not totally unlike Perl, Tcl, or Python


A Data
-
Description Language


Not totally unlike XML


An “Embeddable” Language


A library (in ANSI C)


A Language framework


extensible to “new” languages (e.g. LuaOrb)

PUC
-
Rio


Lua & CLI

Why Lua?


Small


Lua 4.0: 1
2
0K


Tcl (? < 8.0): 655K (~ 5x)


Python 2.0: 860K (~ 6.5x)


Perl 5.6: 1.1M (~ 8.5x)


Embeddable


C library, with simple API


C/C++, Fortran, Java, Ruby, OPL (EPOC language),
Objective
-
C


Efficient


Faster than Perl, Python; much faster than Tcl
(www.bagley.org/~doug/shootout)

PUC
-
Rio


Lua & CLI

Why Lua? (2)


Portable


Unix (e.g. Linux, AIX, ULTRIX, Solaris, IRIX), Windows (e.g.
NT, 3.1, 95, ME, CE), Xbox, Mac, RISC OS, Playstation II
(Sony), IBM OS/390, M5206eLITE (Motorola), EPOC, RISC
OS, BeOS, MS
-
DOS, OS/2, etc.


same source code (no “#ifdef”)


Simple and flexible


“Simple things simple, complex things possible”


PUC
-
Rio


Lua & CLI

function fact (n)


if n == 0 then


return 1


else


return n*fact(n
-
1)


end

end

How is Lua?


Pascal
-
like Syntax


Dynamically typed


Automatic memory
management


dynamic data structures


garbage collection


Seven basic types


numbers, booleans, tables,
functions, strings, userdata, nil

PUC
-
Rio


Lua & CLI

Strings


No size limits, good performance for long strings


common practice to read a whole text file in a single string
before processing


Strings can store arbitrary binary data


not ‘
\
0’ terminated


Pattern matching facilities, implemented through a
standard library


Pattern substitution can be called with a function to
compute the replacement string

PUC
-
Rio


Lua & CLI

Tables


Implement associative arrays


any value (including functions and other tables) can be used
both for indices and values


Grow and shrink dynamically


without generating garbage


Referential semantics


Tables implement most common data structures easily
and efficiently


arrays, records, sets, sparse
arrays
, tables(!), lists, etc.

PUC
-
Rio


Lua & CLI

Tables x Data Structures


arrays: numbers as keys


very efficient



algorithm to store arrays as arrays



records: literal strings as keys


sugar:
a.x



"
x
"
]


sets: values as keys




local a = {}

for i=1,N do


a[i] = 0

end

a.x =
"
hello
"

print(a.x)

a[x] = 1
--

insert x

a[y] = nil
--

remove y

if a[z] ...
--

membership?

PUC
-
Rio


Lua & CLI

Constructors


Expressions to create and initialize tables


Record style

point = {x=10, y=20}

print(point.y)

--
> 20

days = {
"
Sun
"
,
"
Mon
"
,
"
Tue
"
,
"
Wed
"
,
"
Thu
"
,
"
Fri
"
,
"
Sat
"
}

print(days[3])
--
> Tue

points = {{x=0,y=0}, point; n=2}


List style



Mixed style

PUC
-
Rio


Lua & CLI

Constructors


Data description uses:

book{


author=
"
F.P.Brooks
"
,


title=
"
The Mythical Man
-
Month
"
,


year=1975,

}


book{


author=
"
Jon Bentley
",


title =
"
Programming Pearls
"
,


year=1986,

}

sugar for
book({...})

PUC
-
Rio


Lua & CLI

Tables x Objects


Tables are dynamically created objects

value = v

next =

list

old list

...

list = {value=v, next=list}

PUC
-
Rio


Lua & CLI

Functions


First class values

function inc (x)


return x+1

end

inc = function (x)


return x+1


end


sugar

function count (x)


return function () x = x+1; return x; end

end

a = count(10)

print(a())
--
> 11

print(a())
--
> 12


Lexical Scoping

PUC
-
Rio


Lua & CLI

Objects


First class functions + tables = almost OO.


tables can have functions as field values (


methods)


Syntactic sugar for defining and calling methods


handles hidden parameter
self

a.foo(a,x)

a:foo(x)

a.foo = function(self,x)


...

end

function a:foo(x)


...

end

sugar

sugar

PUC
-
Rio


Lua & CLI

Coroutines


simple (and portable) alternative for multi
-
threading


powerful mechanism per se


generators, “pipes”, simulation, etc.


Implemented as semi
-
coroutines


coroutine.create


call + yield

PUC
-
Rio


Lua & CLI

LuaOrb


Binding between Lua and several middleware systems


CORBA, COM, “Java”


Dynamic binding, based on reflexivity


no static stubs


Flexible type mapping


straightforward for arrays, records, strings, primitive types


obj = lo.createproxy(“corbaloc:hostname:6666/Id”,


“InterfaceName”)


obj:foo({x=10, y=20}, {1,2,3})