Advertisement
ZNZNCOOP

translator v0.22

Jun 30th, 2015
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.07 KB | None | 0 0
  1. fs = require("filesystem")
  2. file = "test"
  3. args = {...}
  4.  
  5. function loadFile(filename)
  6.    code = ""
  7.    for line in io.lines(filename) do
  8.       code = code..line.."\n"
  9.    end
  10. end
  11.  
  12. function translate(filename)
  13.    i = 0
  14.    last = 1
  15.    while i<=#code do
  16.       i = i+1
  17.       p1,p2 = string.find(code,"}.-else.-if.-{",i)
  18.       if p1 ~= nil and p2 ~= nil then
  19.          i = p2
  20.          t = string.sub(code,p1,p2)
  21.          stat = string.match(t,"}.-else.-if(.-){")
  22.          t = "elseif "..stat.." then"
  23.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  24.       end
  25.    end
  26.    code = string.gsub(code,"}.-else.-{","else")
  27.    i = 0
  28.    while i<=#code do
  29.       i = i+1
  30.       p1,p2 = string.find(code,"if.-{",i)
  31.       if p1 ~= nil and p2 ~= nil then
  32.          pp1,pp2 = string.find(code,"if.-else.-{",i)
  33.          if pp2 ~= p2 then
  34.             t = string.sub(code,p1,p2)
  35.             t = string.gsub(t,"{"," then ")
  36.             code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  37.          end
  38.       end
  39.       p1,p2 = string.find(code,"while.-{",i)
  40.       if p1 ~= nil and p2 ~= nil then
  41.          t = string.sub(code,p1,p2)
  42.          t = string.gsub(t,"{"," do ")
  43.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  44.       end
  45.       p1,p2 = string.find(code,"for.-{",i)
  46.       if p1 ~= nil and p2 ~= nil then
  47.          t = string.sub(code,p1,p2)
  48.          t = string.gsub(t,"{"," do ")
  49.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  50.       end
  51.       p1,p2 = string.find(code,"[a-zA-Z1-9]+%+%+",i)
  52.       if p1 ~= nil and p2 ~= nil then
  53.          t = string.sub(code,p1,p2)
  54.          var = string.match(t,"(.+)%+%+")
  55.          t = "(function()"..var.."="..var.."+1".." return "..var.." end)()"
  56.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  57.       end
  58.       p1,p2 = string.find(code,"[a-zA-Z1-9]+%-%-",i)
  59.       if p1 ~= nil and p2 ~= nil then
  60.          t = string.sub(code,p1,p2)
  61.          var = string.match(t,"(.+)%-%-")
  62.          t = "(function()"..var.."="..var.."-1".." return "..var.." end)()"
  63.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  64.       end
  65.        p1,p2 = string.find(code,"%-%-[a-zA-Z1-9]+",i)
  66.       if p1 ~= nil and p2 ~= nil then
  67.          t = string.sub(code,p1,p2)
  68.          var = string.match(t,"%-%-(.+)")
  69.          t = "(function()"..var.."="..var.."-1".." return "..var.." end)()"
  70.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  71.       end
  72.       p1,p2 = string.find(code,"%+%+[a-zA-Z1-9]+",i)
  73.       if p1 ~= nil and p2 ~= nil then
  74.          t = string.sub(code,p1,p2)
  75.          var = string.match(t,"%+%+(.+)")
  76.          t = "(function()"..var.."="..var.."+1".." return "..var.." end)()"
  77.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  78.       end
  79.       p1,p2 = string.find(code,"%S+%+=%S+",i)
  80.       if p1 ~= nil and p2 ~= nil then
  81.          t = string.sub(code,p1,p2)
  82.          var,st = string.match(t,"(.+)%+=(.+)")
  83.          t = var.."="..var.."+"..st
  84.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  85.       end
  86.       p1,p2 = string.find(code,"%S+%-=%S+",i)
  87.       if p1 ~= nil and p2 ~= nil then
  88.          t = string.sub(code,p1,p2)
  89.          var,st = string.match(t,"(.+)%-=(.+)")
  90.          t = var.."="..var.."-"..st
  91.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  92.       end
  93.       p1,p2 = string.find(code,"%S+%*=%S+",i)
  94.       if p1 ~= nil and p2 ~= nil then
  95.          t = string.sub(code,p1,p2)
  96.          var,st = string.match(t,"(.+)%*=(.+)")
  97.          t = var.."="..var.."*"..st
  98.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  99.       end
  100.       p1,p2 = string.find(code,"%S+%/=%S+",i)
  101.       if p1 ~= nil and p2 ~= nil then
  102.          t = string.sub(code,p1,p2)
  103.          var,st = string.match(t,"(.+)%/=(.+)")
  104.          t = var.."="..var.."/"..st
  105.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  106.       end
  107.       p1,p2 = string.find(code,"function .-{",i)
  108.       if p1 ~= nil and p2 ~= nil then
  109.          t = string.sub(code,p1,p2)
  110.          func = string.match(t,"function (.+){")
  111.          t = "function "..func
  112.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  113.       end
  114.       p1,p2 = string.find(code,"}",i)
  115.       if p1 ~= nil and p2 ~= nil then
  116.          pp1,pp2 = string.find(code,"[a-zA-Z1-9]-=.-{.-}",lastT)
  117.          if pp2 ~= nil then
  118.             if i >= pp2 then lastT = i end
  119.          end
  120.          if pp2 ~= p2 then
  121.             code = string.sub(code,1,p1-1).." end "..string.sub(code,p2+1,#code)
  122.          end
  123.       end
  124.       p1,p2 = string.find(code,"export.-[a-zA-Z1-9]+",i)
  125.       if p1 ~= nil and p2 ~= nil then
  126.          t = string.sub(code,p1,p2)
  127.          lib = string.match(t,"export.-([a-zA-Z1-9]+)")
  128.          code = string.sub(code,1,p1-1).."local "..lib.." = require('"..lib.."')"..string.sub(code,p2+1,#code)
  129.       end
  130.    end
  131.    f = io.open(filename,"w")
  132.    f:write(code)
  133.    f:close()
  134. end
  135.  
  136. if #args == 2 and fs.exists(args[1]) then
  137.    loadFile(args[1])
  138.    translate(args[2])
  139. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement