-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepl
executable file
·80 lines (60 loc) · 1.57 KB
/
repl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/lua
local env = require "scheme.env"
local readline, rl = pcall(require, "readline")
env = env.new_environment()
env:eval_file("core.scm")
-- Quick and dirty way to find out if the chunk has balanced parentheses
local function is_balanced(str)
local open = 0
local close = 0
local instr = false
for i = 1, #str do
if str:sub(i, i) == "\"" and str:sub(i - 1, i - 1) ~= "\\" then
instr = not instr
end
if not instr then
if str:sub(i, i) == "(" then
open = open + 1
elseif str:sub(i, i) == ")" then
close = close + 1
end
end
end
return open == close
end
local running = 0
local evalbuf = ""
while true do
local prompt
if #evalbuf == 0 then
prompt = ("(input):%d > "):format(env.parser.line)
else
prompt = (" %d.. "):format(env.parser.line)
end
local line
if readline then
line = rl.readline(prompt)
else
io.write(prompt)
line = io.read("*l")
end
if not line then
break
end
if readline then
rl.add_history(line)
end
evalbuf = evalbuf .. line
if is_balanced(evalbuf) then
local res = env:eval(evalbuf .. "\n")
if res then
env:define("$" .. tostring(running), res)
env:define("_", res)
print((">> $%s = %s\n %s"):format(
running, tostring(res), res.type))
running = running + 1
end
evalbuf = ""
end
end
print("^D - bye!")