--[[
---------------------------------------------------------------------------
Remote Train Control Program for Windows

© Copyright 2020 by Mark DiVecchio

	This file is part of Remote Train Control.

	Remote Train Control is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	Remote Train Control is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with Remote Train Control.  If not, see <http://www.gnu.org/licenses/>.

 Home Page : http://www.silogic.com/trains/RTC_Running.html
---------------------------------------------------------------------------]]
-- Semicolons are not required in Lua code
--[[---------------------------------------------------------------------------------------]]
-- Comments start with two dashes
--[[
Mulitline comments
start with two dashes and two left brackets.
They end with two right brackets. ]]

title = "Closure Test"
require("defines");

local f, g;

--[[---------------------------------------------------------------------------------------]]
function createclosure(value)
local a = value;
local function h()
	print("value = " .. a);
	a = a + 1;
	end;
return h;
end;

--[[---------------------------------------------------------------------------------------]]
function createclosure1(value)
local a = value;
local saveflag;
local function h(flag)
	saveflag = flag;
	print("value = " .. a);
	a = a - 1;
	if (flag == true) then
		print ("true");
		elseif (saveflag == false) then
			print ("false");
			end;
	end;
return h;
end;
--[[---------------------------------------------------------------------------------------]]
function setup(Engine, TIU)
--
print("setup : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());

f = createclosure(1);
g = createclosure1(100);

			-- printed "value = "
f();		-- 1
g(true);	-- 100, true
f();		-- 2
g(false);	-- 99, false
g(nil);		-- 98

return Stop("Done");
end
--[[---------------------------------------------------------------------------------------]]
function loop(Engine, TIU)
--
print("loop : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());

Sleep(10);
return true;	-- false=do not loop, true=continue to loop
end
--[[---------------------------------------------------------------------------------------]]
function cleanup(Engine, TIU)
--
print("cleanup : TIU " .. TIU .. " Engine " .. Engine .. " Debug " .. Debug());

return true;
end
--[[---------------------------------------------------------------------------------------]]
