Lua - Foundation

p73

exit: C-z Enter [Win] | C-d [Unix] | os.exit()

comment: –this is comment | –[[ this is multiline comment ]] (usually ends with “–]]”) | –[==[ ]==] (any count of =, just make the beginning match the ending)

statement separator: don’t need statement in a single line, don’t need a separator, if need, use “;”

run: lua file.lua | dofile(“filename”) [in interactive mode]

_PROMPT: Lua interpreter prompt text

Env var “LUA_INIT”: init script, supports “@filename”

print exp value: = var

startup arg: global var “arg” is a table and contains all the startup values

chunk: code block, executable

data type: nil, boolean, number, string, table(ref), userdata(ref), function(ref), thread

boolean false: false or nil
boolean true: others except false or nil are true(including 0, “”, ”)

string: ” | “” | [[ ]] (don’t support excape) | [==[ ]==] (any count of =, just make the beginning match the ending)
concatenate strings: ..
string length: #string

Lua store all global var into a table, so no initiated global var is nil
a = {x = 10, y = 20} <--> a = {}; a["x"] = 10; a.y = 20;
a.x == a["x"]
a = {[any_exp] = value}
a = {“a”, “b”, } –can append , to the last
a = {“a”, “b”; x = 10,} –can use “;” instead of “,”, a[1] = “a”, a[2] = “b”
#a: the length of table a (1 index based), if one entry is nil(a table hole) it will treat as the array tail, use “table.maxn()” if have non-continued number index
print(a[#a]): print the last entry value
a[#a] = nil: remove the last entry
a[#a+1] = v: append a new entry to the table a

number operator: +, -, *, /, ^(exponent), %(mode), -(minus)
math.pi – math.pi % 0.01 -> 3.14

<, >, <=, >=, ==, ~= (not equal)
not the same data type, not equal.
for ref types, only point to the save object is equal
<, >, <=, >= only apply to the same data type, and can only apply to number and string

and, or, not
and: if the first param is false, return the first param, else return the second param
or: if the first param is true, return the first param, else return the second param
short-cut evaluation: name = name or “Johnson”
“a ? b : c”: a and b or c (“and” have a higher priority than “or”, so it is equal to “(a and b) or c”, b must be a true value)

a, b = 10, 2*x: multi-assignment
a, b, c = 0: a == 0, b and c will be nil
a, b = 0, 1, 2: 2 will be ignored
x, y = y, x: exchange

local x –local var, access local var is faster than global var, and can be GC

break, return: must be a last statement of a chunk, or just before “else”, “end”, “until”. So could use “do return end” to put a “return” at anywhere

function: if the argument is a string or a table constructor, the () can be elided.
a:foo(x) == A.foo(o,x), a is an instance of A
arguments to parameters (function calling) is like multi-assignment (don’t need match one to one)
multiple return values of a function: if the function invoking is in the middle of a serial expresses, only the first return value is kept. A serial expresses can be multi-assignment, function argument list(in ()), table constructor(in {}) or return statement. Use “(function())” to keep the only first return value.
unpack(list, [i, [j]]): unpack a table list, return list items
…: variable number of arguments
select(index, …): return sub items of …, or return item count of varargs (if index is “#”) …
“local arg = select(i, …)”: pick the item in i position from a varargs
named arguments: implemented through putting all arguments into a table

local function foo(x,y) return x+y end: local foo; foo = function(x, y) return x+y end

Lib = {}
function Lib.foo(x,y) return x+y end

tail-call elimination (return func(params)) & state machine

generic for: iterator function, invariant state, control variable
next(table, key): return the next key and the current key pointed value, if the key is nill, return the first value

we say a language is a interpreter language is because its runtime system includes the compliler and it is very easy to compile dynamic codes.

loadfile(file): compile the file and return it as a function
loadstring(string): compile the string and return it as a function; load and call it: “assert(loadstring(string))()”; no lexical scoping for this funcion; Lua always compile the string in a global env (could not see local vars)
dofile(file): local f = assert(loadfile(file)); return f();

package.loadlib(libname, funcname): dynamically links lib

Exception:
error()
assert()
n = io.read(“*number”); if not n then error(“invalid input”) end
n = assert(io.read(“*number”), “invalid input”)
pcall(): try catch
xpcall()
debug.debug()
debug.traceback()