melee-roguelite/lib/stdio.lua

79 lines
1.8 KiB
Lua
Raw Normal View History

2018-10-17 18:32:39 -07:00
require("love.event")
local fennel = require("lib.fennel")
2018-10-17 18:32:39 -07:00
local view = require("lib.fennelview")
local event, channel = ...
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
local bytestream, clearstream = fennel.granulate(read_chunk)
2018-10-17 18:32:39 -07:00
local function _0_()
local c = (bytestream() or 10)
input = (input .. string.char(c))
return c
2018-10-17 18:32:39 -07:00
end
local read = fennel.parser(_0_)
while true do
prompt()
input = ""
do
local ok, ast = pcall(read)
local function _1_(...)
if not ok then
return display(("Parse error:" .. ast .. "\n"))
else
love.event.push(event, input)
return display(channel:demand())
end
end
_1_(...)
2018-10-17 18:32:39 -07:00
end
end
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
return {start = start_repl}