Bumped to Fennel 6.1 and fixed stdio.fnl readline!

This commit is contained in:
Alexander Griffith 2020-10-14 02:23:38 -04:00
parent b3b17a3838
commit 229449c37b
4 changed files with 1203 additions and 950 deletions

1067
lib/fennel

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -7,23 +7,25 @@
(local (event channel) ...) (local (event channel) ...)
(when channel (when channel
(let [prompt (fn [] (io.write "> ") (io.flush) (io.read "*l"))] (let [prompt (fn [next-line?] (io.write (if next-line? ".." ">> ")) (io.flush) (.. (io.read) "\n"))]
((fn looper [input] ((fn looper [input]
(when input (when input
;; This is consumed by love.handlers[event] ;; This is consumed by love.handlers[event]
(love.event.push event input) (love.event.push event input)
(let [output (: channel :demand)] (let [output (: channel :demand)
next-line (and (. output 2) (= "next-line" (. output 2)))]
;; There is probably a more efficient way of determining an error ;; There is probably a more efficient way of determining an error
(if (and (. output 2) (= "Error:" (. output 2))) (if next-line :ok
(and (. output 2) (= "Error:" (. output 2)))
(print (view output)) (print (view output))
(each [_ ret (ipairs output)] (each [_ ret (ipairs output)]
(print ret)))) (print ret)))
(io.flush) (io.flush)
(looper (prompt)))) (prompt)))) (looper (prompt next-line))))) (prompt))))
{:start (fn start-repl [] {:start (fn start-repl []
(let [code (love.filesystem.read "stdio.fnl") (let [code (love.filesystem.read "lib/stdio.fnl")
luac (if code luac (if code
(love.filesystem.newFileData (love.filesystem.newFileData
(fennel.compileString code) "io") (fennel.compileString code) "io")
@ -33,7 +35,10 @@
coro (coroutine.create fennel.repl) coro (coroutine.create fennel.repl)
out (fn [val] out (fn [val]
(: io-channel :push val)) (: io-channel :push val))
options {:readChunk coroutine.yield options {:readChunk (fn [parser-state]
(when (> parser-state.stack-size 0)
(: io-channel :push ["next-line" "next-line"]))
(coroutine.yield))
:onValues out :onValues out
:onError (fn [kind ...] (out [kind "Error:" ...])) :onError (fn [kind ...] (out [kind "Error:" ...]))
:pp view :pp view
@ -43,4 +48,4 @@
(: thread :start "eval" io-channel) (: thread :start "eval" io-channel)
(set love.handlers.eval (set love.handlers.eval
(fn [input] (fn [input]
(coroutine.resume coro (.. input "\n"))))))} (coroutine.resume coro input)))))}

View file

@ -1,46 +0,0 @@
require("love.event")
local view = require("lib.fennelview") local event, channel = ... local function _0_(...) if channel then
local prompt = nil local function _0_() io.write("> ") io.flush() return io.read("*l") end prompt = _0_
local function looper(input) if input then
love.event.push(event, input)
do local output = channel:demand()
if (output[2] and ("Error:" == output[2])) then
print(view(output)) else
for _, ret in ipairs(output) do
print(ret) end end end
io.flush()
return looper(prompt()) end end return looper(prompt()) end end _0_(...)
local function start_repl()
local code = love.filesystem.read("stdio.fnl") local luac = nil
if code then
luac = love.filesystem.newFileData(fennel.compileString(code), "io") else
luac = love.filesystem.read("lib/stdio.lua") end
local thread = love.thread.newThread(luac)
local io_channel = love.thread.newChannel()
local coro = coroutine.create(fennel.repl) local out = nil
local function _2_(val)
return io_channel:push(val) end out = _2_ local options = nil
local function _3_(kind, ...) return out({kind, "Error:", ...}) end options = {moduleName = "lib.fennel", onError = _3_, onValues = out, pp = view, readChunk = coroutine.yield}
coroutine.resume(coro, options)
thread:start("eval", io_channel)
local function _4_(input)
return coroutine.resume(coro, (input .. "\n")) end love.handlers.eval = _4_ return nil end return {start = start_repl}