-
Notifications
You must be signed in to change notification settings - Fork 295
/
interp.lua
58 lines (53 loc) · 1.09 KB
/
interp.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
require 'socket'
local c = socket.connect('localhost',3333)
local log = io.open('log.txt','a')
function readfile(file)
local f,err = io.open(file)
if not f then return nil,err end
local contents = f:read '*a':gsub('\n','\001')
f:close()
return contents
end
function eval(line)
c:send(line..'\n')
local res = c:receive()
return res:gsub('\001','\n')
end
local init,err = readfile 'init.lua'
if init then
print 'loading init.lua'
io.write(eval(init)..'\n')
end
io.write '> '
local line = io.read()
while line do
log:write(line,'\n')
local cmd,file = line:match '^%.(.)%s+(.+)$'
if file then
local mod
if cmd == 'm' then
mod = file
file = mod:gsub('%.','/')..'.lua'
end
line,err = readfile(file)
if mod and line then
line = '--mod:'..mod..'\001'..line
end
else
local expr = line:match '^%s*=%s*(.+)$'
if expr then
line = 'print('..expr..')'
end
end
if line then
local res = eval(line)
log:write(res,'\n')
io.write(res)
else
print(err)
end
io.write '> '
line = io.read()
end
log:close()
c:close()