-
Notifications
You must be signed in to change notification settings - Fork 187
/
repl.lua
89 lines (73 loc) · 1.74 KB
/
repl.lua
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
80
81
82
83
84
85
86
87
88
89
local uv = require('luv')
local utils = require('lib/utils')
if uv.guess_handle(0) ~= "tty" or
uv.guess_handle(1) ~= "tty" then
error "stdio must be a tty"
end
local stdin = uv.new_tty(0, true)
local stdout = require('lib/utils').stdout
local debug = require('debug')
local c = utils.color
local function gatherResults(success, ...)
local n = select('#', ...)
return success, { n = n, ... }
end
local function printResults(results)
for i = 1, results.n do
results[i] = utils.dump(results[i])
end
print(table.concat(results, '\t'))
end
local buffer = ''
local function evaluateLine(line)
if line == "<3\n" then
print("I " .. c("Bred") .. "♥" .. c() .. " you too!")
return '>'
end
local chunk = buffer .. line
local f, err = loadstring('return ' .. chunk, 'REPL') -- first we prefix return
if not f then
f, err = loadstring(chunk, 'REPL') -- try again without return
end
if f then
buffer = ''
local success, results = gatherResults(xpcall(f, debug.traceback))
if success then
-- successful call
if results.n > 0 then
printResults(results)
end
else
-- error
print(results[1])
end
else
if err:match "'<eof>'$" then
-- Lua expects some more input; stow it away for next time
buffer = chunk .. '\n'
return '>>'
else
print(err)
buffer = ''
end
end
return '>'
end
local function displayPrompt(prompt)
uv.write(stdout, prompt .. ' ')
end
local function onread(err, line)
if err then error(err) end
if line then
local prompt = evaluateLine(line)
displayPrompt(prompt)
else
uv.close(stdin)
end
end
coroutine.wrap(function()
displayPrompt '>'
uv.read_start(stdin, onread)
end)()
uv.run()
print("")