From dfd67aed3c8d2f587a4902e8e1a101d96a01948a Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 25 Apr 2022 22:10:20 -0700 Subject: [PATCH] copy how Lua 5.3 REPL prints expr values --- mu.lua | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/mu.lua b/mu.lua index 8917476..e0342fb 100644 --- a/mu.lua +++ b/mu.lua @@ -5,9 +5,6 @@ curses.echo(false) -- unclear why implicit echo can't handle newlines, regardles stdscr:clear() stdscr:scrollok(true) --- unclear how Lua (post 5.2) is able to selectively print values of variables --- at the repl - local function gather_results(success, ...) local n = select('#', ...) return success, { n = n, ... } @@ -34,7 +31,7 @@ while true do stdscr:addstr('>> ') end buf = buf .. readline() - local f, err = load(buf, 'REPL') + local f, err = load('return '..buf, 'REPL') if f then buf = '' new_expr = true @@ -47,14 +44,28 @@ while true do print(results[1]) end else - stdscr:addstr(err..'\n') - if string.match(err, "''$") or string.match(err, "$") then - buf = buf .. '\n' - new_expr = false - else - print(err) + 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 + print(result) + end + else + print(results[1]) + end + else + stdscr:addstr(err..'\n') + if string.match(err, "''$") or string.match(err, "$") then + buf = buf .. '\n' + new_expr = false + else + print(err) + buf = '' + new_expr = true + end end end end