Advertisement
Tatantyler

Operator Replacer

Nov 23rd, 2012
423
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.82 KB | None | 0 0
  1. -- a simple fix for the fact that lua doesn't have the operator= operators
  2. -- works for +=, -=, /=, *=, and %=
  3.  
  4. local args = {...}
  5. local file = io.open(args[1], "r")
  6. local file2 = fs.open(args[1]..".withOperators", "w")
  7.  
  8. local function concentate(...)
  9.     local fnArgs = {...}
  10.     local ret = ""
  11.     for i, v in ipairs(fnArgs) do
  12.         ret = ret..v
  13.     end
  14.     return ret
  15. end
  16.  
  17. for line in file:lines() do
  18.     local editLine = line
  19.     local words = {}
  20.     for word in string.gmatch(line, "([^ \t]+)") do
  21.         table.insert(words, word)
  22.     end
  23.     for i,word in ipairs(words) do
  24.         if word == "+=" then
  25.             editLine = concentate(words[1], " = "..words[1].." + ", unpack(words, 3))
  26.             break
  27.         elseif word == "-=" then
  28.             editLine = concentate(words[1], " = "..words[1].." - ", unpack(words, 3))
  29.             break
  30.         elseif word == "*=" then
  31.             editLine = concentate(words[1], " = "..words[1].." * ", unpack(words, 3))
  32.             break
  33.         elseif word == "/=" then
  34.             editLine = concentate(words[1], " = "..words[1].." / ", unpack(words, 3))
  35.             break
  36.         elseif word == "%=" then
  37.             editLine = concentate(words[1], " = "..words[1].." % ", unpack(words, 3))
  38.             break
  39.         elseif word == ">>=" then
  40.             editLine = concentate(words[1], " = bit.brshift("..words[1]..", ", unpack(words, 3),")")
  41.             break
  42.         elseif word == "<<=" then
  43.             editLine = concentate(words[1], " = bit.blshift("..words[1]..", ", unpack(words, 3),")")
  44.             break
  45.         elseif word == "&=" then
  46.             editLine = concentate(words[1], " = bit.band("..words[1]..", ", unpack(words, 3),")")
  47.             break
  48.         elseif word == "^=" then
  49.             editLine = concentate(words[1], " = bit.bxor("..words[1]..", ", unpack(words, 3),")")
  50.             break
  51.         elseif word == "|=" then
  52.             editLine = concentate(words[1], " = bit.bor("..words[1]..", ", unpack(words, 3),")")
  53.             break
  54.         end
  55.     end
  56.     file2.writeLine(editLine)
  57. end
  58. file2.close()
  59. file:close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement