From fbd42f89f607251adb572461f66c8dd56f7b624c Mon Sep 17 00:00:00 2001 From: ywang Date: Tue, 2 Feb 2021 19:49:02 +0100 Subject: Add command to view generated lua code; add unittests for repeating code --- advtrains/atc.lua | 14 +++++++++- advtrains/atcjit.lua | 57 +++++++++++++++++++++++------------------ advtrains/tests/atcjit_spec.lua | 15 +++++++++++ 3 files changed, 60 insertions(+), 26 deletions(-) diff --git a/advtrains/atc.lua b/advtrains/atc.lua index 9f909c5..0005093 100644 --- a/advtrains/atc.lua +++ b/advtrains/atc.lua @@ -193,7 +193,19 @@ function atc.execute_atc_command(id, train) end end - +minetest.register_chatcommand("at_get_lua",{ + params = "", + description = "Compile the given ATC command into Lua code and show the given code", + privs = {train_admin = true}, + func = function(name, params) + local f, s = advtrains.atcjit.compile(params) + if not f then + return false, s or ("Unknown compilation error") + else + return true, s + end + end, +}) --move table to desired place advtrains.atc=atc diff --git a/advtrains/atcjit.lua b/advtrains/atcjit.lua index 0d400ea..a7221e0 100644 --- a/advtrains/atcjit.lua +++ b/advtrains/atcjit.lua @@ -1,4 +1,5 @@ local aj_cache = {} +local aj_strout = {} local aj_tostring @@ -155,8 +156,9 @@ aj_tostring = function(cmd, pos, noreset) t[#t+1] = str pos = pos+len if endp then - if endp then - t[#t+1] = string.format("_c[#_c+1]=%q",string.sub(cmd, pos)) + local cont = string.sub(cmd, pos) + if not string.match(cont, "^%s*$") then + t[#t+1] = string.format("_c[#_c+1]=%q",cont) end break end @@ -165,32 +167,37 @@ aj_tostring = function(cmd, pos, noreset) end local function aj_compile(cmd) - if aj_cache[cmd] then - local x = aj_cache[cmd] - if type(x) == "function" then - return x + local c = aj_cache[cmd] + if c then + if type(c) == "function" then + return c, aj_strout[cmd] else - return nil, x + return nil, c end + else + local str, err = aj_tostring(cmd) + if not str then + aj_cache[cmd] = err + return nil, err + end + str = string.format([[return function(id, train) + local _c={} + local _w={} + %s + if _c[1] then train.atc_command=table.concat(_c) + else train.atc_command=nil end + return _w, nil + end]], str) + local f, e = loadstring(str) + if not f then + aj_cache[cmd] = e + return nil, e + end + f = f() + aj_cache[cmd] = f + aj_strout[cmd] = str + return f, str end - local str, err = aj_tostring(cmd) - if not str then - aj_cache[cmd] = err - return nil, err - end - local str = string.format([[return function(id,train) - local _c = {} - local _w = {} - %s - if _c[1] then train.atc_command=table.concat(_c) - else train.atc_command = nil end - return _w, nil - end]], str) - local f, e = loadstring(str) - if not f then return nil, e end - f = f() - aj_cache[cmd] = f - return f end local function aj_execute(id,train) diff --git a/advtrains/tests/atcjit_spec.lua b/advtrains/tests/atcjit_spec.lua index 8e2a8b7..6d8b3f4 100644 --- a/advtrains/tests/atcjit_spec.lua +++ b/advtrains/tests/atcjit_spec.lua @@ -154,3 +154,18 @@ describe("ATC track with invalid I condition", function() local t = { atc_command = "I?;" } thisatc("should report an error", t, {}, "Invalid I statement", t) end) + +describe("ATC track reusing existing code", function() + local t = { atc_command = " B12WB8WBBWOLD15ORD15OCD1RS10WSM", tarvelocity = 15 } + thisatc("should do the same thing as in the first test", t, {}, nil, { + atc_brake_target = 12, + atc_command = "B8WBBWOLD15ORD15OCD1RS10WSM", + atc_wait_finish = true, + tarvelocity = 12 + }) +end) + +describe("ATC track reusing malformed code", function() + local t = {atc_command = "I?;"} + thisatc("Should report the invalid I statement", t, {}, "Invalid I statement", t) +end) -- cgit v1.2.3