Posted 09 June 2014 - 04:04 AM
I am working on a program that changes my code from a java/lua mix into pure lua, and i was trying to change the java operator "while(conditional){" into "while conditional do" by using string:gsub. The code used is:
EDIT: It will not keep my code indented. Help? Sorry
local args={...};
local repls={
awhile={"while%(%s+.+%s+%){","while%s+.+%s+do"},
sif={"if%(","if "},
eif={"%)then"," then"},
alteif={"%) then"," then"},
pfor={"for%(","for "},
pwhile={"while%(","while "},
aforwhile={"%)do"," do"},
altaforwhile={"%) do"," do"},
rand={"&&"," and "},
ror={"||"," or "},
rnot={"!"," not "},
rend={"}","end"}
}
if not #args<=2 then
print("Only 1 arg needed");
return;
end
t={};
print("prefor");
file=fs.open(args[1],"r");
local sLine=file.readLine();
while sLine do
for f,y in pairs(repls) do
sLine=sLine:gsub(y[1],y[2]);
end
table.insert(t,sLine);
sLine=file.readLine();
end
file.close();
file=fs.open(args[1]..".c","w");
for f,y in pairs(t) do
file.write(y.."\n");
end
file.close();
I used this input file:
while(true){
print("Hi");
return;
}
The expected output was:
while true do
print("Hi");
return;
end
The actual output was:
while true){
print("Hi");
return;
end
Can anyone tell me a way to close the %s+.+%s? Thanks in advanceEDIT: It will not keep my code indented. Help? Sorry
Edited on 09 June 2014 - 02:04 AM