PtokaX forum

Development Section => LUA & PtokaX-Scripting-Interface => Topic started by: plop on 06 October, 2004, 01:07:14

Title: 425 and 425_converter
Post by: plop on 06 October, 2004, 01:07:14
425: load this file @ the start of a lua 4 script and you can run it in lua 5.
works for many simple scripts, but fails on several things on the file handling.
--------------------------------------
-- 425 by plop
--------------------------------------
-- convert lua 4 code into lua 5.
-- not the best way but it instandly converts simple scripts.
-- add "require("425/425.lua")" above all the other code
-- in the lua 4 script.
--------------------------------------

-- string stuff
strfind = string.find
strsub  = string.sub
gsub = string.gsub
strchar = string.char
strbyte = string.byte
strlen = string.len
strlower = string.lower
strrep = string.rep
strupper = string.upper
format = string.format

-- table stuff
tinsert = table.insert
tremove = table.remove
concat = table.concat
foreach = table.foreach
foreachi = table.foreachi
getn = table.getn
sort = table.sort
setn = table.setn

-- math stuff
abs = math.abs
acos = math.acos
asin = math.asin
atan = math.atan
atan2 = math.atan2
ceil = math.ceil
cos = math.cos
deg = math.deg
exp = math.exp
floor = math.floor
log = math.log
log10 = math.log10
max = math.max
min = math.min
mod = math.mod
pow = math.pow
rad = math.rad
sin = math.sin
sqrt = math.sqrt
tan = math.tan
frexp = math.frexp
ldexp = math.ldexp
random = math.random
randomseed = math.randomseed
pi = math.pi

-- os stuff
date = os.date
execute = os.execute
clock = os.clock
remove = os.remove

-- file stuff
readfrom = function(arg)
   if arg then
      return io.open
   else
      return io.close
   end
end
openfile = readfrom
read = function()
   return io.lines
end
write = io.write
--seek = file:seek
writeto = function(arg)
   if arg then
      return io.output
   else
      return io.close
   end
end
flush = io.flush
call = function(func, arg)
   if type(func) == "function" then
      return func(unpack(arg)) -- primitive call
   else
      local h = metatable(func).__call
      if h then
         return h(func, unpack(arg))
      else
         error("...")
      end
   end
end
appendto = function(file)
   return io.open(file, "a+")
end

the next script rewrites all lua 4 syntax and safes it into a new file in lua 5 syntax.
the failing input/output from 425 is droped in this.
so you have 2 rewrite that manualy @ the moment.
file = "zzword_replacer_1.1.lua"

tTable = {
   -- string stuff
   ["strfind"] = "string.find",
   ["strsub"]  = "string.sub",
   ["gsub"] = "string.gsub",
   ["strchar"] = "string.char",
   ["strbyte"] = "string.byte",
   ["strlen"] = "string.len",
   ["strlower"] = "string.lower",
   ["strrep"] = "string.rep",
   ["strupper"] = "string.upper",
   ["format"] = "string.format",
   
   -- table stuff
   ["tinsert"] = "table.insert",
   ["tremove"] = "table.remove",
   ["concat"] = "table.concat",
   ["foreach"] = "table.foreach",
   ["foreachi"] = "table.foreachi",
   ["getn"] = "table.getn",
   ["sort"] = "table.sort",
   ["setn"] = "table.setn",
   
   -- math stuff
   ["abs"] = "math.abs",
   ["acos"] = "math.acos",
   ["asin"] = "math.asin",
   ["atan"] = "math.atan",
   ["atan2"] = "math.atan2",
   ["ceil"] = "math.ceil" ,
   ["cos"] = "math.cos" ,
   ["deg"] = "math.deg" ,
   ["exp"] = "math.exp" ,
   ["floor"] = "math.floor",
   ["log"] = "math.log" ,
   ["log10"] = "math.log10",
   ["max"] = "math.max" ,
   ["min"] = "math.min" ,
   ["mod"] = "math.mod",
   ["pow"] = "math.pow" ,
   ["rad"] = "math.rad" ,
   ["sin"] = "math.sin" ,
   ["sqrt"] = "math.sqrt",
   ["tan"] = "math.tan",
   ["frexp"] = "math.frexp",
   ["ldexp"] = "math.ldexp",
   ["random"] = "math.random",
   ["randomseed"] = "math.randomseed",
   ["pi"] = "math.pi",
   
   -- os stuff
   ["date"] = "os.date",
   ["execute"] = "os.execute",
   ["clock"] = "os.clock",
   ["remove"] = "os.remove",
   
   -- other
   ["flush"] = "io.flush"
}
tTmp = {}

print("starting the convert job")
iT = clock()

--io.open(file)
readfrom( file)
while 1 do
   local line = read()
   if line == nil then
      break
   end
   for a,b in tTable do
      line = gsub(line, (a.."%("), b.."(")
   end
   tinsert(tTmp, line)
end
readfrom()
--s,e,filename =strfind(file, "^(.+)%.")

--print(filename)

writeto(file.."_5.0.lua")
for i=1,getn(tTmp) do
   write(tTmp[i].."\n")
end
writeto()

print("done. "..(clock() - iT).." secs wasted")
now i'm just lost why i writen this in lua 4???
would have been handyer in lua 5. lol

plop
Title:
Post by: Herodes on 06 October, 2004, 01:15:52
"now i'm just lost why i writen this in lua 4???
would have been handyer in lua 5. lol "

LOL
Title:
Post by: NotRabidWombat on 07 October, 2004, 03:30:02
The reverse would probably be more usefull so Lua devs can start making Lua 5 scripts for the current Ptokax.
-- Created 7/14/04
-- This file moves global functions into the table system
-- of Lua 5 as best as possible.
-- I hope that this will improve table lookups and over all speed.

-- String

string = {};

string.byte , strbyte = strbyte, nil;
string.char , strchar = strchar, nil;
string.find , strfind = strfind, nil;
string.len , strlen = strlen, nil;
string.lower , strlower = strlower, nil;
string.rep , strrep = strrep, nil;
string.sub , strsub = strsub, nil;
string.upper , strupper = strupper, nil;
string.format , format = format, nil;
-- string.gfind, gfind = gfind, nil;
string.gsub , gsub = gsub, nil;

-- Math

math = {};

math.abs, abs = abs, nil;
math.acos, acos = acos, nil;
math.asin, asin = asin, nil;
math.atan, atan = atan, nil;
math.atan2, atan2 = atan2, nil;
math.ceil, ceil = ceil, nil;
math.cos, cos = cos, nil;
math.deg, deg = deg, nil;
math.exp, exp = exp, nil;
math.floor, floor = floor, nil;
math.log, log = log, nil;
math.log10, log10 = log10, nil;
math.max, max = max, nil;
math.min, min = min, nil;
math.mod, mod = mod, nil;
-- math.pow, pow = pow, nil;
math.rad, rad = rad, nil;
math.sin, sin = sin, nil;
math.sqrt, sqrt = sqrt, nil;
math.tan, tan = tan, nil;
math.frexp, frexp = frexp, nil;
math.ldexp, ldexp = ldexp, nil;
math.random, random = random, nil;
math.randomseed, randomseed = randomseed, nil;
-- PI is lowercase in lua5
math.pi, PI = PI, nil;

-- IO

io = {};

-- TODO: This will be tricky because of the new file userData.

io.openfile, openfile = openfile, nil;
io.close, closefile = closefile, nil;
io.input, readfrom = readfrom, nil;
io.output, writeto = writeto, nil;
--io.appendto, appendto = appendto, nil;
appendto = nil; -- this function was lame anyway
io.flush, flush = flush, nil;
io.seek, seek = seek, nil;
io.read, read = read, nil;
io.write, write = write, nil;

-- Table

table = {};

-- table.concat
table.foreach, foreach = foreach, nil;
table.foreachi, foreachi = foreachi, nil;
table.getn, getn = getn, nil;
table.sort, sort = sort, nil;
table.insert, tinsert = tinsert, nil;
table.remove, tremove = tremove, nil;
-- table.setn

-- OS

os = {};

os.clock, clock = clock, nil;
os.date, date = date, nil;
--os.difftime
os.execute, execute = execute, nil;
os.exit, exit = exit, nil;
os.getenv, getenv = getenv, nil;
os.remove, remove = remove, nil;
os.rename, rename = rename, nil;
os.setlocale, setlocale = setlocale, nil;
--os.time
os.tmpname, tmpname = tmpname, nil;

-- Debug

debug = {}

-- TODO: I do not believe PtokaX support debug
-- Perhaps all these should be contained in functions,
-- run at the top of the file, and niled at the bottom

debug.getinfo, getinfo = getinfo, nil;
debug.getlocal, getlocal = getlocal, nil;
debug.setlocal, setlocal = setlocal, nil;
--debug.setcallhook, setcallhook = setcallhook, nil;
setcallhook = nil;
--debug.setlinehook, setlinehook = setlinehook, nil;
setlinehook = nil;

-- Globals

_G, globals = globals(), nil;
-- obsolete, use _G
foreachvar, nextvar, rawsetglobal, rawgetglobal = nil, nil, nil, nil;
unpack = function(tTable, iIndex, iLimit)
assert(type(tTable) == "table");
if( not iIndex ) then iLimit, iIndex = getn(tTable), 1; end
if( iIndex > iLimit ) then return nil; end
return tTable[iIndex], unpack(tTable, iIndex + 1);
end
-- deprecated, Use f(unpack(tab)) instead of call(f, tab)
call = nil;
-- obsolete, replaced with metatables
settagmethod, copytagmethods, tag, newtag, gettagmethod, settag = nil, nil, nil, nil, nil, nil;
-- getargs is no longer used when running from the command line
if( getargs ~= nil ) then
arg = getargs();
getargs = nil;
end
-- rawgettable, rawsettable were ranamed to rawget, rawset
rawgettable, rawsettable = nil, nil;
-- deprecated
getglobal, setglobal = nil, nil;

-- Deprecated

deprecated = {};

deprecated.dostring, dostring = dostring, nil;

-- New

loadstring = function(sString)
deprecated.dostring(sString);
return function() end
end

-- dofile("test_lua4to5.lua");

I haven't looked at this in months. Just thought I would post.

-NotRabidWombat
Title:
Post by: Alexandros on 17 December, 2004, 01:53:29
dd "require("425/425.lua")" above all the other code <i get:
stack traceback:
   1:  main of string "require("425/425.lua")..." at line 1

thanks
Title:
Post by: plop on 21 December, 2004, 00:49:38
QuoteOriginally posted by Alexandros
dd "require("425/425.lua")" above all the other code <i get:
stack traceback:
   1:  main of string "require("425/425.lua")..." at line 1

thanks
save the script as 425.lua inside a folder called 425.
the script can't be in the scripts folder from ptokax itself as ptokax would launch it as a normal script.

plop
Title:
Post by: VidFamne on 22 December, 2004, 18:25:42
Does Lua4 understand the function require ?
Title:
Post by: plop on 22 December, 2004, 22:49:24
QuoteOriginally posted by VidFamne
Does Lua4 understand the function require ?
nope, use dofile here.

plop
Title: Help!
Post by: Jerry on 19 February, 2005, 14:35:22
Hi ppl,

I need help!

How can I convert script in LUA 4 to LUA 5 with convertor by NotRabidWombat?

  PLS HELP ME!!!!
Title:
Post by: plop on 19 February, 2005, 19:01:14
QuoteOriginally posted by Jerry
Hi ppl,

I need help!

How can I convert script in LUA 4 to LUA 5 with convertor by NotRabidWombat?

  PLS HELP ME!!!!
wombat's version works the other way around, it converts lua 5 scripts back 2 lua 4.

plop
Title:
Post by: Quattro on 19 February, 2005, 23:00:32
isn't there a way to write a little prog that can automatically replace the lua 4 commands with lua 5.02 commands?
so in fact really changing the script?

like you load a lua 4 script in the program, hit the button convert and it will convert everyting it can convert and will point out what he can't convert?

Lot's of programs have an option called wordreplace, can't that be used....

*edit*
because i want to really change the script so i can try to convert it to a dch++ script eventually :D
and it isn't a simple script i wanna change i think...
it's the shoutcast script....
Title: lua5
Post by: Jerry on 20 February, 2005, 09:28:11
QuoteOriginally posted by Quattro
isn't there a way to write a little prog that can automatically replace the lua 4 commands with lua 5.02 commands.

This is good idea!!! ;)

QuoteOriginally posted by plop
wombat's version works the other way around, it converts lua 5 scripts back 2 lua 4

Thank you, plop!!! :D
Title:
Post by: bastya_elvtars on 20 February, 2005, 12:57:02
QuoteOriginally posted by Jerry
QuoteOriginally posted by Quattro
isn't there a way to write a little prog that can automatically replace the lua 4 commands with lua 5.02 commands.

This is good idea!!! ;)

Every editor has a "replace all" function. This will satisfy you and it's much, much safer IMHO. BTW, if there is a % before a variable name (%var) it should be replaced to var.
Title:
Post by: plop on 21 February, 2005, 01:41:36
QuoteOriginally posted by Quattro
isn't there a way to write a little prog that can automatically replace the lua 4 commands with lua 5.02 commands?
so in fact really changing the script?

like you load a lua 4 script in the program, hit the button convert and it will convert everyting it can convert and will point out what he can't convert?

Lot's of programs have an option called wordreplace, can't that be used....

*edit*
because i want to really change the script so i can try to convert it to a dch++ script eventually :D
and it isn't a simple script i wanna change i think...
it's the shoutcast script....
the second script i posted does that, just you need a bit more buttons, namely your keyboard.
but there is a topic which explains how 2 use a certain editor with 2 user commands 2 do the same.

plop
Title:
Post by: plop on 23 February, 2005, 01:06:00
this next script converts the new dataarrival handling from lua 5.0 ptokax versions.
works just like 425.lua.
SupportsArrival = DataArrival
ChatArrival = DataArrival
KeyArrival = DataArrival
ValidateNickArrival = DataArrival
PasswordArrival = DataArrival
VersionArrival = DataArrival
GetNickListArrival = DataArrival
MyINFOArrival = DataArrival
GetINFOArrival = DataArrival
SearchArrival = DataArrival
ToArrival = DataArrival
ConnectToMeArrival = DataArrival
MultiConnectToMeArrival = DataArrival
RevConnectToMeArrival = DataArrival
SRArrival = DataArrival
KickArrival = DataArrival
OpForceMoveArrival = DataArrival
UserIPArrival = DataArrival
UnknownArrival = DataArrival

plop