-
Notifications
You must be signed in to change notification settings - Fork 6
/
interpreter.lua
317 lines (281 loc) · 8.06 KB
/
interpreter.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
--[[
Signs Bot
=========
Copyright (C) 2019-2021 Joachim Stolberg
GPL v3
See LICENSE.txt for more information
Signs Bot: Command interpreter
]]--
-- Load support for I18n.
local S = signs_bot.S
local MAX_SIZE = 1000 -- max number of tokens
local tCmdDef = {}
local lCmdLookup = {}
local tSymbolTbl = {}
local CodeCache = {}
local api = {}
-- Possible command results
api.BUSY = 1 -- execute the same command again
api.DONE = 2 -- next command
api.NEW = 3 -- switch to a new script, provided as second value
api.ERROR = 4 -- stop execution with error, error message provided as second value
api.EXIT = 5 -- stop execution
-------------------------------------------------------------------------------
-- Compiler
-------------------------------------------------------------------------------
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local function get_line_tokens(script)
local idx = 0
script = script or ""
script = script:gsub("\r\n", "\n")
script = script:gsub("\r", "\n")
local lines = string.split(script, "\n", true)
return function()
while idx < #lines do
idx = idx + 1
-- remove comments
local line = string.split(lines[idx], "--", true, 1)[1] or ""
-- remove blanks
line = trim(line)
if #line > 0 then
-- split into tokens
return idx, unpack(string.split(line, " "))
end
end
end
end
local function dbg_out(opcode, num_param, code, pc)
if num_param == 0 then
print(">>"..lCmdLookup[opcode][3])
elseif num_param == 1 then
print(">>"..lCmdLookup[opcode][3].." "..code[pc+1])
elseif num_param == 2 then
print(">>"..lCmdLookup[opcode][3].." "..code[pc+1].." "..code[pc+2])
else
print(">>"..lCmdLookup[opcode][3].." "..code[pc+1].." "..code[pc+2].." "..code[pc+3])
end
end
local function tokenizer(script)
local tokens = {}
for _, cmnd, param1, param2, param3 in get_line_tokens(script) do
if tCmdDef[cmnd] then
local num_param = tCmdDef[cmnd].num_param
tokens[#tokens + 1] = cmnd
if num_param >= 1 then
tokens[#tokens + 1] = param1 or "nil"
end
if num_param >= 2 then
tokens[#tokens + 1] = param2 or "nil"
end
if num_param >= 3 then
tokens[#tokens + 1] = param3 or "nil"
end
elseif cmnd:find("%w+:$") then
tokens[#tokens + 1] = cmnd
end
end
tokens[#tokens + 1] = "exit"
return tokens
end
local function pass1(tokens)
local pc = 1
tSymbolTbl = {}
for _, token in ipairs(tokens) do
if token:find("%w+:$") then
tSymbolTbl[token] = pc
else
pc = pc + 1
end
end
end
local function pass2(tokens)
local code = {}
local num_param = 0
for _, token in ipairs(tokens) do
if num_param > 0 then
code[#code + 1] = tonumber(token) or tSymbolTbl[token..":"] or token
num_param = num_param - 1
elseif tCmdDef[token] then
num_param = tCmdDef[token].num_param
code[#code + 1] = tCmdDef[token].opcode
end
end
return code
end
local function compile(script)
local tokens = tokenizer(script)
pass1(tokens)
return pass2(tokens)
end
local function gen_string_cmnd(code, pc, num_param, script)
local tokens = tokenizer(script)
pc = math.min(pc, #tokens)
if num_param == 0 then
return tokens[pc]
elseif num_param == 1 then
return tokens[pc] .. " " .. (tokens[pc+1] or "")
elseif num_param == 2 then
return tokens[pc] .. " " .. (tokens[pc+1] or "") .. " " .. (tokens[pc+2] or "")
else
return tokens[pc] .. " " .. (tokens[pc+1] or "") .. " " .. (tokens[pc+2] or "") .. " " .. (tokens[pc+3] or "")
end
end
-------------------------------------------------------------------------------
-- Commands
-------------------------------------------------------------------------------
local function register_command(cmnd_name, num_param, cmnd_func, check_func)
assert(num_param, cmnd_name..": num_param = "..dump(num_param))
assert(cmnd_func, cmnd_name..": cmnd_func = "..dump(cmnd_func))
assert(check_func or num_param == 0, cmnd_name..": check_func = "..dump(check_func))
lCmdLookup[#lCmdLookup + 1] = {num_param, cmnd_func, cmnd_name}
tCmdDef[cmnd_name] = {
num_param = num_param,
cmnd = cmnd_func,
name = cmnd_name,
check = check_func,
opcode = #lCmdLookup,
}
end
register_command("repeat", 1,
function(base_pos, mem, cnt)
mem.Stack[#mem.Stack + 1] = tonumber(cnt)
mem.Stack[#mem.Stack + 1] = mem.pc + 1
return api.DONE
end,
function(cnt)
cnt = tonumber(cnt) or 0
return cnt > 0 and cnt < 1000
end
)
register_command("end", 0,
function(base_pos, mem)
if #mem.Stack < 2 then
return api.ERROR
end
mem.Stack[#mem.Stack - 1] = mem.Stack[#mem.Stack - 1] - 1
if mem.Stack[#mem.Stack - 1] > 0 then
mem.pc = mem.Stack[#mem.Stack]
else
mem.Stack[#mem.Stack] = nil
mem.Stack[#mem.Stack] = nil
end
return api.DONE
end
)
register_command("call", 1,
function(base_pos, mem, addr)
if #mem.Stack > 99 then
return api.ERROR, "call stack overrun"
end
mem.Stack[#mem.Stack + 1] = mem.pc + 2
mem.pc = addr - 2
return api.DONE
end,
function(addr)
return addr and tSymbolTbl[addr..":"]
end
)
register_command("return", 0,
function(base_pos, mem)
if #mem.Stack < 1 then
return api.ERROR, "no return address"
end
mem.pc = (mem.Stack[#mem.Stack] or 1) - 1
mem.Stack[#mem.Stack] = nil
return api.DONE
end
)
register_command("jump", 1,
function(base_pos, mem, addr)
mem.pc = addr - 2
return api.DONE
end,
function(addr)
return addr and tSymbolTbl[addr..":"]
end
)
register_command("exit", 0,
function(base_pos, mem)
return api.EXIT
end
)
-------------------------------------------------------------------------------
-- API functions
-------------------------------------------------------------------------------
function api.register_command(cmnd_name, num_param, cmnd_func, check_func)
register_command(cmnd_name, num_param, cmnd_func, check_func)
end
function api.check_label(label)
return label and tSymbolTbl[label..":"] ~= nil
end
-- function returns: true/false, error_string, line-num
function api.check_script(script)
local tbl = {}
local num_token = 0
-- to fill the symbol table
local tokens = tokenizer(script)
pass1(tokens)
for idx, cmnd, param1, param2, param3 in get_line_tokens(script) do
if tCmdDef[cmnd] then
num_token = num_token + 1 + tCmdDef[cmnd].num_param
if num_token > MAX_SIZE then
return false, S("Maximum programm size exceeded"), idx
end
param1 = tonumber(param1) or param1
param2 = tonumber(param2) or param2
param3 = tonumber(param3) or param3
local num_param = (param1 and 1 or 0) + (param2 and 1 or 0) + (param3 and 1 or 0)
if tCmdDef[cmnd].num_param < num_param then
return false, S("Too many parameters"), idx
end
if tCmdDef[cmnd].num_param > 0 and not tCmdDef[cmnd].check(param1, param2, param3) then
return false, S("Parameter error"), idx
end
elseif not cmnd:find("%w+:$") then
return false, S("Command error"), idx
end
tbl[cmnd] = (tbl[cmnd] or 0) + 1
end
if (tbl["end"] or 0) > (tbl["repeat"] or 0) then
return false, S("'repeat' missing"), 0
elseif (tbl["end"] or 0) < (tbl["repeat"] or 0) then
return false, S("'end' missing"), 0
end
return true, S("Checked and approved"), 0
end
-- function returns: true/false, error-string
-- default_cmnd is used for the 'cond_move'
function api.run_script(base_pos, mem)
local hash = minetest.hash_node_position(base_pos)
CodeCache[hash] = CodeCache[hash] or compile(mem.script)
local code = CodeCache[hash]
mem.pc = mem.pc or 1
mem.Stack = mem.Stack or {}
local opcode = code[mem.pc]
if opcode then
local num_param, func = unpack(lCmdLookup[opcode])
--dbg_out(opcode, num_param, code, mem.pc)
local res, err = func(base_pos, mem, code[mem.pc+1], code[mem.pc+2], code[mem.pc+3])
if res == api.DONE then
mem.pc = mem.pc + 1 + num_param
elseif res == api.NEW then
CodeCache[hash] = compile(mem.script)
mem.pc = 1
mem.Stack = {}
elseif res == api.ERROR then
return res, err, gen_string_cmnd(code, mem.pc, num_param, mem.script)
end
return res, err, ""
end
return api.EXIT
end
function api.reset_script(base_pos, mem)
local hash = minetest.hash_node_position(base_pos)
CodeCache[hash] = nil
mem.pc = 1
mem.Stack = {}
mem.bot_falling = nil
end
return api