diff --git a/main.lua b/main.lua index c2e6aa6..a2d0a57 100644 --- a/main.lua +++ b/main.lua @@ -1,5 +1,6 @@ require 'keychord' require 'button' +require 'repl' local utf8 = require 'utf8' lines = {} @@ -66,8 +67,9 @@ function love.draw() -- cursor love.graphics.print('_', 25+text:getWidth()*1.5, y) + -- display side effect if exec_payload then - call_gather(exec_payload) + run(exec_payload) end end @@ -117,9 +119,10 @@ function keychord_pressed(chord) end end elseif chord == 'C-r' then - eval(lines[#lines]) ---? lines[#lines+1] = eval(lines[#lines])[1] ---? lines[#lines+1] = '' + lines[#lines+1] = eval(lines[#lines])[1] + lines[#lines+1] = '' + elseif chord == 'C-d' then + parse_into_exec_payload(lines[#lines]) end end @@ -129,31 +132,3 @@ end function love.mousepressed(x, y, button) propagate_to_button_handers(x, y, button) end - -function eval(buf) - local f = load('return '..buf, 'REPL') - if f then - exec_payload = f - return ---? return call_gather(f) - end - local f, err = load(buf, 'REPL') - if f then - exec_payload = f - return ---? return call_gather(f) - else - return {err} - end -end - --- based on https://github.com/hoelzro/lua-repl -function call_gather(f) - local success, results = gather_results(xpcall(f, function(...) return debug.traceback() end)) - return results -end - -function gather_results(success, ...) - local n = select('#', ...) - return success, { n = n, ... } -end diff --git a/repl.lua b/repl.lua new file mode 100644 index 0000000..44bba97 --- /dev/null +++ b/repl.lua @@ -0,0 +1,42 @@ +-- beginnings of a repl + +function eval(buf) + local f = load('return '..buf, 'REPL') + if f then + return run(f) + end + local f, err = load(buf, 'REPL') + if f then + return run(f) + else + return {err} + end +end + +-- you could perform parse and run separately +-- usually because there's a side-effect like drawing that you want to control the timing of +function parse_into_exec_payload(buf) + local f = load('return '..buf, 'REPL') + if f then + exec_payload = f + return + end + local f, err = load(buf, 'REPL') + if f then + exec_payload = f + return + else + return {err} + end +end + +-- based on https://github.com/hoelzro/lua-repl +function run(f) + local success, results = gather_results(xpcall(f, function(...) return debug.traceback() end)) + return results +end + +function gather_results(success, ...) + local n = select('#', ...) + return success, { n = n, ... } +end