如何使用lua脚本中的脚本重新启动lua程序
又是我 我正在尝试用Lua制作一个终端程序,因为这是我所知道的最好的语言,我正在用它制作计算器程序,我正在尝试这样做,如果用户键入“退出”,程序将重新启动并返回终端,但我不知道如何通过代码重置程序。如果有人能帮忙,我将不胜感激 代码如下:如何使用lua脚本中的脚本重新启动lua程序,lua,application-restart,Lua,Application Restart,又是我 我正在尝试用Lua制作一个终端程序,因为这是我所知道的最好的语言,我正在用它制作计算器程序,我正在尝试这样做,如果用户键入“退出”,程序将重新启动并返回终端,但我不知道如何通过代码重置程序。如果有人能帮忙,我将不胜感激 代码如下: io.write(“终端正在启动---完成!”) 写下(“确保一切正常——完成!”) cmd=io.read() io.写(“>”) 如果cmd==”“那么 io.写入(“>\n”) 结束 如果cmd==“cal”,则 io.write(“计算器终端程序v1.
io.write(“终端正在启动---完成!”)
写下(“确保一切正常——完成!”)
cmd=io.read()
io.写(“>”)
如果cmd==”“那么
io.写入(“>\n”)
结束
如果cmd==“cal”,则
io.write(“计算器终端程序v1.0”)
io.write(“什么操作?/n”)
op=io.read()
如果op==“退出”,则
io.写入(“退出”)
结束
结束
您可能需要os.exit()
,它可以终止整个程序。我认为这可能通过创造性地使用load()
和协同程序来实现
当发生3个总错误时,将停止重新启动
if innerProgram == nil then --innerProgram will set to true when it load itself
local filename = nil
local errorLimit = 3 --Change this to any value to enable this code to restart itself when error occur until this amount of time set zero or below to exit instantly when error occur
local errors = 0
local filename = function()
local str = debug.getinfo(2, "S").source:sub(2)
return str:match("^.*/(.*)") or str
end
filename = filename()
local src_h = io.open(filename, "r") --open in read mode
local src = src_h:read("*a")
src_h:close()
local G = _G
local quit = false --set true when you want to exit instead restart
local code = nil
local request = false
local restart = false --set true when you want restart
local program
local yield = coroutine.yield --Incase when coroutine get removed in your calculator code for no reason
local running = coroutine.running
local exit = os.exit
function G.restart()
restart = true --Always refer to restart variable above
request = true
yield() --Always refer to yield above
end
function G.os.exit(exitcode) --Replace os.exit with this
quit = true --Always refer to quit variable above
reuqest = true
code = exitcode or nil
yield() --Always refer to yield above
end
function G.coroutine.yield()
if running() == program and request == false then --Emulating coroutine.yield when it not run inside coroutine
error("attempt to yield from outside a coroutine")
end
end
G.innerProgram = true --So the inner program not keep loading itself forever
function copy(obj, seen)
if type(obj) ~= 'table' then return obj end --got from https://stackoverflow.com/questions/640642/how-do-you-copy-a-lua-table-by-value for us to clone _G variable without reference to original _G thus we can do total restart without using same _G
if seen and seen[obj] then return seen[obj] end
local s = seen or {}
local res = setmetatable({}, getmetatable(obj))
s[obj] = res
for k, v in pairs(obj) do res[copy(k, s)] = copy(v, s) end
return res
end
print("Loading "..filename)
program = coroutine.create(load(src, filename, "bt", copy(G)))
while errors < errorLimit do
restart = false
local status, err = coroutine.resume(program)
if restart == true then
print("Restarting...")
program = coroutine.create(load(src, filename, "bt", copy(G)))
--Put errors = errors + 1 if you want errors counter to reset every time the program request restart
end
if status == false and restart ~= true then
print(filename.." errored with "..err.."\nRestarting...")
program = coroutine.create(load(src, filename, "bt", copy(G)))
errors = errors + 1
elseif restart ~= true then
print(filename.." done executing.")
exit()
end
end
return
else
innerProgram = nil --Nil-ing the variable
end
为了直接回答你的问题,我认为“重启程序”是不可能的。但是,通过利用,您可以获得相同的结果 例如,此代码可能满足您的要求:
print('Terminal is starting up --- done!')
print('Making sure everything works --- Done!')
repeat
io.write('>')
cmd = io.read()
if cmd == 'cal' then
print('Calculator Terminal Program v1.0')
repeat
io.write('Operation: ')
op = io.read()
until op == 'exit'
print('Exiting')
elseif cmd == 'command' then
--another command
else
print('Unknown command.')
end
until cmd == 'exit'
其他提示:
- 您应该利用
而不是编写多个单独的elseif
语句来提高可读性if
- 如果您想在写入一些文本后换行以获得更好的终端体验,请考虑使用
功能。您还可以使用print
io.write('\n')
print('Terminal is starting up --- done!')
print('Making sure everything works --- Done!')
repeat
io.write('>')
cmd = io.read()
if cmd == 'cal' then
print('Calculator Terminal Program v1.0')
repeat
io.write('Operation: ')
op = io.read()
until op == 'exit'
print('Exiting')
elseif cmd == 'command' then
--another command
else
print('Unknown command.')
end
until cmd == 'exit'