added io library and a skex version that uses it

This commit is contained in:
Izaya 2017-06-26 12:47:16 +00:00
parent 2da090371d
commit 3bc4ceae15
3 changed files with 130 additions and 0 deletions

View File

@ -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

View File

@ -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

34
modules/library/io.lua Normal file
View File

@ -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