extract a function

This commit is contained in:
Kartik K. Agaram 2022-04-25 22:21:47 -07:00
parent 8fb1885348
commit a200d713ef
1 changed files with 19 additions and 27 deletions

46
mu.lua
View File

@ -22,6 +22,21 @@ local function readline()
return result return result
end end
function eval_print(f)
local success, results = gather_results(xpcall(f, function(...) return debug.traceback() end))
if success then
for i, result in ipairs(results) do
if i > 1 then
stdscr:addch('\t')
end
stdscr:addstr(tostring(result))
end
else
stdscr:addstr(tostring(result[1]))
end
stdscr:addch('\n')
end
local new_expr = true local new_expr = true
local buf = '' local buf = ''
while true do while true do
@ -31,46 +46,23 @@ while true do
stdscr:addstr('>> ') stdscr:addstr('>> ')
end end
buf = buf .. readline() buf = buf .. readline()
local f, err = load('return '..buf, 'REPL') local f = load('return '..buf, 'REPL')
if f then if f then
buf = '' buf = ''
new_expr = true new_expr = true
local success, results = gather_results(xpcall(f, function(...) return debug.traceback() end)) eval_print(f)
if success then
for i, result in ipairs(results) do
if i > 1 then
stdscr:addch('\t')
end
stdscr:addstr(tostring(result))
end
else
stdscr:addstr(tostring(result[1]))
end
stdscr:addch('\n')
else else
local f, err = load(buf, 'REPL') local f, err = load(buf, 'REPL')
if f then if f then
buf = '' buf = ''
new_expr = true new_expr = true
local success, results = gather_results(xpcall(f, function(...) return debug.traceback() end)) eval_print(f)
if success then
for _, result in ipairs(results) do
if i > 1 then
stdscr:addch('\t')
end
stdscr:addstr(tostring(result))
end
else
stdscr:addstr(tostring(result[1]))
end
stdscr:addch('\n')
else else
stdscr:addstr(err..'\n')
if string.match(err, "'<eof>'$") or string.match(err, "<eof>$") then if string.match(err, "'<eof>'$") or string.match(err, "<eof>$") then
buf = buf .. '\n' buf = buf .. '\n'
new_expr = false new_expr = false
else else
print(err) stdscr:addstr(err..'\n')
buf = '' buf = ''
new_expr = true new_expr = true
end end