您正在查看: Lua 分类下的文章

Lua 5.1 Reference Manual - 8 - The Complete Syntax of Lua

8 - The Complete Syntax of lua

Here is the complete syntax of lua in extended BNF. (It does not describe operator precedences.)

chunk ::= {stat [`<b>;</b>&acute;]} [laststat [`<b>;</b>&acute;]]

block ::= chunk

stat ::=  varlist1 `<b>=</b>&acute; explist1 | 

functioncall |
do block end |
while exp do block end |
repeat block until exp |
if exp then block {elseif exp then block} [else block] end |
for Name <b>=</b>&acute; exp,´ exp [<b>,</b>&acute; exp] <b>do</b> block <b>end</b> | <b>for</b> namelist <b>in</b> explist1 <b>do</b> block <b>end</b> | <b>function</b> funcname funcbody | <b>local</b> <b>function</b> Name funcbody | <b>local</b> namelist [=´ explist1]laststat ::= return [explist1] | break

funcname ::= Name {`<b>.</b>&acute; Name} [`<b>:</b>&acute; Name]

varlist1 ::= var {`<b>,</b>&acute; var}

var ::=  Name | prefixexp `<b>[</b>&acute; exp `<b>]</b>&acute; | prefixexp `<b>.</b>&acute; Namenamelist ::= Name {`<b>,</b>&acute; Name}

explist1 ::= {exp `<b>,</b>&acute;} exp

exp ::=  <b>nil</b> | <b>false</b> | <b>true</b> | Number | String | `<b>...</b>&acute; | function | 

prefixexp | tableconstructor | exp binop exp | unop expprefixexp ::= var | functioncall | <b>(</b>&acute; exp)´

functioncall ::=  prefixexp args | prefixexp `<b>:</b>&acute; Name argsargs ::=  `<b>(</b>&acute; [explist1] `<b>)</b>&acute; | tableconstructor | Stringfunction ::= <b>function</b> funcbody

funcbody ::= `<b>(</b>&acute; [parlist1] `<b>)</b>&acute; block <b>end</b>

parlist1 ::= namelist [`<b>,</b>&acute; `<b>...</b>&acute;] | `<b>...</b>&acute;

tableconstructor ::= `<b>{</b>&acute; [fieldlist] `<b>}</b>&acute;

fieldlist ::= field {fieldsep field} [fieldsep]

field ::= `<b>[</b>&acute; exp `<b>]</b>&acute; `<b>=</b>&acute; exp | Name `<b>=</b>&acute; exp | exp

fieldsep ::= `<b>,</b>&acute; | `<b>;</b>&acute;

binop ::= `<b>+</b>&acute; | `<b>-</b>&acute; | `<b>*</b>&acute; | `<b>/</b>&acute; | `<b>^</b>&acute; | `<b>%</b>&acute; | `<b>..</b>&acute; | 

<b>&lt;</b>&acute; |<=´ | <b>&gt;</b>&acute; |>=´ | <b>==</b>&acute; |~=´ |
and | or

unop ::= `<b>-</b>&acute; | <b>not</b> | `<b>#</b>&acute;

Last update: Mon Jun 5 17:05:27 BRT 2006

Lua 5.1 Reference Manual - 7 - Incompatibilities with the Previous Version

7 - Incompatibilities with the Previous Version

Here we list the incompatibilities that may be found when moving a program from lua 5.0 to lua 5.1. You can avoid most of the incompatibilities compiling Lua with appropriate options (see file luaconf.h). However, all these compatibility options will be removed in the next version of Lua.

Lua 5.1 Reference Manual - 7.1 - Changes in the Language

7.1 - Changes in the Language

  • The vararg system changed from the pseudo-argument arg with a table with the extra arguments to the vararg expression. (Option lua_COMPAT_VARARG in luaconf.h.)
  • There was a subtle change in the scope of the implicit variables of the for statement and for the repeat statement.
  • The long string/long comment syntax ([[string]]) does not allow nesting. You can use the new syntax ([=[string]=]) in these cases. (Option LUA_COMPAT_LSTR in luaconf.h.)

Lua 5.1 Reference Manual - 7.2 - Changes in the Libraries

7.2 - Changes in the Libraries

  • Function string.gfind was renamed string.gmatch. (Option lua_COMPAT_GFIND)
  • When string.gsub is called with a function as its third argument, whenever this function returns nil or false the replacement string is the whole match, instead of the empty string.
  • Function table.setn was deprecated. Function table.getn corresponds to the new length operator (#); use the operator instead of the function. (Option lua_COMPAT_GETN)
  • Function loadlib was renamed package.loadlib. (Option LUA_COMPAT_LOADLIB)
  • Function math.mod was renamed math.fmod. (Option LUA_COMPAT_MOD)
  • Functions table.foreach and table.foreachi are deprecated. You can use a for loop with pairs or ipairs instead.
  • There were substantial changes in function require due to the new module system. However, the new behavior is mostly compatible with the old, but require gets the path from package.path instead of from LUA_PATH.
  • Function collectgarbage has different arguments. Function gcinfo is deprecated; use collectgarbage("count") instead.

Lua 5.1 Reference Manual - 7.3 - Changes in the API

7.3 - Changes in the API

  • The luaopen_* functions (to open libraries) cannot be called directly, like a regular C function. They must be called through lua, like a Lua function.
  • Function lua_open was replaced by lua_newstate to allow the user to set a memory-allocation function. You can use luaL_newstate from the standard library to create a state with a standard allocation function (based on realloc).
  • Functions luaL_getn and luaL_setn (from the auxiliary library) are deprecated. Use lua_objlen instead of luaL_getn and nothing instead of luaL_setn.
  • Function luaL_openlib was replaced by luaL_register.
  • Function luaL_checkudata now throws an error when the given value is not a userdata of the expected type. (In Lua 5.0 it returned NULL.)