clean up repl functionality
Now we have separate keywords for returning vs drawing something.
This commit is contained in:
parent
d8e5a1ce12
commit
ba0d0028ea
39
main.lua
39
main.lua
|
@ -1,5 +1,6 @@
|
||||||
require 'keychord'
|
require 'keychord'
|
||||||
require 'button'
|
require 'button'
|
||||||
|
require 'repl'
|
||||||
local utf8 = require 'utf8'
|
local utf8 = require 'utf8'
|
||||||
|
|
||||||
lines = {}
|
lines = {}
|
||||||
|
@ -66,8 +67,9 @@ function love.draw()
|
||||||
-- cursor
|
-- cursor
|
||||||
love.graphics.print('_', 25+text:getWidth()*1.5, y)
|
love.graphics.print('_', 25+text:getWidth()*1.5, y)
|
||||||
|
|
||||||
|
-- display side effect
|
||||||
if exec_payload then
|
if exec_payload then
|
||||||
call_gather(exec_payload)
|
run(exec_payload)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -117,9 +119,10 @@ function keychord_pressed(chord)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
elseif chord == 'C-r' then
|
elseif chord == 'C-r' then
|
||||||
eval(lines[#lines])
|
lines[#lines+1] = eval(lines[#lines])[1]
|
||||||
--? lines[#lines+1] = eval(lines[#lines])[1]
|
lines[#lines+1] = ''
|
||||||
--? lines[#lines+1] = ''
|
elseif chord == 'C-d' then
|
||||||
|
parse_into_exec_payload(lines[#lines])
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -129,31 +132,3 @@ end
|
||||||
function love.mousepressed(x, y, button)
|
function love.mousepressed(x, y, button)
|
||||||
propagate_to_button_handers(x, y, button)
|
propagate_to_button_handers(x, y, button)
|
||||||
end
|
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
|
|
||||||
|
|
|
@ -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
|
Loading…
Reference in New Issue