From 3bc4ceae15e92350b3757efc1fc663b8cb32b831 Mon Sep 17 00:00:00 2001 From: XeonSquared Date: Mon, 26 Jun 2017 12:47:16 +0000 Subject: [PATCH] added io library and a skex version that uses it --- modules/applications/skex2-io.lua | 65 +++++++++++++++++++++++++++++++ modules/library/buffer.lua | 31 +++++++++++++++ modules/library/io.lua | 34 ++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 modules/applications/skex2-io.lua create mode 100644 modules/library/buffer.lua create mode 100644 modules/library/io.lua diff --git a/modules/applications/skex2-io.lua b/modules/applications/skex2-io.lua new file mode 100644 index 0000000..2494894 --- /dev/null +++ b/modules/applications/skex2-io.lua @@ -0,0 +1,65 @@ +function skex(s) + local c,cs,cT,lT,lP="","",{},{},1 + local function lf(s) + local f=io.open(s,"rb") + c=f:read("*a") + f:close() + end + local function wf(s) + local f,c=io.open(s,"wb") + for k,v in ipairs(lT) do f:write(v.."\n") end + f:close() + end + if s then + lf(s) + for l in c:gmatch("(.-)\n") do lT[#lT+1]=l end + end + while true do + cs=readln() + cT={} + for w in cs:gmatch("%S+") do cT[#cT+1]=w end + if cT[1] == "q" then break + elseif cT[1] == "l" then + for i = (TN(cT[2]) or 1), (TN(cT[3]) or #lT) do + print(TS(i).."\t"..(lT[i] or "")) + end + elseif cT[1] == "a" or cT[1] == "i" or cT[1] == "s" then + if TN(cT[2]) then lP=TN(cT[2]) end + if cT[1] == "s" then for i = 1,TN(cT[3]) do T.remove(lT,i+(TN(cT[2])-1)) end end + if cT[1] == "a" then lP=lP+1 end + while true do + cs=readln() + if cs~="." then + T.insert(lT,lP,cs) + lP=lP+1 + else break end + end + elseif cT[1] == "f" then + s=cT[2] or s + print(s) + elseif cT[1] == "e" then + c="" + for k,v in ipairs(lT) do c=c..v.."\n" end + print(pcall(load(c))) + elseif cT[1] == "r" then + s=cT[2] or s + wf(s) + elseif cT[1] == "w" then + s=cT[2] or s + wf(s) + elseif cT[1] == "d" then + for i = 1, TN(cT[3])-TN(cT[2]) do + T.remove(lT,cT[2]) + end + elseif cT[1] == "p" then + lP=TN(cT[2]) or lP + print(lP) + elseif cs:sub(1,1) == "!" then + c="" + for k,v in ipairs(lT) do c=c..v.."\n" end + print(pcall(load(cs:sub(2)))) + else + print("?") + end + end +end diff --git a/modules/library/buffer.lua b/modules/library/buffer.lua new file mode 100644 index 0000000..e886bb1 --- /dev/null +++ b/modules/library/buffer.lua @@ -0,0 +1,31 @@ +function cB(w,c) + local t={} + t.b="" + function t.w(s,d) + s.b=s.b..TS(d) + end + t.write = t.w + function t.r(s,l) + if type(l) == "number" then + local ns,bs=s.b:sub(1,l+1),s.b:sub(l+2) + s.b=bs + return ns + elseif type(l) == "string" then + local oS=s.b + if l == "*a" then + s.b="" + return oS + elseif l == "*l" then + S=s.b:find("\n") or #s.b + s.b=s.b:sub(S+1) + rS = oS:sub(1,S-1) + if rS:len() < 1 then return nil end + return rS + end + end + end + t.read = t.r + t.close = c + w(t) + return t +end diff --git a/modules/library/io.lua b/modules/library/io.lua new file mode 100644 index 0000000..ae42c3d --- /dev/null +++ b/modules/library/io.lua @@ -0,0 +1,34 @@ +_G.io = {} +io.write = write +function io.open(n,m) + m=m or "rb" + local h=fopen(n,m) + if h then + if h and m:sub(1,1) == "w" then + return cB(function(bt) + bt.s="o" + s("io worker: "..n,function() + while true do + if bt.s ~= "o" and bt.b == "" then fclose(h) break end + local nd = bt:read(2048) + if nd ~= nil and nd ~= "" then + fwrite(h,nd) + end + C.yield() + end + end) end,function(bt) bt.s="c" end) + elseif h and m:sub(1,1) == "r" then + return cB(function(bt) + local sb = "" + repeat + bt.b=bt.b..sb + sb=fread(h,2048) + until sb == "" or sb == nil + fclose(h) + C.yield() + end,function() end) + end + else + return false + end +end