-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinterpreter.js
719 lines (663 loc) · 28.4 KB
/
interpreter.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
let grid = [];
let instructions = [];
let robot;
let memory;
let tags = [];
let tagIndex = [];
let logs = [];
let instruction_index;
let log_i;
let logTA;
let instTA;
let objective_sprite;
function mapFileSelected(file) {
grid = []; //Clearing map matrix
loadedImages = true;
let metadata = "";
let contents;
let i = 0;
if (file.type !== 'text') {
AddTetxtConsole('The selected file must be a text file.');
console.log('The selected file must be a text file.');
} else {
let metaRegex = new RegExp('[0-9]+, *[0-9]+');
let dimensions;
while (file.data[i] != '\n') {
metadata += file.data[i];
i++;
}
if (metaRegex.test(metadata)) {
dimensions = metadata.match(metaRegex);
dimensions = dimensions[0].split(',');
contents = file.data.substring(metadata.length + 1, file.data.length);
let str_i = 0;
for (i = 0; i < Number(dimensions[0]); i++) {
grid[i] = [];
for (let j = 0; j < Number(dimensions[1]); j++) {
grid[i][j] = contents[str_i];
if (contents[str_i] === '^' || contents[str_i] === '>' || contents[str_i] === 'v' || contents[str_i] === '<') {
let dir;
switch (contents[str_i]) {
case '^':
dir = 0;
break;
case '>':
dir = 1;
break;
case 'v':
dir = 2;
break;
case '<':
dir = 3;
break;
}
robot = new Robot(j, i, dir);
}
str_i++;
}
str_i++;
}
AddTetxtCode('');
} else {
AddTetxtConsole('The selected map file is not compatible.');
}
}
}
function drawRobot() {
let rep;
switch (robot.dir) {
case 'UP':
rep = '^';
break;
case 'RT':
rep = '>';
break;
case 'DN':
rep = 'v';
break;
case 'LT':
rep = '<';
break;
}
return rep;
}
function checkBoundaries() {
let obX, obY;
switch (robot.dir) {
case 'UP':
obX = robot.x_coor;
obY = robot.y_coor - 1;
break;
case 'RT':
obX = robot.x_coor + 1;
obY = robot.y_coor;
break;
case 'DN':
obX = robot.x_coor;
obY = robot.y_coor + 1;
break;
case 'LT':
obX = robot.x_coor - 1;
obY = robot.y_coor;
break;
}
if (obY < 0 || obY >= grid.length || obX < 0 || obX >= grid[0].length) {
return false;
}
return true;
}
function sensors(objective) {
let obX, obY;
switch (robot.dir) {
case 'UP':
obX = robot.x_coor;
obY = robot.y_coor - 1;
break;
case 'RT':
obX = robot.x_coor + 1;
obY = robot.y_coor;
break;
case 'DN':
obX = robot.x_coor;
obY = robot.y_coor + 1;
break;
case 'LT':
obX = robot.x_coor - 1;
obY = robot.y_coor;
break;
}
return grid[obY][obX] === objective ? 1 : 0;
}
function exec(instruction, lineno) {
let success = true;
if (instruction === "" || instruction[0] === '/'){
return { 'success': success, 'index': lineno };
}
instruction = instruction.replace(/\t/g, ' ');
let instData = instruction.split(' ');
instData = instData.filter((elem) => {
return elem !== "";
});
if (instData.length <= 4) {
if (instData[0].includes('.')) {
instData = instData.slice(1);
}
switch (instData[0].toLowerCase()) {
//Memory instructions
case 'sett':
if (instData.length < 4) {
success = memory.SetT(instData[1], instData[2]);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for SetT function with " + instData.length + " arguments.");
console.log("No match for SetT function with " + instData.length + " arguments.");
success = false;
}
break;
case 'copy':
if (instData.length < 4) {
success = memory.Copy(instData[1], instData[2]);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Copy function with " + instData.length + " arguments.");
console.log("No match for Copy function with " + instData.length + " arguments.");
success = false;
}
break;
case 'put':
if (instData.length < 3) {
success = memory.Put(instData[1]);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Put function with " + instData.length + " arguments.");
console.log("No match for Put function with " + instData.length + " arguments.");
success = false;
}
break;
case 'take':
if (instData.length < 3) {
success = memory.Take(instData[1]);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Take function with " + instData.length + " arguments.");
console.log("No match for Take function with " + instData.length + " arguments.");
success = false;
}
break;
//Arithmetic instructions
case 'sum':
if (instData.length < 4) {
success = memory.Sum(instData[1], instData[2]);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Sum function with " + instData.length + " arguments.");
console.log("No match for Sum function with " + instData.length + " arguments.");
success = false;
}
break;
case 'res':
if (instData.length < 4) {
success = memory.Res(instData[1], instData[2]);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Res function with " + instData.length + " arguments.");
console.log("No match for Res function with " + instData.length + " arguments.");
success = false;
}
break;
case 'mul':
if (instData.length < 4) {
success = memory.Mult(instData[1], instData[2]);
} else {
console.log("Runtime error in line " + lineno+1 + ": " + "No match for Mul function with " + instData.length + " arguments.");
success = false;
}
break;
case 'div':
if (instData.length < 4) {
success = memory.Div(instData[1], instData[2]);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Div function with " + instData.length + " arguments.");
console.log("No match for Div function with " + instData.length + " arguments.");
success = false;
}
break;
//Flow control instructions
case 'vaya':
if (instData.length < 3) {
lineno = tagIndex[tags.indexOf(instData[1])] - 1;
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Vaya function with " + instData.length + " arguments.");
console.log("No match for Vaya function with " + instData.length + " arguments.");
success = false;
}
break;
case 'comp':
if (instData.length < 4) {
let retTuple = memory.Comp(instData[1], instData[2]);
success = retTuple['success'];
memory.SetT('TF', retTuple['comp']);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Comp function with " + instData.length + " arguments.");
console.log("No match for Comp function with " + instData.length + " arguments.");
success = false;
}
break;
case 'vig':
if (instData.length < 3) {
if (memory.registers['TF'] === 0) {
lineno = tagIndex[tags.indexOf(instData[1])] - 1;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Vig function with " + instData.length + " arguments.");
console.log("No match for Vig function with " + instData.length + " arguments.");
success = false;
}
break;
case 'vnig':
if (instData.length < 3) {
if (memory.registers['TF'] !== 0) {
lineno = tagIndex[tags.indexOf(instData[1])] - 1;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Vnig function with " + instData.length + " arguments.");
console.log("No match for Vnig function with " + instData.length + " arguments.");
success = false;
}
break;
case 'vma':
if (instData.length < 3) {
if (memory.registers['TF'] === 1) {
lineno = tagIndex[tags.indexOf(instData[1])] - 1;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Vma function with " + instData.length + " arguments.");
console.log("No match for Vma function with " + instData.length + " arguments.");
success = false;
}
break;
case 'vmai':
if (instData.length < 3) {
if (memory.registers['TF'] >= 0) {
lineno = tagIndex[tags.indexOf(instData[1])] - 1;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Vmai function with " + instData.length + " arguments.");
console.log("No match for Vmai function with " + instData.length + " arguments.");
success = false;
}
break;
case 'vme':
if (instData.length < 3) {
if (memory.registers['TF'] === -1) {
lineno = tagIndex[tags.indexOf(instData[1])] - 1;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Vme function with " + instData.length + " arguments.");
console.log("No match for Vme function with " + instData.length + " arguments.");
success = false;
}
break;
case 'vmei':
if (instData.length < 3) {
if (memory.registers['TF'] <= 0) {
lineno = tagIndex[tags.indexOf(instData[1])] - 1;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Vmei function with " + instData.length + " arguments.");
console.log("No match for Vmei function with " + instData.length + " arguments.");
success = false;
}
break;
//Action instructions
case 'mov':
if (instData.length < 3) {
if (memory.getRegister(instData[1], false)) {
if (checkBoundaries() && sensors('O') !== 1) {
//grid[robot.y_coor][robot.x_coor] = '.';
robot.move(memory.registers[instData[1]]);
drawRobot();
} else {
AddTetxtConsole('Runtime error: the robot can\'t move to that position.');
console.log('Error: the robot can\'t move to that position.');
success = false;
}
} else if (!isNaN(instData[1])) {
if (checkBoundaries() && sensors('O') !== 1) {
//grid[robot.y_coor][robot.x_coor] = '.';
robot.move(Number(instData[1]));
drawRobot();
} else {
AddTetxtConsole('Runtime error: the robot can\'t move to that position.');
console.log('Error: the robot can\'t move to that position.');
success = false;
}
} else {
AddTetxtConsole('Invalid value.');
console.log('Invalid value.');
success = false;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Mov function with " + instData.length + " arguments.");
console.log("No match for Mov function with " + instData.length + " arguments.");
success = false;
}
break;
case 'gir':
if (instData.length < 3) {
if (memory.getRegister(instData[1], false)) {
robot.rotate(memory.registers[instData[1]]);
} else if (!isNaN(instData[1])) {
robot.rotate(Number(instData[1]));
} else {
AddTetxtConsole('Invalid value.');
console.log('Invalid value.');
success = false;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Gir instruction with " + instData.length + " arguments.");
console.log("No match for Gir instruction with " + instData.length + " arguments.");
success = false;
}
break;
case 'car':
if (instData.length < 2) {
if (checkBoundaries() && sensors('C')) {
robot.is_loaded = true;
switch (robot.dir) {
case 'UP':
grid[robot.y_coor - 1][robot.x_coor] = '.';
break;
case 'RT':
grid[robot.y_coor][robot.x_coor + 1] = '.';
break;
case 'DN':
grid[robot.y_coor + 1][robot.x_coor] = '.';
break;
case 'LT':
grid[robot.y_coor][robot.x_coor - 1] = '.';
break;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + 'There is no object to load.');
console.log('There is no object to load.');
success = false;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Car function with " + instData.length + " arguments.");
console.log("No match for Car function with " + instData.length + " arguments.");
success = false;
}
break;
case 'dcar':
if (instData.length < 2) {
if (checkBoundaries() && sensors('M')) {
switch (robot.dir) {
case 'UP':
grid[robot.y_coor - 1][robot.x_coor] = 'C';
break;
case 'RT':
grid[robot.y_coor][robot.x_coor + 1] = 'C';
break;
case 'DN':
grid[robot.y_coor + 1][robot.x_coor] = 'C';
break;
case 'LT':
grid[robot.y_coor][robot.x_coor - 1] = 'C';
break;
}
robot.deload();
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + 'Cannot unload robot in this place.');
console.log('Cannot unload robot in this place.');
success = false;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Dcar function with " + instData.length + " arguments.");
console.log("No match for Dcar function with " + instData.length + " arguments.");
success = false;
}
break;
//Sensor instructions
case 'obpX':
if (instData.length < 3) {
success = memory.SetT(instData[1], robot.x_coor);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No mathc for ObPX function with " + instData.length + " arguments.");
console.log("No mathc for ObPX function with " + instData.length + " arguments.");
success = false;
}
break;
case 'obpy':
if (instData.length < 3) {
success = memory.SetT(instData[1], robot.y_coor);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No mathc for ObPY function with " + instData.length + " arguments.");
console.log("No mathc for ObPY function with " + instData.length + " arguments.");
success = false;
}
break;
case 'obrt':
if (instData.length < 3) {
success = memory.SetT(instData[1], robot.dirs.indexOf(robot.dir) + 1);
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No mathc for ObRT function with " + instData.length + " arguments.");
console.log("No mathc for ObRT function with " + instData.length + " arguments.");
success = false;
}
break;
case 'obob':
if (instData.length < 3) {
if (checkBoundaries()) {
success = memory.SetT(instData[1], sensors('O'));
} else {
AddTetxtConsole("Checking out of bounds.");
console.log("Checking out of bounds.");
//success = false;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for ObOb function with " + instData.length + " arguments.");
console.log("No match for ObOb function with " + instData.length + " arguments.");
success = false;
}
break;
case 'obme':
if (instData.length < 3) {
if (checkBoundaries()) {
success = memory.SetT(instData[1], sensors('C'));
} else {
AddTetxtConsole("Checking out of bounds.");
console.log("Checking out of bounds.");
//success = false;
}
} else {
AddTetxtConsole();
console.log("Runtime error in line " + lineno+1 + ": " + "No match for ObMe function with " + instData.length + " arguments.");
success = false;
}
break;
case 'obds':
if (instData.length < 3) {
if (checkBoundaries()) {
success = memory.SetT(instData[1], sensors('M'));
} else {
AddTetxtConsole("Checking out of bounds.");
console.log("Checking out of bounds.");
//success = false;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for ObMe function with " + instData.length + " arguments.");
console.log("No match for ObMe function with " + instData.length + " arguments.");
success = false;
}
break;
case 'obcr':
success = memory.SetT(instData[1], robot.is_loaded ? 1 : 0);
break;
//Outputs
case 'log':
if (instData.length < 3) {
if (memory.getRegister(instData[1], false)) {
AddTetxtConsole("Log: " + memory.registers[instData[1]]);
} else if (!isNaN(instData[1])) {
AddTetxtConsole("Log: " + Number(instData[1]));
} else {
AddTetxtConsole('Invalid value.');
console.log('Invalid value.');
success = false;
}
} else {
AddTetxtConsole("Runtime error in line " + lineno+1 + ": " + "No match for Log instruction with " + instData.length + " arguments.");
console.log("No match for Gir instruction with " + instData.length + " arguments.");
success = false;
}
break;
default:
success = false;
AddTetxtConsole('Invalid token in line: ' + (lineno + 1));
console.log('Invalid token in line: ' + (lineno + 1));
AddTetxtConsole(instData[0] + ' is not an instruction.');
console.log(instData[0] + ' is not an instruction.');
break;
}
} else {
success = false;
AddTetxtConsole('Invalid token in line: ' + (lineno + 1));
console.log('Invalid token in line: ' + (lineno + 1));
}
AddTetxtCode(instruction);
return { 'success': success, 'index': lineno };
}
function wait(time) {
return new Promise(resolve => {
setTimeout(() => {
resolve();
}, time);
});
}
function checkState() {
let m_count = 0;
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
if (grid[i][j] === 'M') {
m_count++;
}
}
}
if (m_count > 0) {
AddTetxtConsole("Failed program: the robot wasn't able to put all the cargo boxes in their respective objectives.");
} else {
AddTetxtConsole("Success: all the cargo boxes are in their objectives.");
}
}
async function autorun() {
for (instruction_index; instruction_index < instructions.length; instruction_index++) {
if (instructions[instruction_index][0] !== '/') {
let retTuple = exec(instructions[instruction_index], instruction_index);
instruction_index = retTuple['index'];
if (!retTuple['success']) {
AddTetxtConsole("Stopping execution.");
console.log("Stopping execution.");
instruction_index = instructions.length;
}
}
await wait(0);
}
checkState();
}
function exec_next() {
if (instructions[instruction_index][0] !== '/') {
if (instruction_index < instructions.length) {
let retTuple = exec(instructions[instruction_index], instruction_index);
instruction_index = retTuple['index'];
if (!retTuple['success']) {
AddTetxtConsole("Stopping execution.");
console.log("Stopping execution.");
instruction_index = instructions.length;
}
instruction_index++;
}
}
}
function AddTetxtConsole(newline_added){
logTA.value +=newline_added+'\n' ;
}
function AddTetxtCode(linea_codigo) {
instTA.value = 'Current instruction:\n';
instTA.value += linea_codigo + '\n';
instTA.value += 'Robot direction: ' + drawRobot() + '\n';
instTA.value += 'Loaded: ' + (robot.is_loaded ? "true" : "false");
}
function srcFileSelected(file) {
if (file.type !== 'text') {
AddTetxtConsole('The selected file must be a text file.');
console.log('The selected file must be a text file.');
} else {
memory = new Memory();
tags = [];
tagIndex = [];
instruction_index = 0;
logTA.value = '';
instTA.value = '';
instructions = file.data.split('\n');
console.log(instructions);
for (let i = 0; i < instructions.length; i++) {
if (instructions[i].includes('.')) {
let tagName = instructions[i].slice(0, instructions[i].indexOf('.'));
if (tagName.length < 6) {
tags.push(tagName);
tagIndex.push(i);
} else {
AddTetxtConsole(tagName + ' is not a valid tag name.');
console.log(tagName + ' is not a valid tag name.');
return;
}
}
}
}
}
const gridScreen = (gs) => {
gs.w = (gs.displayWidth / 2) - 90;
gs.drawGrid = () => {
gs.background(0);
for (let i = 0; i < grid.length; i++) {
for (let j = 0; j < grid[0].length; j++) {
if (i === robot.y_coor && j === robot.x_coor) {
gs.fill('blue');
} else if (grid[i][j] === 'M') {
gs.fill('red');
} else if (grid[i][j] === 'C') {
gs.fill('yellow');
} else if (grid[i][j] == 'O') {
gs.fill('gray');
} else {
gs.fill(255);
}
gs.rect(j * gs.w / grid[0].length, i * gs.w / grid.length, gs.w / grid[0].length, gs.w / grid.length);
}
}
};
gs.preload = () => {
memory = new Memory();
};
gs.setup = () => {
logTA = document.getElementById('Console_Display');
instTA = document.getElementById('Code_Display');
logTA.value = '';
instTA.value = '';
gs.createCanvas(gs.windowWidth / 2, gs.windowHeight - 90);
//Creating the bottom buttons
let mapButton = gs.createElement('label', 'Load map file');
let mapSelect = gs.createFileInput(mapFileSelected);
mapButton.child(mapSelect);
mapSelect.hide();
let codeButton = gs.createElement('label', 'Load instruction file');
let codeSelect = gs.createFileInput(srcFileSelected);
codeSelect.style('background-color:')
codeButton.child(codeSelect);
codeSelect.hide();
let autoplayButton = gs.createElement('label', 'Autorun');
autoplayButton.mouseClicked(autorun);
let nextInstructionButton = gs.createElement('label', ' Next ');
nextInstructionButton.mouseClicked(exec_next);
};
gs.draw = () => {
if (grid.length > 0)
gs.drawGrid();
};
};
let gScreen = new p5(gridScreen, 'grid_div');