2018-10-17 18:32:39 -07:00
|
|
|
(require "love.event")
|
|
|
|
(local view (require "lib.fennelview"))
|
|
|
|
|
|
|
|
;; This module exists in order to expose stdio over a channel so that it
|
|
|
|
;; can be used in a non-blocking way from another thread.
|
|
|
|
|
|
|
|
(local (event channel) ...)
|
|
|
|
|
|
|
|
(when channel
|
2020-10-13 23:23:38 -07:00
|
|
|
(let [prompt (fn [next-line?] (io.write (if next-line? ".." ">> ")) (io.flush) (.. (io.read) "\n"))]
|
2019-12-02 18:30:30 -08:00
|
|
|
((fn looper [input]
|
|
|
|
(when input
|
|
|
|
;; This is consumed by love.handlers[event]
|
|
|
|
(love.event.push event input)
|
2020-10-13 23:23:38 -07:00
|
|
|
(let [output (: channel :demand)
|
|
|
|
next-line (and (. output 2) (= "next-line" (. output 2)))]
|
2019-12-02 18:30:30 -08:00
|
|
|
;; There is probably a more efficient way of determining an error
|
2020-10-13 23:23:38 -07:00
|
|
|
(if next-line :ok
|
|
|
|
(and (. output 2) (= "Error:" (. output 2)))
|
2019-12-02 18:30:30 -08:00
|
|
|
(print (view output))
|
|
|
|
(each [_ ret (ipairs output)]
|
2020-10-13 23:23:38 -07:00
|
|
|
(print ret)))
|
|
|
|
(io.flush)
|
|
|
|
(looper (prompt next-line))))) (prompt))))
|
2018-10-17 18:32:39 -07:00
|
|
|
|
|
|
|
{:start (fn start-repl []
|
2019-12-02 18:30:30 -08:00
|
|
|
|
2020-10-13 23:23:38 -07:00
|
|
|
(let [code (love.filesystem.read "lib/stdio.fnl")
|
2019-12-02 18:30:30 -08:00
|
|
|
luac (if code
|
|
|
|
(love.filesystem.newFileData
|
|
|
|
(fennel.compileString code) "io")
|
|
|
|
(love.filesystem.read "lib/stdio.lua"))
|
|
|
|
thread (love.thread.newThread luac)
|
|
|
|
io-channel (love.thread.newChannel)
|
|
|
|
coro (coroutine.create fennel.repl)
|
|
|
|
out (fn [val]
|
|
|
|
(: io-channel :push val))
|
2020-10-13 23:23:38 -07:00
|
|
|
options {:readChunk (fn [parser-state]
|
|
|
|
(when (> parser-state.stack-size 0)
|
|
|
|
(: io-channel :push ["next-line" "next-line"]))
|
|
|
|
(coroutine.yield))
|
2019-12-02 18:30:30 -08:00
|
|
|
:onValues out
|
|
|
|
:onError (fn [kind ...] (out [kind "Error:" ...]))
|
|
|
|
:pp view
|
2020-04-01 21:42:57 -07:00
|
|
|
:moduleName "lib.fennel"}]
|
2018-10-17 18:32:39 -07:00
|
|
|
;; this thread will send "eval" events for us to consume:
|
2019-12-02 18:30:30 -08:00
|
|
|
(coroutine.resume coro options)
|
2018-10-17 18:32:39 -07:00
|
|
|
(: thread :start "eval" io-channel)
|
|
|
|
(set love.handlers.eval
|
|
|
|
(fn [input]
|
2020-10-13 23:23:38 -07:00
|
|
|
(coroutine.resume coro input)))))}
|