extract a function

This commit is contained in:
Kartik K. Agaram 2022-04-25 22:21:47 -07:00
parent 8fb1885348
commit a200d713ef

46
mu.lua
View File

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