diff options
Diffstat (limited to 'repl.lua')
-rw-r--r-- | repl.lua | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/repl.lua b/repl.lua new file mode 100644 index 0000000..3cdfe4e --- /dev/null +++ b/repl.lua | |||
@@ -0,0 +1,41 @@ | |||
1 | --- lam.repl | ||
2 | |||
3 | local repl = {} | ||
4 | local eval = require "eval" | ||
5 | local read = require "read" | ||
6 | local util = require "util" | ||
7 | |||
8 | function schemestr(x) | ||
9 | if type(x) == "table" then | ||
10 | local ts = "(" .. schemestr(util.pop(x)) | ||
11 | for i,v in ipairs(x) do | ||
12 | ts = string.format("%s %s", ts, schemestr(v)) | ||
13 | end | ||
14 | ts = ts .. ")" | ||
15 | return ts | ||
16 | elseif x == true then | ||
17 | return "#t" | ||
18 | elseif x == false then | ||
19 | return "#f" | ||
20 | else | ||
21 | return tostring(x) | ||
22 | end | ||
23 | end | ||
24 | |||
25 | function repl.repl (prompt) | ||
26 | prompt = prompt or "lam> " | ||
27 | repeat | ||
28 | io.write(prompt) | ||
29 | io.output():flush() | ||
30 | input = io.read() | ||
31 | if input == ",q" or input == ",quit" then | ||
32 | break | ||
33 | else | ||
34 | val = eval(read(input)) | ||
35 | if val then print(schemestr(val)) end | ||
36 | end | ||
37 | until false | ||
38 | end | ||
39 | |||
40 | --- | ||
41 | return repl | ||