Posted 05 May 2015 - 10:39 PM
I am trying to make a golfing language in lua (Stupid, I know, but fun) and this is the code for my compiler:
tbl = {}
h = fs.open("testfile", "r")
r = h.readLine()
while r do
if string.find(r, "if ") then
table.insert(tbl, r.." then")
ify = 1
else
if ify == 1 then
if string.sub(r, 1, 1) ~= " " then
table.insert(tbl, table.getn(tbl)+1, "end")
ify = 0
else
table.insert(tbl, r)
end
end
end
r = h.readLine()
end
h.close()
h = fs.open("testfile", "w")
for k,v in pairs(tbl) do
h.writeLine(v)
print(v)
end
And what this code is supposed to do is turn this:
if 2 == 2
print("Yes")
into this:
if 2 == 2 then
print("Yes")
end
But for some reason it just wipes the file and there becomes nothing in tbl. Why is this?