Вы находитесь на странице: 1из 9

% Lua

% 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 Resources


- [Lua Homepage][1]
- [Lua Reference Manual][2]
- [Programming in Lua][3]
### Alternate Implementations
- [LuaJIT][4]
+ Optimized bytecode engine plus native code on most platforms
+ Really nice C FFI
- [eLua][5]
+ Bare-metal Lua for microcontrollers
+ Supports many boards based on CortexM3, AVR32, etc.
+ Modules for common interfaces (i2c, spi, pwm, etc.)
### Platforms Embedding Lua
- Game Engines
+ Leading scripting language for game industry
+ Game platforms like LOVE, Moai, Corona, Gideros
- Adobe Lightroom is ~40% Lua
- Wireshark
- nginx
### Syntax Overview
-

Borrows from the Pascal/Modula family, but more concise


Favors delimiting with keywords (e.g. 'end') over punctuation
Provides a flexible data literal syntax
Just a little bit of sugar

# 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

local greet = function() print(salutation .. " " .. name) end


while count > 0 do
greet()
count = count - 1
end
end
hello("World", "english")
hello("Jose", "spanish")
hello("Bob")
~~~
### Features Illustrated
+
+
+
+
+
+
+
+
+
+
+
+

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

in the global environment.


### Control and scoping statements
+
+
+
+
+
+

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
~~~
+
+
+
+

No parens required around the test


`then` and `end` are always required
No punctuation or significant whitespace required
Each alternative list of statements is considered to be in its own
nested block for scope purposes.

### `while` loops


~~~ {.lua}
local x = 0
while x < 20 do
x = x + 1
end
while next() ~= 0 do act() done
~~~
+ Again, no parens or punctuation required
+ The statements are again in a nested block scope
+ If the test is false the first time, the statements will not be run
### `repeat` loops
~~~ {.lua}
local x = 0
repeat
x = x + 1
until x == 19
~~~~
+ The statement block will always execute at least once
+ Note the test here is a *termination* test, not a *continuation*
test as with the `while` loop, so the logic must be inverted if you
switch between them
### `for` loops
+ There is a simple *numerical* `for` loop as well as a more general
*generic* `for` loop.
~~~ {.lua}
for i = 1, 5 do
print(x[i])
end
for j = 5, 1, -1 do
print(x[i])
end
~~~
+ The *numerical* `for` takes a loop index variable and expressions
that evaluate to three numeric values-- start, terminate, and step

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
+
+
+
+

The `nil` type has only one value, `nil`


The `boolean` type has two values, `true` and `false`
All `number`s are internally double-precision floating point values
`string`s are internally arrays of 8-bit clean data, and may
contain embedded zero bytes.
+ A `function` value is a reference to a function written either in
Lua or C, and is a first-class value
+ A `userdata` value is an arbitrary and opaque chunk of C-managed
memory for use via the C API.
+ `thread` values represent flows of execution, and are used in Lua's

*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
+
+
+
+
+
+

one of the basic types of value


can be created anonymously
can be passed as parameters and returned
can take multiple parameters and return multiple values
parameter passing is flexible
tail-calls are optimized

## Lexically scoped variables


+ references are looked up in textual scope, inside-out
+ scope is delimited by blocks
- body of a control structure (including do-blocks)
- body of a function
- a chunk (the file or string containing the code)
+ declarations are active from their line to the end of the block

+ if no local declaration in scope, references are to globals


+ nested functions "close over" locals in enclosing scope
+ variables in closure can outlive their activation record
## Tables for compound data
+
+
+
+
+
+
+

the table type is a reference type (they are 'boxed')


created via table construction literal syntax
to use as an array, index the values with integers 1-n
otherwise, data of any type can be used to index tables
string index keys can be used with 'object reference' syntax
basic table primitives can be changed via metatables
more on tables later!

## Built-in asymmetric coroutines


+
+
+
+

very
more
less
More

lightweight cooperative threads of execution


powerful than Python's 'yield'
powerful than Scheme's continuations
on coroutines later!

## 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
+
+
+
+
+
+

has a boolean type, with 'false' and 'true' values


boolean expressions return a boolean-typed value
'nil' and 'false' test false, all other values test true
comparisons between mismatched types are an error
strings always compare in locale's alphanumeric order
reference types (tables, userdata, functions) are only
equal if they refer to the same location

--### Expressions - Precedence Table


-------- -------- -------- -------- -------- -------^
not
-(unary)
*
/

+
..
<
>
<=
>=
~=
==
-------- -------- -------- -------- -------- -------- Only `..` and `^` are right-associative

Вам также может понравиться