clean up repl functionality

Now we have separate keywords for returning vs drawing something.
This commit is contained in:
Kartik K. Agaram 2022-05-11 13:12:13 -07:00
parent d8e5a1ce12
commit ba0d0028ea
2 changed files with 49 additions and 32 deletions

View File

@ -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

42
repl.lua Normal file
View File

@ -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