aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorywang <yw05@forksworld.de>2021-02-02 19:49:02 +0100
committerywang <yw05@forksworld.de>2021-06-09 15:31:21 +0200
commitfbd42f89f607251adb572461f66c8dd56f7b624c (patch)
tree7f449ca067e618f5516077dacbe7a6897e588629
parentc12107d306aefa77a7f4c6aa5caf4ed431fe4f92 (diff)
downloadadvtrains-fbd42f89f607251adb572461f66c8dd56f7b624c.tar.gz
advtrains-fbd42f89f607251adb572461f66c8dd56f7b624c.tar.bz2
advtrains-fbd42f89f607251adb572461f66c8dd56f7b624c.zip
Add command to view generated lua code; add unittests for repeating code
-rw-r--r--advtrains/atc.lua14
-rw-r--r--advtrains/atcjit.lua57
-rw-r--r--advtrains/tests/atcjit_spec.lua15
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 = "<command>",
+ 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)