文档章节

lua的Chunks和block

andrew810810
 andrew810810
发布于 2017/02/14 18:07
字数 430
阅读 11
收藏 0
lua

reference:

http://www.lua.org/manual/5.3/manual.html

 

3.3.1 – Blocks

A block is a list of statements, which are executed sequentially:

block ::= {stat}

Lua has empty statements that allow you to separate statements with semicolons, start a block with a semicolon or write two semicolons in sequence:

stat ::= ‘;

Function calls and assignments can start with an open parenthesis. This possibility leads to an ambiguity in Lua's grammar. Consider the following fragment:

a = b + c
     (print or io.write)('done')

The grammar could see it in two ways:

a = b + c(print or io.write)('done')
     
     a = b + c; (print or io.write)('done')

The current parser always sees such constructions in the first way, interpreting the open parenthesis as the start of the arguments to a call. To avoid this ambiguity, it is a good practice to always precede with a semicolon statements that start with a parenthesis:

;(print or io.write)('done')

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 statement in the middle of another block (see §3.3.4).

明确的限定一个block表示独立的语句:do..end 内的部分。

 

3.3.2 – Chunks

The unit of compilation of Lua is called a chunk. Syntactically, a chunk is simply a block:

chunk ::= block

Lua handles a chunk as the body of an anonymous function with a variable number of arguments (see §3.4.11). As such, chunks can define local variables, receive arguments, and return values. Moreover, such anonymous function is compiled as in the scope of an external local variable called _ENV (see §2.2). The resulting function always has _ENV as its only upvalue, even if it does not use that variable.

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

Chunks can also be precompiled into binary form; see program luac and function string.dump for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly (see load).

 

Lua编译的单元称为Chunk,语法上一个chunk是一个简单的block。

一个chunk可以是一条语句,一个函数,一个文件。

 

© 著作权归作者所有

共有 人打赏支持
andrew810810
粉丝 3
博文 141
码字总数 117648
作品 0
朝阳
OpenResty 1.7.10.1 发布,基于 Nginx 的应用服务器

OpenResty 1.7.10.1 发布,OpenResty (也称为 ngxopenresty)是一个全功能的 Web 应用服务器。它打包了标准的 Nginx 核心,很多的常用的第三方模块,以及它们的大多数依赖项。 upgraded the...

oschina
2015/03/02
3.9K
5
Lua4.0 参考手册(九)7-8

(接上篇) -------------------------------------- 7 调试接口 -------------------------------------- Lua 没有内置的调试功能。而是借助于函数和钩子(hook)提供了一个特殊接口,可以用...

晓寒
2014/11/13
0
0
lua解析脚本过程中的关键数据结构介绍

在这一篇文章中我先来介绍一下lua解析一个脚本文件时要用到的一些关键的数据结构,为将来的一系列代码分析打下一个良好的基础。在整个过程中,比较重要的几个源码文件分别是:llex.h,lpars...

技术小阿哥
2017/11/27
0
0
【精选】Nginx模块Lua-Nginx-Module学习笔记(二)Lua指令详解(Directives)

源码地址:https://github.com/Tinywan/Lua-Nginx-Redis Nginx与Lua编写脚本的基本构建块是指令。 指令用于指定何时运行用户Lua代码以及如何使用结果。 下面是显示指令执行顺序的图。 当一个...

tinywan1227
2017/03/09
0
0
Rspamd 0.9 发布,反垃圾邮件系统

Rspamd 0.9 发布了,下载地址:https://rspamd.com/downloads/rspamd-0.9.0.tar.xz Rspamd是一个反垃圾邮件系统,因为使用事件模型和正则表达式优化,其设计工作速度比SpamAssassin还要快。目...

oschina
2015/05/15
614
0

没有更多内容

加载失败,请刷新页面

加载更多

ReentrantLock_可重入

package cunrent;import java.util.Calendar;import java.util.concurrent.locks.ReentrantLock;public class TestReentrantLock { public static void main(String[] args......

noteman
36分钟前
2
0
CentOS7下安装mysql5.7

1、安装YUM Repo 由于CentOS 的yum源中没有mysql,需要到mysql的官网下载yum repo配置文件。 wget https://dev.mysql.com/get/mysql57-community-release-el7-9.noarch.rpm 然后进行repo的安...

JungleKing
44分钟前
2
0
DevExpress v18.1新版亮点——DevExtreme篇(三)

用户界面套包DevExpress v18.1日前终于正式发布,本站将以连载的形式为大家介绍各版本新增内容。本文将介绍了DevExtreme JavaScript Controls v18.1 的新功能,快来下载试用新版本!点击下载...

Miss_Hello_World
45分钟前
1
0
LoadRunner 安装

如果安装LoadRunner 11时弹窗提示"Micosoft Visual C++ 2005 SP1 可再发行组件包(X86):'命令行选项语法错误。键入命令 / ? 可获得帮助信息'"。或者弹窗提示"此计算机缺少 vc2005_sp1_wit...

ww1234
54分钟前
1
0
两个时间段相隔自然月Util

public class DateUtil { public static int getDay(Date date) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); return calend......

木九天
57分钟前
1
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部