Simple parsing function replacing required() and dofile() that allows you to use +=, -=, *=, /=, ++ in your lua script
Posted: Sat Mar 18, 2017 3:29 am
Firstly, appologies if such scripts have already been published - cba to search the forum from top to bottom.
So I made a simple code'ie that might help some of you ++ fans cope with the fact lua doesn't really ++
The script basically loads the code from the .lua files and does all the string replacing required to turn "var+=val" into "var=var+val" so that you can use ++ operators without a fuss.
First parameter is the lua file filename (if the string doesn't have .lua at the end, the extension will be added automagically) and the second paramater (if existing) will determine whether (value other than 0) the script will dump the parsed result into a file (with a "dome." prefix into the savedirectory) - or (when value==0) virtually run the lua code dynamically with assert(loadstring())() - The latter is obviously more handy with the production version of your code - but it might be easier to debug your code if you dump the parsed data into files.
handled operators are += -= /= *= ++
the operator -- isn't handled since thats the commenting sequence - however doing a -+ will give you a var-=1 result
I'm open to suggestion if anyone has an idea for optimizing it or making it more useful (thinking of handling ..= soon)
So without further adieu - here is the code - I moved everything i had in my game project from main.lua into main_base.lua and this is everything that is in the main.lua file:
So I made a simple code'ie that might help some of you ++ fans cope with the fact lua doesn't really ++
The script basically loads the code from the .lua files and does all the string replacing required to turn "var+=val" into "var=var+val" so that you can use ++ operators without a fuss.
First parameter is the lua file filename (if the string doesn't have .lua at the end, the extension will be added automagically) and the second paramater (if existing) will determine whether (value other than 0) the script will dump the parsed result into a file (with a "dome." prefix into the savedirectory) - or (when value==0) virtually run the lua code dynamically with assert(loadstring())() - The latter is obviously more handy with the production version of your code - but it might be easier to debug your code if you dump the parsed data into files.
handled operators are += -= /= *= ++
the operator -- isn't handled since thats the commenting sequence - however doing a -+ will give you a var-=1 result
I'm open to suggestion if anyone has an idea for optimizing it or making it more useful (thinking of handling ..= soon)
So without further adieu - here is the code - I moved everything i had in my game project from main.lua into main_base.lua and this is everything that is in the main.lua file:
Code: Select all
function dome(fn,toStringV)
if toStringV and domeVirtual==nil then
if toStringV==0 then domeVirtual=true
else domeVirtual=false
end
end
function escapePattern(cc)
local sb=string.byte
if (sb(cc)>=sb("a") and sb(cc)<=sb("z"))
or (sb(cc)>=sb("A") and sb(cc)<=sb("Z"))
or (sb(cc)>=sb("0") and sb(cc)<=sb("9"))
then return cc
else return "%"..cc
end
end
function getLastVar(ss)
local sAllowed="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890()[]{}\"'.:_"
local levUp="])}"
local levDown="[({"
local ssCh="\"'"
local curCh=""
local allPos=#ss+1
local sLevel=0
repeat allPos=allPos-1
local tch = escapePattern(string.sub(ss,allPos,allPos))
if curCh=="" then
if string.match(ssCh,tch) then curCh=string.sub(ss,allPos,allPos) end
if string.match(levUp,tch) then sLevel=sLevel+1 end
if string.match(levDown,tch) then sLevel=sLevel-1 end
elseif string.sub(ss,allPos,allPos)==curCh then curCh="" end
until not (string.match(sAllowed,tch) or sLevel>0 or curCh~="")
return string.sub(ss,allPos+1,#ss)
end
local sLegal=" \n\t"..[[
]]..string.char(10)..string.char(13)
local fext=string.sub(fn,#fn-3,#fn)
if not (string.lower(fext) == ".lua") then fn=fn..".lua" end
local str=love.filesystem.read(fn)
local fpath= love.filesystem.getSaveDirectory( )
local curPos=1
while string.match(str,"[+-/*][+=]",curPos) do
local fop=string.match(str,"([+-/*][+=])",curPos)
local sFi=string.sub(fop,1,1)
local sSe=string.sub(fop,2,2)
local fopPos=string.find(str,"([+-/*][+=])", curPos)
local legalPos=fopPos-1
print(string.byte(string.sub(str,legalPos-0,legalPos-0)))
while string.match(sLegal,string.sub(str,legalPos,legalPos)) do legalPos=legalPos-1 end
local sVar = string.sub(str,1,legalPos)
sVar = getLastVar(sVar)
if sSe=="=" then
str = string.sub(str,1, legalPos - #sVar ) ..sVar.."="..sVar..sFi .. string.sub(str,fopPos+2)
elseif sSe=="+" then
str = string.sub(str,1, legalPos - #sVar ) ..sVar.."="..sVar..sFi.."1" .. string.sub(str,fopPos+2)
end
curPos=fopPos+3+#sVar+(fopPos-legalPos+1)
end
str=string.gsub(str,"require","dome")
str=string.gsub(str,"dofile","dome")
if not domeVirtual then
love.filesystem.write("dome."..string.gsub(string.gsub(fn,"%/","%."),"%\\","%."),str)
dofile(fpath.."/dome."..string.gsub(string.gsub(fn,"%/","%."),"%\\","%."))
else assert(loadstring(str))()
end
end
dome("main_base",0)