-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloader.js
425 lines (356 loc) · 8.78 KB
/
loader.js
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/**
Utilities for loading and saving a program and set of tapes in JSON format
The basic format is like this:
{
title: title-string,
desc: desc-string,
testCases: [test-case-description1, ..., test-case-description2],
program: { ... program-description ... },
}
tape-description:
A string of the characters R,B,G,Y in any combination or order
test-case-description:
A test vector for the user's program. Specified using a string with this format:
[a|r]:tape-description:tape-description[:cycle-limit]
1 2 3 4
1: Accept or reject
2: Input tape (can be empty)
3: Output tape (can be empty)
4: Max iterations as number (optional)
program-description:
{
cols: Number,
rows: Number,
cells: [ cell-description1, cell-description2 ],
start: {
x: Number,
y: Number,
orientation: orientation-description
},
end: {
x: Number,
y: Number,
orientation: orientation-description
}
}
cell-description:
{
type: type-description,
x: Number,
y: Number,
orientation: orientation-description
}
orientation-description:
One of the strings ID, ROT1, ROT2, ROT3, MIR, MROT1, MROT2, MROT3
type-description:
String specifying the type of the cell. Currently these are:
Conveyor
CrossConveyor
BranchBR
BranchGY
WriteB
WriteR
WriteG
WriteY
*/
import core from 'core';
import codeCell from 'codeCell';
import tmath from 'tmath';
import program from 'program';
function isTape(t) {
// Ensure tapeDesc only contains B,R,G,Y
const invalidChars = t.match(/[^RGBY]/);
if (invalidChars != null)
return false;
return true;
}
function isOrientation(o) {
const index = ['ID', 'ROT1', 'ROT2', 'ROT3', 'MIR', 'MROT1', 'MROT2', 'MROT3'].indexOf(o);
if (index == -1)
return false;
return true;
}
function isCellType(t) {
const validTypes = Object.keys(codeCell.codeCells);
const index = validTypes.indexOf(t);
if (index == -1) {
return false;
}
return true;
}
function isCoordinate(c) {
return !isNaN(c);
}
function hasAll(ob, required) {
const keys = Object.keys(ob);
return required.every(_.partial(_.contains, keys, _));
}
function isCellDesc(cellDesc) {
if (!hasAll(cellDesc, ['type', 'x', 'y', 'orientation'])) {
return false;
}
return allTrue([
isCellType(cellDesc.type),
isOrientation(cellDesc.orientation),
isCoordinate(cellDesc.x),
isCoordinate(cellDesc.y)
]);
}
function isEndpoint(e) {
if (!hasAll(e, ['orientation', 'x', 'y'])) {
return false;
}
return allTrue([
isOrientation(e.orientation),
isCoordinate(e.x),
isCoordinate(e.y)
]);
}
function isWithinBounds(MAX_X, MAX_Y) {
return function(cell) {
return (cell.x >= 0 && cell.x <= MAX_X && cell.y >= 0 && cell.y <= MAX_Y);
};
}
function allTrue(l) {
return l.every(function(p) {
return Boolean(p);
});
}
function isProgram(p) {
if (!hasAll(p, ['start', 'end', 'cols', 'rows', 'cells'])) {
return false;
}
const basic = allTrue([
isCoordinate(p.cols),
isCoordinate(p.rows),
p.cells.every(isCellDesc),
isEndpoint(p.start),
isEndpoint(p.end)
]);
const bounds = isWithinBounds(p.cols - 1, p.rows - 1);
return basic && p.cells.every(bounds) && bounds(p.start) && bounds(p.end);
}
function isValid(level) {
if (!hasAll(level, ['title', 'testCases', 'program'])) {
return false;
}
return allTrue([
level.testCases.every(isTestVector),
isProgram(level.program)
]);
}
function orientationToJson(o) {
const mat = tmath.Mat2x2;
if (_.isEqual(o, mat.kID))
return 'ID';
else if (_.isEqual(o, mat.kROT1))
return 'ROT1';
else if (_.isEqual(o, mat.kROT2))
return 'ROT2';
else if (_.isEqual(o, mat.kROT3))
return 'ROT3';
else if (_.isEqual(o, mat.kMIR))
return 'MIR';
else if (_.isEqual(o, mat.kMROT1))
return 'MROT1';
else if (_.isEqual(o, mat.kMROT2))
return 'MROT2';
else if (_.isEqual(o, mat.kMROT3))
return 'MROT3';
else
return 'INVALID';
}
function jsonToOrientation(json) {
const mat = tmath.Mat2x2;
switch (json) {
case 'ID':
return mat.kID;
case 'ROT1':
return mat.kROT1;
case 'ROT2':
return mat.kROT2;
case 'ROT3':
return mat.kROT3;
case 'MIR':
return mat.kMIR;
case 'MROT1':
return mat.kROT1;
case 'MROT2':
return mat.kROT2;
case 'MROT3':
return mat.kROT3;
default:
return null;
}
}
function programToJson(p) {
const json = {
cols: p.cols,
rows: p.rows,
cells: [],
start: null,
end: null
};
p.cells.forEach(function(column, x) {
column.forEach(function(cell, y) {
if (cell.type != 'Empty') {
const ob = {x:x, y:y, orientation: orientationToJson(cell.orientation)};
if (cell.type == 'Start')
json.start = ob;
else if (cell.type == 'End')
json.end = ob;
else {
ob.type = cell.type;
json.cells.push(ob);
}
}
});
});
return json;
}
function jsonToProgram(json) {
const p = new program.Program(parseInt(json.cols), parseInt(json.rows));
json.cells.forEach(function(cell) {
p.setCell(cell.x, cell.y, cell.type, jsonToOrientation(cell.orientation));
});
p.setStart(
json.start.x,
json.start.y,
jsonToOrientation(json.end.orientation)
);
p.setEnd(
json.end.x,
json.end.y,
jsonToOrientation(json.end.orientation)
);
return p;
}
function tapeToJson(t) {
return t.symbols.reduce(
function(prev, cur) {
let end = '';
if (cur == core.RED)
end = 'R';
if (cur == core.BLUE)
end = 'B';
if (cur == core.GREEN)
end = 'G';
if (cur == core.YELLOW)
end = 'Y';
return prev + end;
},
''
);
}
function jsonToTape(json) {
const t = new core.Tape();
Array.prototype.forEach.call(json, function(letter) {
t.append(core.symbols[letter]);
});
return t;
}
/**
Validate test vector string
*/
function isTestVector(json) {
const parts = json.split(':');
if (parts.length < 3) {
console.log('ERROR: test vector string does not contain all required parts');
return false;
}
if (parts.length == 3) {
parts[3] = 0; // fill in optional field with default value
}
return allTrue([
parts[0].match(/^[ar]$/),
isTape(parts[1]),
isTape(parts[2]),
!isNaN(parseInt(parts[3]))
]);
}
/**
Convert test vector object to string
*/
function testVectorToJson(ob) {
return [
ob.accept ? 'a' : 'r',
tapeToJson(ob.input),
tapeToJson(ob.output),
ob.limit
].join(':');
}
/**
Parse test vector string to object
*/
function jsonToTestVector(json) {
const parts = json.split(':'),
accept = parts[0] == 'a' ? true : false,
input = parts[1],
output = parts[2],
limit = parts.length > 3 ? parseInt(parts[3]) : 0;
return {
accept: accept,
input: jsonToTape(input),
output: jsonToTape(output),
limit: isNaN(limit) ? 0 : limit
};
}
function levelToJson(title, testCases, prog) {
const json = {
title: title,
testCases: (_.isArray(testCases) ? testCases : [testCases]).map(testVectorToJson),
program: programToJson(prog)
};
return json;
}
function jsonToLevel(json) {
const level = {
title: json.title,
testCases: json.testCases.map(jsonToTestVector),
program: jsonToProgram(json.program)
};
return level;
}
function fromJson(jsonString) {
try {
const dejsoned = JSON.parse(jsonString);
if (!isValid(dejsoned))
return null;
return jsonToLevel(dejsoned);
} catch (e) {
return null;
}
};
function toJson(title, tapes, prog) {
return JSON.stringify(levelToJson(title, tapes, prog));
};
export {
isTape,
isOrientation,
isCellType,
isCoordinate,
hasAll,
isCellDesc,
isEndpoint,
isWithinBounds,
allTrue,
isProgram,
isValid,
orientationToJson,
jsonToOrientation,
programToJson,
jsonToProgram,
tapeToJson,
jsonToTape,
isTestVector,
testVectorToJson,
jsonToTestVector,
levelToJson,
jsonToLevel,
fromJson,
toJson
};
export default {
fromJson,
toJson
};