2018-10-17 18:32:39 -07:00
|
|
|
require("love.event")
|
2018-11-10 19:50:10 -08:00
|
|
|
local fennel = require("lib.fennel")
|
2018-10-17 18:32:39 -07:00
|
|
|
local view = require("lib.fennelview")
|
|
|
|
local event, channel = ...
|
2018-11-10 19:50:10 -08:00
|
|
|
local function display(s)
|
|
|
|
io.write(s)
|
|
|
|
return io.flush()
|
|
|
|
end
|
|
|
|
do local _ = display end
|
|
|
|
local function prompt()
|
|
|
|
return display("\n>> ")
|
|
|
|
end
|
|
|
|
do local _ = prompt end
|
|
|
|
local function read_chunk()
|
|
|
|
local input = io.read()
|
|
|
|
if input then
|
|
|
|
return (input .. "\n")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
do local _ = read_chunk end
|
|
|
|
local input = ""
|
2018-10-17 18:32:39 -07:00
|
|
|
local function _0_(...)
|
|
|
|
if channel then
|
2018-11-10 19:50:10 -08:00
|
|
|
local bytestream, clearstream = fennel.granulate(read_chunk)
|
2018-10-17 18:32:39 -07:00
|
|
|
local function _0_()
|
2018-11-10 19:50:10 -08:00
|
|
|
local c = (bytestream() or 10)
|
|
|
|
input = (input .. string.char(c))
|
|
|
|
return c
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
2018-11-10 19:50:10 -08:00
|
|
|
local read = fennel.parser(_0_)
|
|
|
|
while true do
|
|
|
|
prompt()
|
|
|
|
input = ""
|
|
|
|
do
|
|
|
|
local ok, ast = read()
|
|
|
|
local function _1_(...)
|
|
|
|
if not ok then
|
|
|
|
display(("Error:" .. view(ast) .. "\n"))
|
|
|
|
return clearstream()
|
|
|
|
else
|
|
|
|
love.event.push(event, input)
|
|
|
|
return display(channel:demand())
|
|
|
|
end
|
|
|
|
end
|
|
|
|
_1_(...)
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
end
|
2018-11-10 19:50:10 -08:00
|
|
|
return nil
|
2018-10-17 18:32:39 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
_0_(...)
|
|
|
|
local function start_repl()
|
|
|
|
local code = love.filesystem.read("stdio.fnl")
|
|
|
|
local function _1_()
|
|
|
|
if code then
|
|
|
|
return love.filesystem.newFileData(fennel.compileString(code), "io")
|
|
|
|
else
|
|
|
|
return love.filesystem.read("lib/stdio.lua")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
local lua = _1_()
|
|
|
|
local thread = love.thread.newThread(lua)
|
|
|
|
local io_channel = love.thread.newChannel()
|
|
|
|
thread:start("eval", io_channel)
|
|
|
|
local function _2_(input)
|
|
|
|
local ok, val = pcall(fennel.eval, input)
|
|
|
|
local function _3_()
|
|
|
|
if ok then
|
|
|
|
return view(val)
|
|
|
|
else
|
|
|
|
return val
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return io_channel:push(_3_())
|
|
|
|
end
|
|
|
|
love.handlers.eval = _2_
|
|
|
|
return nil
|
|
|
|
end
|
2018-11-10 19:50:10 -08:00
|
|
|
return {start = start_repl}
|