Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
This file: http://angg.twu.net/eev-puro/mini-lua-intro.e http://angg.twu.net/eev-puro/mini-lua-intro.e.html Version: 2012dec30 **Please see:** http://angg.twu.net/eev-intros/find-eepitch-intro.html http://angg.twu.net/eev-intros/find-code-c-d-intro.html http://angg.twu.net/eev-current/eepitch.el Preparation: (code-c-d "lua51manual" "/usr/share/doc/lua5.1-doc/doc/manual.html") (code-c-d "luamanual" "/usr/share/doc/lua5.1-doc/doc/manual.html") (code-c-d "pil" "$S/http/www.lua.org/pil/") Quick index: # «.intro:types» (to "intro:types") # «.intro:PP» (to "intro:PP") # «.intro:functions» (to "intro:functions") # «.intro:lists» (to "intro:lists") # «.intro:coercion» (to "intro:coercion") # «.intro:string-literals» (to "intro:string-literals") # «.intro:table-constructors» (to "intro:table-constructors") # «.intro:keys» (to "intro:keys") # «.intro:length» (to "intro:length") # «.intro:for» (to "intro:for") # «.intro:global-vars» (to "intro:global-vars") # «.intro:local-vars» (to "intro:local-vars") # «.intro:eval» (to "intro:eval") # «.intro:assert» (to "intro:assert") # «.intro:closures» (to "intro:closures") # «.intro:vararg» (to "intro:vararg") # «.intro:iterators» (to "intro:iterators") # «.intro:__tostring» (to "intro:__tostring") # «.intro:metamethods» (to "intro:metamethods") # «.intro:io» (to "intro:io") # «.intro:string.gsub» (to "intro:string.gsub") # «.intro:charclasses» (to "intro:charclasses") # «.intro:string.match» (to "intro:string.match") # «.intro:io.popen» (to "intro:io.popen") # «.intro:5.1-isms» (to "intro:5.1-isms") # «.intro:5.1-deprecated» (to "intro:5.1-deprecated") # «.intro:coroutines» (to "intro:coroutines") # # «.prep:debs» (to "prep:debs") # «.prep:LUA_INIT» (to "prep:LUA_INIT") # «.prep:setenv» (to "prep:setenv") # «.prep:PiL1» (to "prep:PiL1") # «.prep:lua-src» (to "prep:lua-src") # # «.api:test1» (to "api:test1") # «.api:test2» (to "api:test2") «intro:types» (to ".intro:types") Lua has just a few basic data types. We will see later that file handlers are not basic data types. (find-pilw3m "2.html" "eight basic types") (find-luamanualw3m "#2.2" "eight basic types") (find-luamanualw3m "#pdf-type") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) print(1, type(1)) --> 1 number print(1.0, type(1.0)) --> 1 number print("abc", type("abc")) --> abc string print(nil, type(nil)) --> nil nil print(true, type(true)) --> true boolean print(false, type(false)) --> false boolean print(print, type(print)) --> function: 0x804d218 function print({2,3,5}, type({})) --> table: 0x8053ab0 table «intro:PP» (to ".intro:PP") I will sometimes use the function "PP", defined in my init file, instead of "print". (find-angg "LUA/lua50init.lua" "PP") (find-angg "LUA/lua50init.lua" nil "LUA_INIT") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) print(22, "22", print) --> 22 22 function: 0x9f11388 PP (22, "22", print) --> 22 "22" <function: 0x9f11388> print({2, 3, 5}) --> table: 0x9f1df70 PP ({2, 3, 5}) --> {1=2, 2=3, 3=5} «intro:functions» (to ".intro:functions") Functions are values, and "function f(args) body end" is just syntactical sugar for "f=function(args) body end". (find-luamanualw3m "#2.5.9" "f = function () body end") (find-pilw3m "6.html" "More about Functions") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) function square(a) return a*a end square = function (a) return a*a end print(square, square(2)) --> function: 0xa067dc0 4 «intro:lists» (to ".intro:lists") Expressions may return lists of results, and there are two ways to truncate lists to a single result. Multiple assignments work similarly to functions receiving multiple arguments. (find-pilw3m "5.1.html" "Multiple Results") (find-luamanualw3m "#2.4.3" "Assignment") (find-luamanualw3m "#2.5" "Expressions") Vlists: http://lua-users.org/lists/lua-l/2011-02/msg01467.html Dracula: http://lua-users.org/lists/lua-l/2011-02/msg01477.html * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) function foo() return 1, 2, 3 end print(1, 2, 3) --> 1 2 3 print(foo()) --> 1 2 3 print(99, foo()) --> 99 1 2 3 print(99, foo(), 200) --> 99 1 200 print(99, (foo())) --> 99 1 zero, one, two, three, four = 0, 1, 2, 3, nil zero, one, two, three, four = 0, 1, 2, 3 zero, one, two, three, four = 0, foo() print(zero, one, two, three, four) --> 0 1 2 3 nil f = function (zero, one, two, three, four) print(zero, one, two, three, four) end f(0, foo()) --> 0 1 2 3 nil «intro:coercion» (to ".intro:coercion") Numbers are automatically coerced to strings (and vice-versa) in certain situations. (find-luamanualw3m "#2.2.1" "Coercion") (find-pilw3m "2.4.html" "coercions") (find-luamanualw3m "#2.5.4" "Concatenation" "..") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) print(1+"2") --> 3 print("ab".."cd") --> abcd print("<".. 11 .. 22 ..">") --> <1122> «intro:string-literals» (to ".intro:string-literals") String literals can be quoted with '', "", [[]], and also with [=[...]=], [==[...]==], etc. There's a similar syntax for multi-line comments. (find-luamanualw3m "#2.1" "Literal strings") (find-luamanualw3m "#2.1" "Literal strings can also be defined") (find-luamanualw3m "#2.1" "opening long bracket of level n") -- A multi-line comment delimited by long brackets of level 4: --[====[ (inside the comment) --]====] * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) = "foo\nbar" = [[foo\nbar]] = [==[foo[[plic ploc]]bar]==] «intro:table-constructors» (to ".intro:table-constructors") Table constructors (find-luamanualw3m "#2.5.7" "Table Constructors") (find-pilw3m "2.5.html" "Tables") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) a = {10, 20, 30} print(a[2]) --> 20 print(200, "some string", a) --> 200 some string table: 0x8e2eee0 PP (200, "some string", a) --> 200 "some string" {1=10, 2=20, 3=30} b = {11, a, "foo", print} PP(b) --> {1=11, 2={1=10, 2=20, 3=30}, 3="foo", 4=<function: 0x8e1e020>} function foo() return 30, 40, 50 end c = {10, 20, foo()} --> {1=10, 2=20, 3=30, 4=40, 5=50} PP(c) «intro:keys» (to ".intro:keys") More on tables: keys and values don't need to be numbers, reading and changing key/value pairs, the {..., [key]=val, ...} syntax. * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) c = {11, 22, 33} c[2] = c[2]+c[3] -- change c[2] to 55 c[5] = 55 -- change c[5] to 55 (note that there's no c[4]) c["foo"] = "FOO" -- change c["foo"] to "FOO" print(c) --> table: 0x84ee130 PP(c) --> {1=11, 2=55, 3=33, 5=55, "foo"="FOO"} d = {11, 22, 33, [5]=555, ["bar"]="BAR", [c]="!"} PP(d) --> {1=11, 2=22, 3=33, 5=555, "bar"="BAR", {...}="!"} d[2] = nil -- delete d[2] d[c] = nil -- delete d[c] PP(d, d[2]) --> {1=11, 3=33, 5=555, "bar"="BAR"} <nil> x = {10, 20} y = {10, 20} PP(x, y) --> {1=10, 2=20} {1=10, 2=20} print(x, y) --> table: 0x84fc048 table: 0x84fb770 x[1] = 1000 PP(x, y) --> {1=1000, 2=20} {1=10, 2=20} «intro:length» (to ".intro:length") The somewhow non-deterministic "#" operator (find-luamanualw3m "#2.5.5" "The Length Operator") The four "#"s: http://lua-users.org/lists/lua-l/2011-04/msg00013.html "is"/"or": http://lua-users.org/lists/lua-l/2011-04/msg00065.html * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) f = function (T) printf("#%s = %d\n", mytostring(T), #T) end s = function (n) a[n] = n; f(a) end r = function (n) a[n] = nil; f(a) end a = {1, 2, 3, 4, 5} r(3) --> #{1=1, 2=2, 4=4, 5=5} = 5 s(7) --> #{1=1, 2=2, 4=4, 5=5, 7=7} = 5 s(6) --> #{1=1, 2=2, 4=4, 5=5, 6=6, 7=7} = 7 r(2) --> #{1=1, 4=4, 5=5, 6=6, 7=7} = 7 r(7) --> #{1=1, 4=4, 5=5, 6=6} = 6 s(100) --> #{1=1, 4=4, 5=5, 6=6, 100=100} = 1 f("Foo") --> #"Foo" = 3 «intro:for» (to ".intro:for") The "for" statement in its two forms. (find-es "lua5" "for") (find-luamanualw3m "#2.4.5" "For Statement") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) -- Numeric for: for i=1,4 do print(i) end --> 1 2 3 4 for i=5,-2,-2 do print(i) end --> 5 3 1 -1 for i=2,2 do print(i) end --> 2 for i=2,0 do print(i) end --> (nothing) -- Introduction to the generic for: a={4, 5, k="V"} PP(a) --> {1=4, 2=5, "k"="V"} for i=1,#a do print(i, a[i]) end --> 1 4 / 2 5 for key,val in pairs(a) do print(key, val) end --> 1 4 / 2 5 / k V for key,val in ipairs(a) do print(key, val) end --> 1 4 / 2 5 -- pairs(T) and ipairs(T) return "iterators". a={4,5,k="V"} print(a) --> table: 0x8237440 print(pairs(a)) --> function: 0x82279e8 table: 0x8237440 nil print(ipairs(a)) --> function: 0x8228370 table: 0x8237440 0 «intro:global-vars» (to ".intro:global-vars") Global variables are stored in a table (usually called _G). The T.key syntax is syntactic sugar for T["key"]. (find-luamanualw3m "#2.3" "environments") (find-luamanualw3m "#pdf-_G") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) for key,val in pairs(_G) do print(key, val) end print(string) --> table: 0x9961910 print(string.format) --> function: 0x9963d68 print(string["format"]) --> function: 0x9963d68 print(string.format("1+2=%d", 1+2)) --> 1+2=3 for key,val in pairs(string) do print(key, val) end print(print) --> function: 0x9961388 print(_G["print"]) --> function: 0x9961388 print(_G.print) --> function: 0x9961388 print(_G) --> table: 0x9960450 print(_G["_G"]) --> table: 0x9960450 print(_G._G) --> table: 0x9960450 «intro:local-vars» (to ".intro:local-vars") Each block can have local variables. Local variables are created dynamically by "local". * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) a = 22 do print(a) --> 22 local a = 33 -- the local "a" shadows the previous "a" print(a) --> 33 end -- discard the local "a" print(a) --> 22 «intro:eval» (to ".intro:eval") Look at this example, from PiL: (find-pilw3m "8.html") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) f = loadstring("i = i + 1") i = 0 f(); print(i) --> 1 f(); print(i) --> 2 The "loadstring" function is used by the interpreter. The interpreter reads code as a string, and executes that. This is done in two steps, both acessible by the user: "f = loadstring(str)" converts str to a function, f, and "f()" executes that function. (This is similar to Lisp's "read" and "eval", by the way). Actually we should use "f, err = loadstring(str)". * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) str = [[ print("hello") ]] print(str) --> print("hello") f, err = loadstring(str) print(f, err) --> function: 0x88ee6c8 nil f() --> hello str = [[ print("no closing quo ]] print(str) --> print("no closing quo f, err = loadstring(str) print(f, err) --> nil ...: unfinished string near '<eof>' «intro:assert» (to ".intro:assert") Assert (find-luamanualw3m "#pdf-assert") (find-pilw3m "8.3.html" "assert") http://lua-users.org/wiki/FinalizedExceptions * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) print(assert(10, 20, 30)) --> 10 20 30 print(assert(nil)) --> assertion failed print(assert(nil, "Error message")) --> error message str = [[ print("no closing quo ]] print (loadstring(str)) assert(loadstring(str)) str = "print('foo'); return 1+2" print(assert(loadstring(str))()) --> foo / 3 «intro:closures» (to ".intro:closures") Capture of local variables (a.k.a. "closures"). (find-pilw3m "6.1.html" "closures") (find-luamanualw3m "#2.6" "The loop creates ten closures") (find-es "lua5" "closure-reductions") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) foo = function () -- foo returns two functions... local storage return function () return storage end, -- a "getter", function (x) storage = x; return x end -- and a "setter". end get1, set1 = foo() -- get1 and set1 use a first "storage" get2, set2 = foo() -- get2 and set2 use a second, different "storage" print(set1(22), get1()) --> 22 22 print(set2(33), get1(), get2()) --> 33 22 33 «intro:vararg» (to ".intro:vararg") In functions declared with a "..." we can use "..." as an expression. (find-pilw3m "5.2.html" "Variable Number of Arguments") (find-lua51manualw3m "#2.5.9" "parlist ::=") (find-lua51manualw3m "#2.5" "a,b = ...") (find-lua51manualw3m "#2.5" "return ...") (find-lua51manualw3m "#2.5" "{...}") (find-pil2page (+ 19 41) "Lua 5.0" "..." "arg" "n field") (find-pil2page (+ 19 66) "local x = ...") (find-angg "LUA/lua50init.lua" "pack-and-unpack") Vlists: http://lua-users.org/lists/lua-l/2011-02/msg01467.html return nil: http://lua-users.org/lists/lua-l/2011-09/msg00301.html [No examples here yet, sorry!] «intro:iterators» (to ".intro:iterators") We can use closures to construct "iterators" for the generic for. (find-pilw3m "7.1.html" "Iterators and Closures") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) myiterator = function (n) return function () n = n / 2 if n >= 1 then return n end end end for n in myiterator(1024) do print(n) end --> 512 256 128 64 32 16 8 4 2 1 «intro:__tostring» (to ".intro:__tostring") Introduction to metatables: the "__tostring" metamethod (find-luamanualw3m "#2.8" "__add") (find-luamanualw3m "#pdf-tostring" "__tostring") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) mt = {} A = { name = "A" } B = { name = "B" } setmetatable(A, mt) setmetatable(B, mt) print(A) --> table: 0x9622d50 mt.__tostring = function (a) return a.name end print(A) --> A print(B) --> B PP(A) --> A PP(B) --> B PP({A, B}) --> {1=A, 2=B} = string.format("%s", A) -- error «intro:metamethods» (to ".intro:metamethods") Metatables: other metamethods (find-luamanualw3m "#2.8" "__add") See also: (find-TH "__mt") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) mt = {} A = { name = "A" } B = { name = "B" } setmetatable(A, mt) setmetatable(B, mt) mt.__tostring = function (a) return a.name end testmt = function (methodname) mt[methodname] = function (a, b) return methodname.."("..tostring(a)..", "..tostring(b)..")" end end testmt("__add"); print(A+1, 1+A) --> __add(A, 1) __add(1, A) testmt("__sub"); print(A-2, 2-A) --> __sub(A, 2) __sub(2, A) testmt("__mul"); print(A*3, 3*A) --> __mul(A, 3) __mul(3, A) testmt("__div"); print(A/4, 4/A) --> __div(A, 4) __div(4, A) testmt("__mod"); print(A%5, 5%A) --> __mod(A, 5) __mod(5, A) testmt("__pow"); print(A^6, 6^A) --> __pow(A, 6) __pow(6, A) testmt("__concat"); print(A..7) --> __concat(A, 7) testmt("__index"); print(A[8]) --> __index(A, 8) testmt("__unm"); print(-A) --> __unm(A, A) testmt("__call"); print(A(10)) --> __unm(A, A) Missing: (find-luamanualw3m "#2.8" "\"len\":") (find-luamanualw3m "#2.8" "\"eq\":") (find-luamanualw3m "#2.8" "\"eq\":" "getcomphandler") (find-luamanualw3m "#2.8" "\"lt\":") (find-luamanualw3m "#2.8" "\"le\":") (find-luamanualw3m "#2.8" "\"index\":") (find-luamanualw3m "#2.8" "\"newindex\":") (find-luamanualw3m "#2.8" "\"call\":") «intro:io» (to ".intro:io") Files and IO (find-luamanualw3m "#5.7") (find-pilw3m "21.1.html") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) print(io) for key,val in pairs(io) do print(key) end print(io.stdin) print(getmetatable(io.stdin)) print(getmetatable(io.stdin).__index) for key,val in pairs(getmetatable(io.stdin)) do print(key) end «intro:string.gsub» (to ".intro:string.gsub") Strings: string.gsub (find-luamanualw3m "#pdf-string.gsub") (find-luamanualw3m "#5.4.1" "Patterns") (find-pilw3m "20.html" "The String Library") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) pg = function (...) print(string.gsub(...)) end pg = function (...) PP((string.gsub(...))) end pg = function (...) PP(string.gsub(...)) end t = {name="lua", ver="5.1.4"} tv = function (v) return t[v] end run = function (s) return loadstring(s)() end PP(tv("ver")) --> "5.1.4" PP(run("return 22, 33")) --> 22 33 PP(os.getenv("HOME")) --> "/home/edrx" PP(os.getenv("FOO")) --> <nil> pg("hel wo", "(%w+)", "%1 %1") --> "hel hel wo wo" 2 pg("hel wo", "(%w+)", "%1 %1", 1) --> "hel hel wo" 1 pg("hel wo fr Lu", "(%w+)%s*(%w+)", "%2 %1") --> "wo hel Lu fr" 2 pg("ho=$HOME, u=$USER", "%$(%w+)", os.getenv) --> "ho=/home/edrx, u=edrx" 2 pg("ho=$HOME, u=$FOO", "%$(%w+)", os.getenv) --> "ho=/home/edrx, u=$FOO" 2 pg("4+5=$return 4+5$", "%$(.-)%$", run) --> "4+5=9" 1 pg("$name_$ver.tar.gz", "%$(%w+)", tv) --> "lua_5.1.tar.gz" 2 «intro:charclasses» (to ".intro:charclasses") (find-luamanualw3m "#5.4.1" "Character Class") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) pc = function (pat) io.write("\"") for k=32,126 do local c = string.char(k) if c:match(pat) then io.write(c) end end print("\"") end pc "%d" --> "0123456789" pc "%x" --> "0123456789ABCDEFabcdef" pc "%w" --> "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" pc "%a" --> "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" pc "%u" --> "ABCDEFGHIJKLMNOPQRSTUVWXYZ" pc "%l" --> "abcdefghijklmnopqrstuvwxyz" pc "%p" --> "!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~" pc "%s" --> " " pc "[A-Jk-z]" --> "ABCDEFGHIJklmnopqrstuvwxyz" pc "[^A-Za-z]" --> " !"#$%&'()*+,-./0123456789:;<=>?@[\]^_`{|}~" «intro:string.match» (to ".intro:string.match") Strings: string.match (find-luamanualw3m "#pdf-string.match") (find-luamanualw3m "#5.4.1" "Patterns") (find-pilw3m "20.html" "The String Library") (find-pil2page (+ 19 175) "20. The String Library") [[ This section is not ready yet ]] «intro:io.popen» (to ".intro:io.popen") (find-angg "LUA/lua50init.lua" "getoutput") [[ This section is not ready yet ]] «intro:5.1-isms» (to ".intro:5.1-isms") PiL1 - whose HTML version is available at: http://www.lua.org/pil/ is about Lua 5.0. Here are pointers to some important changes: In 5.1 we use '#' instead of table.getn and the 'n' field. And in 5.1 we can use #str instead of string.len(str). See: (find-lua50manualw3m "#5.4" "size") (find-lua50manualw3m "#getn" "table.getn") (find-lua50manualw3m "#getn" "table.setn") (find-lua51manualw3m "#7.2" "table.setn" "deprecated") (to "intro:length") In 5.1 we can use ':' on strings. Note that on literals we need "()"s. (find-lua51manualw3m "#pdf-string" "metatable" "s:byte(i)") Modules are a 5.1-ism (described in chapter 15 of PiL2), but my advice is: ***don't write modules yourself***! One day _ENV will be ready, and it will be much better. (find-lua51manualw3m "#5.3" "Modules") (find-lua52manualw3m "#6.3" "Modules") (find-lua51manualw3m "#pdf-module") (find-lua52manualw3m "#8.2" "module is deprecated") (find-pil2page (+ 19 137) "15. Modules and Packages") In 5.0 we had to use "arg" for vararg functions. In 5.1 we can use "..." as an expression, and "arg" has been deprecated - except as a global variable in Lua stand-alone. (find-pilw3m "5.2.html" "Variable Number of Arguments") (find-lua51manualw3m "#6" "lua -la b.lua t1 t2") The function string.match appeared in Lua 5.1, and it is usually much more convenient than string.find. (find-lua50manualw3m "#5.3" "string.find") (find-lua51manualw3m "#pdf-string.find") (find-lua51manualw3m " #pdf-string.match") (find-pil2page (+ 19 178) "20. The String Library") «intro:5.1-deprecated» (to ".intro:5.1-deprecated") In 5.0 we could write "for k,v in t", but that was deprecated. In 5.1 this has gone, and we have to use "for k,v in pairs(t)" instead. (find-lua40manualw3m "#for" "table for") (find-lua50manualw3m "#6" "Incompatibilities with version 4.0" "for k,v in t") (find-lua51manualw3m "#pdf-pairs") Also: (find-lua50manualw3m "#5.4" "table.foreach") (find-lua50manualw3m "#5.4" "table.foreachi") (find-lua51manualw3m "#7.2" "table.foreach and table.foreachi") «intro:coroutines» (to ".intro:coroutines") (find-pilw3m "7.1.html" "Iterators and Closures") (find-pilw3m "9.3.html" "Coroutines as Iterators") (find-es "lua5" "generators-pseudocode") (find-es "lua5" "coroutines") * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) co = coroutine.create(function () print("hi") end) print(co) --> thread: 0x8071d98 print(coroutine.status(co)) --> suspended coroutine.resume(co) --> hi print(coroutine.status(co)) --> dead * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) co = coroutine.create(function () for i=1,4 do print("co", i) coroutine.yield() end end) coroutine.resume(co) --> co 1 print(coroutine.status(co)) --> suspended coroutine.resume(co) --> co 2 coroutine.resume(co) --> co 3 coroutine.resume(co) --> co 4 print(coroutine.status(co)) --> suspended coroutine.resume(co) --> (nothing) print(coroutine.status(co)) --> dead coroutine.resume(co) --> (nothing) * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) co = coroutine.create(function (a, b) print(a, b) print(coroutine.yield("foo", "bar")) print(coroutine.yield("bletch")) return "plic", "ploc" end) print(coroutine.resume(co, 22, 33)) --> 22 33 / true foo bar print(coroutine.resume(co, 44, 55)) --> 44 55 / true bletch print(coroutine.resume(co, 66, 77)) --> 66 77 / true plic ploc print(coroutine.status(co)) --> dead print(coroutine.resume(co, 88, 99)) --> false cannot resume dead coroutine * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) function permgen (a, n) if n == 0 then -- PP(a) coroutine.yield(a) else for i=1,n do a[n], a[i] = a[i], a[n] permgen(a, n - 1) a[n], a[i] = a[i], a[n] end end end function perm (a) local n = table.getn(a) local co = coroutine.create(function () permgen(a, n) end) return function () -- iterator local code, res = coroutine.resume(co) return res end end for p in perm{"a", "b", "c"} do PP(p) end function perm (a) local n = table.getn(a) return coroutine.wrap(function () permgen(a, n) end) end for p in perm{"a", "b", "c"} do PP(p) end ifoo = function () return coroutine.wrap(function () coroutine.yield("foo") coroutine.yield("bar") coroutine.yield("plic") end) end for i in ifoo() do print(i) end «prep:debs» (to ".prep:debs") (find-status "lua5.1") (find-vldifile "lua5.1.list") (find-udfile "lua5.1/") (find-status "lua5.1-doc") (find-vldifile "lua5.1-doc.list") (find-udfile "lua5.1-doc/") «prep:LUA_INIT» (to ".prep:LUA_INIT") # http://angg.twu.net/LUA/lua50init.lua # (eek "<up> "M-x brep") * (eepitch-shell) * (eepitch-kill) * (eepitch-shell) mkdir -p $S/http/angg.twu.net/LUA/ cd $S/http/angg.twu.net/LUA/ wget -N 'http://angg.twu.net/LUA/lua50init.lua' echo 'http://angg.twu.net/LUA/lua50init.lua' >> ~/.psne.log cp -v $S/http/angg.twu.net/LUA/lua50init.lua ~/ «prep:setenv» (to ".prep:setenv") * ;; (find-angg ".zshrc" "lua") * ;; (find-luamanualw3m "#6" "LUA_INIT" "@filename") * ;; (find-man "1 lua5.1" "LUA_INIT") * (setenv "LUA_INIT" nil) * (setenv "LUA_INIT" (format "@%s/LUA/lua50init.lua" (getenv "HOME"))) «prep:PiL1» (to ".prep:PiL1") Download a local copy of PiL1 - http://www.lua.org/pil/ Use a tgz because the internet at PURO is REALLY slow. # (find-es "lua5" "pil1.tgz") # http://angg.twu.net/tmp/pil1.tgz # (eek "<up> M-x brfl") # http://www.lua.org/pil/index.html # (eek "<up> M-x brwl") * (eepitch-shell) * (eepitch-kill) * (eepitch-shell) mkdir -p $S/http/angg.twu.net/tmp/ cd $S/http/angg.twu.net/tmp/ wget -N 'http://angg.twu.net/tmp/pil1.tgz' echo 'http://angg.twu.net/tmp/pil1.tgz' >> ~/.psne.log # mkdir -p $S/http/www.lua.org/ cd $S/http/www.lua.org/ tar -C $S/http/www.lua.org/ -xvzf $S/http/angg.twu.net/tmp/pil1.tgz «prep:lua-src» (to ".prep:lua-src") # http://www.lua.org/ftp/lua-5.1.4.tar.gz # (find-es "lua5" "install-5.1.4") # (find-lua51file "") # (find-lua51file "INSTALL") # (find-lua51file "omltl") # (find-lua51file "src/Makefile") # (find-node "(make)Phony Targets" "`.PHONY'") * (eepitch-shell) * (eepitch-kill) * (eepitch-shell) mkdir -p $S/http/www.lua.org/ftp/ cd $S/http/www.lua.org/ftp/ wget -N 'http://www.lua.org/ftp/lua-5.1.4.tar.gz' echo 'http://www.lua.org/ftp/lua-5.1.4.tar.gz' >> ~/.psne.log * (eepitch-shell) * (eepitch-kill) * (eepitch-shell) mkdir ~/usrc/ rm -Rfv ~/usrc/lua-5.1.4/ mkdir ~/usrc/lua-5.1.4/ tar -C ~/usrc/ -xvzf $S/http/www.lua.org/ftp/lua-5.1.4.tar.gz cd ~/usrc/lua-5.1.4/ find * -name '*.[ch]' | sort > .files.ch ;# (find-lua51file ".files.ch") etags $(cat .files.ch) ;# (find-lua51file "TAGS") cp -iv src/Makefile src/Makefile.orig patch -p0 src/Makefile <<'%%%' 11c11 < CFLAGS= -O2 -Wall $(MYCFLAGS) --- > CFLAGS= -O2 -Wall $(MYCFLAGS) -g ;# (find-lua51file "om") %%% make linux test local 2>&1 | tee omltl «api:test1» (to ".api:test1") # (find-lua51manualw3m "#lua_call") # (find-lua51manualw3m "#lua_call" "Here it is in C:") # (find-angg ".lua51/PP.gdb") * (find-sh0 "echo 'math.sin(0)' > /tmp/foo.lua") * (eepitch-gdb-lua) * (eepitch-kill) * (eepitch-gdb-lua) set args /tmp/foo.lua tbr main run br math_sin cont define lua_getglobal call lua_getfield (L, -10002, $arg0) end lua_getglobal "print" call lua_pushstring(L, "2") call lua_pushstring(L, "3") call lua_call(L, 2, 0) «api:test2» (to ".api:test2") * (eepitch-gdb-lua) * (eepitch-kill) * (eepitch-gdb-lua) tbr main run br math_sin cont a = 22 b = 33 math.sin(0) define lua_getglobal call lua_getfield (L, -10002, $arg0) end lua_getglobal "print" lua_getglobal "a" lua_getglobal "b" call lua_call(L, 2, 0) cont # Local Variables: # coding: raw-text-unix # ee-anchor-format: "«%s»" # End: