diff options
author | Kartik K. Agaram <vc@akkartik.com> | 2022-04-25 22:21:47 -0700 |
---|---|---|
committer | Kartik K. Agaram <vc@akkartik.com> | 2022-04-25 22:21:47 -0700 |
commit | a200d713ef762a5fbaf575531c62400fd827a0fd (patch) | |
tree | 7907e054d7620894bf5941427594e229466ea801 | |
parent | 8fb1885348c97b6577ec51be2e838b8679e7064f (diff) | |
download | text.love-a200d713ef762a5fbaf575531c62400fd827a0fd.tar.gz |
extract a function
-rw-r--r-- | mu.lua | 46 |
1 files changed, 19 insertions, 27 deletions
diff --git a/mu.lua b/mu.lua index 8091eca..7469920 100644 --- a/mu.lua +++ b/mu.lua @@ -22,6 +22,21 @@ local function readline() return result 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 buf = '' while true do @@ -31,46 +46,23 @@ while true do stdscr:addstr('>> ') end buf = buf .. readline() - local f, err = load('return '..buf, 'REPL') + local f = load('return '..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 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') + 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 - else - stdscr:addstr(tostring(result[1])) - end - stdscr:addch('\n') + eval_print(f) 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 |