Warning: this is an htmlized version!
The original is here, and the conversion rules are here. |
-- This file: -- http://anggtwu.net/LUA/CoroRepl2.lua.html -- http://anggtwu.net/LUA/CoroRepl2.lua -- (find-angg "LUA/CoroRepl2.lua") -- Author: Eduardo Ochs <eduardoochs@gmail.com> -- -- (defun e () (interactive) (find-angg "LUA/CoroRepl2.lua")) -- (defun o () (interactive) (find-angg "LUA/CoroRepl1.lua")) -- «.ReplIOB» (to "ReplIOB") -- «.ReplIOB-tests» (to "ReplIOB-tests") -- «.ReplOut» (to "ReplOut") -- «.ReplOut-tests» (to "ReplOut-tests") -- «.ReplIOC» (to "ReplIOC") -- «.ReplIOC-tests» (to "ReplIOC-tests") -- «.ReplTest» (to "ReplTest") -- «.ReplTest-tests» (to "ReplTest-tests") sprint = function (...) local args = pack(...) return mapconcat(tostring, args, "\t", args.n).."\n" end -- ____ _ ___ ___ ____ -- | _ \ ___ _ __ | |_ _/ _ \| __ ) -- | |_) / _ \ '_ \| || | | | | _ \ -- | _ < __/ |_) | || | |_| | |_) | -- |_| \_\___| .__/|_|___\___/|____/ -- |_| -- -- "Repl I/O, basic version" -- «ReplIOB» (to ".ReplIOB") ReplIOB = Class { type = "ReplIOB", __index = { pwrite = function (riob,p) io.write(p) end, owrite = function (riob,o) io.write(o) end, oprint = function (riob,...) print(...) end, iread = function (riob) return io.read() end, stop = function (riob) end, }, } -- «ReplIOB-tests» (to ".ReplIOB-tests") -- ____ _ ___ _ -- | _ \ ___ _ __ | |/ _ \ _ _| |_ -- | |_) / _ \ '_ \| | | | | | | | __| -- | _ < __/ |_) | | |_| | |_| | |_ -- |_| \_\___| .__/|_|\___/ \__,_|\__| -- |_| -- -- «ReplOut» (to ".ReplOut") ReplOut = Class { type = "ReplOut", from = function (i,o,p,s) return ReplOut {i=i, o=o, p=p, s=s} end, __tostring = function (ro) local f = function (x) return (mytostring(x):gsub("\\\n", "|")) end return format("[i=%s, o=%s, p=%s, s=%s]", f(ro.i), f(ro.o), f(ro.p), f(ro.s)) end, __index = { concat = function (ro) return (ro.i or "")..(ro.o or "")..(ro.p or "") end, }, } -- «ReplOut-tests» (to ".ReplOut-tests") -- ____ _ ___ ___ ____ -- | _ \ ___ _ __ | |_ _/ _ \ / ___| -- | |_) / _ \ '_ \| || | | | | | -- | _ < __/ |_) | || | |_| | |___ -- |_| \_\___| .__/|_|___\___/ \____| -- |_| -- -- "Repl I/O, coroutine version" -- «ReplIOC» (to ".ReplIOC") ReplIOC = Class { type = "ReplIOC", __index = { data = function (rioc,stop) return ReplOut.from(rioc.i, rioc.o, rioc.p, stop) end, setdata = function (rioc,i,s) rioc.i,rioc.o,rioc.p,rioc.s = i,nil,nil,s end, pwrite = function (rioc,p) rioc.p = p end, owrite = function (rioc,o) rioc.o = (rioc.o or "")..o end, oprint = function (rioc,...) rioc:owrite(sprint(...)) end, stop = function (rioc) return rioc:data(nil,"stop") end, iread = function (rioc) local i = coy(rioc:data()) rioc:setdata(i) return rioc.i end, }, } -- «ReplIOC-tests» (to ".ReplIOC-tests") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "CoroRepl2.lua" r = ReplIOC {} = r:data() = r:setdata("iiii") = r:data() = r:data("STOP") = r:oprint(20, nil) = r:data() = r:oprint(30, 40) = r:data() --]==] -- ____ _ _____ _ -- | _ \ ___ _ __ | |_ _|__ ___| |_ -- | |_) / _ \ '_ \| | | |/ _ \/ __| __| -- | _ < __/ |_) | | | | __/\__ \ |_ -- |_| \_\___| .__/|_| |_|\___||___/\__| -- |_| -- «ReplTest» (to ".ReplTest") ReplTest = Class { type = "ReplTest", __index = { read1 = function (r) r.rio:pwrite("c> "); return r.rio:iread() end, read2 = function (r) r.rio:pwrite("c>> "); return r.rio:iread() end, simple = function (r) while true do local a = r:read1() local b = r:read2() r.rio:oprint("Received: "..bitrim(a)..","..bitrim(b)) if b:match"stop" then return r.rio:stop() end end end, }, } -- «ReplTest-tests» (to ".ReplTest-tests") --[==[ * (eepitch-lua51) * (eepitch-kill) * (eepitch-lua51) dofile "CoroRepl2.lua" rt = ReplTest { rio = ReplIOB {} } = rt:simple() foo bar plic ploc qux stop rt = ReplTest { rio = ReplIOC {} } f = cow(function () return rt:simple() end) = f() = f("foo") = f(" bar") = f("plic") = f(" ploc") = f("qux") = f(" stop") = f("") f = cow(function () rt:simple() end) g = function (...) PP(f(...)) end g() g("foo") g(" bar") g("plic") g(" ploc") g("qux") g(" stop") g("") h = cow(function () coy(20) coy(30) return 40 end) = h() --> 20 = h() --> 30 = h() --> 40 = h() --> error: cannot resume dead coroutine --]==] -- Local Variables: -- coding: utf-8-unix -- End: