Advertisement
billysback

subpart

Oct 5th, 2012
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.17 KB | None | 0 0
  1. local root = ".SUBP"
  2. local lastpar = nil
  3.  
  4. local function getTableSize(table)
  5.     local cur = 1
  6.     for i,v in ipairs(table) do
  7.         cur = i
  8.     end
  9.     return cur
  10. end
  11.  
  12. local function split(str, pat)
  13.     local t = {}  -- NOTE: use {n = 0} in Lua-5.0
  14.     if str ~= nil then
  15.        local fpat = "(.-)" .. pat
  16.        local last_end = 1
  17.        local s, e, cap = str:find(fpat, 1)
  18.        while s do
  19.           if s ~= 1 or cap ~= "" then
  20.          table.insert(t,cap)
  21.           end
  22.           last_end = e+1
  23.           s, e, cap = str:find(fpat, last_end)
  24.        end
  25.        if last_end <= #str then
  26.           cap = str:sub(last_end)
  27.           table.insert(t, cap)
  28.        end
  29.     else
  30.         print("FAILED TO SPLIT by:"..pat)
  31.     end
  32.     return t
  33. end
  34.  
  35. local function getLines(dir)
  36.     local data = {}
  37.     local cur = 1
  38.     local file = fs.open(dir, "r")
  39.     local line = file.readLine()
  40.     while line ~= nil do
  41.         data[cur] = line
  42.         cur = cur + 1
  43.         line = file.readLine()
  44.     end
  45.     file.close()
  46.     return data
  47. end
  48.  
  49. local function endsWith(String, End)
  50.     return End=='' or string.sub(String,-string.len(End))==End
  51. end
  52.  
  53. local function isInTable(line, findtable)
  54.     local found = nil
  55.     for i,v in ipairs(findtable) do
  56.         if string.find(line, v) == nil then else
  57.             found = v
  58.         end
  59.     end
  60.     return found
  61. end
  62.  
  63. local function convert(prog, toprog)
  64.     if fs.exists(prog) and fs.exists(root.."/"..toprog.."/")then
  65.         local fp = fs.open(prog, "a")
  66.         fp.close()
  67.         local lines = getLines(prog)
  68.         if fs.exists(root.."/"..toprog.."/"..prog, "a") then
  69.             fs.delete(root.."/"..toprog.."/"..prog)
  70.         end
  71.         local file = fs.open(root.."/"..toprog.."/"..prog, "a")
  72.         for i,v in ipairs(lines) do
  73.             print(line)
  74.             local line = v
  75.             file.writeLine(line)
  76.         end
  77.         file.close()
  78.         return true
  79.     else
  80.         return false
  81.     end
  82. end
  83.  
  84. local function delete(program)
  85.     if program ~= nil and program ~= "" then
  86.         if fs.exists(program) then
  87.             fs.delete(program)
  88.             local files = fs.list(root.."/"..program.."/")
  89.             for i,v in ipairs(files) do
  90.                 local line = v
  91.                 if fs.exists(root.."/"..program.."/"..line) then
  92.                     fs.delete(root.."/"..program.."/"..line)
  93.                 end
  94.             end
  95.             if fs.exists(root.."/"..program.."/") then
  96.                 fs.delete(root.."/"..program.."/")
  97.             end
  98.             return true
  99.         else
  100.             return false
  101.         end
  102.     else
  103.         return false
  104.     end
  105. end
  106.  
  107. local function decompile(program)
  108.     if fs.exists(root.."/"..program) then
  109.         local lines = getLines(program)
  110.         if fs.exists(program) then
  111.             fs.delete(program)
  112.         end
  113.         local file = fs.open(program, "a")
  114.         local nline = {}
  115.         local ncur = 1
  116.         local hasdone = alse
  117.         for i,v in ipairs(lines) do
  118.             local line = v
  119.             if endsWith(line, "--PART@@") then
  120.                 if hasdone then else
  121.                     nline[ncur] = "--input parts"
  122.                     ncur = ncur + 1
  123.                     hasdone = true
  124.                 end
  125.             elseif endsWith(line, "@FUNCTION OF")then
  126.                 local splt = split(line, "@")
  127.                 oline = splt[getTableSize(splt)-1]
  128.                 oline = string.gsub(oline, "<@#@>", "@")
  129.                 nline[ncur] = oline
  130.                
  131.                 ncur = ncur + 1
  132.             else
  133.                 nline[ncur] = line
  134.                 ncur = ncur + 1
  135.             end
  136.         end
  137.        
  138.         for i,v in ipairs(nline) do
  139.             local line = v
  140.             file.writeLine(line)
  141.         end
  142.         file.close()
  143.         return true
  144.     else
  145.         return false
  146.     end
  147. end
  148.  
  149. local function compile(program)
  150.     if fs.exists(root.."/"..program) then
  151.         local files = fs.list(root.."/"..program)
  152.         local fnames = {}
  153.         local fcur = 1
  154.         for i,v in ipairs(files) do
  155.             local line = string.gsub(v, ".fpt")
  156.             fnames[fcur] = line
  157.             fcur = fcur + 1
  158.         end
  159.         local lines = getLines(program)
  160.        
  161.         local before = {}
  162.         local bcur = 1
  163.         local after = {}
  164.         local acur = 1
  165.         local done = false
  166.         for i,v in ipairs(lines) do
  167.             if string.lower(v) == "--input parts" then
  168.                 done = true
  169.             else
  170.                 if done then
  171.                     after[acur] = v
  172.                     acur = acur + 1
  173.                 else
  174.                     before[bcur] = v
  175.                     bcur = bcur + 1
  176.                 end
  177.             end
  178.         end
  179.         if lines ~= nil and lines ~= {} and done then
  180.             local tf = fs.open(program, "a")
  181.             tf.close()
  182.             if fs.exists(program) then
  183.                 fs.delete(program)
  184.             end
  185.             local file = fs.open(program, "a")
  186.             for i,v in ipairs(before) do
  187.                 local fname = isInTable(v, fnames)
  188.                 if fname ~= nil then
  189.                     local line = v
  190.                     local oline = v
  191.                     line = string.gsub(line, fname.."_", "")
  192.                     line = line.." --@"..string.gsub(oline, "@", "<@#@>").."@FUNCTION OF"
  193.                     file.writeLine(line)
  194.                 else
  195.                     file.writeLine(v)
  196.                 end
  197.             end
  198.             for i,v in ipairs(files) do
  199.                 local tlines = getLines(root.."/"..program.."/"..v)
  200.                
  201.                 for j,k in ipairs(tlines) do
  202.                     local line = k
  203.                     local fname = isInTable(line, fnames)
  204.                     if fname ~= nil then
  205.                         local oline = v
  206.                         line = string.gsub(line, fname.."_", "")
  207.                         line = line.." --@"..string.gsub(oline, "@", "<@#@>").."@FUNCTION OF"
  208.                     end
  209.                     file.writeLine(line.." --PART@@")
  210.                 end
  211.             end
  212.             for i,v in ipairs(after) do
  213.                 local fname = isInTable(v, fnames)
  214.                 if fname ~= nil then
  215.                     local line = v
  216.                     local oline = v
  217.                     line = string.gsub(line, fname.."_", "")
  218.                     line = line.." --@"..string.gsub(oline, "@", "<@#@>").."@FUNCTION OF"
  219.                     file.writeLine(line)
  220.                 else
  221.                     file.writeLine(v)
  222.                 end
  223.             end
  224.             file.close()
  225.             return true
  226.         else
  227.             return false
  228.         end
  229.     else
  230.         return false
  231.    
  232.     end
  233. end
  234.  
  235. local function setUp(prog)
  236.     if prog ~= nil then
  237.         if fs.exists(root) then else
  238.             fs.makeDir(root)
  239.         end
  240.         if fs.exists(root.."/"..prog.."/") then else
  241.             fs.makeDir(root.."/"..prog.."/")
  242.         end
  243.         if fs.exists(prog) then else
  244.             local fp = fs.open(prog, "a")
  245.             fp.close()
  246.         end
  247.         return true
  248.     else
  249.         return false
  250.     end
  251. end
  252.  
  253. local function writeFunction(program, name, data)
  254.     if fs.exists(root.."/"..program.."/") then else
  255.         fs.makeDir(root.."/"..program.."/")
  256.     end
  257.     local dir = program
  258.     if name ~= nil and name ~= "" then
  259.         dir = root.."/"..program.."/"..name
  260.     end
  261.     if fs.exists(dir) then
  262.         fs.delete(dir)
  263.     end
  264.     local file = fs.open(dir, "a")
  265.     for i,v in ipairs(data) do
  266.         local line = v
  267.         if line ~= nil and line ~= "" then
  268.             file.writeLine(line)
  269.         end
  270.     end
  271.     file.close()
  272. end
  273.  
  274. local function getFunction(program, name)
  275.     if name ~= nil and name ~= "" then
  276.         if fs.exists(root.."/"..program.."/"..name) then
  277.             return getLines(root.."/"..program.."/"..name)
  278.         else
  279.             return nil
  280.         end
  281.     else
  282.         if fs.exists(program) then
  283.             return getLines(program)
  284.         else
  285.             return nil
  286.         end
  287.     end
  288. end
  289.  
  290.  
  291. local function insertLine(table, index, line)
  292.     local sz = getTableSize(table)
  293.     local x = index
  294.     for x=sz,index,-1 do
  295.         table[x+1] = table[x]
  296.     end
  297.     table[index] = line
  298. end
  299.  
  300. local function getInput()
  301.     local input = io.read()
  302.     while input == nil do
  303.         input = io.read()
  304.         sleep(0.2)
  305.     end
  306.     return input
  307. end
  308.  
  309. local function clear(x, y)
  310.     term.clear()
  311.     term.setCursorPos(x,y)
  312. end
  313.  
  314. local type = "write"
  315.  
  316. print("##Enter command (write/compile/decompile/delete/convert/cancel):")
  317. type = getInput()
  318.  
  319. if type == "compile" then
  320.     print("##Enter program to compile:")
  321.     local program = getInput()
  322.     if compile(program) then
  323.         clear(1,1)
  324.         print("Compiled program: "..program)
  325.     else
  326.         clear(1,1)
  327.         print("Couldn't compile program: "..program.."! does it exist?")
  328.     end
  329. elseif type == "decompile" then
  330.     print("##Enter program to decompile:")
  331.     local program = getInput()
  332.     if decompile(program) then
  333.         clear(1,1)
  334.         print("Decompiled program: "..program)
  335.     else
  336.         clear(1,1)
  337.         print("Couldn't decompile program: "..program.."! does it exist?")
  338.     end
  339. elseif type == "convert" then
  340.     print("##Enter program to convert:")
  341.     local program = getInput()
  342.     print("##Enter program to convert it to:")
  343.     local toprogram = getInput()
  344.     if convert(program, toprogram) then
  345.         clear(1,1)
  346.         print("Converted program: "..program.." to the program: "..toprogram)
  347.     else
  348.         clear(1,1)
  349.         print("Couldn't convert program: are you sure both "..program.." and "..toprogram.." are real programs?")
  350.     end
  351. elseif type == "delete" then
  352.     print("##Enter program to delete:")
  353.     local program = getInput()
  354.     if delete(program) then
  355.         clear(1,1)
  356.         print("Deleted program: "..program)
  357.     else
  358.         clear(1,1)
  359.         print("Couldn't delete program: "..program.."! does it exist?")
  360.     end
  361. elseif type == "cancel" then
  362.     clear(1,1)
  363. elseif type == "write" then
  364.     local partdat = {" "}
  365.     local cur = 2
  366.     local curs = 2
  367.     print("##Enter program name:")
  368.     local program = getInput()
  369.     print("##Program = "..program..", Enter part name (none to edit program):")
  370.     local part = getInput()
  371.     local extra = "## editing part: "..part.." for program: "..program
  372.     if part == nil or part == "" then
  373.         extra = "## editing program: "..program
  374.     end
  375.     clear(1, 1)
  376.     print(extra)
  377.     print("## COMMANDS: ##save ; ##reset ; ##exit ; ##undo ")
  378.     if setUp(program) then
  379.         local temppd = getFunction(program, part)
  380.         if temppd ~= nil then
  381.             partdat = temppd
  382.             if partdat[1] ~= " " then
  383.                 insertLine(partdat, 1, " ")
  384.             end
  385.             lastpar = partdat
  386.             cur = getTableSize(temppd)+1
  387.         end
  388.         local on = true
  389.         while on do
  390.             clear(1,1)
  391.             print(extra)
  392.             print("## COMMANDS:  ##save ; ##reset ; ##exit ; ##undo ; ##up ; ##down ; ##input")
  393.             for i,v in ipairs(partdat) do
  394.                 local line = v
  395.                 local ex = ">"
  396.                 if i == curs then
  397.                     ex = ">>"
  398.                 end
  399.                 print(ex..line)
  400.             end
  401.             if cur == curs then
  402.                 print(">>")
  403.             else
  404.                 print(">")
  405.             end
  406.             local inp = getInput()
  407.             if string.lower(inp) == "##save" then
  408.                 writeFunction(program, part, partdat)
  409.                 lastpar = partdat
  410.             elseif string.lower(inp) == "##reset" then
  411.                 if lastpar ~= nil then
  412.                     partdat = lastpar
  413.                 else
  414.                     partdat = {}
  415.                 end
  416.             elseif string.lower(inp) == "##exit" then
  417.                 on = false
  418.             elseif string.lower(inp) == "##undo" then
  419.                 if cur > 1 then
  420.                     cur = cur - 1
  421.                     partdat[curs] = nil
  422.                 end
  423.             elseif string.lower(inp) == "##up" then
  424.                 curs = curs - 1
  425.                 if curs < 2 then
  426.                     curs = cur
  427.                 end
  428.             elseif string.lower(inp) == "##down" then
  429.                 curs = curs + 1
  430.                 if curs > cur then
  431.                     curs = 2
  432.                 end
  433.             elseif string.lower(inp) == "##input" then
  434.                 insertLine(partdat, curs, "")
  435.             else
  436.                 partdat[curs] = inp
  437.                 if curs == cur then
  438.                     cur = cur + 1
  439.                 end
  440.                 curs = curs + 1
  441.             end
  442.             sleep(0.25)
  443.         end
  444.         clear(1,1)
  445.     else
  446.         clear(1, 1)
  447.         print("Program: "..program.." does not exist, could not create part: "..part.."!")
  448.     end
  449. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement