lua

Lua is a powerful, efficient, lightweight, embeddable scripting language.
Lua 即為脚本语言


更新記錄

item note
20160810 第一版

目錄


Lua

  • Lua 并不是唯一的脚本语言,除了 Lua 以外,还有很多能够提供类似功能的脚本语言,例如Node Perl、Tcl、Ruby、Forth、Python 等
  • lua is the stand-alone Lua interpreter. It loads and executes Lua programs, either in textual source form or in precompiled binary form
  • Precompiled binaries are output by luac, the Lua compiler

直譯器比較表

  • LuaJIT 比 Lua快了一倍以上
  • LuaJIT 比 Node還快

LuaJIT > Node > Lua

[直譯器比較表]

出處:深入淺出 Nodje.js page 34

JIT說明

  • JIT 是 just in time 的缩写, 也就是即时编译编译器
  • 如果一段代码频繁的调用方法,或是一个循环,也就是这段代码被多次执行,那么编译就非常值得了
  • 如同binary已被compile可直接執行而不是直譯(一行一行的解譯),此時即為JIT的功用
[java JIT]

出處:深入浅出 JIT 编译器

Lua command

  • If no arguments are given, then “-v -i” is assumed when the standard input is a terminal
1
lua [ options ] [ script [ args ] ]
  • OPTIONS
    • -i enter interactive mode after script is executed.
    • -v show version information.
    • -l name call require(‘name’) before executing script. Typically used to load libraries.

出處: lua-5.1.5/doc/lua.html

Luac

  • Lua compiler
  • It translates programs written in the Lua programming language into binary files that can be later loaded and executed.
  • The main advantages of precompiling chunks are:
    • faster loading,
    • protecting source code from accidental user changes,
    • off-line syntax checking.
  • luac produces a single output file containing the bytecodes for all source files given
1
luac [ options ] [ filenames ]
  • OPTIONS

    • -o file output to file, instead of the default luac.out
    • -p load files but do not generate any output file
      Used mainly for syntax checking and for testing precompiled chunks: corrupted files will probably generate errors when loaded.
    • -s strip debug information before writing the output file.
  • 使用luac -p好處是預先檢查語法是否有問題


Lua Language

Lexical Conventions

  • Lua is a case-sensitive language

  • The following keywords are reserved and cannot be used as names:

1
2
3
4
and       break     do        else      elseif
end false for function if
in local nil not or
repeat return then true until while
  • The following strings denote other tokens:
1
2
3
4
+     -     *     /     %     ^     #
== ~= <= >= < > =
( ) { } [ ]
; : , . .. ...

Values and Types

  • Lua is a dynamically typed language
  • All values in Lua are first-class values
  • There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.

    • nil
      it usually represents the absence of a useful value
      value nil
    • boolean
      values false and true
      Both nil and false make a condition false; any other value makes it true
    • number
      represents real (double-precision floating-point) numbers
    • string
      String represents arrays of characters. Lua is 8-bit clean: strings can contain any 8-bit character,
    • userdata
      The type userdata is provided to allow arbitrary C data to be stored in Lua variables.
    • thread
      Lua supports coroutines, also called collaborative multithreading.
      ex. coroutine.create / coroutine.resume
    • table
      The type table implements associative arrays, that is, arrays that can be indexed not only with numbers, but with any value (except nil).
  • Tables, functions, threads, and (full) userdata values are objects: variables do not actually contain these values, only references to them.

  • There are three kinds of variables in Lua: global variables, local variables, and table fields.

1
2
3
4
5
6
7
8
9
10
> a = 10                                                                         
> print(type(a))
number
> a = "a string!!"
> print(type(a))
string
> a = print
> print(type(a))
function
>

ex.如何定義-註解

  • 單行
    Lua 的单行注释语句以双连接符“–”开头

  • 多行

1
2
3
--[[ 
print(10) -- no action (comment)
--]]

ex. 多重赋值

1
2
3
function foo0() end       -- returns no results 
function foo1() return 'a' end -- returns 1 result
function foo2() return 'a', 'b' end -- returns 2 results
1
2
3
x, y = foo2()         -- x = 'a', y = 'b' 
x = foo2() -- x = 'a', 'b' is discarded
x, y, z = 10, foo2() -- x = 10, y = 'a', z = 'b'

參考來源