Lua

How we are going to look at languages

  • Use chapters 5-10 of textbook as a guide
    • Variable Names, Binding, and Scope
    • Data types
    • Assignment, Epression, and Control Statements
    • Subprograms
  • What effect do these have on implementation and use of a language?

Brief Lua History

  • Designed by Roberto Ierusalimschy, Luiz Henrique de Figueiredo, and Waldemar Celes
    • All with Tecgraf at PUC-Rio at the time
  • Version 1.0 came out in 1993, version 5.3 is the current version
  • Very small - Lua interpreter is less than 150K
  • Notable Uses
    • Game Scripting
    • Adobe Lightroom Frontend
    • Deep Learning (torch library)

Lua Basics

Variables in Lua

  • Can be any combination of letters, numbers and _ (the underscore character)
  • Cannot start with a digit or be the same as a reserved word
  • Do not need to be declared
  • Are global by default

Scope in Lua

  • The scope of a variable is the portion of the code for which a variable is defined
  • In Lua, variables are global by default
  • To make the scope of a variable the current block, use the local keyword
In [3]:
x = 1
do 
    x = 2
    do 
        local q = 1
        print(q)
    end
    print(q)
end

do
    _y = 10
end

do
    local Z = -1
end

print(x)
print(_y)
print(Z)
Out[3]:
1	
nil	
2	
10	
nil	
In [ ]:

Data Types in Lua

  • Lua has a very simple type system, with just 8 types
    • number
    • string
    • boolean
    • nil
    • table
    • function
    • userdata
    • thread

Number

  • There is only one type for all numbers in Lua
  • Stored internally as a float
  • Can be written in scientific notation
  • Operators for numbers are: + , - , * , / , % , ^
In [5]:
print(2e0,2,02,2.00,2.00001,2e2,200000000000000000000)
Out[5]:
2	2	2	2	2.00001	200	2e+20	
In [10]:
type(0/0)
Out[10]:
number	
In [9]:
type(3/4)
Out[9]:
number	
In [7]:
3 / 0
Out[7]:
inf	

String

  • Can be denoted by using either single or double quotes
  • Multiline strings can be defined using [[ ]]
  • Strings are immutable
  • Can hold any unicode character
  • .. is the concatination operator
  • # can be used to get the length of a string

String Examples

In [11]:
'dog' == "dog"
Out[11]:
true	
In [12]:
[[these
are on
different lines
]]
Out[12]:
these
are on
different lines
	
In [15]:
'the'..' strings '..'are together'
Out[15]:
the strings are together	
In [21]:
string.lower('A')
Out[21]:
a	
In [22]:
'abc'[0]
[string "'abc'[0]..."]:1: unexpected symbol near ''abc''
In [14]:
#'how many characters are in this string?'
Out[14]:
39	

Booleans

  • false and nil evaluate to False
  • Everything else is true
  • and and or return their arguments
    • and returns the first argument when that argument evaluates to false, otherwise returns the second arguemnt
    • or returns the first argument when that argument evaluates to true, otherwise returns the second argument
In [23]:
4 and 5
Out[23]:
5	
In [24]:
4 or 5
Out[24]:
4	
In [25]:
false or 5
Out[25]:
5	
In [26]:
x = 4
y = 100
print((x > y) and x or y)
--False or y
Out[26]:
100	

Nil

  • Represents absence of a useful value (Programming in Lua)
In [27]:
type(nil)
Out[27]:
nil	
In [28]:
print(empty)
Out[28]:
nil	
In [29]:
nil + nil
[string "local f = function() return nil + nil end; lo..."]:1: attempt to perform arithmetic on a nil value
stack traceback:
	[string "local f = function() return nil + nil end; lo..."]:1: in function 'f'
	[string "local f = function() return nil + nil end; lo..."]:1: in main chunk
	[C]: in function 'xpcall'
	/home/bryan/torch/install/share/lua/5.1/itorch/main.lua:179: in function </home/bryan/torch/install/share/lua/5.1/itorch/main.lua:143>
	/home/bryan/torch/install/share/lua/5.1/lzmq/poller.lua:75: in function 'poll'
	/home/bryan/torch/install/share/lua/5.1/lzmq/impl/loop.lua:307: in function 'poll'
	/home/bryan/torch/install/share/lua/5.1/lzmq/impl/loop.lua:325: in function 'sleep_ex'
	/home/bryan/torch/install/share/lua/5.1/lzmq/impl/loop.lua:370: in function 'start'
	/home/bryan/torch/install/share/lua/5.1/itorch/main.lua:350: in main chunk
	[C]: in function 'require'
	(command line):1: in main chunk
	[C]: at 0x00406670

Control Statements

  • All control statements must be terminated, usually with an end
  • if , elseif , else , then are the keywords
In [32]:
x = 10
y = 20
z = 0

if x > y then
    print(x)
elseif z > y then
    print(z)
    
else 
    print(y)
    print(z)
end
Out[32]:
20	
0	

Looping

  • Two types of conditional loops while loops and repeat loops
  • repeat is equivalent to do while loops in many other languages
  • for comes in two variations, numeric ( traditional ) and generic (foreach)
In [33]:
x = 10
while x < 20 do
    print(x)
    x = x + 1
end
Out[33]:
10	
11	
12	
13	
14	
15	
16	
17	
18	
19	
In [2]:
x = 10
repeat
    print(x)
    x = x + 1
until x > 20
Out[2]:
10	
11	
12	
13	
14	
15	
16	
17	
18	
19	
20	
In [3]:
for x = 10, 20, 1 do
    print(x)
end
Out[3]:
10	
11	
12	
13	
14	
15	
16	
17	
18	
19	
20