Advertisement
ZNZNCOOP

translator v0.24

Jun 30th, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.19 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,"&&","and")
  36.             t = string.gsub(t,"||","or")
  37.             t = string.gsub(t,"!=","~=")
  38.             t = string.gsub(t,"{"," then ")
  39.             code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  40.          end
  41.       end
  42.       p1,p2 = string.find(code,"while.-{",i)
  43.       if p1 ~= nil and p2 ~= nil then
  44.          t = string.sub(code,p1,p2)
  45.          t = string.gsub(t,"{"," do ")
  46.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  47.       end
  48.       p1,p2 = string.find(code,"for.-{",i)
  49.       if p1 ~= nil and p2 ~= nil then
  50.          t = string.sub(code,p1,p2)
  51.          t = string.gsub(t,"{"," do ")
  52.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  53.       end
  54.       p1,p2 = string.find(code,"[a-zA-Z1-9]+%+%+",i)
  55.       if p1 ~= nil and p2 ~= nil then
  56.          t = string.sub(code,p1,p2)
  57.          var = string.match(t,"(.+)%+%+")
  58.          t = "(function()"..var.."="..var.."+1".." return "..var.." end)()"
  59.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  60.       end
  61.       p1,p2 = string.find(code,"[a-zA-Z1-9]+%-%-",i)
  62.       if p1 ~= nil and p2 ~= nil then
  63.          t = string.sub(code,p1,p2)
  64.          var = string.match(t,"(.+)%-%-")
  65.          t = "(function()"..var.."="..var.."-1".." return "..var.." end)()"
  66.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  67.       end
  68.        p1,p2 = string.find(code,"%-%-[a-zA-Z1-9]+",i)
  69.       if p1 ~= nil and p2 ~= nil then
  70.          t = string.sub(code,p1,p2)
  71.          var = string.match(t,"%-%-(.+)")
  72.          t = "(function()"..var.."="..var.."-1".." return "..var.." end)()"
  73.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  74.       end
  75.       p1,p2 = string.find(code,"%+%+[a-zA-Z1-9]+",i)
  76.       if p1 ~= nil and p2 ~= nil then
  77.          t = string.sub(code,p1,p2)
  78.          var = string.match(t,"%+%+(.+)")
  79.          t = "(function()"..var.."="..var.."+1".." return "..var.." end)()"
  80.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  81.       end
  82.       p1,p2 = string.find(code,"%S+%+=%S+",i)
  83.       if p1 ~= nil and p2 ~= nil then
  84.          t = string.sub(code,p1,p2)
  85.          var,st = string.match(t,"(.+)%+=(.+)")
  86.          t = var.."="..var.."+"..st
  87.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  88.       end
  89.       p1,p2 = string.find(code,"%S+%-=%S+",i)
  90.       if p1 ~= nil and p2 ~= nil then
  91.          t = string.sub(code,p1,p2)
  92.          var,st = string.match(t,"(.+)%-=(.+)")
  93.          t = var.."="..var.."-"..st
  94.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  95.       end
  96.       p1,p2 = string.find(code,"%S+%*=%S+",i)
  97.       if p1 ~= nil and p2 ~= nil then
  98.          t = string.sub(code,p1,p2)
  99.          var,st = string.match(t,"(.+)%*=(.+)")
  100.          t = var.."="..var.."*"..st
  101.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  102.       end
  103.       p1,p2 = string.find(code,"%S+%/=%S+",i)
  104.       if p1 ~= nil and p2 ~= nil then
  105.          t = string.sub(code,p1,p2)
  106.          var,st = string.match(t,"(.+)%/=(.+)")
  107.          t = var.."="..var.."/"..st
  108.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  109.       end
  110.       p1,p2 = string.find(code,"function .-{",i)
  111.       if p1 ~= nil and p2 ~= nil then
  112.          t = string.sub(code,p1,p2)
  113.          func = string.match(t,"function (.+){")
  114.          t = "function "..func
  115.          code = string.sub(code,1,p1-1)..t..string.sub(code,p2+1,#code)
  116.       end
  117.       p1,p2 = string.find(code,"}",i)
  118.       if p1 ~= nil and p2 ~= nil then
  119.          pp1,pp2 = string.find(code,"[a-zA-Z1-9]-=.-{.-}",lastT)
  120.          if pp2 ~= nil then
  121.             if i >= pp2 then lastT = i end
  122.          end
  123.          if pp2 ~= p2 then
  124.             code = string.sub(code,1,p1-1).." end "..string.sub(code,p2+1,#code)
  125.          end
  126.       end
  127.       p1,p2 = string.find(code,"export.-[a-zA-Z1-9]+",i)
  128.       if p1 ~= nil and p2 ~= nil then
  129.          t = string.sub(code,p1,p2)
  130.          lib = string.match(t,"export.-([a-zA-Z1-9]+)")
  131.          code = string.sub(code,1,p1-1).."local "..lib.." = require('"..lib.."')"..string.sub(code,p2+1,#code)
  132.       end
  133.    end
  134.    f = io.open(filename,"w")
  135.    f:write(code)
  136.    f:close()
  137. end
  138.  
  139. if #args == 2 and fs.exists(args[1]) then
  140.    loadFile(args[1])
  141.    translate(args[2])
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement