博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lua的Chunks和block
阅读量:5918 次
发布时间:2019-06-19

本文共 2350 字,大约阅读时间需要 7 分钟。

hot3.png

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 ).

明确的限定一个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 ). 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 ). 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  for details. Programs in source and compiled forms are interchangeable; Lua automatically detects the file type and acts accordingly (see ).

 

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

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

 

转载于:https://my.oschina.net/u/2326611/blog/838656

你可能感兴趣的文章
JS 和 CSS 间战火熊熊,该如何熄灭?
查看>>
React Native 和 Native 之间自由切换
查看>>
你需要了解的load和initialize
查看>>
阿里云云数据库RDS秒级监控功能解锁,通宵加班找故障将成为过去式
查看>>
一句代码让特定页面支持横竖屏切换
查看>>
音视频转码技术指南:国内主流云转码服务提供商对比测评
查看>>
constructor、prototype、__proto__详解笔记
查看>>
面试相关
查看>>
前端面试之前要准备的那些事
查看>>
Python3入门与实践(五):IO 与 异常
查看>>
Spring Boot系列20 Spring Websocket实现向指定的用户发送消息
查看>>
TiDB 源码阅读系列文章(七)基于规则的优化
查看>>
你对CommonJS规范了解多少?
查看>>
HTML、JS与PHP之间的数据传输
查看>>
opencv图片处理与OCR识别
查看>>
《Android艺术开发探索》学习笔记之View的事件体系(View的弹性滑动)
查看>>
从零开始学Python(九):搭建一个基于SMTP的简单邮件预警系统
查看>>
Springmvc+mybatis+Dubbo+ZooKeeper+Redis+KafKa
查看>>
命令行-程序员方式Mp4视频转换成Gif格式图片(工具)
查看>>
[译] 让 Apache Cassandra 尾部延迟减小 10 倍,已开源
查看>>