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.lua81
1 files changed, 4 insertions, 77 deletions
diff --git a/repl.lua b/repl.lua index c4a6546..4bdd918 100644 --- a/repl.lua +++ b/repl.lua
@@ -1,25 +1,9 @@
1--- lam.repl 1--- lam.repl
2 2
3local m = {} 3local m = {}
4local _r = require("read") 4local load = require("load").load
5local read, inport, read_string, eof =
6 _r.read, _r.inport, _r.read_string, _r.eof
7local eval = require("eval").eval
8 5
9local function schemeprint (x) 6m.logo = [[
10 -- if x == nil then return end
11 if x == true then
12 print("#t")
13 elseif x == false then
14 print("#f")
15 elseif x == nil then
16 print("#<nil>")
17 else
18 print(x)
19 end
20end
21
22local lam = [[
23 @,,,@ 7 @,,,@
24<|^ ^|> l a m 8<|^ ^|> l a m
25 | /) 0015 9 | /) 0015
@@ -27,66 +11,9 @@ local lam = [[
27 ------------- 11 -------------
28]] 12]]
29 13
30local function handle_error (e)
31 local start = e:find(": ")
32 return e:sub(start + 2)
33end
34
35function m.read_eval (filename, interactive)
36 -- interactive = { out = file or handle, prompt = string, }
37 local inport = inport(filename)
38 local prompt = interactive and interactive.prompt or "> "
39 if interactive then
40 io.output(interactive.out or io.stdout)
41 io.write(lam)
42 io.output():setvbuf("line")
43 else
44 io.output():setvbuf("no")
45 end
46 repeat
47 if interactive then
48 io.stderr:write(prompt)
49 io.stderr:flush()
50 end
51 -- read
52 local ok, x = xpcall(
53 function ()
54 local nxt = read(inport)
55 return nxt
56 end,
57 handle_error
58 )
59 if not ok then
60 io.stderr:write("(read) not ok: ", x, "\n")
61 -- in interactive mode, errors should not be fatal. in
62 -- batch mode, they should be.
63 if not interactive then return nil end
64 end
65 -- eval
66 if ok then
67 local ok, v = xpcall(
68 function () return eval(x) end,
69 handle_error
70 )
71 if not ok then
72 io.stderr:write("(eval) not ok: ", v, "\n")
73 if not interactive then return nil end
74 end
75 -- print
76 if ok and interactive then schemeprint(v) end
77 elseif interactive then
78 ok = "recover"
79 end
80 until x == eof -- loop
81 inport:close()
82end
83
84function m.repl (prompt) 14function m.repl (prompt)
85 return m.read_eval(nil, { prompt = prompt, }) 15 io.stderr:write(m.logo)
86end 16 return load(nil, {prompt = prompt or "> ", })
87
88function m.load (filename)
89 return m.read_eval(filename)
90end 17end
91 18
92-------- 19--------