forked from mkottman/AndroLua
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinterp.lua
170 lines (152 loc) · 3.6 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
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
require 'socket'
interp = {}
local macros = {}
function interp.macro (def)
macros[def.name] = def
end
local function expand_macro (M,a)
if M.arg then
if not a then
a = M.last_arg
if not a then
return print 'you need to pass a parameter to this macro'
end
else
M.last_arg = a
end
else
a = '' -- keep format happy
end
for i,line in ipairs(M) do
local cmd = line:format(a)
print('! '..cmd)
interp.process_line(cmd)
end
end
local f,err = io.open 'defs.lua'
if f then
f:close()
dofile 'defs.lua'
end
local winapi = require 'winapi'
local t,c2,err
local addr = arg[2] or 'localhost'
local c = socket.connect(addr,3333)
c:send ('yes\n')
c:receive()
local m = winapi.mutex()
t = winapi.thread(function()
c2,err = socket.connect(addr,3334)
if err then
c:close()
error('cannot connect to secondary socket '..err)
end
while true do
local res = c2:receive()
res = res:gsub('\001','\n')
m:pcall(io.write,res)
end
end,'ok')
winapi.sleep(50)
local log = io.open('log.txt','a')
local function prep_for_send (s)
return (s:gsub('\n','\001'))
end
local function exists (f)
local f,err = io.open(f)
if f then
f:close()
return true
end
end
local function mod2file (mod)
local root = mod:gsub('%.','/')
local file = root..'.lua'
if not exists(file) then
file = root..'/init.lua'
mod = mod..'.init'
if not exists(file) then
return nil,"module not found"
end
end
print('mod',mod,file)
return mod,file
end
function readfile(file)
local f,err = io.open(file)
if not f then return nil,err end
local contents = f:read '*a'
f:close()
return contents
end
function eval(line)
local res
m:pcall(function()
c:send(line..'\n')
res = c:receive()
end)
return (res or '?'):gsub('\001','\n')
end
print 'Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio'
local init,err = readfile 'init.lua'
if init then
print 'loading init.lua'
eval(prep_for_send(init))
end
function interp.process_line (line)
log:write(line,'\n')
local cmd,file = line:match '^%.(%S+)(.*)$'
if cmd then
file = file:gsub('^%s*','')
file = #file > 0 and file or nil
if macros[cmd] then
expand_macro(macros[cmd],file)
return
elseif file then -- either .l (load) or .m (upload module)
local mod,kind = file,'run'
if cmd == 'm' then -- given in Lua module form
mod,file = mod2file(mod)
if not mod then return print(file) end
kind = 'mod'
end
line,err = readfile(file)
if err then
return print(err)
end
if kind == 'mod' then
local ok,err = loadstring(line)
if not ok then return print(err) end
end
line = prep_for_send(line)
line = '--'..kind..':'..mod..'\001'..line
else
return print 'unknown command'
end
else
-- = EXPR becomes print(EXPR)
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
end
io.write '> '
local line = io.read()
while line do
interp.process_line(line)
io.write '> '
line = io.read()
end
log:close()
c:close()
if c2 then
if t then t:kill() end
c2:close()
end