Add NanoBasic to TA3 Terminal
This commit is contained in:
parent
849112013f
commit
d7576fa6f6
@ -124,10 +124,9 @@ local function get_action(nvm, fields)
|
||||
if nvm.status == "" then
|
||||
nvm.status = "init"
|
||||
end
|
||||
print("get_action", nvm.status, dump(fields))
|
||||
--print("get_action", nvm.status, dump(fields))
|
||||
for _,key in ipairs(keys) do
|
||||
if fields[key] and Actions[nvm.status] and Actions[nvm.status][key] then
|
||||
print("get_action", nvm.status, key)
|
||||
return Actions[nvm.status][key]
|
||||
end
|
||||
end
|
||||
@ -393,21 +392,34 @@ minetest.register_node("techage:basic_terminal", {
|
||||
-- Register VM external/callback functions
|
||||
--
|
||||
local function get_num_param(pos, num_param)
|
||||
local payload3 = 0
|
||||
local payload2 = 0
|
||||
local payload1 = 0
|
||||
local cmnd, num, owner, own_num
|
||||
|
||||
if num_param == 4 then
|
||||
if num_param == 5 then
|
||||
payload3 = nanobasic.pop_num(pos) or 0
|
||||
if payload3 >= 0x8000000 then
|
||||
payload3 = payload3 - 0x100000000
|
||||
end
|
||||
end
|
||||
if num_param >= 4 then
|
||||
payload2 = nanobasic.pop_num(pos) or 0
|
||||
if payload2 >= 0x8000000 then
|
||||
payload2 = payload2 - 0x100000000
|
||||
end
|
||||
end
|
||||
if num_param >= 3 then
|
||||
payload1 = nanobasic.pop_num(pos) or 0
|
||||
if payload1 >= 0x8000000 then
|
||||
payload1 = payload1 - 0x100000000
|
||||
end
|
||||
end
|
||||
cmnd = nanobasic.pop_num(pos)
|
||||
num = nanobasic.pop_num(pos) or 0
|
||||
owner = M(pos):get_string("owner")
|
||||
own_num = M(pos):get_string("node_number")
|
||||
return owner, num, own_num, {payload1, payload2}
|
||||
return owner, num, own_num, {payload1, payload2, payload3}
|
||||
end
|
||||
|
||||
local function get_str_param(pos, num_param)
|
||||
@ -424,10 +436,11 @@ local function get_str_param(pos, num_param)
|
||||
return owner, num, own_num, payload1
|
||||
end
|
||||
|
||||
local function error_handling(pos, sts)
|
||||
local function error_handling(pos, num, sts)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
if sts > 0 and nvm.error_label_addr and nvm.error_label_addr > 0 then
|
||||
local err = ErrorStr[sts] or "unknown error"
|
||||
nanobasic.push_num(pos, num)
|
||||
nanobasic.push_str(pos, err)
|
||||
nanobasic.set_pc(pos, nvm.error_label_addr)
|
||||
end
|
||||
@ -470,10 +483,10 @@ register_ext_function("time", {}, nanobasic.NB_NUM, function(pos, nvm)
|
||||
return true
|
||||
end)
|
||||
|
||||
-- num: cmd(num: node_num, num: cmnd, any: pyld1, any: pyld2)
|
||||
register_ext_function("cmd", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_ANY, nanobasic.NB_ANY}, nanobasic.NB_NUM, function(pos, nvm)
|
||||
-- num: cmd(num: node_num, num: cmnd, any: pyld1, num: pyld2, num: pyld3)
|
||||
register_ext_function("cmd", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_ANY, nanobasic.NB_ANY, nanobasic.NB_ANY}, nanobasic.NB_NUM, function(pos, nvm)
|
||||
local num_param = nanobasic.stack_depth(pos)
|
||||
if num_param >= 2 and num_param <= 4 then
|
||||
if num_param >= 2 and num_param <= 5 then
|
||||
local cmnd = nanobasic.peek_num(pos, num_param - 1) or 0
|
||||
if cmnd < 64 then -- command with payload as number(s)
|
||||
local owner, num, own_num, payload = get_num_param(pos, num_param)
|
||||
@ -481,10 +494,10 @@ register_ext_function("cmd", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_A
|
||||
techage.counting_add(owner, 1)
|
||||
local sts, resp = techage.beduino_send_cmnd(own_num, num, cmnd, payload)
|
||||
nanobasic.push_num(pos, sts)
|
||||
error_handling(pos, sts)
|
||||
error_handling(pos, num, sts)
|
||||
else
|
||||
nanobasic.push_num(pos, 4)
|
||||
error_handling(pos, 4)
|
||||
error_handling(pos, num, 4)
|
||||
end
|
||||
elseif cmnd < 128 then -- command with payload as string
|
||||
local owner, num, own_num, payload = get_str_param(pos, num_param)
|
||||
@ -492,40 +505,39 @@ register_ext_function("cmd", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_A
|
||||
techage.counting_add(owner, 1)
|
||||
local sts, resp = techage.beduino_send_cmnd(own_num, num, cmnd, payload)
|
||||
nanobasic.push_num(pos, sts)
|
||||
error_handling(pos, sts)
|
||||
error_handling(pos, num, sts)
|
||||
else
|
||||
nanobasic.push_num(pos, 4)
|
||||
error_handling(pos, 4)
|
||||
error_handling(pos, num, 4)
|
||||
end
|
||||
else -- request with payload as number(s) and result as number
|
||||
local owner, num, own_num, payload = get_num_param(pos, num_param)
|
||||
if techage.not_protected(tostring(num), owner) then
|
||||
techage.counting_add(owner, 1)
|
||||
local sts, resp = techage.beduino_request_data(own_num, num, cmnd, payload)
|
||||
print("cmd resp", sts, dump(resp))
|
||||
if type(resp) == "table" then
|
||||
nanobasic.push_num(pos, resp[1] or 0)
|
||||
else
|
||||
nanobasic.push_num(pos, 5)
|
||||
sts = 5
|
||||
end
|
||||
error_handling(pos, sts)
|
||||
error_handling(pos, num, sts)
|
||||
else
|
||||
nanobasic.push_num(pos, 4)
|
||||
error_handling(pos, 4)
|
||||
error_handling(pos, num, 4)
|
||||
end
|
||||
end
|
||||
else
|
||||
nanobasic.push_num(pos, 6)
|
||||
error_handling(pos, 6)
|
||||
error_handling(pos, num, 6)
|
||||
end
|
||||
return true
|
||||
end)
|
||||
|
||||
-- str: cmd(num: node_num, num: cmnd, any: pyld1, any: pyld2)
|
||||
register_ext_function("cmd$", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_ANY, nanobasic.NB_ANY}, nanobasic.NB_STR, function(pos, nvm)
|
||||
-- str: cmd(num: node_num, num: cmnd, any: pyld1, any: pyld2, num: pyld3)
|
||||
register_ext_function("cmd$", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_ANY, nanobasic.NB_ANY, nanobasic.NB_ANY}, nanobasic.NB_STR, function(pos, nvm)
|
||||
local num_param = nanobasic.stack_depth(pos)
|
||||
if num_param >= 2 and num_param <= 4 then
|
||||
if num_param >= 2 and num_param <= 5 then
|
||||
local cmnd = nanobasic.peek_num(pos, num_param - 1) or 0
|
||||
if cmnd >= 128 then -- request with payload as number(s) and result as string
|
||||
local owner, num, own_num, payload = get_num_param(pos, num_param)
|
||||
@ -538,15 +550,15 @@ register_ext_function("cmd$", {nanobasic.NB_NUM, nanobasic.NB_NUM, nanobasic.NB_
|
||||
nanobasic.push_str(pos, "")
|
||||
sts = 5
|
||||
end
|
||||
error_handling(pos, sts)
|
||||
error_handling(pos, num, sts)
|
||||
else
|
||||
nanobasic.push_str(pos, "")
|
||||
error_handling(pos, 4)
|
||||
error_handling(pos, num, 4)
|
||||
end
|
||||
end
|
||||
else
|
||||
nanobasic.push_str(pos, "")
|
||||
error_handling(pos, 6)
|
||||
error_handling(pos, num, 6)
|
||||
end
|
||||
return true
|
||||
end)
|
||||
@ -619,7 +631,6 @@ end)
|
||||
-- str: iname(str: item_name)
|
||||
register_ext_function("iname$", {nanobasic.NB_STR}, nanobasic.NB_STR, function(pos, nvm)
|
||||
local item_name = nanobasic.pop_str(pos) or ""
|
||||
print("iname", item_name)
|
||||
local item = minetest.registered_items[item_name]
|
||||
if item and item.description then
|
||||
local s = minetest.get_translated_string("en", item.description)
|
||||
@ -641,6 +652,7 @@ register_action({"init", "stopped", "error", "break"}, "Edit", function(pos, nvm
|
||||
end)
|
||||
|
||||
register_action({"edit"}, "Save", function(pos, nvm, fields)
|
||||
nanobasic.clear_screen(pos)
|
||||
local code = sort_lines(pos, nvm, fields.code)
|
||||
if code == nil then
|
||||
nvm.status = "error"
|
||||
@ -654,6 +666,7 @@ register_action({"edit"}, "Save", function(pos, nvm, fields)
|
||||
end)
|
||||
|
||||
register_action({"edit"}, "Renum", function(pos, nvm, fields)
|
||||
nanobasic.clear_screen(pos)
|
||||
local code = sort_lines(pos, nvm, fields.code)
|
||||
if code == nil then
|
||||
nvm.status = "error"
|
||||
@ -663,7 +676,6 @@ register_action({"edit"}, "Renum", function(pos, nvm, fields)
|
||||
return nanobasic.get_screen_buffer(pos) or ""
|
||||
end
|
||||
code = renumber_lines(pos, nvm, code)
|
||||
print("Renum", code)
|
||||
M(pos):set_string("code", code)
|
||||
return code
|
||||
end)
|
||||
@ -686,7 +698,6 @@ register_action({"init", "edit", "stopped"}, "Run", function(pos, nvm, fields)
|
||||
nvm.input = ""
|
||||
nvm.variables = nanobasic.get_variable_list(pos)
|
||||
nvm.error_label_addr = nanobasic.get_label_address(pos, "65000") or 0
|
||||
--print("nvm.variables", dump(nvm.variables))
|
||||
minetest.get_node_timer(pos):start(0.2)
|
||||
return nanobasic.get_screen_buffer(pos) or ""
|
||||
else
|
||||
@ -721,11 +732,9 @@ register_action({"break"}, "Enter", function(pos, nvm, fields)
|
||||
if var_name == nil then
|
||||
var_name, arr_idx = s, "0"
|
||||
end
|
||||
print("fields.input:lower()", s, var_name, arr_idx)
|
||||
if nvm.variables[var_name] then
|
||||
arr_idx = tonumber(arr_idx)
|
||||
local var_type, var_idx = nvm.variables[var_name][1], nvm.variables[var_name][2]
|
||||
print("break / Enter", var_type, var_idx, arr_idx, dump(nvm.variables[var_name]))
|
||||
local val = nanobasic.read_variable(pos, var_type, var_idx, arr_idx)
|
||||
if var_type == nanobasic.NB_NUM then
|
||||
nanobasic.print(pos, string.format("%s = %u\n", var_name, val));
|
||||
@ -767,13 +776,11 @@ end)
|
||||
|
||||
register_action(States, "larger", function(pos, nvm, fields)
|
||||
nvm.trm_text_size = math.min((nvm.trm_text_size or 0) + 1, 8)
|
||||
print("larger", nvm.trm_text_size)
|
||||
return fields.code or nanobasic.get_screen_buffer(pos) or ""
|
||||
end)
|
||||
|
||||
register_action(States, "smaller", function(pos, nvm, fields)
|
||||
nvm.trm_text_size = math.max((nvm.trm_text_size or 0) - 1, -8)
|
||||
print("smaller", nvm.trm_text_size)
|
||||
return fields.code or nanobasic.get_screen_buffer(pos) or ""
|
||||
end)
|
||||
|
||||
@ -805,7 +812,6 @@ end)
|
||||
|
||||
techage.register_node({"techage:basic_terminal"}, {
|
||||
on_node_load = function(pos)
|
||||
print("register_lbm")
|
||||
nanobasic.vm_restore(pos)
|
||||
local nvm = techage.get_nvm(pos)
|
||||
if nvm.status == "running" then
|
||||
|
Loading…
Reference in New Issue
Block a user