Created
March 24, 2013 05:13
-
-
Save AndrewTsao/5230653 to your computer and use it in GitHub Desktop.
repl.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function repl() | |
local cmd, res, msg1, msg2 | |
-- R | |
io.write '>' | |
cmd = io.read() | |
if not cmd or #cmd == 0 then | |
return repl() | |
end | |
res, msg1 = loadstring(cmd, 'repl') | |
if not res then | |
res, msg2 = loadstring('return (' .. cmd .. ')', 'repl') | |
end | |
while not res do | |
io.write ' >' | |
local part = io.read() | |
if not part then | |
io.write(msg1) | |
io.write '\n' | |
return repl() | |
end | |
cmd = cmd .. '\n' .. part | |
res, msg1 = loadstring(cmd, 'repl') | |
if not res then | |
res, msg2 = loadstring('return (' .. cmd .. ')', 'repl') | |
end | |
end | |
-- E | |
local ret = pack(pcall(res)) | |
-- P | |
if ret.n == 1 then | |
return repl() | |
end | |
for i = 2, ret.n do | |
io.write(tostring(ret[i])) | |
if i < ret.n then io.write('\t') end | |
end | |
io.write('\n') | |
return repl() | |
end | |
repl() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment