Lua

3.1k 词

Lua 巴西里约热内卢天主教大学 1993年 脚本语言 标准 C 语言

官网: www.lua.org

搭建环境

  • 编译官方源码(windews 编译相对较繁琐,要有编译程序,比如 vs,windows 建议使用 LuaForWindows 安装包;linux 和 macos 相对简单,在下载的源源文件的 doc 中有编译步骤)

    <p><a href="http://www.lua.org/download.html">http://www.lua.org/download.html</a></p>
    
  • 下载编译好的文件直接安装(可能版本比较旧)

  • 使用第三方的 IDE

解释器和编译器

个人理解,解释器类似于 Java 中的 java 命令,可以直接使用 java className 差不多,只不过 java 得先编译,而 Lua 不用先编译(边编译边执行),直接执行 lua fileName 。Lua 的编译器就和 Java 的编译器类一样了,都是编译成二进制文件。

约定

  • Chunks 和 Blocks

    <blockquote>
      <p>Block: A block is a list of statements, which are executed sequentially</p>
    
      <p>Chunk: The unit of compilation of Lua is called a <em>chunk</em>. Syntactically, a chunk is simply a block</p>
    </blockquote>
    
    <p>Block 是一个或多个顺序执行的语句,Chunk 是 Lua 的一个编译单元(可以是一个 Lua 文件,一系列语法集合等),在语法结构上 Chunk 仅仅是一个 Block。</p>
    
    <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code>chunk ::= {stat [&#39;;&#39;]} -- chunk 可以是一个语句,可以是多个语句集合,还可以是函数,比如一个文件(文件中可能有比较复杂的操作),或者交互模式下的一行命令(一行命令也可能有很多的操作)
    

    stat ::= do black end – do … end 之间的部分被称为一个 block
    chunk ::= block – Syntactically, a chunk is simply a block

  • 命名

    <blockquote>
      <p><em>Names</em> (also called <em>identifiers</em>) in Lua can be any string of letters, digits, and underscores, not beginning with a digit and not being a reserved word. Identifiers are used to name variables, table fields, and labels.</p>
    </blockquote>
    
    <blockquote>
      <p>Lua is a case-sensitive language.</p>
    </blockquote>
    
    <p><em>Lua 大小写敏感</em></p>
    
    <p>保留字:</p>
    
    <div class="highlighter-rouge"><div class="highlight"><pre class="highlight"><code> and       break     do        else      elseif    end
    

    false for function goto if in
    local nil not or repeat return
    then true until while


  • 尽量不要声明以下滑线开头,有一个或多个大写字母的变量名称,类似 _VERSION 等,这些 Lua 可能内置了全局变量。一切皆变量,没有赋值前,所有的变量都是 nil,包括不存在的变量。

  • 变量

    <blockquote>
      <p>Variables are places that store values. There are three kinds of variables in Lua: global variables, local variables, and table fields.</p>
    </blockquote>
    
    <p>全局变量,局部变量,表域</p>
    
    <div class="language-lua highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">-- 不使用 local 声明的变量都是全局变量(函数的形式参数是特殊的局部变量)</span>
    

    a = 5 – global
    local b = 5 – local

    function t()
    c = 5 – global
    local d = 5 – local
    end
    print(c, d) –> 5, nil

    do
    e = 5 – global
    local f = 5 – local
    end
    print(e, f) –> 5, nil

  • 注释

    <p>单行:<code class="highlighter-rouge">--</code></p>
    
    <p>多行:<code class="highlighter-rouge">--[[ --]]</code></p>
    
    <div class="language-lua highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="c1">-- 单行注释</span>
    

    –[[
    多行注释
    –]]

  • 命令行操作

    <p><code class="highlighter-rouge">lua [options] [script [args]] </code></p>
    
    <p>-e:直接执行命令 <code class="highlighter-rouge">lua -e &#34;print(_VERSION)&#34;</code></p>
    
    <p>-l:加载文件</p>
    
    <p>-i:进入交互模式</p>