-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstandardbefehlssatz.lua
367 lines (331 loc) · 7.08 KB
/
standardbefehlssatz.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
local WNOA = "wrong number of arguments"
local UAT = "unsupported argument type"
local SE = "error with cmd-cmd executing: "
local STRL = "attempt on exceeding maximum string length"
local s
s = {
mov = function(params)
return true, params[2]
end,
xchg = function(params)
return true, params[2], params[1]
end,
getvar = function(params, faden)
local p = params[1]
if type(p) ~= "string" then
return false, UAT
end
p = faden.vars[p]
return true, {p, p ~= nil}
end,
copytable = function(params, thread)
if type(params[1]) ~= "string"
or type(params[2]) ~= "string" then
return false, "two strings expected"
end
local fromprefix = params[2] .. "."
local toprefix = params[1] .. "."
if fromprefix == toprefix then
return false, "origin and target table names must be different"
end
local tocopy = {}
local frmln = #fromprefix
for name in pairs(thread.vars) do
if name:sub(1, frmln) == fromprefix then
tocopy[#tocopy+1] = name:sub(frmln+1)
end
end
for i = 1,#tocopy do
local field = tocopy[i]
thread.vars[toprefix .. field] = thread.vars[fromprefix .. field]
end
return true
end,
add = function(params, faden)
local p1,p2 = unpack(params)
local t1 = type(p1)
if t1 ~= type(p2) then
return false, "different argument types"
end
if t1 == "number" then
return true, p1 + p2
end
if t1 == "boolean" then
for _,k in pairs(params) do
if k then
return true, true
end
end
return true, false
end
if t1 == "string" then
if #p1 + #p2 > faden.strlen_max then
return false, STRL
end
return true, p1 .. p2
end
return false, UAT
end,
sub = function(params)
local b = params[2]
b = b and tonumber(b)
local t2 = type(b)
if t2 ~= "number" then
return false, UAT
end
local t1 = type(params[1])
if t1 == "number" then
return true, params[1] - b
end
if t1 == "string" then
return true, params[1]:sub(b, params[3] and tonumber(params[3]))
end
end,
mul = function(params, faden)
local p1,p2 = unpack(params)
local t1 = type(p1)
local t2 = type(p2)
if t1 == "string"
and t2 == "number" then
if #p1 * p2 > faden.strlen_max then
return false, STRL
end
return true, p1:rep(p2)
end
if t1 ~= t2 then
return false, "different argument types"
end
if t1 == "number" then
return true, p1 * p2
end
if t1 == "boolean" then
for _,k in pairs(params) do
if not k then
return true, false
end
end
return true, p1 and p2 -- nil is tested in the first two params
end
return false, UAT
end,
div = function(params)
local p1,p2 = unpack(params)
if type(p1) ~= "number"
or type(p2) ~= "number" then
return false, UAT
end
return true, p1 / p2
end,
inc = function(params)
if type(params[1]) ~= "number" then
return false, UAT
end
return true, params[1] + 1
end,
dec = function(params)
if type(params[1]) ~= "number" then
return false, UAT
end
return true, params[1] - 1
end,
neg = function(params)
local v = params[1]
local t = type(v)
if t == "number" then
return true, -v
end
if t == "boolean" then
return true, not v
end
if t == "string" then
return true, v:rev() -- does string.rev exist?
end
return false, UAT
end,
inv = function(params)
local p = params[1]
if type(p) ~= "number" then
return false, UAT
end
return true, 1 / p
end,
mod = function(params)
local p1,p2 = unpack(params)
if type(p1) ~= "number"
or type(p2) ~= "number" then
return false, UAT
end
return true, p1 % p2
end,
jmp = function(params, faden)
if #params >= 2
and not params[2] then
return true
end
local p = params[1]
if type(p) ~= "number" then
return false, UAT
end
if p < 1
or p%1 ~= 0
or p > #faden.liste + 1 then
return false, "jump target out of range"
end
faden.ip = p-1
if not s.usleep({0}, faden) then
error(SE)
end
return true
end,
call = function(params, faden)
local subsucc,msg = s.push({faden.ip+1}, faden)
if not subsucc then
return false, SE .. msg
end
subsucc,msg = s.jmp(params, faden)
if not subsucc then
return false, SE .. msg
end
return true
end,
ret = function(_, faden)
local subsucc,msg = s.pop({true}, faden)
if not subsucc then
return false, SE .. msg
end
subsucc,msg = s.jmp(msg, faden)
if not subsucc then
return false, SE .. msg
end
return true
end,
push = function(params, faden)
local pc = #params
if pc == 0 then
return false, WNOA
end
for i = 1,pc do
faden.stack[faden.sp] = params[i]
faden.sp = faden.sp-1
end
if faden.sp < 0 then
return false, "stack overflow"
end
return true
end,
pop = function(params, faden)
local pc = #params
if pc == 0 then
return false, WNOA
end
local rt = {}
for i = 1,pc do
faden.sp = faden.sp+1
rt[i] = faden.stack[faden.sp]
end
if faden.sp > faden.sb then
return false, "stack underflow"
end
return true, rt
end,
equal = function(params)
return true, params[1] == params[2]
end,
less = function(params)
local p1,p2 = unpack(params)
local t1 = type(p1)
if t1 ~= type(p2) then
return false, "different argument types"
end
if t1 ~= "number"
and t1 ~= "string" then
return false, UAT
end
return true, p1 < p2
end,
greater = function(params)
return s.less{params[2], params[1]}
end,
usleep = function(params, faden)
local p = params[1]
if type(p) ~= "number" then
return false, UAT
end
faden.rebirth = minetest.get_us_time() + p
faden:stop()
return true
end,
sleep = function(params, faden)
local p = params[1]
if type(p) ~= "number" then
return false, UAT
end
local subsucc,msg = s.usleep({p * 1000000}, faden)
if not subsucc then
error(SE .. msg)
end
return true
end,
get_us_time = function()
return true, minetest.get_us_time()
end,
tostring = function(params)
return true, tostring(params[1])
end,
tonumber = function(params)
return true, tonumber(params[1])
end,
toboolean = function(params)
return true, params[1] and true or false
end,
print = function(params, faden)
for i = 1,#params do
params[i] = tostring(params[i])
end
faden.log = faden.log .. table.concat(params, "\t") .. "\n"
return true
end,
flush = function(params, faden)
return faden:flush(params)
end,
}
local so_math_fcts = {"sin", "asin", "cos", "acos", "tan", "atan", "exp", "log",
"abs", "sign", "floor", "ceil"}
for i = 1,#so_math_fcts do
i = so_math_fcts[i]
s[i] = function(params)
local p = params[1]
if type(p) ~= "number" then
return false, "this arithmetic function needs a number as argument"
end
return true, math[i](p)
end
end
local to_math_fcts = {"pow"}
for i = 1,#to_math_fcts do
i = to_math_fcts[i]
s[i] = function(params)
local p1,p2 = unpack(params)
if type(p1) ~= "number"
or type(p2) ~= "number" then
return false, "this arithmetic function needs 2 numbers as args"
end
return true, math[i](p1, p2)
end
end
local abbreviations = {
mov = ":=",
add = "+",
sub = "-",
mul = "*",
div = "/",
inc = "++",
dec = "--",
neg = "!",
less = "<",
equal = "==",
jmp = "@",
usleep = "...",
}
-- TODO set metatable
return s