-
Notifications
You must be signed in to change notification settings - Fork 8
/
basic.js
1200 lines (892 loc) · 33 KB
/
basic.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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import * as canvas from "./canvas.js";
import * as term from "./term.js";
import * as hid from "./hid.js";
import * as audio from "./audio.js";
import * as syntax from "./syntax.js";
import * as commands from "./commands.js";
import * as extensions from "./extensions.js";
export const MAX_STACK_SIZE = 100;
export const MAX_RENDER_HOLD_TIME = 10;
export const MAX_RENDER_CALL_DEPTH = 100;
export const trigModes = {
DEGREES: 0,
RADIANS: 1,
GRADIANS: 2,
TURNS: 3
};
export var fileExporter;
export var fileImporter;
export var editingProgram = [];
export var parsedProgram = [];
export var programLabels = {};
export var programVariables = {};
export var programStack = [];
export var running = false;
export var currentPosition = 0;
export var renderHoldTime = 0;
export var renderCallDepth = 0;
export var trigMode = trigModes.DEGREES;
export var graphicsX = 0;
export var graphicsY = 0;
export var graphicsStrokeWidth = 1;
export var graphicsPolygonPoints = [];
export var turtleHeading = Math.PI / 2;
export var turtlePenDown = true;
export var turtleShown = true;
export var turtleNotMoved = true;
export var lastConditionalState = null;
export var delayTimeout = null;
export var currentKey = "";
export class BasicError extends Error {
constructor(message, lineNumber) {
super(message);
this.lineNumber = lineNumber;
this.name = this.constructor.name;
}
}
export class ParsingSyntaxError extends BasicError {}
export class RuntimeError extends BasicError {}
export class Command {
constructor(callable, parameters = []) {
this.callable = callable;
this.parameters = parameters;
}
call() {
return this.callable(...this.parameters);
}
}
export class OpeningCommand extends Command {}
export class ClosingCommand extends Command {}
export function trigModeToRadians(value, mode = trigMode) {
if (mode == trigModes.RADIANS) {
return value;
}
if (mode == trigModes.DEGREES) {
return value * (Math.PI / 180);
}
if (mode == trigModes.GRADIANS) {
return value * (Math.PI / 200);
}
if (mode == trigModes.TURNS) {
return value * (2 * Math.PI);
}
}
export function radiansToTrigMode(value, mode = trigMode) {
if (mode == trigModes.RADIANS) {
return value;
}
if (mode == trigModes.DEGREES) {
return value / (Math.PI / 180);
}
if (mode == trigModes.GRADIANS) {
return value / (Math.PI / 200);
}
if (mode == trigModes.TURNS) {
return value / (2 * Math.PI);
}
}
export function setTrigMode(value) {
trigMode = value;
}
export function findLineNumberByPosition(position) {
return Number(Object.keys(programLabels).find((i) => programLabels[i] == position && !Number.isNaN(Number(programLabels[i]))));
}
function expectFactory(tokens) {
return function(i, ...expectations) {
for (var j = 0; j < expectations.length; j++) {
if (i + j >= tokens.length) {
throw new ParsingSyntaxError(`Unexpected end of program`, tokens[tokens.length - 1].lineNumber);
}
if (!expectations[j](tokens[i + j]) && tokens[i + j] instanceof syntax.StatementEnd) {
throw new ParsingSyntaxError(`Unexpected end of line`, tokens[i + j].lineNumber);
}
if (!expectations[j](tokens[i + j])) {
console.warn("Unexpected:", i, tokens[i]);
throw new ParsingSyntaxError(
tokens[i + j].code != null ? `Unexpected \`${tokens[i + j].code}\`` : `Unexpected token`,
tokens[i + j].lineNumber
);
}
}
}
}
function conditionFactory(tokens) {
return function(i, ...conditions) {
for (var j = 0; j < conditions.length; j++) {
if (!conditions[j](tokens[i + j])) {
return false;
}
}
return true;
}
}
export function parseProgram(program) {
var tokens = syntax.tokenise(program);
var additionalEnds = 0;
var repeatMode = false;
parsedProgram = [];
programLabels = {};
for (var i = 0; i < tokens.length; i++) {
var expect = expectFactory(tokens);
var condition = conditionFactory(tokens);
if (condition(i, (x) => x instanceof syntax.ExecutionLabel)) {
programLabels[tokens[i].code] = parsedProgram.length;
continue;
}
if (
condition(i, (x) => x instanceof syntax.Keyword && (
Object.keys(commands.keywords).includes(x.code.toLocaleLowerCase()) ||
x instanceof syntax.ExtensionKeyword
))
) { // Command
var keyword = tokens[i];
var commandName = tokens[i].code;
var parameters = [];
while (true) {
i++;
if (condition(i, (x) => x instanceof syntax.Expression)) {
parameters.push(tokens[i]);
expect(++i, (x) => x instanceof syntax.ParameterSeperator || x instanceof syntax.StatementEnd);
}
if (condition(i, (x) => x instanceof syntax.StatementEnd)) {
i--;
break;
}
}
if (keyword instanceof syntax.ExtensionKeyword) {
(function(commandName, i) {
var extensionName = commandName.split(".")[0];
parsedProgram.push(new Command(function() {
if (!extensions.hasLoaded(extensionName)) {
throw new RuntimeError(
`Extension \`${commandName.split(".")[0]}\` has not been loaded`,
tokens[i].lineNumber
);
}
if (!(extensions.getCommand(commandName) instanceof Function)) {
throw new RuntimeError(
`Extension \`${commandName.split(".")[0]}\` has no command \`${commandName.split(".")[1]}\``,
tokens[i].lineNumber
);
}
return extensions.getCommand(commandName)(...arguments);
}, parameters));
})(commandName, i);
} else {
parsedProgram.push(new Command(commands.keywords[commandName.toLocaleLowerCase()], parameters));
}
i++;
} else if (condition(i, (x) => x instanceof syntax.Expression && x.getPrimaryIdentifier() != null)) { // Assignment
expect(++i, (x) => x instanceof syntax.Assignment, (x) => x instanceof syntax.Expression);
parsedProgram.push(new Command(commands.assign, [tokens[i - 1], tokens[++i]]));
expect(++i, (x) => x instanceof syntax.StatementEnd);
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "if")) { // If
expect(++i, (x) => x instanceof syntax.Expression);
parsedProgram.push(new OpeningCommand(commands.ifCondition, [tokens[i]]));
expect(++i, (x) => x instanceof syntax.StatementEnd);
} else if (condition(i,
(x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "else",
(x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "if"
)) { // Else if
parsedProgram.push(new ClosingCommand(commands.genericEnd));
parsedProgram.push(new OpeningCommand(commands.elseCondition));
additionalEnds++;
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "else")) { // Else
parsedProgram.push(new ClosingCommand(commands.genericEnd));
parsedProgram.push(new OpeningCommand(commands.elseCondition));
expect(++i, (x) => x instanceof syntax.StatementEnd);
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "for")) { // For loop
var identifier = null;
var start = null;
var end = null;
var step = null;
expect(++i, (x) => x instanceof syntax.Expression && x.getPrimaryIdentifier() != null);
identifier = tokens[i];
expect(++i, (x) => x instanceof syntax.Assignment);
expect(++i, (x) => x instanceof syntax.Expression);
start = tokens[i];
expect(++i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "to");
expect(++i, (x) => x instanceof syntax.Expression);
end = tokens[i];
if (condition(++i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "step")) {
expect(++i, (x) => x instanceof syntax.Expression);
step = tokens[i++];
}
expect(i, (x) => x instanceof syntax.StatementEnd);
parsedProgram.push(new OpeningCommand(commands.forLoop, [identifier, start, end, step]));
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "repeat")) { // Repeat loop
parsedProgram.push(new OpeningCommand(commands.repeatLoop));
expect(++i, (x) => x instanceof syntax.StatementEnd);
repeatMode = true;
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "while") && !repeatMode) { // While loop
expect(++i, (x) => x instanceof syntax.Expression);
parsedProgram.push(new OpeningCommand(commands.whileLoop, [tokens[i]]));
expect(++i, (x) => x instanceof syntax.StatementEnd);
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "until") && !repeatMode) { // Until loop
expect(++i, (x) => x instanceof syntax.Expression);
parsedProgram.push(new OpeningCommand(commands.untilLoop, [tokens[i]]));
expect(++i, (x) => x instanceof syntax.StatementEnd);
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "end")) { // Generic end
parsedProgram.push(new ClosingCommand(commands.genericEnd));
for (var j = 0; j < additionalEnds; j++) {
parsedProgram.push(new ClosingCommand(commands.genericEnd));
}
additionalEnds = 0;
expect(++i, (x) => x instanceof syntax.StatementEnd);
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "next")) { // For end
parsedProgram.push(new ClosingCommand(commands.forEnd));
if (condition(++i, (x) => x instanceof syntax.Expression)) {
i++;
}
expect(i, (x) => x instanceof syntax.StatementEnd);
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "while") && repeatMode) { // Repeat while end
expect(++i, (x) => x instanceof syntax.Expression);
parsedProgram.push(new ClosingCommand(commands.repeatWhileEnd, [tokens[i]]));
expect(++i, (x) => x instanceof syntax.StatementEnd);
repeatMode = false;
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "until") && repeatMode) { // Repeat until end
expect(++i, (x) => x instanceof syntax.Expression);
parsedProgram.push(new ClosingCommand(commands.repeatUntilEnd, [tokens[i]]));
expect(++i, (x) => x instanceof syntax.StatementEnd);
repeatMode = false;
} else if (condition(i, (x) => x instanceof syntax.Keyword && x.code.toLocaleLowerCase() == "loop")) { // Loop end
parsedProgram.push(new ClosingCommand(commands.loopEnd));
for (var j = 0; j < additionalEnds; j++) {
parsedProgram.push(new ClosingCommand(commands.loopEnd));
}
expect(++i, (x) => x instanceof syntax.StatementEnd);
} else if (condition(i, (x) => x instanceof syntax.StatementEnd)) {
// Pass; could be a comment
} else {
console.warn("Unexpected:", i, tokens[i]);
throw new ParsingSyntaxError(
tokens[i].code != null ? `Unexpected \`${tokens[i].code}\`` : `Unexpected token`,
tokens[i].lineNumber
);
}
}
}
export function displayError(error) {
if (!(error instanceof BasicError)) {
throw error;
}
var defaultForeground = term.foregroundColour;
term.foreground("red");
if (term.backgroundColour == term.foregroundColour) {
term.foreground("black");
}
term.print(error.message);
if (typeof(error.lineNumber) == "number" && error.lineNumber > 0) {
term.print(` at line ${error.lineNumber}`);
}
term.print("\n");
term.setColours(term.backgroundColour, defaultForeground);
}
export function startProgram(clearVariables = true) {
running = true;
currentPosition = 0;
programStack = [];
turtleNotMoved = true;
canvas.copyToBuffer();
setGraphicsPosition(0, 0);
setGraphicsStrokeWidth(1);
clearGraphicsPolygonPoints();
setTurtleHeading(Math.PI / 2);
setTurtlePenDown(true);
setTurtleShown(true);
if (clearVariables) {
programVariables = {};
}
hid.unfocusInput();
if (parsedProgram.length == 0) {
term.print("Nothing to run\n");
hid.startProgramInput();
return;
}
executeStatement(0);
}
export function executeStatement(position = currentPosition + 1) {
if (!running) {
return;
}
currentPosition = position;
function performExecution() {
renderHoldTime = new Date().getTime();
renderCallDepth++;
if (currentPosition >= parsedProgram.length) {
running = false;
term.print("Ready\n");
hid.startProgramInput();
return;
}
function handleError(error) {
displayError(error);
running = false;
term.print("Ready\n");
hid.startProgramInput();
}
try {
var result = parsedProgram[currentPosition].call();
if (result instanceof Promise) {
result.catch(function(error) {
handleError(new RuntimeError(error?.message || String(error) || "Error", findLineNumberByPosition(currentPosition)));
});
}
} catch (e) {
handleError(e);
return;
}
}
if (new Date().getTime() - renderHoldTime > MAX_RENDER_HOLD_TIME || renderCallDepth > MAX_RENDER_CALL_DEPTH) {
renderCallDepth = 0;
requestAnimationFrame(performExecution);
} else {
performExecution();
}
}
export function graphicsTakeFrame() {
renderHoldTime = 0;
}
export function stopProgram() {
if (!running) {
return;
}
running = false;
programStack = [];
term.print("Ready\n");
hid.startProgramInput();
}
export function interruptProgram(byUser = true) {
if (!running) {
return;
}
running = false;
programStack = [];
if (delayTimeout != null) {
clearTimeout(delayTimeout);
delayTimeout = null;
}
audio.quiet();
hid.unfocusInput();
if (byUser) {
term.print("Interrupt\n");
hid.startProgramInput();
}
}
export function seekOpeningMark() {
var stackLevel = 0;
var oldPosition = currentPosition;
while (!(parsedProgram[currentPosition] instanceof OpeningCommand) || stackLevel > 0) {
if (currentPosition < 0) {
throw new ParsingSyntaxError("Mismatched statement closing mark", findLineNumberByPosition(oldPosition));
}
if (parsedProgram[currentPosition] instanceof ClosingCommand) {
stackLevel++;
}
currentPosition--;
if (parsedProgram[currentPosition] instanceof OpeningCommand) {
stackLevel--;
}
}
}
export function seekClosingMark() {
var stackLevel = 0;
var oldPosition = currentPosition;
while (!(parsedProgram[currentPosition] instanceof ClosingCommand) || stackLevel > 0) {
if (currentPosition >= parsedProgram.length) {
throw new ParsingSyntaxError("Mismatched statement opening mark", findLineNumberByPosition(oldPosition));
}
if (parsedProgram[currentPosition] instanceof OpeningCommand) {
stackLevel++;
}
currentPosition++;
if (parsedProgram[currentPosition] instanceof ClosingCommand) {
stackLevel--;
}
}
}
export function seekLoopOpeningMark() {
var oldPosition = currentPosition;
while (!(parsedProgram[currentPosition] instanceof OpeningCommand && [
commands.forLoop,
commands.repeatLoop,
commands.whileLoop,
commands.untilLoop
].includes(parsedProgram[currentPosition].callable))) {
currentPosition--;
if (currentPosition < 0) {
throw new ParsingSyntaxError("Loop control command was used outside of loop", findLineNumberByPosition(oldPosition));
}
}
}
export function isValidDataType(value) {
if (Number.isNaN(value) || (Math.abs(value) == Infinity && typeof(value) != "string")) {
return false;
}
if (value == null || value == undefined) {
return false;
}
return true;
}
export function getValueComparative(value, lineNumber = running ? editingProgram[currentPosition] : null) {
if (!isValidDataType(value)) {
throw new RuntimeError("Type conversion error", lineNumber);
}
if (typeof(value) == "number") {
return Math.round(value * 1e10) / 1e10;
} else if (typeof(value) == "object") {
var mappableValue = [];
for (var i = 0; i < value.length; i++) {
if (value[i] == undefined) {
mappableValue.push(0);
continue;
}
mappableValue.push(value[i]);
}
return mappableValue.map((i) => getValueDisplay(i)).join(", ");
} else {
return String(value).normalize();
}
}
export function getValueDisplay(value, lineNumber = running ? editingProgram[currentPosition] : null) {
return String(getValueComparative(value, lineNumber));
}
export function getVariable(identifierName) {
var type = null;
setConstants();
if (identifierName.endsWith("$")) {
type = String;
} else if (identifierName.endsWith("%")) {
type = Number;
}
identifierName = identifierName.replace(/[$%]/g, "").toLocaleLowerCase();
if (typeof(programVariables[identifierName]) == "object") {
return programVariables[identifierName];
}
if (programVariables.hasOwnProperty(identifierName)) {
if (type == null) {
return programVariables[identifierName];
}
return type(programVariables[identifierName]);
}
if (type == String) {
return "";
} else {
return 0;
}
}
export function setVariable(identifierName, value, lineNumber = null) {
if (!isValidDataType(value)) {
throw new RuntimeError("Type conversion error", lineNumber);
}
identifierName = identifierName.replace(/[$%]/g, "").toLocaleLowerCase();
programVariables[identifierName] = value;
}
export function setConstants() {
setVariable("true", 1);
setVariable("false", 0);
setVariable("pi", Math.PI);
setVariable("e", Math.E);
setVariable("phi", (1 + Math.sqrt(5)) / 2);
setVariable("epoch", new Date().getTime());
setVariable("random", Math.random());
setVariable("col", term.col);
setVariable("row", term.row);
setVariable("key", currentKey);
var headingDeg = radiansToTrigMode(turtleHeading, trigModes.DEGREES) % 360;
if (headingDeg < 0) {
headingDeg = 360 + headingDeg;
}
setVariable("heading", radiansToTrigMode(trigModeToRadians(headingDeg, trigModes.DEGREES)));
}
export function getListItem(identifierName, index, lineNumber = null) {
identifierName = identifierName.replace(/[$%]/g, "").toLocaleLowerCase();
if (typeof(programVariables[identifierName]) != "object") {
throw new RuntimeError("Cannot access item in non-list value", lineNumber);
}
return programVariables[identifierName][index] || 0;
}
export function setListItem(identifierName, index, value, lineNumber = null) {
identifierName = identifierName.replace(/[$%]/g, "").toLocaleLowerCase();
if (typeof(programVariables[identifierName]) != "object") {
throw new RuntimeError("Cannot set item in non-list variable", lineNumber);
}
programVariables[identifierName][index] = value;
}
export function setStore(store, value) {
var identifier = store.getPrimaryIdentifier();
if (identifier instanceof syntax.ListAccessIdentifier) {
setListItem(identifier.listIdentifier.code, identifier.childExpression.value, value, identifier.lineNumber);
return;
}
if (identifier instanceof syntax.Identifier) {
setVariable(identifier.code, value, identifier.lineNumber);
return;
}
throw new RuntimeError("Expected variable name", identifier?.lineNumber);
}
export function pushStack(lineNumber, position = currentPosition) {
if (programStack.length >= MAX_STACK_SIZE) {
throw new RuntimeError("Maximum recursion depth limit reached", lineNumber);
}
programStack.push(position);
}
export function popStack() {
if (programStack.length == 0) {
throw new RuntimeError("Nothing to return to", findLineNumberByPosition(currentPosition));
}
return programStack.pop();
}
export function setGraphicsPosition(x, y) {
graphicsX = x;
graphicsY = y;
}
export function setGraphicsStrokeWidth(width) {
graphicsStrokeWidth = width;
}
export function clearGraphicsPolygonPoints() {
graphicsPolygonPoints = [];
}
export function addGraphicsPolygonPoint(x, y) {
graphicsPolygonPoints.push([x, y]);
}
export function setTurtleHeading(heading) {
turtleHeading = heading;
}
export function setTurtlePenDown(penDown) {
turtlePenDown = penDown;
}
export function setTurtleShown(shown) {
turtleShown = shown;
}
export function setTurtleMoved(fromTurtleCommand = true) {
if (turtleNotMoved) {
if (fromTurtleCommand) {
canvas.copyToBuffer();
}
setGraphicsPosition(canvas.DISP_WIDTH / 2, canvas.DISP_HEIGHT / 2);
}
turtleNotMoved = false;
}
export function preTurtleRender() {
canvas.restoreFromBuffer();
graphicsTakeFrame();
}
export function renderTurtle() {
canvas.copyToBuffer();
if (!turtleShown) {
return;
}
var points = [
[graphicsX, graphicsY],
[graphicsX - (30 * Math.cos(turtleHeading - (Math.PI / 8) - (Math.PI / 2))), graphicsY - (30 * Math.sin(turtleHeading - (Math.PI / 8) - (Math.PI / 2)))],
[graphicsX - (20 * Math.cos(turtleHeading - (Math.PI / 2))), graphicsY - (20 * Math.sin(turtleHeading - (Math.PI / 2)))],
[graphicsX - (30 * Math.cos(turtleHeading + (Math.PI / 8) - (Math.PI / 2))), graphicsY - (30 * Math.sin(turtleHeading + (Math.PI / 8) - (Math.PI / 2)))],
[graphicsX, graphicsY]
];
canvas.setColour(new canvas.Colour(0, 0, 0));
canvas.drawPolygon(points);
canvas.setColour(new canvas.Colour(255, 255, 255));
canvas.drawLine(points[0][0], points[0][1], points[1][0], points[1][1]);
for (var i = 1; i < points.length - 1; i++) {
canvas.drawLine(points[i][0], points[i][1], points[i + 1][0], points[i + 1][1]);
}
}
export function declareLastConditionalState(state) {
lastConditionalState = state;
}
export function setDelayTimeout(timeout) {
clearTimeout(delayTimeout);
delayTimeout = timeout;
}
export function listLines(startLimit = null, endLimit = null) {
if (startLimit != null && !Number.isNaN(Number(startLimit))) {
startLimit = Number(startLimit);
} else {
startLimit = 0;
}
if (endLimit != null && !Number.isNaN(Number(endLimit))) {
endLimit = Math.min(Number(endLimit), editingProgram.length);
} else {
endLimit = editingProgram.length;
}
var listingLength = editingProgram.filter((element, i) => typeof(element) == "string" && i >= startLimit && i <= endLimit).length;
var editingProgramAddedLines = 0;
for (var i = 0; i < editingProgram.length; i++) {
if (i < startLimit) {
continue;
}
if (i > endLimit) {
break;
}
if (typeof(editingProgram[i]) != "string") {
continue;
}
editingProgramAddedLines++;
hid.startProgramInput(editingProgram[i], false, term.scrollDelta + term.row, editingProgramAddedLines + canvas.TERM_ROWS > listingLength);
}
}
export function renumberLines() {
var newProgram = [];
var newLineNumber = 10;
var gotoLines = [];
var renumberings = [];
var tokens = syntax.tokenise(editingProgram);
for (var i = 0; i < editingProgram.length; i++) {
if (i in editingProgram) {
renumberings[i] = newLineNumber;
newLineNumber += 10;
}
}
newLineNumber = 10;
for (var i = 0; i < tokens.length; i++) {
if (tokens[i] instanceof syntax.Keyword && ["goto", "gosub"].includes(tokens[i].code.toLocaleLowerCase()) && tokens[i + 1] instanceof syntax.Expression) {
gotoLines[tokens[i].lineNumber] = gotoLines[tokens[i].lineNumber] || [];
gotoLines[tokens[i].lineNumber].push(renumberings[tokens[i + 1].value] || tokens[i + 1].value);
}
}
for (var i = 0; i < editingProgram.length; i++) {
if (i in editingProgram) {
var newLineCode = editingProgram[i].replace(/^\d+/, String(newLineNumber));
if (typeof(gotoLines[i]) == "object") {
for (var j = 0; j < gotoLines[i].length; j++) {
newLineCode = newLineCode.replace(/((?:goto|gosub)\s*)\d+/i, `$1\0${gotoLines[i][j]}`);
}
}
newLineCode = newLineCode.replace(/((?:goto|gosub)\s*)\0/gi, "$1");
newProgram[newLineNumber] = newLineCode;
newLineNumber += 10;
}
}
editingProgram = newProgram;
}
export function programToText() {
return editingProgram.filter((i) => typeof(i) == "string").join("\n");
}
export function textToProgram(text) {
editingProgram = [];
text.split("\n").forEach(function(line) {
if (/^\d+/.exec(line.trim()) && Number(/^(\d+)/.exec(line.trim())[1]) > 0) {
editingProgram[/^(\d+)/.exec(line.trim())[1]] = line;
}
});
}
export function exportToFile(filename = "untitled.atto") {
var file = new Blob([programToText()], {type: "text/plain"});
fileExporter.href = URL.createObjectURL(file);
fileExporter.download = filename;
fileExporter.click();
}
export function importFromFile() {
fileImporter.click();
}
export function autosave() {
if (programToText().length > 0) {
localStorage.setItem("atto_lastSessionProgram", programToText());
} else {
localStorage.removeItem("atto_lastSessionProgram");
}
}
export function shareProgram() {
var code = programToText();
var url = `${window.location.href.split("?")[0]}?code=${encodeURIComponent(code)}`;
hid.hidCopy.value = url;
hid.hidCopy.hidden = false;
hid.hidCopy.focus();
hid.hidCopy.select();
document.execCommand("copy");
hid.hidCopy.hidden = true;
hid.hidInput.focus();
window.history.pushState("", "", url);
term.print("Copied link to clipboard\n");
}
window.shareProgramLink = function() {
interruptProgram(false);
shareProgram();
hid.startProgramInput();
};
export function processCommand(value, movementOnly) {
var command = value.trim().toLocaleLowerCase();
if (/^\d+/.exec(command) && Number(/^(\d+)/.exec(command)[1]) > 0) {
var lineNumber = Number(/^(\d+)/.exec(value.trim())[1]);
if (value.trim() == String(lineNumber)) {
delete editingProgram[lineNumber];
} else {
editingProgram[lineNumber] = value;
}
autosave();
if (!movementOnly) {
hid.startProgramInput();
}
return;
}
if (movementOnly) {
return;
}
if (command == "help") {
canvas.toggleDocs();
hid.startProgramInput();
return;
}
if (command.substring(0, 4) == "list") {
var startLimit = command.substring(4).split("-")[0].trim();
var endLimit = (command.substring(4).split("-")[1] || "").trim();
listLines(startLimit || null, endLimit || null);
hid.startProgramInput();
return;
}
if (command == "new") {
editingProgram = [];
hid.clearProgramInputs();
term.print("Created new program\n");
autosave();
hid.startProgramInput();
return;
}
if (command == "run") {
hid.clearProgramInputs();
try {