Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local args = {...}
- if #args == 0 then
- print ("Combiner v0.0.1 usage: combiner file -o out")
- error ()
- end
- shell.run ("lexer")
- function split (string, d)
- local out = {}
- local i = 1
- local tmp = ""
- while i <= #string do
- if string:sub (i, i) == d then
- table.insert (out, tmp)
- tmp = ""
- elseif i == #string then
- table.insert (out, tmp..string:sub(i, i))
- tmp = ""
- else
- tmp = tmp..string:sub (i, i)
- end
- i = i + 1
- end
- return out
- end
- function strip (text)
- return text:sub (2, #text-1)
- end
- function parse (text)
- local o = ""
- local t = createLexer ()
- lex (t, text)
- local j = 1
- while j <= #t.tokens do
- local tok = t.tokens[j][1]
- if tok == "#" then
- if t.tokens[j+1][1] == "include" then
- j = j + 1
- while t.tokens[j][1] == " " do
- j = j + 1
- end
- j = j + 2
- local name = strip (t.tokens[j][1])
- local f = fs.open (shell.dir ().."/"..name, "r")
- local text = f.readAll ()
- f.close ()
- local add = parse (text)
- o = o.."\n"..add.."\n"
- j = j + 1
- else
- o = o..tok
- end
- else
- o = o..tok
- end
- j = j + 1
- end
- return o
- end
- local i = 1
- local out = ""
- local file = ""
- while i <= #args do
- local val = args[i]
- if fs.exists (shell.dir ().."/"..val) then
- file = val
- elseif val == "-o" then
- out = args[i+1]
- i = i + 1
- end
- i = i + 1
- end
- if #out == 0 then
- print ("error: I need an output location")
- error ()
- end
- if #file == 0 then
- print ("error: I need an input file")
- error ()
- end
- local f = fs.open (shell.dir ().."/"..file, "r")
- local text = f.readAll ()
- f.close ()
- local output = parse (text)
- local source, err = loadstring (output)
- if type (err) == "string" then
- local a = split (err, ":")
- local msg = file..":"
- for i=3, #a do
- msg = msg..a[i]
- end
- print (msg)
- error ()
- end
- local dump = string.dump (source)
- local pre = "local bytecode=\""
- local clock = os.clock () + 4.5
- f = fs.open (shell.dir ().."/"..out, "wb")
- for i=1, #pre do
- f.write (string.byte (pre:sub (i, i)))
- end
- f.close ()
- local formatted = ""
- for i = 1, #dump do
- dec, _ = ("\\%3d"):format(dump:sub(i, i):byte()):gsub(' ', '0')
- formatted = formatted..dec
- end
- tmp = io.open (shell.dir ().."/"..out, "ab")
- local i = 1
- while i <= #formatted do
- local t = formatted:sub (i, i+3)
- for j=1, 4 do
- tmp:write (string.byte (t:sub (j, j)))
- end
- i = i + 4
- end
- tmp:close ()
- f = fs.open (shell.dir ().."/"..out, "ab")
- f.write (string.byte ("\""))
- f.write (string.byte ("\n"))
- 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))"
- for i = 1, #runCode do
- f.write (string.byte (runCode:sub (i, i)))
- end
- f.close ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement