Mercurial Hosting > luan
comparison docs/manual.html @ 242:b5a926c481a5
Handle first h1 tag differently.
git-svn-id: https://luan-java.googlecode.com/svn/trunk@243 21e917c8-12df-6dd8-5cb6-c86387c605b9
author | hugo.tech@gmail.com <hugo.tech@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9> |
---|---|
date | Tue, 07 Oct 2014 01:43:07 +0000 |
parents | 852840d64a7f |
children | c912f6de2053 |
comparison
equal
deleted
inserted
replaced
241:852840d64a7f | 242:b5a926c481a5 |
---|---|
9 </head> | 9 </head> |
10 | 10 |
11 <body> | 11 <body> |
12 | 12 |
13 <hr> | 13 <hr> |
14 <h1> | 14 <h1 class="main"> |
15 Luan Reference Manual | 15 Luan Reference Manual |
16 </h1> | 16 </h1> |
17 | 17 |
18 <P> | 18 <P> |
19 The reference manual is the official definition of the Luan language. | 19 The reference manual is the official definition of the Luan language. |
174 For a discussion of the decisions behind the design of Lua, | 174 For a discussion of the decisions behind the design of Lua, |
175 see the technical papers available at Lua's web site. | 175 see the technical papers available at Lua's web site. |
176 For a detailed introduction to programming in Lua, | 176 For a detailed introduction to programming in Lua, |
177 see Roberto's book, <em>Programming in Lua</em>. | 177 see Roberto's book, <em>Programming in Lua</em>. |
178 | 178 |
179 | |
180 <h1>2 – <a name="2">Basic Concepts</a></h1> | 179 <h1>2 – <a name="2">Basic Concepts</a></h1> |
181 | 180 |
182 <p> | 181 <p> |
183 This section describes the basic concepts of the language. | 182 This section describes the basic concepts of the language. |
184 | 183 |
1174 <pre> | 1173 <pre> |
1175 function foo (a) | 1174 function foo (a) |
1176 print("foo", a) | 1175 print("foo", a) |
1177 return coroutine.yield(2*a) | 1176 return coroutine.yield(2*a) |
1178 end | 1177 end |
1179 | 1178 |
1180 co = coroutine.create(function (a,b) | 1179 co = coroutine.create(function (a,b) |
1181 print("co-body", a, b) | 1180 print("co-body", a, b) |
1182 local r = foo(a+1) | 1181 local r = foo(a+1) |
1183 print("co-body", r) | 1182 print("co-body", r) |
1184 local r, s = coroutine.yield(a+b, a-b) | 1183 local r, s = coroutine.yield(a+b, a-b) |
1185 print("co-body", r, s) | 1184 print("co-body", r, s) |
1186 return b, "end" | 1185 return b, "end" |
1187 end) | 1186 end) |
1188 | 1187 |
1189 print("main", coroutine.resume(co, 1, 10)) | 1188 print("main", coroutine.resume(co, 1, 10)) |
1190 print("main", coroutine.resume(co, "r")) | 1189 print("main", coroutine.resume(co, "r")) |
1191 print("main", coroutine.resume(co, "x", "y")) | 1190 print("main", coroutine.resume(co, "x", "y")) |
1192 print("main", coroutine.resume(co, "x", "y")) | 1191 print("main", coroutine.resume(co, "x", "y")) |
1193 </pre><p> | 1192 </pre><p> |
1511 </pre><p> | 1510 </pre><p> |
1512 The grammar could see it in two ways: | 1511 The grammar could see it in two ways: |
1513 | 1512 |
1514 <pre> | 1513 <pre> |
1515 a = b + c(print or io.write)('done') | 1514 a = b + c(print or io.write)('done') |
1516 | 1515 |
1517 a = b + c; (print or io.write)('done') | 1516 a = b + c; (print or io.write)('done') |
1518 </pre><p> | 1517 </pre><p> |
1519 The current parser always sees such constructions | 1518 The current parser always sees such constructions |
1520 in the first way, | 1519 in the first way, |
1521 interpreting the open parenthesis | 1520 interpreting the open parenthesis |
1991 g(x, f()) -- g gets x plus all results from f() | 1990 g(x, f()) -- g gets x plus all results from f() |
1992 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) | 1991 a,b,c = f(), x -- f() is adjusted to 1 result (c gets nil) |
1993 a,b = ... -- a gets the first vararg parameter, b gets | 1992 a,b = ... -- a gets the first vararg parameter, b gets |
1994 -- the second (both a and b can get nil if there | 1993 -- the second (both a and b can get nil if there |
1995 -- is no corresponding vararg parameter) | 1994 -- is no corresponding vararg parameter) |
1996 | 1995 |
1997 a,b,c = x, f() -- f() is adjusted to 2 results | 1996 a,b,c = x, f() -- f() is adjusted to 2 results |
1998 a,b,c = f() -- f() is adjusted to 3 results | 1997 a,b,c = f() -- f() is adjusted to 3 results |
1999 return f() -- returns all results from f() | 1998 return f() -- returns all results from f() |
2000 return ... -- returns all received vararg parameters | 1999 return ... -- returns all received vararg parameters |
2001 return x,y,f() -- returns x, y, and all results from f() | 2000 return x,y,f() -- returns x, y, and all results from f() |
2462 Then, we have the following mapping from arguments to parameters and | 2461 Then, we have the following mapping from arguments to parameters and |
2463 to the vararg expression: | 2462 to the vararg expression: |
2464 | 2463 |
2465 <pre> | 2464 <pre> |
2466 CALL PARAMETERS | 2465 CALL PARAMETERS |
2467 | 2466 |
2468 f(3) a=3, b=nil | 2467 f(3) a=3, b=nil |
2469 f(3, 4) a=3, b=4 | 2468 f(3, 4) a=3, b=4 |
2470 f(3, 4, 5) a=3, b=4 | 2469 f(3, 4, 5) a=3, b=4 |
2471 f(r(), 10) a=1, b=10 | 2470 f(r(), 10) a=1, b=10 |
2472 f(r()) a=1, b=2 | 2471 f(r()) a=1, b=2 |
2473 | 2472 |
2474 g(3) a=3, b=nil, ... --> (nothing) | 2473 g(3) a=3, b=nil, ... --> (nothing) |
2475 g(3, 4) a=3, b=4, ... --> (nothing) | 2474 g(3, 4) a=3, b=4, ... --> (nothing) |
2476 g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 | 2475 g(3, 4, 5, 8) a=3, b=4, ... --> 5 8 |
2477 g(5, r()) a=5, b=1, ... --> 2 3 | 2476 g(5, r()) a=5, b=1, ... --> 2 3 |
2478 </pre> | 2477 </pre> |
8128 Here are some examples: | 8127 Here are some examples: |
8129 | 8128 |
8130 <pre> | 8129 <pre> |
8131 x = string.gsub("hello world", "(%w+)", "%1 %1") | 8130 x = string.gsub("hello world", "(%w+)", "%1 %1") |
8132 --> x="hello hello world world" | 8131 --> x="hello hello world world" |
8133 | 8132 |
8134 x = string.gsub("hello world", "%w+", "%0 %0", 1) | 8133 x = string.gsub("hello world", "%w+", "%0 %0", 1) |
8135 --> x="hello hello world" | 8134 --> x="hello hello world" |
8136 | 8135 |
8137 x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") | 8136 x = string.gsub("hello world from Lua", "(%w+)%s*(%w+)", "%2 %1") |
8138 --> x="world hello Lua from" | 8137 --> x="world hello Lua from" |
8139 | 8138 |
8140 x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) | 8139 x = string.gsub("home = $HOME, user = $USER", "%$(%w+)", os.getenv) |
8141 --> x="home = /home/roberto, user = roberto" | 8140 --> x="home = /home/roberto, user = roberto" |
8142 | 8141 |
8143 x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) | 8142 x = string.gsub("4+5 = $return 4+5$", "%$(.-)%$", function (s) |
8144 return load(s)() | 8143 return load(s)() |
8145 end) | 8144 end) |
8146 --> x="4+5 = 9" | 8145 --> x="4+5 = 9" |
8147 | 8146 |
8148 local t = {name="lua", version="5.2"} | 8147 local t = {name="lua", version="5.2"} |
8149 x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) | 8148 x = string.gsub("$name-$version.tar.gz", "%$(%w+)", t) |
8150 --> x="lua-5.2.tar.gz" | 8149 --> x="lua-5.2.tar.gz" |
8151 </pre> | 8150 </pre> |
8152 | 8151 |
8815 | 8814 |
8816 | 8815 |
8817 <p> | 8816 <p> |
8818 When called without arguments, | 8817 When called without arguments, |
8819 returns a uniform pseudo-random real number | 8818 returns a uniform pseudo-random real number |
8820 in the range <em>[0,1)</em>. | 8819 in the range <em>[0,1)</em>. |
8821 When called with an integer number <code>m</code>, | 8820 When called with an integer number <code>m</code>, |
8822 <code>math.random</code> returns | 8821 <code>math.random</code> returns |
8823 a uniform pseudo-random integer in the range <em>[1, m]</em>. | 8822 a uniform pseudo-random integer in the range <em>[1, m]</em>. |
8824 When called with two integer numbers <code>m</code> and <code>n</code>, | 8823 When called with two integer numbers <code>m</code> and <code>n</code>, |
8825 <code>math.random</code> returns a uniform pseudo-random | 8824 <code>math.random</code> returns a uniform pseudo-random |
10169 when the standard input (<code>stdin</code>) is a terminal, | 10168 when the standard input (<code>stdin</code>) is a terminal, |
10170 and as <code>lua -</code> otherwise. | 10169 and as <code>lua -</code> otherwise. |
10171 | 10170 |
10172 | 10171 |
10173 <p> | 10172 <p> |
10174 When called without option <code>-E</code>, | 10173 When called without option <code>-E</code>, |
10175 the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_2"><code>LUA_INIT_5_2</code></a> | 10174 the interpreter checks for an environment variable <a name="pdf-LUA_INIT_5_2"><code>LUA_INIT_5_2</code></a> |
10176 (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined) | 10175 (or <a name="pdf-LUA_INIT"><code>LUA_INIT</code></a> if it is not defined) |
10177 before running any argument. | 10176 before running any argument. |
10178 If the variable content has the format <code>@<em>filename</em></code>, | 10177 If the variable content has the format <code>@<em>filename</em></code>, |
10179 then <code>lua</code> executes the file. | 10178 then <code>lua</code> executes the file. |
10515 | 10514 |
10516 chunk ::= block | 10515 chunk ::= block |
10517 | 10516 |
10518 block ::= {stat} [retstat] | 10517 block ::= {stat} [retstat] |
10519 | 10518 |
10520 stat ::= ‘<b>;</b>’ | | 10519 stat ::= ‘<b>;</b>’ | |
10521 varlist ‘<b>=</b>’ explist | | 10520 varlist ‘<b>=</b>’ explist | |
10522 functioncall | | 10521 functioncall | |
10523 label | | 10522 label | |
10524 <b>break</b> | | 10523 <b>break</b> | |
10525 <b>goto</b> Name | | 10524 <b>goto</b> Name | |
10526 <b>do</b> block <b>end</b> | | 10525 <b>do</b> block <b>end</b> | |
10527 <b>while</b> exp <b>do</b> block <b>end</b> | | 10526 <b>while</b> exp <b>do</b> block <b>end</b> | |
10528 <b>repeat</b> block <b>until</b> exp | | 10527 <b>repeat</b> block <b>until</b> exp | |
10529 <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | | 10528 <b>if</b> exp <b>then</b> block {<b>elseif</b> exp <b>then</b> block} [<b>else</b> block] <b>end</b> | |
10530 <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | | 10529 <b>for</b> Name ‘<b>=</b>’ exp ‘<b>,</b>’ exp [‘<b>,</b>’ exp] <b>do</b> block <b>end</b> | |
10531 <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | | 10530 <b>for</b> namelist <b>in</b> explist <b>do</b> block <b>end</b> | |
10532 <b>function</b> funcname funcbody | | 10531 <b>function</b> funcname funcbody | |
10533 <b>local</b> <b>function</b> Name funcbody | | 10532 <b>local</b> <b>function</b> Name funcbody | |
10534 <b>local</b> namelist [‘<b>=</b>’ explist] | 10533 <b>local</b> namelist [‘<b>=</b>’ explist] |
10535 | 10534 |
10536 retstat ::= <b>return</b> [explist] [‘<b>;</b>’] | 10535 retstat ::= <b>return</b> [explist] [‘<b>;</b>’] |
10537 | 10536 |
10538 label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ | 10537 label ::= ‘<b>::</b>’ Name ‘<b>::</b>’ |
10539 | 10538 |
10540 funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] | 10539 funcname ::= Name {‘<b>.</b>’ Name} [‘<b>:</b>’ Name] |
10541 | 10540 |
10542 varlist ::= var {‘<b>,</b>’ var} | 10541 varlist ::= var {‘<b>,</b>’ var} |
10543 | 10542 |
10544 var ::= Name | prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | prefixexp ‘<b>.</b>’ Name | 10543 var ::= Name | prefixexp ‘<b>[</b>’ exp ‘<b>]</b>’ | prefixexp ‘<b>.</b>’ Name |
10545 | 10544 |
10546 namelist ::= Name {‘<b>,</b>’ Name} | 10545 namelist ::= Name {‘<b>,</b>’ Name} |
10547 | 10546 |
10548 explist ::= exp {‘<b>,</b>’ exp} | 10547 explist ::= exp {‘<b>,</b>’ exp} |
10549 | 10548 |
10550 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Number | String | ‘<b>...</b>’ | functiondef | | 10549 exp ::= <b>nil</b> | <b>false</b> | <b>true</b> | Number | String | ‘<b>...</b>’ | functiondef | |
10551 prefixexp | tableconstructor | exp binop exp | unop exp | 10550 prefixexp | tableconstructor | exp binop exp | unop exp |
10552 | 10551 |
10553 prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ | 10552 prefixexp ::= var | functioncall | ‘<b>(</b>’ exp ‘<b>)</b>’ |
10554 | 10553 |
10555 functioncall ::= prefixexp args | prefixexp ‘<b>:</b>’ Name args | 10554 functioncall ::= prefixexp args | prefixexp ‘<b>:</b>’ Name args |
10556 | 10555 |
10557 args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | tableconstructor | String | 10556 args ::= ‘<b>(</b>’ [explist] ‘<b>)</b>’ | tableconstructor | String |
10558 | 10557 |
10559 functiondef ::= <b>function</b> funcbody | 10558 functiondef ::= <b>function</b> funcbody |
10560 | 10559 |
10561 funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> | 10560 funcbody ::= ‘<b>(</b>’ [parlist] ‘<b>)</b>’ block <b>end</b> |
10562 | 10561 |
10568 | 10567 |
10569 field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp | 10568 field ::= ‘<b>[</b>’ exp ‘<b>]</b>’ ‘<b>=</b>’ exp | Name ‘<b>=</b>’ exp | exp |
10570 | 10569 |
10571 fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ | 10570 fieldsep ::= ‘<b>,</b>’ | ‘<b>;</b>’ |
10572 | 10571 |
10573 binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ | ‘<b>..</b>’ | | 10572 binop ::= ‘<b>+</b>’ | ‘<b>-</b>’ | ‘<b>*</b>’ | ‘<b>/</b>’ | ‘<b>^</b>’ | ‘<b>%</b>’ | ‘<b>..</b>’ | |
10574 ‘<b><</b>’ | ‘<b><=</b>’ | ‘<b>></b>’ | ‘<b>>=</b>’ | ‘<b>==</b>’ | ‘<b>~=</b>’ | | 10573 ‘<b><</b>’ | ‘<b><=</b>’ | ‘<b>></b>’ | ‘<b>>=</b>’ | ‘<b>==</b>’ | ‘<b>~=</b>’ | |
10575 <b>and</b> | <b>or</b> | 10574 <b>and</b> | <b>or</b> |
10576 | 10575 |
10577 unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ | 10576 unop ::= ‘<b>-</b>’ | <b>not</b> | ‘<b>#</b>’ |
10578 | 10577 |
10579 </pre> | 10578 </pre> |