Advertisement
Guest User

Combiner

a guest
Dec 14th, 2016
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.85 KB | None | 0 0
  1.  
  2. local args = {...}
  3.  
  4. if #args == 0 then
  5.     print ("Combiner v0.0.1 usage: combiner file -o out")
  6.     error ()
  7. end
  8.  
  9. shell.run ("lexer")
  10.  
  11. function split (string, d)
  12.     local out = {}
  13.     local i = 1
  14.     local tmp = ""
  15.     while i <= #string do
  16.         if string:sub (i, i) == d then
  17.             table.insert (out, tmp)
  18.             tmp = ""
  19.         elseif i == #string then
  20.             table.insert (out, tmp..string:sub(i, i))
  21.             tmp = ""
  22.         else
  23.             tmp = tmp..string:sub (i, i)
  24.         end
  25.         i = i + 1
  26.     end
  27.    
  28.     return out
  29. end
  30.  
  31. function strip (text)
  32.     return text:sub (2, #text-1)
  33. end
  34.  
  35. function parse (text)
  36.     local o = ""
  37.     local t = createLexer ()
  38.     lex (t, text)
  39.    
  40.     local j = 1
  41.     while j <= #t.tokens do
  42.         local tok = t.tokens[j][1]
  43.        
  44.         if tok == "#" then
  45.             if t.tokens[j+1][1] == "include" then
  46.                 j = j + 1
  47.                 while t.tokens[j][1] == " " do
  48.                     j = j + 1
  49.                 end
  50.                 j = j + 2
  51.                
  52.                 local name = strip (t.tokens[j][1])
  53.                
  54.                
  55.                 local f = fs.open (shell.dir ().."/"..name, "r")
  56.                 local text = f.readAll ()
  57.                 f.close ()
  58.                
  59.                 local add = parse (text)
  60.                 o = o.."\n"..add.."\n"
  61.                
  62.                 j = j + 1
  63.             else
  64.                 o = o..tok
  65.             end
  66.         else
  67.             o = o..tok
  68.         end
  69.        
  70.         j = j + 1
  71.     end
  72.    
  73.     return o
  74. end
  75.  
  76.  
  77. local i = 1
  78. local out = ""
  79. local file = ""
  80.  
  81. while i <= #args do
  82.     local val = args[i]
  83.     if fs.exists (shell.dir ().."/"..val) then
  84.         file = val
  85.     elseif val == "-o" then
  86.         out = args[i+1]
  87.         i = i + 1
  88.     end
  89.     i = i + 1
  90. end
  91.  
  92. if #out == 0 then
  93.     print ("error: I need an output location")
  94.     error ()
  95. end
  96.  
  97. if #file == 0 then
  98.     print ("error: I need an input file")
  99.     error ()
  100. end
  101.  
  102.  
  103. local f = fs.open (shell.dir ().."/"..file, "r")
  104. local text = f.readAll ()
  105. f.close ()
  106. local output = parse (text)
  107.  
  108. local source, err = loadstring (output)
  109.  
  110. if type (err) == "string" then
  111.     local a = split (err, ":")
  112.     local msg = file..":"
  113.     for i=3, #a do
  114.         msg = msg..a[i]
  115.     end
  116.     print (msg)
  117.     error ()
  118. end
  119.  
  120. local dump = string.dump (source)
  121. local pre = "local bytecode=\""
  122. local clock = os.clock () + 4.5
  123. f = fs.open (shell.dir ().."/"..out, "wb")
  124. for i=1, #pre do
  125.     f.write (string.byte (pre:sub (i, i)))
  126. end
  127. f.close ()
  128. local formatted = ""
  129.  
  130. for i = 1, #dump do
  131.     dec, _ = ("\\%3d"):format(dump:sub(i, i):byte()):gsub(' ', '0')
  132.     formatted = formatted..dec
  133. end
  134.  
  135. tmp = io.open (shell.dir ().."/"..out, "ab")
  136.  
  137. local i = 1
  138. while i <= #formatted do
  139.     local t = formatted:sub (i, i+3)
  140.    
  141.     for j=1, 4 do
  142.         tmp:write (string.byte (t:sub (j, j)))
  143.     end
  144.    
  145.     i = i + 4
  146. end
  147.  
  148. tmp:close ()
  149.  
  150. f = fs.open (shell.dir ().."/"..out, "ab")
  151. f.write (string.byte ("\""))
  152. f.write (string.byte ("\n"))
  153.  
  154. local runCode = "local args={...}\nlocal code = {}\nfor i=1, #bytecode do\n\ttable.insert (code, bytecode:sub (i, i))\nend\nlocal func = loadstring (table.concat (code))\nfunc (unpack (args))"
  155.  
  156. for i = 1, #runCode do
  157.     f.write (string.byte (runCode:sub (i, i)))
  158. end
  159. f.close ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement