Basically this
function runProgram(program, loops)
if program[1] == "set" then
table.remove(program, 1)
program["NAME"] = program[1]
table.remove(program, 1)
elseif program["NAME"] then
else
program["NAME"] = "MAIN"
end
print("\nExecuting: ", program["NAME"], " of length ", #program, ", ", loops, " times")
while loops > 0 do
loops = loops - 1
program["COUNT"] = 0
depth = 0
while program["COUNT"] < #program do
program["COUNT"] = program["COUNT"] + 1
print(" ", program["NAME"], " >[count] ", program["COUNT"], " ( ", program[program["COUNT"]], " )")
if type(program[program["COUNT"]]) == "table" then
if tonumber(program[program["COUNT"] + 1]) then
runProgram(program[program["COUNT"]], tonumber(program[program["COUNT"] + 1]))
program["COUNT"] = program["COUNT"] + 1
else
runProgram(program[program["COUNT"]], 1)
end
elseif program[program["COUNT"]] == "(" then
depth = depth + 1
program[program["COUNT"]] = {}
if program[program["COUNT"] + 1] == "set" then
table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"]+1))
program[program["COUNT"]]["NAME"] = program[program["COUNT"] + 1] .. program["NAME"]
table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"] + 1))
else
table.insert(program[program["COUNT"]], "set")
table.insert(program[program["COUNT"]], program["NAME"] .. " >] " .. tostring(program["COUNT"]))
end
--print("\nNew SubProgram: ", program[program["COUNT"]]["NAME"])
--print("DEPTH: ", depth)
while depth ~= 0 and program["COUNT"] < #program do
if program[program["COUNT"] + 1] == "(" then
depth = depth + 1
table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"] + 1))
--print("DEPTH: ", depth)
elseif program[program["COUNT"] + 1] == ")" then
depth = depth - 1
--print("DEPTH: ", depth)
if depth == 0 then
table.remove(program, program["COUNT"] + 1)
else
table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"] + 1))
end
else
table.insert(program[program["COUNT"]], table.remove(program, program["COUNT"] + 1))
end
end
if tonumber(program[program["COUNT"] + 1]) then
program[program["COUNT"]] = runProgram(program[program["COUNT"]], tonumber(program[program["COUNT"] + 1]))
program["COUNT"] = program["COUNT"] + 1
else
program[program["COUNT"]] = runProgram(program[program["COUNT"]], 1)
end
elseif program[program["COUNT"]] == ")" then
print("\n\nFound \")\" when not in a sub-program, maybe missing a space at \"(\"?")
break
elseif APIList["API"][program[program["COUNT"]]] then
if tonumber(program[program["COUNT"] + 1]) then
program["COUNT"] = program["COUNT"] + 1
print(" ", program["NAME"], " [-", program[program["COUNT"]], "-> ", APIList["NAME"], ".", program[program["COUNT"]-1], "()")
for i=1,tonumber(program[program["COUNT"]]) do
APIList["API"][program[program["COUNT"]-1]]()
end
else
print(program["NAME"], " [-> ", APIList["NAME"], ".", program[program["COUNT"]], "()")
APIList["API"][program[program["COUNT"]]]()
end
elseif tonumber(program[program["COUNT"]]) then
print("\tNumber: ", program[program["COUNT"]])
else
os.run( {}, tostring(program[program["COUNT"]]))
end
--os.pullEvent( "key" )
end
end
program["COUNT"] = nil
return program
end
I found a way to make it work, by using the fact that it's a table to my advantage, but was honestly not expecting the variables to do such a thing.
you can initialize variables outside of the function. I imagine your code looks something like this:
local function foo()
a =1
b = 2
do something to a and b
recursive call
end
But if you make the variables global, they will retain their states and not get reset. Like so:
local a =1
local b = 2
local function foo()
do something to a and b
recursive call
end
Of course, I could be totally wrong. Code would be nice to see, like Lyqyd said.
You are not wrong, but the problem was that I needed a fresh set of the variables each call, that would then return to their old value after the call, like c++ and such, but found a way to make it work, as mentioned, thanks anyways!