-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmylang_repl.lua
More file actions
72 lines (65 loc) · 1.42 KB
/
Copy pathmylang_repl.lua
File metadata and controls
72 lines (65 loc) · 1.42 KB
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
local mylang = require'mylang'
local pack = table.pack or function(...) return {n=select('#',...),...} end
local unpack = table.unpack or unpack
if arg[1] then
local f = assert(io.open(arg[1]))
local function stream()
return f:read(1)
end
local chunk, errstr, errtbl = mylang.load(stream, arg[1])
f:close()
if chunk then
local newarg = {}
for k, v in pairs(arg) do
newarg[k-1] = v
end
arg = newarg
local success, result = pcall(chunk, unpack(arg))
if not success then
io.stdout:write(tostring(result)..'\n')
end
else
io.stdout:write(errstr..'\n')
end
return
end
local function readln()
local line = io.stdin:read()
if not line then
io.stdout:write('\n')
os.exit(0)
end
return line
end
local srcname = "(repl input)"
while true do
io.stdout:write('>>> ')
local line = readln()..'\n'
local chunk, errstr, errtbl = mylang.load(line, srcname, env, true)
while not chunk do
if errtbl.cont then
io.stdout:write('... ')
line = line..readln()..'\n'
chunk, errstr, errtbl = mylang.load(line, srcname, env, true)
else
io.stdout:write(errstr..'\n')
break
end
end
if chunk then
local result = pack(pcall(chunk))
if result[1] then
if result.n >= 2 then
for i = 2, result.n do
if i > 2 then
io.stdout:write'\t'
end
io.stdout:write(tostring(result[i]))
end
io.stdout:write'\n'
end
else
io.stdout:write(tostring(result[2])..'\n')
end
end
end