Last active
August 24, 2019 15:38
-
-
Save snipsnipsnip/164364 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
local function print_if_not_nil(...) | |
if ... ~= nil then | |
print("-> ", ...) | |
end | |
end | |
local function try_to_print(line) | |
local compiled, errmes = loadstring("return " .. line) | |
if compiled then | |
print("=> ", compiled()) | |
else | |
print("failed to print. error message was:") | |
print(errmes) | |
end | |
end | |
local maybe_printable | |
do | |
local errs = { | |
"'=' expected near '<eof>'", | |
"unexpected symbol near" | |
} | |
function maybe_printable(errmes) | |
for mes in ipairs(errs) do | |
if string.find(errmes, mes, 9, true) then | |
return true | |
end | |
end | |
return false | |
end | |
end | |
while true do | |
io.write "> " | |
local line = io.read() | |
if not line then | |
break | |
elseif line ~= "" then | |
local compiled, errmes = loadstring(line) | |
if compiled then | |
print_if_not_nil(compiled()) | |
elseif maybe_printable(errmes) then | |
try_to_print(line) | |
else | |
print(errmes) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment