Академический Документы
Профессиональный Документы
Культура Документы
% Levi Pearson
% June 4, 2013
# Preliminary Stuff
### Quick Look at Lua
-
Lua
Lua
Lua
Lua
Lua
is
is
is
is
is
small
simple
fast
flexible
embeddable
# Lua Example
### Hello World
~~~ {.lua}
greetings = {
english = "Hello",
spanish = "Hola",
}
-- greet someone, optionally in a specific language
function hello(name, language)
local salutation = greetings[language] or "Hi"
local count = 3
Global variables
Data literals
Comments
Named function definition
Anonymous function definition
Local variables
While loops
Table lookups
Function calls
Mathematical expressions
Logical expressions + short-circuiting
Optional function arguments
# Lua Basics
## Statements
### Local Variable Declarations
~~~ {.lua}
local foo
local bar = "Hi"
local a, b, c = 1, 2, 3
~~~
+ Variables are untyped, any variable may hold a value of any type
+ Scope is *lexical* and extends from declaration to end of block
+ If not given a value when declared, it holds the nil value
### Assignment Statements
~~~ {.lua}
foo = 13
foo, bar = bar, foo
~~~
+ Multiple assignment evaluates everything on the right, then assigns
to the left, so the example swaps the values in `foo` and `bar`
+ If there are more variables on the left than values on the right,
values are assigned in left-to-right order and then `nil` is
assigned to the remaining variables
+ If there are more values on the right than variables on the left,
the extra values are discarded
+ Assigning to a variable name that has not been declared as a local
variable in the current or enclosing scopes assigns to that variable
chunks
`do` blocks
`if` statements
`while` loops
`repeat` loops
`for` loops
- numeric
- generic
+ escape statements: `break` and `return`
### Chunks
+ The basic unit of execution is the *chunk*, which is a sequence of
statements.
+ Each statement may optionally be terminated by a semicolon.
+ If you load a script on the command line, the contents of the file
are a single chunk
+ If you type in the REPL, each top-level line you type is a chunk!
This is important because each chunk is considered a lexical block
for scoping purposes
+ There are a number of other ways to load and run chunks
+ A chunk is essentially a no-parameter function, and it may return
values
### `do` blocks
~~~ {.lua}
local a, b = 1, 2
do
print(a, b)
local a, b, c = 10, 20, 30
print(a, b, c)
end
print(a, b, c)
~~~
Evaluates to:
~~~
1
2
10 20 30
1
2
nil
~~~
+ `do` introduces a new block scope for local variables
+ Variables inside a block may *shadow* variables of the same name in
enclosing blocks
+ The *extent* of a local variable is the block that contains it
### `if` statements
~~~ {.lua}
if foo == 3 then
print("Hi.")
elseif foo == 4
print("Bye.")
end
if val1 and val2 then f1() else f2() end
~~~
+
+
+
+
values
+ The step value is assumed to be `1` if omitted. Negative step values
reverse the direction of the arithmetic progression
+ The statements inside the loop once again form a block
+ The blocks are executed until the index variable *goes past* the
termination value; this means that they will execute if the index
variable equals the termination value
+ The index variable is local to the loop, you must assign its value
to a variable in an outside scope if you want to keep its value for
later use
--~~~ {.lua}
for k, v in ipairs(nametab) do
print("key: ", k)
print("val: ", v)
end
~~~
~~~ {.lua}
local function makeiter(v)
local function iter(idx, term)
if idx == term then return nil
else return v[idx] end
end
local first, last = 1, #v
return first, last, iter
end
local first, last, iter = makeiter({"a", "b", "c"})
for x in first, last, iter do
print("the letter " .. x)
end
~~~
+ The *generic* `for` is for use with *iterator* functions.
+ The first example illustrates the built-in ipairs() function for
iterating through the key, value pairs of a table
+ The second example gets into the mechanics of how a function like
ipairs() interacts with the `for` statement
### Escape statements
~~~ {.lua}
local count = 0
while true do
print "Hi"
if count == 10 then break else count + count + 1 end
end
~~~
As the last statement of a block within a `while`, `repeat`, or `for`
loop, the `break` statement will immediately terminate the loop.
~~~ {.lua}
function add2(x)
return 2 + x
end
do
return add2
end
print("Won't get here!")
~~~
+ As the last statement of *any* block, the `return` statement will
terminate the enclosing function or chunk and return the value(s)
indicated.
+ You can use `break` or `return` in pretty much any other location by
enclosing them in a `do`-`end` block so they become the last
statement of that block.
### Function call statements
+ A function call statement allows a function to be used like a
*procedure* instead of a *function* (as the distinctions is made in
Pascal and Ada, etc.)
+ It is evaluated for its side-effects and any return value is
discarded
+ Syntactically, it is just a function call expression where a
statement would normally go
## Expressions
### Basic values
An expression is:
+ A value *or*
+ Something that *evaluates* to a value or values
Values in Lua belong to a *type* that does not change, one of:
+
+
+
+
+
+
+
+
nil
boolean
number
string
function
userdata
thread
table
That's it!
### More about types
+
+
+
+
*coroutine* implementation
+ All Lua-accessible aggregate data is stored in `table`s, which are
*associative arrays*. These can act as structs, arrays, tables,
dictionaries, objects, etc. Much of Lua's power and elegance come
from this data type!
Note: Some types are *value* types (variables hold the values
directly) while `function`, `userdata`, `thread`, and `table` are
*reference* types (variables hold references to the values, not the
values themselves)
### Arithmetic expressions
Lua has the typical infix mathematical operators:
* `+` for addition
* `-` for subtraction
* `*` for multiplication
* `/` for division
* `%` for modulo (remainder after integer division)
* `^` for exponentiation
The precedence and associativty are unsurprising; use parentheses for
grouping if you are in doubt.
### Comparison expressions
Comparison operators are mostly what you would expect:
* `==` for testing equality; reference types must refer to the same
value to be considered equal
* `~=` for the not-equal test
* `<=` and `>=` for less-or-equal and greater-or-equal tests
* `<` and `>` for less-than and greater-than tests
### Logical expressions
The logical operators are all keywords: `and`, `or`, and `not`
## Dynamic typing
+ variables have no type, can hold data of any type
+ values are typed and carry the type with them
+ some automatic coercion to string types can occur
## First-class functions
+
+
+
+
+
+
very
more
less
More
## Statements
+ Standard but small set of statement types
- Local variable declarations
- Assignment
- Control: (`if`, `while`, `repeat`, `for`)
* no `switch`, use chained-if or a table lookup
* two types of `for`, numeric and generic
- Escape: `break` and `return`
## Expressions
- Logic expressions use `and`, `or`, and `not` keywords
+ `(a and b) or c` is like C's `a ? b : c`
+ `x = x or v` can be used like `if not x then x = v end`
- All numbers are double-precision floating point internally
- Strings can be concatenated with the `..` operator
+ If either operand is a number, it converts to a string
--### Expressions - Boolean sanity
+
+
+
+
+
+
+
..
<
>
<=
>=
~=
==
-------- -------- -------- -------- -------- -------- Only `..` and `^` are right-associative