lua学习笔记

lua学习笔记

lua是一门用来支持通用程序设计的扩展语言,同时,它也为面向对象编程,函数式编程,数据驱动式编程,提供提供了一个很好的支持。特别地,作为可嵌入脚本语言提供任何需要的程序使用。lua是C语言实现的一个库,是C和C++标准的通用子集。

lua有8大基础类型:

  • nil:nil is the type of the value nil
  • boolean:the type of the values false and true,but,Both nil and false make a condition false,any other value makes it true
  • number:Number represents real (double-precision floating-point) numbers
  • string:String represents immutable sequences(不可变序列) of bytes,strings can contain any 8-bit value, including embedded zeros (’\0’)
  • function:Lua can call (and manipulate) functions written in Lua and functions written in C
  • useerdata:provided to allow arbitrary C data to be stored in Lua variables,A userdata value is a pointer to a block of raw memory,Userdata values cannot be created or modified in Lua, only through the C API,This guarantees the integrity of data owned by the host program.有两种userdata:
    • full userdata:where the block of memory is managed by Lua
    • light userdata:where the block of memory is managed by the host
  • thread:The type thread represents independent threads of execution and it is used to implement coroutines(协同程序)
  • table:The type table implements associative(组合) arrays,they can contain values of all types (except nil and NaN),any key that is not part of a table has an associated value nil.Tables are the sole data structuring mechanism in Lua; they can be used to represent ordinary arrays, sequences, symbol tables, sets, records, graphs, trees, etc

需要注意的是:Tables, functions, threads, and (full) userdata values are objects,variables do not actually contain these values, only references to them

Metatables and Metamethods

Every value in Lua can have a metatable,This metatable is an ordinary Lua table that defines the behavior of the original value under certain special operations.

coroutines

Lua supports coroutines, also called collaborative multithreading.a coroutine only suspends its execution by explicitly calling a yield function

The Language

The following keywords are reserved and cannot be used as names(也就是我们所说的关键字):

 and       break     do        else      elseif    end
 false     for       function  goto      if        in
 local     nil       not       or        repeat    return
 then      true      until     while

The following strings denote other tokens:

 +     -     *     /     %     ^     #
 ==    ~=    <=    >=    <     >     =
 (     )     {     }     [     ]     ::
 ;     :     ,     .     ..    ...

Strings in Lua can contain any 8-bit value, including embedded zeros, which can be specified as ‘\0’.

Variables

Variables are places that store values. There are three kinds of variables in Lua.

  • global variables
  • local variables
  • and table fields

需要注意事是:形参也是一种特殊的局部变量。

Before the first assignment to a variable, its value is nil.

lua中两个变量互换: x, y = y, x

x = val 相当于 _ENV.x = val

lua中的0 和 “” 是true的,只有nil 和false 是false

Expressions

The basic expressions in Lua are the following:

exp ::= prefixexp
exp ::= nil | false | true
exp ::= Number
exp ::= String
exp ::= functiondef
exp ::= tableconstructor
exp ::= ‘...’
exp ::= exp binop exp
exp ::= unop exp
prefixexp ::= var | functioncall | ‘(’ exp ‘)’
  • “0”==0 evaluates to false

  • t[0] and t[“0”] denote different entries in a table.

Logical Operators

Here are some examples of logical operators

10 or 20            --> 10
10 or error()       --> 10
nil or "a"          --> "a"
nil and 10          --> nil
false and error()   --> false
false and nil       --> false
false or nil        --> nil
10 and 20           --> 20

Precedence

Operator precedence in Lua follows the table below, from lower to higher priority:

or
and
<     >     <=    >=    ~=    ==
..
+     -
*     /     %
not   #     - (unary)
^

Table Constructors:

a = { [f(1)] = g; "x", "y"; x = 1, f(x), [30] = 23; 45 }

is equivalent to

 do
   local t = {}
   t[f(1)] = g
   t[1] = "x"         -- 1st exp
   t[2] = "y"         -- 2nd exp
   t.x = 1            -- t["x"] = 1
   t[3] = f(x)        -- 3rd exp
   t[30] = 23
   t[4] = 45          -- 4th exp
   a = t
 end

Function Definitions

The statement

 local function f () body end

translates to

 local f; f = function () body end

not to

 local f = function () body end

As an example, consider the following definitions:

function f(a, b) end
function g(a, b, ...) end
function r() return 1,2,3 end

Then, we have the following mapping from arguments to parameters and to the vararg expression:

CALL            PARAMETERS
 
f(3)             a=3, b=nil
f(3, 4)          a=3, b=4
f(3, 4, 5)       a=3, b=4
f(r(), 10)       a=1, b=10
f(r())           a=1, b=2
 
g(3)             a=3, b=nil, ... -->  (nothing)
g(3, 4)          a=3, b=4,   ... -->  (nothing)
g(3, 4, 5, 8)    a=3, b=4,   ... -->  5  8
g(5, r())        a=5, b=1,   ... -->  2  3

The colon syntax is used for defining methods, that is, functions that have an implicit extra parameter ‘self’.

function t.a.b.c:f (params) body end

is syntactic sugar for

 t.a.b.c.f = function (self, params) body end

Visibility Rules

Lua is a lexically scoped language. The scope of a local variable begins at the first statement after its declaration and lasts until the last non-void statement of the innermost block that includes the declaration

x = 10                -- global variable
 do                    -- new block
   local x = x         -- new 'x', with value 10
   print(x)            --> 10
   x = x+1
   do                  -- another block
     local x = x+1     -- another 'x'
     print(x)          --> 12
   end
   print(x)            --> 11
 end
 print(x)              --> 10  (the global one)

Notice that each execution of a local statement defines new local variables. Consider the following example:

a = {}
local x = 20
for i=1,10 do
	local y = 0
	a[i] = function () y=y+1; return x+y end
end
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值