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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
|
package bjc.utils.ioutils.format.directives;
import java.io.*;
import java.util.*;
import bjc.esodata.*;
import bjc.utils.ioutils.format.*;
/**
* Implements the { directive.
*
* @author student
*
*/
public class IterationDirective implements Directive {
@Override
public Edict compile(CompileContext compCTX) {
IterationEdict.Mode mode;
List<SimpleDecree> body = new ArrayList<>();
Iterator<SimpleDecree> dirIter = compCTX.directives;
// :GroupDecree
while (dirIter.hasNext()) {
SimpleDecree decr = dirIter.next();
if (decr.isLiteral) {
body.add(decr);
} else {
String dirName = decr.name;
if (dirName != null) {
if (dirName.equals("}")) break;
else body.add(decr);
}
}
}
CLParameters params = compCTX.decr.parameters;
CLModifiers mods = compCTX.decr.modifiers;
CLValue maxItr = CLValue.nil();
if (params.length() > 0) {
params.mapIndices("maxitr");
maxItr = params.resolveKey("maxitr");
}
if (mods.atMod && mods.colonMod) {
mode = IterationEdict.Mode.ALL_SUBLISTS;
} else if (mods.atMod) {
mode = IterationEdict.Mode.ALL;
} else if (mods.colonMod) {
mode = IterationEdict.Mode.SUBLIST;
} else {
mode = IterationEdict.Mode.NORMAL;
}
return new IterationEdict(mode, body, compCTX.formatter, maxItr);
}
}
class IterationEdict implements Edict {
private static final char DIR_NAME = '{';
public static enum Mode {
ALL_SUBLISTS, ALL, SUBLIST, NORMAL
}
private Mode mode;
private CLString body;
private CLFormatter fmt;
private CLValue maxItrVal;
public IterationEdict(Mode mode, List<SimpleDecree> body, CLFormatter fmt, CLValue maxItr) {
this.mode = mode;
this.body = new CLString(fmt.compile(body));
this.fmt = fmt;
this.maxItrVal = maxItr;
}
@Override
public void format(FormatContext formCTX) throws IOException {
int maxIterations = maxItrVal.asInt(formCTX.items, "maximum iterations", "{",
Integer.MAX_VALUE);
int numIterations = 0;
Object iter = formCTX.items.item();
boolean usingString = false;
String currBody = null;
if (body.isEmpty()) {
/* Grab an argument. */
if (!(iter instanceof String)) {
throw new IllegalFormatConversionException('{', String.class);
}
usingString = true;
currBody = (String) iter;
if (!formCTX.items.right()) {
throw new IllegalArgumentException(
"Not enough parameters to '{' directive");
}
iter = formCTX.items.item();
}
switch (mode) {
case ALL_SUBLISTS:
try {
do {
if (numIterations > maxIterations) break;
numIterations += 1;
if (!(iter instanceof Iterable<?>)) {
throw new IllegalFormatConversionException(DIR_NAME,
iter.getClass());
}
@SuppressWarnings("unchecked")
Iterable<Object> nitr = (Iterable<Object>) iter;
Tape<Object> nParams = new SingleTape<>(nitr);
try {
if (usingString) {
// @Speed @Memory :DynamicFormatString
// For a speed/memory tradeoff here, we could be compiling
// strang into a
// CLString and then caching those compiled string. However,
// we aren't
// doing that now. -- ben, 12/17/19
fmt.doFormatString(currBody, formCTX.writer, nParams, false);
} else {
body.format(formCTX.writer, nParams);
}
} catch (DirectiveEscape eex) {
if (eex.endIteration) {
if (formCTX.items.atEnd()) throw eex;
}
}
formCTX.items.right();
iter = formCTX.items.item();
} while (formCTX.items.position() < formCTX.items.size());
} catch (DirectiveEscape eex) {
// Do nothing
}
break;
case ALL:
try {
while (!formCTX.items.atEnd()) {
if (numIterations > maxIterations) break;
numIterations += 1;
if (usingString) {
// :DynamicFormatString
fmt.doFormatString(currBody, formCTX.writer, formCTX.items, false);
} else {
body.format(formCTX);
}
}
} catch (DirectiveEscape eex) {
if (eex.endIteration)
throw new UnsupportedOperationException(
"Colon mod not allowed on escape marker without colon mod on iteration");
}
break;
case SUBLIST:
if (!(iter instanceof Iterable<?>)) {
throw new IllegalFormatConversionException(DIR_NAME, iter.getClass());
}
try {
@SuppressWarnings("unchecked")
Iterable<Object> itb = (Iterable<Object>) iter;
Iterator<Object> itr = itb.iterator();
while (itr.hasNext()) {
Object obj = itr.next();
if (numIterations > maxIterations) break;
numIterations += 1;
if (!(obj instanceof Iterable<?>)) {
throw new IllegalFormatConversionException(DIR_NAME,
obj.getClass());
}
@SuppressWarnings("unchecked")
Iterable<Object> nitr = (Iterable<Object>) obj;
Tape<Object> nParams = new SingleTape<>(nitr);
try {
if (usingString) {
// :DynamicString
fmt.doFormatString(currBody, formCTX.writer, nParams, false);
} else {
body.format(formCTX.writer, nParams);
}
} catch (DirectiveEscape eex) {
if (eex.endIteration && !itr.hasNext()) throw eex;
}
}
} catch (DirectiveEscape eex) {
// Do nothing
}
break;
case NORMAL:
if (!(iter instanceof Iterable<?>)) {
throw new IllegalFormatConversionException(DIR_NAME, iter.getClass());
}
try {
@SuppressWarnings("unchecked")
Iterable<Object> itr = (Iterable<Object>) iter;
Tape<Object> nParams = new SingleTape<>(itr);
while (!nParams.atEnd()) {
if (numIterations > maxIterations) break;
numIterations += 1;
if (usingString) {
// :DynamicString
fmt.doFormatString(currBody, formCTX.writer, nParams, false);
} else {
body.format(formCTX.writer, nParams);
}
}
} catch (DirectiveEscape eex) {
if (eex.endIteration)
throw new UnsupportedOperationException(
"Colon mod not allowed on escape marker without colon mod on iteration");
}
break;
default:
throw new IllegalArgumentException("Unimplemented iteration mode " + mode);
}
formCTX.items.right();
}
}
|