Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local root = ".SUBP"
- local lastpar = nil
- local function getTableSize(table)
- local cur = 1
- for i,v in ipairs(table) do
- cur = i
- end
- return cur
- end
- local function split(str, pat)
- local t = {} -- NOTE: use {n = 0} in Lua-5.0
- if str ~= nil then
- local fpat = "(.-)" .. pat
- local last_end = 1
- local s, e, cap = str:find(fpat, 1)
- while s do
- if s ~= 1 or cap ~= "" then
- table.insert(t,cap)
- end
- last_end = e+1
- s, e, cap = str:find(fpat, last_end)
- end
- if last_end <= #str then
- cap = str:sub(last_end)
- table.insert(t, cap)
- end
- else
- print("FAILED TO SPLIT by:"..pat)
- end
- return t
- end
- local function getLines(dir)
- local data = {}
- local cur = 1
- local file = fs.open(dir, "r")
- local line = file.readLine()
- while line ~= nil do
- data[cur] = line
- cur = cur + 1
- line = file.readLine()
- end
- file.close()
- return data
- end
- local function endsWith(String, End)
- return End=='' or string.sub(String,-string.len(End))==End
- end
- local function isInTable(line, findtable)
- local found = nil
- for i,v in ipairs(findtable) do
- if string.find(line, v) == nil then else
- found = v
- end
- end
- return found
- end
- local function convert(prog, toprog)
- if fs.exists(prog) and fs.exists(root.."/"..toprog.."/")then
- local fp = fs.open(prog, "a")
- fp.close()
- local lines = getLines(prog)
- if fs.exists(root.."/"..toprog.."/"..prog, "a") then
- fs.delete(root.."/"..toprog.."/"..prog)
- end
- local file = fs.open(root.."/"..toprog.."/"..prog, "a")
- for i,v in ipairs(lines) do
- print(line)
- local line = v
- file.writeLine(line)
- end
- file.close()
- return true
- else
- return false
- end
- end
- local function delete(program)
- if program ~= nil and program ~= "" then
- if fs.exists(program) then
- fs.delete(program)
- local files = fs.list(root.."/"..program.."/")
- for i,v in ipairs(files) do
- local line = v
- if fs.exists(root.."/"..program.."/"..line) then
- fs.delete(root.."/"..program.."/"..line)
- end
- end
- if fs.exists(root.."/"..program.."/") then
- fs.delete(root.."/"..program.."/")
- end
- return true
- else
- return false
- end
- else
- return false
- end
- end
- local function decompile(program)
- if fs.exists(root.."/"..program) then
- local lines = getLines(program)
- if fs.exists(program) then
- fs.delete(program)
- end
- local file = fs.open(program, "a")
- local nline = {}
- local ncur = 1
- local hasdone = alse
- for i,v in ipairs(lines) do
- local line = v
- if endsWith(line, "--PART@@") then
- if hasdone then else
- nline[ncur] = "--input parts"
- ncur = ncur + 1
- hasdone = true
- end
- elseif endsWith(line, "@FUNCTION OF")then
- local splt = split(line, "@")
- oline = splt[getTableSize(splt)-1]
- oline = string.gsub(oline, "<@#@>", "@")
- nline[ncur] = oline
- ncur = ncur + 1
- else
- nline[ncur] = line
- ncur = ncur + 1
- end
- end
- for i,v in ipairs(nline) do
- local line = v
- file.writeLine(line)
- end
- file.close()
- return true
- else
- return false
- end
- end
- local function compile(program)
- if fs.exists(root.."/"..program) then
- local files = fs.list(root.."/"..program)
- local fnames = {}
- local fcur = 1
- for i,v in ipairs(files) do
- local line = string.gsub(v, ".fpt")
- fnames[fcur] = line
- fcur = fcur + 1
- end
- local lines = getLines(program)
- local before = {}
- local bcur = 1
- local after = {}
- local acur = 1
- local done = false
- for i,v in ipairs(lines) do
- if string.lower(v) == "--input parts" then
- done = true
- else
- if done then
- after[acur] = v
- acur = acur + 1
- else
- before[bcur] = v
- bcur = bcur + 1
- end
- end
- end
- if lines ~= nil and lines ~= {} and done then
- local tf = fs.open(program, "a")
- tf.close()
- if fs.exists(program) then
- fs.delete(program)
- end
- local file = fs.open(program, "a")
- for i,v in ipairs(before) do
- local fname = isInTable(v, fnames)
- if fname ~= nil then
- local line = v
- local oline = v
- line = string.gsub(line, fname.."_", "")
- line = line.." --@"..string.gsub(oline, "@", "<@#@>").."@FUNCTION OF"
- file.writeLine(line)
- else
- file.writeLine(v)
- end
- end
- for i,v in ipairs(files) do
- local tlines = getLines(root.."/"..program.."/"..v)
- for j,k in ipairs(tlines) do
- local line = k
- local fname = isInTable(line, fnames)
- if fname ~= nil then
- local oline = v
- line = string.gsub(line, fname.."_", "")
- line = line.." --@"..string.gsub(oline, "@", "<@#@>").."@FUNCTION OF"
- end
- file.writeLine(line.." --PART@@")
- end
- end
- for i,v in ipairs(after) do
- local fname = isInTable(v, fnames)
- if fname ~= nil then
- local line = v
- local oline = v
- line = string.gsub(line, fname.."_", "")
- line = line.." --@"..string.gsub(oline, "@", "<@#@>").."@FUNCTION OF"
- file.writeLine(line)
- else
- file.writeLine(v)
- end
- end
- file.close()
- return true
- else
- return false
- end
- else
- return false
- end
- end
- local function setUp(prog)
- if prog ~= nil then
- if fs.exists(root) then else
- fs.makeDir(root)
- end
- if fs.exists(root.."/"..prog.."/") then else
- fs.makeDir(root.."/"..prog.."/")
- end
- if fs.exists(prog) then else
- local fp = fs.open(prog, "a")
- fp.close()
- end
- return true
- else
- return false
- end
- end
- local function writeFunction(program, name, data)
- if fs.exists(root.."/"..program.."/") then else
- fs.makeDir(root.."/"..program.."/")
- end
- local dir = program
- if name ~= nil and name ~= "" then
- dir = root.."/"..program.."/"..name
- end
- if fs.exists(dir) then
- fs.delete(dir)
- end
- local file = fs.open(dir, "a")
- for i,v in ipairs(data) do
- local line = v
- if line ~= nil and line ~= "" then
- file.writeLine(line)
- end
- end
- file.close()
- end
- local function getFunction(program, name)
- if name ~= nil and name ~= "" then
- if fs.exists(root.."/"..program.."/"..name) then
- return getLines(root.."/"..program.."/"..name)
- else
- return nil
- end
- else
- if fs.exists(program) then
- return getLines(program)
- else
- return nil
- end
- end
- end
- local function insertLine(table, index, line)
- local sz = getTableSize(table)
- local x = index
- for x=sz,index,-1 do
- table[x+1] = table[x]
- end
- table[index] = line
- end
- local function getInput()
- local input = io.read()
- while input == nil do
- input = io.read()
- sleep(0.2)
- end
- return input
- end
- local function clear(x, y)
- term.clear()
- term.setCursorPos(x,y)
- end
- local type = "write"
- print("##Enter command (write/compile/decompile/delete/convert/cancel):")
- type = getInput()
- if type == "compile" then
- print("##Enter program to compile:")
- local program = getInput()
- if compile(program) then
- clear(1,1)
- print("Compiled program: "..program)
- else
- clear(1,1)
- print("Couldn't compile program: "..program.."! does it exist?")
- end
- elseif type == "decompile" then
- print("##Enter program to decompile:")
- local program = getInput()
- if decompile(program) then
- clear(1,1)
- print("Decompiled program: "..program)
- else
- clear(1,1)
- print("Couldn't decompile program: "..program.."! does it exist?")
- end
- elseif type == "convert" then
- print("##Enter program to convert:")
- local program = getInput()
- print("##Enter program to convert it to:")
- local toprogram = getInput()
- if convert(program, toprogram) then
- clear(1,1)
- print("Converted program: "..program.." to the program: "..toprogram)
- else
- clear(1,1)
- print("Couldn't convert program: are you sure both "..program.." and "..toprogram.." are real programs?")
- end
- elseif type == "delete" then
- print("##Enter program to delete:")
- local program = getInput()
- if delete(program) then
- clear(1,1)
- print("Deleted program: "..program)
- else
- clear(1,1)
- print("Couldn't delete program: "..program.."! does it exist?")
- end
- elseif type == "cancel" then
- clear(1,1)
- elseif type == "write" then
- local partdat = {" "}
- local cur = 2
- local curs = 2
- print("##Enter program name:")
- local program = getInput()
- print("##Program = "..program..", Enter part name (none to edit program):")
- local part = getInput()
- local extra = "## editing part: "..part.." for program: "..program
- if part == nil or part == "" then
- extra = "## editing program: "..program
- end
- clear(1, 1)
- print(extra)
- print("## COMMANDS: ##save ; ##reset ; ##exit ; ##undo ")
- if setUp(program) then
- local temppd = getFunction(program, part)
- if temppd ~= nil then
- partdat = temppd
- if partdat[1] ~= " " then
- insertLine(partdat, 1, " ")
- end
- lastpar = partdat
- cur = getTableSize(temppd)+1
- end
- local on = true
- while on do
- clear(1,1)
- print(extra)
- print("## COMMANDS: ##save ; ##reset ; ##exit ; ##undo ; ##up ; ##down ; ##input")
- for i,v in ipairs(partdat) do
- local line = v
- local ex = ">"
- if i == curs then
- ex = ">>"
- end
- print(ex..line)
- end
- if cur == curs then
- print(">>")
- else
- print(">")
- end
- local inp = getInput()
- if string.lower(inp) == "##save" then
- writeFunction(program, part, partdat)
- lastpar = partdat
- elseif string.lower(inp) == "##reset" then
- if lastpar ~= nil then
- partdat = lastpar
- else
- partdat = {}
- end
- elseif string.lower(inp) == "##exit" then
- on = false
- elseif string.lower(inp) == "##undo" then
- if cur > 1 then
- cur = cur - 1
- partdat[curs] = nil
- end
- elseif string.lower(inp) == "##up" then
- curs = curs - 1
- if curs < 2 then
- curs = cur
- end
- elseif string.lower(inp) == "##down" then
- curs = curs + 1
- if curs > cur then
- curs = 2
- end
- elseif string.lower(inp) == "##input" then
- insertLine(partdat, curs, "")
- else
- partdat[curs] = inp
- if curs == cur then
- cur = cur + 1
- end
- curs = curs + 1
- end
- sleep(0.25)
- end
- clear(1,1)
- else
- clear(1, 1)
- print("Program: "..program.." does not exist, could not create part: "..part.."!")
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement