-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathenv_loop.lua
More file actions
150 lines (118 loc) · 2.9 KB
/
env_loop.lua
File metadata and controls
150 lines (118 loc) · 2.9 KB
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
--- Ephemeral environment-based loops.
-- Standard library imports --
local remove = table.remove
-- Modules --
local array = require("impl.array")
-- Imports --
local CallWithEnvironment = array.CallWithEnvironment
local CallWithEnvironment_Args = array.CallWithEnvironment_Args
-- Exports --
local M = {}
-- --
local EnvArgsStack = {}
--
local function GetEnvArgs (mode)
local env_args = remove(EnvArgsStack) or {}
env_args.mode, env_args.step = mode
return env_args
end
--
local function AuxEnvIter (env_args)
local inc, i1, ok = env_args.inc, env_args.step
if env_args.stop_loop then
env_args.stop_loop = nil
else
i1 = i1 + inc
if inc > 0 then
ok = i1 <= env_args.i2
else
ok = i2 >= env_args.i2
end
end
if ok then
env_args.step = i1
return env_args
else
EnvArgsStack[#EnvArgsStack + 1] = env_args
end
end
--
local function EnvIter (mode, i1, i2, inc)
inc = inc or 1
local env_args = GetEnvArgs(mode)
env_args.inc, env_args.step, env_args.i2 = inc, i1 - inc, i2
return AuxEnvIter, env_args
end
--
function M.Add (into)
for k, v in pairs{
--
EnvLoopFromTo = function(i1, i2, func, ...)
for args in EnvIter(nil, i1, i2) do
args.stop_loop = CallWithEnvironment_Args(func, args, ...) == "stop_loop"
end
end,
--
EnvLoopFromTo_Mode = function(i1, i2, mode, func, ...)
for args in EnvIter(mode, i1, i2) do
args.stop_loop = CallWithEnvironment_Args(func, args, ...) == "stop_loop"
end
end,
--
EnvLoopFromToStep = function(i1, i2, step, func, ...)
for args in EnvIter(nil, i1, i2, step) do
args.stop_loop = CallWithEnvironment_Args(func, args, ...) == "stop_loop"
end
end,
--
EnvLoopFromToStep_Mode = function(i1, i2, step, mode, func, ...)
for args in EnvIter(mode, i1, i2, step) do
args.stop_loop = CallWithEnvironment_Args(func, args, ...) == "stop_loop"
end
end,
--
EnvLoopN = function(n, func, ...)
for args in EnvIter(nil, 1, n) do
args.stop_loop = CallWithEnvironment_Args(func, args, ...) == "stop_loop"
end
end,
--
EnvLoopN_Mode = function(n, func, mode, ...)
for args in EnvIter(mode, 1, n) do
args.stop_loop = CallWithEnvironment_Args(func, args, ...) == "stop_loop"
end
end,
--
EnvLoopUntil = function(func, pred, ...)
repeat
CallWithEnvironment(func, ...)
until pred(...)
end,
--
EnvLoopUntil_Mode = function(func, pred, mode, ...)
local args = GetEnvArgs(mode)
repeat
CallWithEnvironment_Args(func, args, ...)
until pred(...)
EnvArgsStack[#EnvArgsStack + 1] = args
end,
--
EnvLoopWhile = function(func, pred, ...)
while pred(...) do
CallWithEnvironment(func, ...)
end
end,
--
EnvLoopWhile_Mode = function(func, pred, mode, ...)
local args = GetEnvArgs(mode)
while pred(...) do
CallWithEnvironment_Args(func, args, ...)
end
EnvArgsStack[#EnvArgsStack + 1] = args
end
} do
into[k] = v
end
end
-- Export the module.
return M