diff options
Diffstat (limited to 'repl.lua')
-rw-r--r-- | repl.lua | 48 |
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 | ||
3 | local repl = {} | 3 | local m = {} |
4 | local eval = require("eval").eval | 4 | local read = require("read") |
5 | local read = require("read").read | 5 | local eval = require("eval") |
6 | local pp = require("pp").pp | ||
6 | 7 | ||
7 | local function schemeprint (x) | 8 | local 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 |
15 | end | 17 | end |
16 | 18 | ||
17 | function repl.repl (prompt) | 19 | function 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() | ||
29 | end | 45 | end |
30 | 46 | ||
31 | --- | 47 | -------- |
32 | return repl | 48 | return m |