aboutsummaryrefslogtreecommitdiff
path: root/advtrains/atcjit.lua
blob: a7221e0e4ccda1cbc47776157a4d71eed4e17c20 (plain)
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
local aj_cache = {}
local aj_strout = {}

local aj_tostring

local matchptn = {
	["A[01FT]"] = function(match)
		return string.format(
			"advtrains.interlocking.set_ars_disable(train,%s)",
			(match=="0" or match=="F") and "true" or "false"), false
	end,
	["BB"] = function()
		return [[do
			train.atc_brake_target = -1
			train.tarvelocity = 0
		end]], 2
	end,
	["B([0-9]+)"] = function(match)
		return string.format([[do
			train.atc_brake_target = %s
			if not train.tarvelocity or train.tarvelocity > train.atc_brake_target then
				train.tarvelocity = train.atc_brake_target
			end
		end]], match),#match+1
	end,
	["D([0-9]+)"] = function(match)
		return string.format("train.atc_delay = %s", match), #match+1, true
	end,
	["(%bI;)"] = function(match)
		local i = 2
		local l = #match
		local epos
		while (i<l) do
			local b, e, c = string.find(match,"([IE])",i)
			if c == "I" then
				b, e = string.find(match,"%bI;", b)
				-- This is unlikely to happen since the %b pattern is balanced
				if not (b and e) then return nil, "Malformed nested I statement" end
				i = e+1
			else
				epos = b
				break
			end
		end
		local endp = string.find(match,"[WDI]") and true
		local cond = string.match(match,"^I([+-])")
		if cond then
			local vtrue = string.sub(match, 3, epos and epos-1 or -2)
			local vfalse = epos and string.sub(match, epos+1, -2)
			local cstr = (cond == "-") and "not" or ""
			local tstr, err = aj_tostring(vtrue, 1, true)
			if not tstr then return nil, err end
			if vfalse then
				local fstr, err = aj_tostring(vfalse, 1, true)
				if not fstr then return nil, err end
				return string.format("if %s train.atc_arrow then %s else %s end",
					cstr, tstr, fstr), l, endp
			else
				return string.format("if %s train.atc_arrow then %s end", cstr, tstr), l, endp
			end
		else
			local op, ref = string.match(match,"^I([<>]=?)([0-9]+)")
			if not op then
				return _, "Invalid I statement"
			end
			local spos = 2+#op+#ref
			local vtrue = string.sub(match, spos, epos and epos-1 or -2)
			local vfalse = epos and string.sub(match, epos+1, -2)
			local cstr = string.format("train.velocity %s %s", op, ref)
			local tstr = aj_tostring(vtrue, 1, true)
			if vfalse then
				local fstr, err = aj_tostring(vfalse, 1, true)
				if not fstr then return nil, err end
				return string.format("if %s then %s else %s end", cstr, tstr, fstr), l, endp
			else
				return string.format("if %s then %s end", cstr, tstr), l, endp
			end
		end
	end,
	["K"] = function()
		return [=[do
			if train.door_open == 0 then
				_w[#_w+1] = attrans("ATC Kick command warning: Doors are closed")
			elseif train.velocity>0 then
				_w[#_w+1] = attrans("ATC Kick command warning: Train moving")
			else
				local tp = train.trainparts
				for i = 1, #tp do
					local data = advtrains.wagons[tp[i]]
					local obj = advtrains.wagon_objects[tp[i]]
					if data and obj then
						local ent = obj:get_luaentity()
						if ent then
							for seatno, seat in pairs(ent.seats) do
								if data.seatp[seatno] and not ent:is_driver_stand(seat) then
									ent:get_off(seatno)
								end
							end
						end
					end
				end
			end
		end]=], 1
	end,
	["O([LR])"] = function(match)
		local tt = {L = -1, R = 1}
		return string.format("train.door_open = %d*(train.atc_arrow and 1 or -1)",tt[match]), 2
	end,
	["OC"] = function(match)
		return "train.door_open = 0", 2
	end,
	["R"] = function()
		return [[
			if train.velocity<=0 then
				advtrains.invert_train(id)
				advtrains.train_ensure_init(id, train)
			else
				_w[#_w+1] = attrans("ATC Reverse command warning: didn't reverse train, train moving!")
			end]], 1
	end,
	["SM"] = function()
		return "train.tarvelocity=train.max_speed", 2
	end,
	["S([0-9]+)"] = function(match)
		return string.format("train.tarvelocity=%s",match), #match+1
	end,
	["W"] = function()
		return "train.atc_wait_finish=true", 1, true
	end,
}

local function aj_tostring_single(cmd, pos)
	if not pos then pos = 1 end
	for pattern, func in pairs(matchptn) do
		local match = {string.match(cmd, "^"..pattern, pos)}
		if match[1] then
			return func(unpack(match))
		end
	end
	return nil
end

aj_tostring = function(cmd, pos, noreset)
	if not pos then pos = 1 end
	local t = {}
	local endp = false
	while pos <= #cmd do
		if string.match(cmd,"^%s+$", pos) then break end
		local _, e = string.find(cmd, "^%s+", pos)
		if e then pos = e+1 end
		local str, len
		str, len, endp = aj_tostring_single(cmd, pos)
		if not str then
			return nil, (len or "Invalid command or malformed I statement: "..string.sub(cmd,pos))
		end
		t[#t+1] = str
		pos = pos+len
		if endp then
			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
	end
	return table.concat(t,"\n"), pos
end

local function aj_compile(cmd)
	local c = aj_cache[cmd]
	if c then
		if type(c) == "function" then
			return c, aj_strout[cmd]
		else
			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
end

local function aj_execute(id,train)
	if not train.atc_command then return end
	local func, err = aj_compile(train.atc_command)
	if func then return func(id,train) end
	return nil, err
end

return {
	compile = aj_compile,
	execute = aj_execute
}