首页 > 其他分享 >Lua学习--3

Lua学习--3

时间:2023-03-04 22:35:18浏览次数:48  
标签:function -- variables 学习 Lua values variable chunk

变量

 

Variables are places that store values. There are three kinds of variables in Lua: global variables, local variables, and table fields.

A single name can denote a global variable or a local variable (or a function's formal parameter, which is a particular kind of local variable):

	var ::= Name

Name denotes identifiers, as defined in §2.1.

Any variable is assumed to be global unless explicitly declared as a local (see §2.4.7). Local variables are lexically scoped: local variables can be freely accessed by functions defined inside their scope (see §2.6).

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

变量分三种类型 全局(默认),本地(local 修饰),表中的域,变量在赋值之前  值是nil

 

Square brackets are used to index a table:

	var ::= prefixexp `[´ exp `]´

The meaning of accesses to global variables and table fields can be changed via metatables. An access to an indexed variable t[i] is equivalent to a call gettable_event(t,i). (See §2.8 for a complete description of the gettable_event function. This function is not defined or callable in Lua. We use it here only for explanatory purposes.)

The syntax var.Name is just syntactic sugar for var["Name"]:

	var ::= prefixexp `.´ Name

All global variables live as fields in ordinary Lua tables, called environment tables or simply environments (see §2.9). Each function has its own reference to an environment, so that all global variables in this function will refer to this environment table. When a function is created, it inherits the environment from the function that created it. To get the environment table of a Lua function, you call getfenv. To replace it, you call setfenv. (You can only manipulate the environment of C functions through the debug library; (see §5.9).)

An access to a global variable x is equivalent to _env.x, which in turn is equivalent to

     gettable_event(_env, "x")

where _env is the environment of the running function. (See §2.8 for a complete description of the gettable_event function. This function is not defined or callable in Lua. Similarly, the _env variable is not defined in Lua. We use them here only for explanatory purposes.)

 

var.Name=var["Name"]

 

2.4.1 – Chunks

The unit of execution of Lua is called a chunk. A chunk is simply a sequence of statements, which are executed sequentially. Each statement can be optionally followed by a semicolon:

	chunk ::= {stat [`;´]}

There are no empty statements and thus ';;' is not legal.

Lua handles a chunk as the body of an anonymous function with a variable number of arguments (see §2.5.9). As such, chunks can define local variables, receive arguments, and return values.

A chunk can be stored in a file or in a string inside the host program. To execute a chunk, Lua first pre-compiles the chunk into instructions for a virtual machine, and then it executes the compiled code with an interpreter for the virtual machine.

Chunks can also be pre-compiled into binary form; see program luac for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly.

 

Lua中一行代码叫chunk ,可以保存在文件内,也可以为一个嵌入式的代码中的一个字符串,块在执行之前 会执行预编译,然后在虚拟机上解释执行,可以选择性的以分号结束

 

2.4.2 – Blocks

A block is a list of statements; syntactically, a block is the same as a chunk:

	block ::= chunk

A block can be explicitly delimited to produce a single statement:

	stat ::= do block end

Explicit blocks are useful to control the scope of variable declarations. Explicit blocks are also sometimes used to add a return or break statement in the middle of another block (see §2.4.4).

一组语句构成一个 Block

 

2.4.3 – Assignment

Lua allows multiple assignments. Therefore, the syntax for assignment defines a list of variables on the left side and a list of expressions on the right side. The elements in both lists are separated by commas:

	stat ::= varlist `=´ explist
	varlist ::= var {`,´ var}
	explist ::= exp {`,´ exp}

Expressions are discussed in §2.5.

Before the assignment, the list of values is adjusted to the length of the list of variables. If there are more values than needed, the excess values are thrown away. If there are fewer values than needed, the list is extended with as many nil's as needed. If the list of expressions ends with a function call, then all values returned by that call enter the list of values, before the adjustment (except when the call is enclosed in parentheses; see §2.5).

The assignment statement first evaluates all its expressions and only then are the assignments performed. Thus the code

     i = 3
     i, a[i] = i+1, 20

sets a[3] to 20, without affecting a[4] because the i in a[i] is evaluated (to 3) before it is assigned 4. Similarly, the line

     x, y = y, x

exchanges the values of x and y, and

     x, y, z = y, z, x

cyclically permutes the values of xy, and z.

The meaning of assignments to global variables and table fields can be changed via metatables. An assignment to an indexed variable t[i] = val is equivalent to settable_event(t,i,val). (See §2.8 for a complete description of the settable_event function. This function is not defined or callable in Lua. We use it here only for explanatory purposes.)

An assignment to a global variable x = val is equivalent to the assignment _env.x = val, which in turn is equivalent to

     settable_event(_env, "x", val)

where _env is the environment of the running function. (The _env variable is not defined in Lua. We use it here only for explanatory purposes.)

 

在Lua中支持变量和值在一个语句中完成多个赋值操作,变量和值 以等号=分开左右两部分,多个变量之间用逗号分开,多个值之间也用逗号分开,
如果右侧存在表达式,则先计算表达式,再完成给左侧变量赋值
如果变量的个数比值 (或表达式) 的个数多,侧从左到右匹配不到的变量将赋nil,如果等号右侧的值(表达式)的个数多于变量的个数,那么多佘的值将被丢弃。

例如:

 

 

标签:function,--,variables,学习,Lua,values,variable,chunk
From: https://www.cnblogs.com/hztech/p/17179359.html

相关文章

  • Attempt to invoke virtual method ‘java.lang.String android.os.Bundle.getString(
    报错日志java.lang.NullPointerException:Attempttoinvokevirtualmethod'java.lang.Stringandroid.os.Bundle.getString(java.lang.String)'onanullobjectref......
  • django终章篇
    从django终章篇1.Cookie和Session1.1基于cookie的登录cookie设置在响应头上,获取在请求头上设置cookie时要先获得一个响应对象(HttpResponse),然后在这个响应对象上......
  • 【LeetCode二叉树#17】在二叉搜索树中插入或删除某个值(涉及重构二叉树、链表基础、以
    二叉搜索树中的插入操作力扣题目链接(opensnewwindow)给定二叉搜索树(BST)的根节点和要插入树中的值,将值插入二叉搜索树。返回插入后二叉搜索树的根节点。输入数据保证......
  • 6.JavaScript异常处理的方式,统一的异常处理方案
    一般来说有两种处理方式①抛出异常functionopenFile(fileName){if(!exists(fileName)){thrownewError('找不到文件'+fileName)}//...}②捕......
  • Blend开启时,不关闭深度写入可能出现的问题
    测试用shader,就是在Unlit基础上开启Blend,开启深度写入(红色加粗部分)Shader"My/Blend/Unlit_Blend_ZWriteOn"{Properties{_MainTex("Texture",2D)......
  • java代码审计-命令执行
    0x01前言在Java中可用于执行系统命令常见的方式有两种,API为:java.lang.Runtime、java.lang.ProcessBuilder0x02java.lang.Runtime@GetMapping("/runtime/exec")pu......
  • 在 .NET Core 中使用 Redis 创建分布式锁
    .NETCoreRedis分布式锁,在本文中,我们将讨论如何在.NETCore中使用Redis创建分布式锁。我们在构建分布式系统的时候,会面临多个进程共同处理一个共享资源,由于一次只能......
  • 数字替换
    题目内容给定两个整数 n,x你可以对 x 进行任意次以下操作:选择 x 的一位数字 y,将 x 替换为 x×y。请你计算通过使用上述操作,将 x 变为一个 n 位数字(不含......
  • MySQL数据库:范式
    按照“数据库规范化”对表进行设计,其目的就是减少数据库中的数据冗余,以增加数据的一致性。常见的范式有1NF、2NF、3NF、BCNF。下面对这几种常见的范式进行简要分析。1、1N......
  • 诚邀报名 | SAP 乘云而上私享会-上海站
     在快速发展中,您的业务与组织是否发生了明显变化? 您是否在思考云转型与数字化转型带来的业务价值? 进入受限制的“客户特定维护”阶段会带来风险吗? 哪些保驾护航服......