diff options
author | Case Duckworth | 2024-04-09 21:04:17 -0500 |
---|---|---|
committer | Case Duckworth | 2024-04-09 21:04:29 -0500 |
commit | 8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e (patch) | |
tree | 124ef31663ed570bed358dffd9c861d10fabce7b /repl.lua | |
parent | Uh (diff) | |
download | lam-8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e.tar.gz lam-8ce2915e3c54598c2fda4fec0980ebfc2a3adf6e.zip |
Reorganization
Diffstat (limited to 'repl.lua')
-rw-r--r-- | repl.lua | 81 |
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 | ||
3 | local m = {} | 3 | local m = {} |
4 | local _r = require("read") | 4 | local load = require("load").load |
5 | local read, inport, read_string, eof = | ||
6 | _r.read, _r.inport, _r.read_string, _r.eof | ||
7 | local eval = require("eval").eval | ||
8 | 5 | ||
9 | local function schemeprint (x) | 6 | m.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 | ||
20 | end | ||
21 | |||
22 | local 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 | ||
30 | local function handle_error (e) | ||
31 | local start = e:find(": ") | ||
32 | return e:sub(start + 2) | ||
33 | end | ||
34 | |||
35 | function 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 | |||
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() | ||
82 | end | ||
83 | |||
84 | function m.repl (prompt) | 14 | function m.repl (prompt) |
85 | return m.read_eval(nil, { prompt = prompt, }) | 15 | io.stderr:write(m.logo) |
86 | end | 16 | return load(nil, {prompt = prompt or "> ", }) |
87 | |||
88 | function m.load (filename) | ||
89 | return m.read_eval(filename) | ||
90 | end | 17 | end |
91 | 18 | ||
92 | -------- | 19 | -------- |