about summary refs log tree commit diff stats
path: root/repl.lua
diff options
context:
space:
mode:
Diffstat (limited to 'repl.lua')
-rw-r--r--repl.lua48
1 files changed, 32 insertions, 16 deletions
diff --git a/repl.lua b/repl.lua index b198880..556525c 100644 --- a/repl.lua +++ b/repl.lua
@@ -1,10 +1,12 @@
1--- lam.repl 1--- lam.repl
2 2
3local repl = {} 3local m = {}
4local eval = require("eval").eval 4local read = require("read")
5local read = require("read").read 5local eval = require("eval")
6local pp = require("pp").pp
6 7
7local function schemeprint (x) 8local function schemeprint (x)
9 -- if x == nil then return end
8 if x == true then 10 if x == true then
9 print("#t") 11 print("#t")
10 elseif x == false then 12 elseif x == false then
@@ -14,19 +16,33 @@ local function schemeprint (x)
14 end 16 end
15end 17end
16 18
17function repl.repl (prompt) 19function m.repl (prompt, infile, out)
18 if not prompt then prompt = "lam> " end 20 -- PROMPT should be a string, INFILE is a filename, and OUT is either a
19 io.input():setvbuf("line") 21 -- filename, nil (in which case it will be stdout), or false (which
20 repeat 22 -- suppresses output)
21 io.write(prompt) 23 local inport = read.inport(infile)
22 io.output():flush() 24 if out ~= false then io.output(out) end
23 local input = io.read() 25 io.output():setvbuf("line")
24 if input ~= "" then 26 if prompt then
25 local value = eval(read(input)) 27 stderr = io.open("/dev/stderr", "w") -- Linux-only !
26 if value ~= nil then schemeprint(value) end 28 end
29 while true do
30 if prompt then
31 stderr:write(prompt)
32 stderr:flush()
33 end
34 local x = read.read(inport)
35 if x then
36 local val = eval.eval(x)
37 if out ~= false then
38 schemeprint(val)
39 end
27 end 40 end
28 until false 41 end
42 inport:close()
43 stderr:close()
44 io.output():close()
29end 45end
30 46
31--- 47--------
32return repl 48return m