-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyexec.lua
284 lines (242 loc) · 6.65 KB
/
pyexec.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
-- Py Exec Lib
function string:uwordClear() return self:gsub("u('.-')","%1"):gsub('u(".-")','%1') end
function string:key_tree()
local tree = {}
local pos = 1
for i = 1, self:len() do
if self:sub(i, i) == "." or self:sub(i, i) == "[" then
if self:sub(i-1, i-1) ~= "]" then
table.insert(tree, self:sub(pos, i - 1))
if self:sub(i, i) == "." then pos = i + 1
else
if self:sub(i+1, i+1) == '"' or self:sub(i+1, i+1) == "'" then
pos = i + 2
else
pos = i + 1
end
end
end
elseif self:sub(i, i) == "]" then
if self:sub(i-1, i-1) == '"' or self:sub(i-1, i-1) == "'" then
table.insert(tree, self:sub(pos, i - 2))
else
table.insert(tree, tonumber(self:sub(pos, i - 1)))
end
end
end
if self:sub(self:len(), self:len()) ~= "]" then
table.insert(tree, self:sub(pos, self:len()))
end
return tree
end
function string:var2pointer(pointer)
return getPointer(pointer, self:key_tree())
end
function getPointer(pointer, tree)
if #tree == 0 then return pointer end
for _, key in ipairs(tree) do
if pointer[key] == nil then error("pointer by tree "..dump(tree).." is no exist") end
pointer = pointer[key]
end
return pointer
end
--[[
FUNC = smth.or.smth["other"](.*)
OP
NUM
EXPR
--]]
function string:isString()
local type = self:sub(1, 1)
if not (type == "'" or type == '"') then return false end
local open = false
for i = 1, self:len() do
if self:sub(i, i) == type and self:sub(i-1, i-1) ~= "\\" then
if open == true then
return i == self:len()
else
open = true
end
end
end
end
function string:isNumber()
return tonumber(self) ~= nil
end
function string:isObject()
if not (self:sub(1, 1) == "{" and self:sub(self:len(), self:len()) == '}') then return false end
local open_count = 0
for i = 1, self:len() do
if self:sub(i, i) == "{" then
open_count = open_count + 1
elseif self:sub(i, i) == "}" then
open_count = open_count - 1
if open_count == 0 then
return i == self:len()
end
end
end
return false
end
function string:isFunc()
return self:match("[%a_][%w_%.%[%]\"'%s]-%(.*%)") == self
end
function string:isVar(pointer)
local tree = self:key_tree()
if #tree == 0 then return false end
for _, key in ipairs(tree) do
if pointer[key] == nil then return false end
pointer = pointer[key]
end
return true
end
function string:charInString(pos)
local open = false
local type = ""
for i = 1, self:len() do
if self:sub(i, i) == "'" or self:sub(i, i) == '"' then
if self:sub(i-1, i-1) ~= "\\" then
if type == self:sub(i, i) then
open = false
type = ""
else
open = true
type = self:sub(i, i)
end
end
end
if i == pos then
return open
end
end
return false
end
function string:testOp(regexp)
local tmp = 1
local replaces = {}
local pos = 0
local pos2 = 0
while true do
pos, pos2 = self:find(regexp, tmp)
if pos == nil then return replaces end
if not self:charInString(pos) then
table.insert(replaces, {pos,pos2})
end
tmp = pos2 + 1
end
end
function cast(result)
if type(result) == 'number' then
result = tostring(result)
elseif type(result) == 'string' then
result = '"'..result..'"'
elseif type(result) == 'table' then
result = dump(result)
end
return result
end
function string:putRenVars()
local len, dx, names
len = self:len()
dx = 0
names = self:testOp("[%a_][%w_%.]*")
for _,pos in ipairs(names) do
local name = self:sub(dx + pos[1], dx + pos[2]):trim()
if name:isVar(ren.settings.var) then
self = self:sub(1, dx + pos[1] - 1) .. 'ren.settings.var.'..name .. self:sub(dx + pos[2] + 1)
dx = self:len() - len
end
end
return self
end
function string:execVars()
local len, dx, names
len = self:len()
dx = 0
names = self:testOp("[%a_][%w_%.]*")
for _,pos in ipairs(names) do
local name = self:sub(dx + pos[1], dx + pos[2]):trim()
if name:isVar(ren.settings.var) then
self = self:sub(1, dx + pos[1] - 1) .. cast(name:var2pointer(ren.settings.var)) .. self:sub(dx + pos[2] + 1)
dx = self:len() - len
end
end
return self
end
function string:execBrackets()
local len, dx, names
len = self:len()
dx = 0
names = self:testOp("[^%a_]%(([%%%+%-%*/%s%d%.eE]+)%)")
for _,pos in ipairs(names) do
self = self:sub(1, dx + pos[1] - 1) .. cast(self:sub(dx + pos[1], dx + pos[2]):eval()) .. self:sub(dx + pos[2] + 1)
dx = self:len() - len
end
return self
end
function string:execMath()
local len, dx, names
len = self:len()
dx = 0
names = self:testOp("[^%a%p]([%%%+%-%*/%s%d%.eE]+)[^%a%p]")
for _,pos in ipairs(names) do
self = self:sub(1, dx + pos[1] - 1) .. cast(self:sub(dx + pos[1], dx + pos[2]):eval()) .. self:sub(dx + pos[2] + 1)
dx = self:len() - len
end
return self
end
function string:execFunc()
local len, dx, names
len = self:len()
dx = 0
names = self:testOp("[%a_][%w_%.]*")
for _,pos in ipairs(names) do
local name = self:sub(dx + pos[1], dx + pos[2]):trim()
if self:sub(dx+pos[2]+1, dx+pos[2]+1) == "(" then
local pos3 = self:nextBracket(dx + pos[2] + 1)
self = self:sub(1, dx + pos[1] - 1) .. cast(name:var2pointer(ren.py)(self:sub(dx + pos[2] + 2, dx + pos3 - 1):uwordClear()) or '') .. self:sub(dx + pos3 + 1)
dx = self:len() - len
--elseif name ~= 'true' and name ~= 'false' then
--error2({"not var and not func?!", self, name})
end
end
return self
end
function string:expr_exec()
self = self:trim()
self = self:execVars() -- variable -> value
self = self:execBrackets() -- calculations in brackets
self = self:execMath() -- calculations
self = self:execFunc() -- func -> value
return self
end
function string:evalpy() -- $ lp_dv = 0
local str = self:sub(3) -- delete $
local op = str:match("[%a_][%w_%.%[%]\"'%s]- (=) .*")
if op ~= nil then
--do right expression
local tree = str:match("([%a_][%w_%.%[%]\"'%s]-) = .*"):key_tree()
local last = table.splice(tree, #tree, 1)[1]
local pointer = getPointer(ren.settings.var, tree)
pointer[last] = str:match("[%a_][%w_%.%[%]\"'%s]- = (.*)"):expr_exec():eval()
else
str:expr_exec()
end
end
function string:nextBracket(pos)
local counter = 0
for i = pos, #self do
if self[i] == "(" then
counter = counter + 1
elseif self[i] == ")" then
counter = counter - 1
if counter == 0 then
return pos
end
end
end
return #self
end
function string:func2obj()
return self:uwordClear():match("%(.*%)"):gsub("[%[%(]","{"):gsub("[%]%)]","}"):eval()
end