CS 3060: Programming Languages

Lua

Robert C. Green II, Ph.D.

Day 1

Indiana Jones

Key Points

  • Table-based
  • Prototype style of Object-Oriented
  • No Integers

The Interpreter

                        
    lua
                        
                    

The Basics

                        
    1989

    print(1989)

    return 1989

    =1989
                        
                    

Syntax

                        
    print "No time for love"

    print
    "No time for love"

    print "No Time" print "For Love"

                        
                    

Strings

                        
    "The dog's name was 'Indiana!'"

    'Separated\tby\t\ttabs'

    'fortune' .. ' and ' .. 'glory'

    #'professor'

                        
                    

nil

                        
    nil
                        
                    

Comparisons

  • and, or, not
  • ==, ~=, <, <=, >, >=
  • No Integers
                        
    not ((true or false) and false)
                        
                    

Functions

                        
function triple(num)
    return 3 * num
end

(function(num) return 3 * num end)(2)
                        
                    

Functions

                        
function print_characters(friend, foe)
    print('*Friend and foe*')
    print(friend)
    print(foe)
end
                        
                    

Too few Arguments?

Too many Arguments?

Variadic Functions

                        
function print_characters(friend, ...)
    print('*Friend and foe*')
    print(friend)
    
    print('*Foes*')
    foes = {...}
    print(foes[1])
    print(foes[2])
end
                        
                    

Return Values

                        
function weapons()
    return 'bullwhip', 'revolver'
end

w1 = weapons()
print(w1)
                        
                    

Named Values

                        
function popcorn_prices(table)
    print('A medium popcorn costs ' .. table.medium)
end

popcorn_prices{small=5.00, medium=7.00, jumbo=15.00}
                        
                    

Control Flow

                        
if film == 'Raiders' then
    print('Good')
elseif film == 'Temple' then
    print('Meh') 
elseif film == 'Crusade' then
    print('Great') 
else
    print('Huh?') 
end
                        
                    

Loops

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

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

Loops

                        
while math.random(100) < 50 do
    print('Tails; flipping again')
end
                        
                    

Variables

                        
function hypotenuse(a, b)
    a2 = a * a
    b2 = b * b
    return math.sqrt(a2 + b2)
end
                        
                    
                        
function hypotenuse(a, b)
    local a2 = a * a
    local b2 = b * b
    return math.sqrt(a2 + b2)
end
                        
                    

Files

{filename}.lua
                        
                    

Assignment

Start the Assignment

Read Day 2

Resources

  • http://www.lua.org