CSci 450/503: Programming Languages
Advanced Lua Functions

H. Conrad Cunningham

14 September 2016

Note: I corrected a few formatting errors on 29 January 2020.

Acknowledgements: These slides are adapted from the slide sets 0-5 for the course Programming in Lua created in July 2013 by Fabio Mascarenhas, Federal University of Rio de Janeiro, Brazil. It was presented at Nankai University, P. R. China, in July 2013. Although that course used Lua 5.2, I have attempted to update these slides to Lua 5.3. I produced these slided for CSci 450/503 (Organization of Programming Languages), Fall 2016.

Advisory: The HTML version of this document may require use of a browser that supports the display of MathML. A good choice as of January 2020 is a recent version of Firefox from Mozilla.

GETTING STARTED WITH LUA

Lua Design Principles (1)

  1. Portability

    • use common subset of standard C and C++
    • limit dependencies to standard C library
    • restrict memory used by core (small size)
  2. Simplicity

    • provide small set of powerful primitives
    • add features conservatively
    • support mechanisms instead of policies

Lua Design Principles (2)

  1. Embeddability

    • embed within applications
    • interoperate with C programs in both directions
    • enable extension
  2. Efficiency

    • probably fastest, interpreted “scripting” language
    • LuaJIT (Just-In-Time compiler) supports Lua 5.1

Lua REPL

Statements & Chunks

Separating Statements

Running Scripts

Basic Functions

Global Variables

Variable Names

Comments

Quiz

Which of the following names are valid variable names? Why/why not?

  1. ---
  2. _end
  3. End
  4. end
  5. until?
  6. Nil
  7. NULL

Quiz Solution

Which of the following names are valid variable names? Why/why not?

  1. --- – NO – a comment
  2. _end – YES
  3. End – Yes
  4. end – No – reserved keyword
  5. until? – No – ? not valid
  6. Nil – Yes
  7. NULL – Yes

LUA TYPES AND VALUES

Types

  1. nil – only value is nil
  2. boolean – values true and false
  3. number
    • until 5.3, double precision floating point
    • beginning 5.3, both 64-bit integers and double precision floating point
  4. string – immutable byte vectors, any byte values, in whatever coding
  5. table – associative arrays
  6. function – both named and anonymous
  7. userdata – opaque blobs handled by external (e.g., C) libraries
  8. thread – really coroutine

Dynamic Typing

Nil Type

Boolean Type

Logical Operator Idioms

Number Type Before Lua 5.3

Number Type Changes in Lua 5.3

String Type

String Literals

Long String Literals

Table Type

Function Type

Quiz

How can you check whether a value is a boolean without using the type function?

Quiz Solution

How can you check whether a value is a boolean without using the type function?

    function isbool(x)
        return (not not x) == x
    end

LUA STATEMENTS

if-then-else

elseif

while and repeat

Numeric for (1)

Numeric for (2)

Local Variables

do-end

Multiple Assignment (1)

Multiple Assignment (2)

Quiz

What is the result of running the following program? Why?

    local i = 5 
    while i do 
        print(i) 
        i = i - 1 
    end 

Quiz Solution

What is the result of running the following program? Why?

    local i = 5 
    while i do 
        print(i) 
        i = i - 1 
    end

Printing integers counting down from 5 – infinitely!

LUA FUNCTIONS

Functions

Functions As Values

Defining Functions

Global Functions

Anonymous Functions

Multiple Results

Using Multiple Results (1)

Using Multiple Results (2)

Variadic Functions

Quiz

What is the output of the following program?

    local function range(a, b, c) 
        if a > b then 
            return 
        else 
            return a, range(a + c, b, c) 
        end 
    end 
    print(range(1, 9, 2)) 

Quiz Solution

What is the output of the following program?

    local function range(a, b, c) 
        if a > b then 
            return 
        else 
            return a, range(a + c, b, c) 
        end 
    end 
    print(range(1, 9, 2)) 

    >1       3       5        7       9 

LUA DATA STRUCTURES

Tables for Everything

Arrays

Array Length

Array Insert & Remove

Array Sort

Concatenation

Iteration with ipairs

Matrices

Records

Sets and Bags

Iteration with pairs

Quiz

What will be the output of following program?

    sunday = "monday" ; monday = "sunday"
    t = { sunday = "monday", [sunday] = monday }
    print(t.sunday, t[sunday], t[t.sunday]) 

Quiz Solution

    sunday = "monday" ; monday = "sunday"
    t = { sunday = "monday", [sunday] = monday }
    print(t.sunday, t[sunday], t[t.sunday]) 

Second line same as:

    t = {} ; t["sunday"] = "monday" ; t["monday"] = "sunday"

Last? Variable sunday yields "monday". So t[sunday] same as t["monday"]. Variable monday yields "sunday". Thus assignment is t["monday"] = "sunday".

Thus t == { sunday = "monday", monday = "sunday" }. So print(t.sunday, t[sunday], t[t.sunday]):

    monday  sunday  sunday