From ab8a02fd30451207578927c7e69aa397ad596459 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Sat, 30 Mar 2024 22:20:36 -0500 Subject: Read from ports now --- repl.lua | 48 ++++++++++++++++++++++++++++++++---------------- 1 file changed, 32 insertions(+), 16 deletions(-) (limited to 'repl.lua') diff --git a/repl.lua b/repl.lua index b198880..556525c 100644 --- a/repl.lua +++ b/repl.lua @@ -1,10 +1,12 @@ --- lam.repl -local repl = {} -local eval = require("eval").eval -local read = require("read").read +local m = {} +local read = require("read") +local eval = require("eval") +local pp = require("pp").pp local function schemeprint (x) + -- if x == nil then return end if x == true then print("#t") elseif x == false then @@ -14,19 +16,33 @@ local function schemeprint (x) end end -function repl.repl (prompt) - if not prompt then prompt = "lam> " end - io.input():setvbuf("line") - repeat - io.write(prompt) - io.output():flush() - local input = io.read() - if input ~= "" then - local value = eval(read(input)) - if value ~= nil then schemeprint(value) end +function m.repl (prompt, infile, out) + -- PROMPT should be a string, INFILE is a filename, and OUT is either a + -- filename, nil (in which case it will be stdout), or false (which + -- suppresses output) + local inport = read.inport(infile) + if out ~= false then io.output(out) end + io.output():setvbuf("line") + if prompt then + stderr = io.open("/dev/stderr", "w") -- Linux-only ! + end + while true do + if prompt then + stderr:write(prompt) + stderr:flush() + end + local x = read.read(inport) + if x then + local val = eval.eval(x) + if out ~= false then + schemeprint(val) + end end - until false + end + inport:close() + stderr:close() + io.output():close() end ---- -return repl +-------- +return m -- cgit 1.4.1-21-gabe81