-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprogram.js
More file actions
996 lines (842 loc) · 28.8 KB
/
program.js
File metadata and controls
996 lines (842 loc) · 28.8 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
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
const mainOutput = document.getElementById('main-output');
const textarea = document.getElementById('main-output');
const executionStatus = {
Image: document.getElementById('execution-status-img'),
Text: document.getElementById('execution-status')
};
function updateExecutionStatus(status)
{
switch(status)
{
case -2: // preload
executionStatus.Image.hidden = true;
executionStatus.Image.src = './res/stopped.png';
executionStatus.Image.src = './res/loading.png';
executionStatus.Image.src = './res/finished.png';
executionStatus.Image.src = './res/idle.png';
executionStatus.Image.hidden = false;
break;
case -1: // forced
executionStatus.Text.innerText = "Forced stop. ";
executionStatus.Text.style.color = "#cd7272";
executionStatus.Image.src = './res/stopped.png';
executionStatus.Image.className = "";
break;
case 0: // not running
executionStatus.Text.innerText = "Not running. ";
executionStatus.Text.style.color = "#7277cd";
executionStatus.Image.src = './res/idle.png';
executionStatus.Image.className = "";
break;
case 1: // running
executionStatus.Text.innerText = "Running... ";
executionStatus.Text.style.color = "#7277cd";
executionStatus.Image.src = './res/loading.png';
executionStatus.Image.className = "rotating";
break;
case 2: // finished
executionStatus.Text.innerText = "Finished. ";
executionStatus.Text.style.color = "#81cd72";
executionStatus.Image.src = './res/finished.png';
executionStatus.Image.className = "";
break;
}
}
updateExecutionStatus(-2); // preload
/*
function KS_HelpButton()
{
let x = document.getElementById("win-draggable-help-modal");
if (x.style.display === "none") {
x.style.display = "block";
} else {
x.style.display = "none";
}
}
*/
function AppendOutput(data)
{
// Nodes are cool but slow :(
mainOutput.value += data;
mainOutput.value = mainOutput.value.slice(-2015);
mainOutput.value = mainOutput.value.split('\n').slice(-32).join('\n');
// [NODES] mainOutput.append(data);
// [NODES] if (mainOutput.childNodes.length >= 600) mainOutput.childNodes[0].remove();
textarea.scrollTop = textarea.scrollHeight;
}
function FlushOutput()
{
// [NODES] mainOutput.innerHTML = '';
mainOutput.value = "";
textarea.scrollTop = textarea.scrollHeight;
}
function RaiseError(err)
{
// [NODES] mainOutput.innerHTML = '';
// [NODES] mainOutput.append(err);
mainOutput.value = err;
// mainOutput.value = mainOutput.value.split('\n').slice(-100).join('\n');
textarea.scrollTop = textarea.scrollHeight;
compil_forceStop = true;
}
async function KS_RunScript()
{
updateExecutionStatus(1); // running
RestartShell();
let sourceCode = document.getElementById('main-input').value;
let lines = sourceCode.split('\n');
FlushOutput();
// Prepare state:
for (let i = 0; i < lines.length; i++)
{
let line = lines[i];
let words = line.split(/[\t ]+/g);
if (words.length >= 1)
{
if (words[0].startsWith(':'))
{
let name = words[0].split(':')[1];
if (name == null) RaiseError(`[ERROR] Jump label name was not set.`);
else
{
if (prog_LocalJumpLabels[name] != null) RaiseError(`[ERROR] Jump label named "${name}" name was already set.`);
else
{
prog_LocalJumpLabels[name] = i;
}
}
}
if (words[0].startsWith('new'))
{
compil_scriptCommands[words[0]](words);
}
else if (words[0].startsWith('get'))
{
if (prog_LocalVariables[words[1]] == null) RaiseError(`[ERROR] Couldn't GET variable "${words[1]}" because it doesn't exists.`);
else if (words[2] == null) RaiseError(`[ERROR] Couldn't GET variable, missing two parameters.\nUssage: get [VARIABLE] [NAME] [VALUE].`);
else if (words[3] == null) RaiseError(`[ERROR] Couldn't GET variable, missing one parameter.\nUssage: get [VARIABLE] [NAME] [VALUE].`);
else if (words[2] == 'sys_input')
{
sys_HookInput(words[3]);
}
}
}
}
// Execution state:
let sleepExecutionKeeper = 0;
for (compil_executionLine = 0; compil_executionLine < lines.length; compil_executionLine++)
{
sleepExecutionKeeper++;
if (sleepExecutionKeeper >= 100)
{
await new Promise(resolve => setTimeout(resolve, 1));
sleepExecutionKeeper = 0;
}
if (compil_forceStop)
{
try {
prog_WebSocket.close()
} catch(e) {}
// compil_executionLine = lines.length;
return;
}
compil_currentLine = lines[compil_executionLine];
let words = compil_currentLine.split(/[\t ]+/g);
if (words.length >= 1)
{
if (words[0].startsWith(';') || words[0].startsWith(':') || words[0].startsWith('new') || !words[0].trim()) continue;
else if (compil_scriptCommands[words[0]] == null) RaiseError(`[ERROR] Instruction "${words[0]}" is unknown.`);
else
{
// RaiseError(`[WARN] Should be calling ${words[0]}`);
await Promise.resolve(compil_scriptCommands[words[0]](words));
}
}
}
// End of interpreting:
sys_UnhookInput();
updateExecutionStatus(2); // finished
}
let compil_currentLine = null;
let compil_executionLine = null;
let compil_scriptCommands = null;
let compil_defaultVariableTypes = null;
let prog_LocalVariables = null;
let prog_LocalJumpLabels = null;
let prog_LocalStack = null;
let prog_WebSocket = null;
let prog_WebSocket_messages = null;
let compil_forceStop = false;
function KS_ForceStop()
{
RestartShell();
compil_forceStop = true;
sys_UnhookInput();
updateExecutionStatus(-1); // forced stop
}
function RestartShell()
{
compil_forceStop = false;
compil_executionLine = 0;
prog_LocalVariables = {};
prog_LocalJumpLabels = {};
prog_LocalJumpEnds = {};
prog_LocalStack = [];
prog_WebSocket = null;
prog_WebSocket_messages = [];
compil_defaultVariableTypes =
{
"int": 0, "bool": false, "string": ""
};
compil_scriptCommands =
{
"new": prog_NEW, "set": prog_SET, "jmp": prog_JMP, "end": prog_END, "out": prog_OUT,
"cpy": prog_CPY, "add": prog_ADD, "sub": prog_SUB, "siz": prog_SIZ, "sin": prog_SIN,
"flp": prog_FLP, "tof": prog_TOF, "len": prog_LEN, "put": prog_PUT, "cut": prog_CUT,
"get": prog_GET, "psh": prog_PSH, "pop": prog_POP, "mul": prog_MUL, "div": prog_DIV,
"mod": prog_MOD, "clr": prog_CLR, "cst": prog_CST, "wsc": prog_WSC, "wsp": prog_WSP,
"wsi": prog_WSI, "wsr": prog_WSR, "wss": prog_WSS, "wse": prog_WSE,
"jmp!": prog_JMP_END, "out!": prog_OUT_NL, "siz!": prog_SIZ_NOT, "sin!": prog_SIN_NOT,
"end!": prog_END_EP
};
}
function prog_NEW(array)
{
let name = array[1];
let type = array[2];
if (prog_LocalVariables[name] != null) RaiseError(`[ERROR] Couldn't create variable "${name}" because it already exists.`);
else
{
if (compil_defaultVariableTypes[type] == null) RaiseError(`[ERROR] Couldn't create variable "${name}" because its type ("${type}") is invalid.`);
else
{
prog_LocalVariables[name] = compil_defaultVariableTypes[type];
}
}
}
function prog_SET(array)
{
let name = array[1];
let value = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't SET variable "${name}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "boolean":
if (value) value = true;
else if (!value) value = false;
else RaiseError(`[ERROR] Couldn't SET variable "${name}" to "${value}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof value}) doesn't match.`);
break;
case "number":
if (!isNaN(value)) prog_LocalVariables[name] = parseInt(value);
else RaiseError(`[ERROR] Couldn't SET variable "${name}" to "${value}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof value}) doesn't match.`);
break;
case "string":
if (compil_currentLine.match(/(')((?:\\\1|(?:(?!\1).))*)\1/g) != null)
{
let matched = compil_currentLine.match(/(')((?:\\\1|(?:(?!\1).))*)\1/g)[0].toString();
prog_LocalVariables[name] = matched.substring(1, matched.length - 1);
}
else RaiseError(`[ERROR] Couldn't SET variable "${name}" because value's format is invalid. \nUssage: set [VARIABLE] '[VALUE]'`);
break;
}
}
}
function prog_CPY(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't CPY variable "${name}" because it doesn't exists.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't CPY variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "boolean":
if (prog_LocalVariables[name2]) prog_LocalVariables[name] = true;
else if (!prog_LocalVariables[name2]) prog_LocalVariables[name] = false;
else RaiseError(`[ERROR] Couldn't SET variable "${name}" to "${prog_LocalVariables[name2]}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof prog_LocalVariables[name2]}) doesn't match.`);
break;
case "number":
if (!isNaN(prog_LocalVariables[name2]) && prog_LocalVariables[name2].toString() !== "") {
prog_LocalVariables[name] = prog_LocalVariables[name2];
}else RaiseError(`[ERROR] Couldn't CPY variable "${name2}" to "${name}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof prog_LocalVariables[name2]}) doesn't match.`);
break;
case "string":
prog_LocalVariables[name] = prog_LocalVariables[name2].toString();
break;
default:
RaiseError(`[ERROR] Couldn't CPY variable "${name}" because it's type is unknown.`);
break;
}
}
}
function prog_ADD(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't ADD variable "${name}" because it doesn't exists.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't ADD variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
if (!isNaN(prog_LocalVariables[name2])) prog_LocalVariables[name] += parseInt(prog_LocalVariables[name2]);
else RaiseError(`[ERROR] Couldn't ADD variable "${name}" with "${name2}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof prog_LocalVariables[name2]}) doesn't match.`);
break;
default:
RaiseError(`[ERROR] Couldn't ADD variable "${name}" because it's type isn't a number.`);
break;
}
}
}
function prog_SUB(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't SUB variable "${name}" because it doesn't exists.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't SUB variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
if (!isNaN(prog_LocalVariables[name2])) prog_LocalVariables[name] -= parseInt(prog_LocalVariables[name2]);
else RaiseError(`[ERROR] Couldn't SUB variable "${name}" with "${name2}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof prog_LocalVariables[name2]}) doesn't match.`);
break;
default:
RaiseError(`[ERROR] Couldn't SUB variable "${name}" because it's type isn't a number.`);
break;
}
}
}
function prog_JMP(array)
{
let name = array[1];
if (prog_LocalJumpLabels[name] == null) RaiseError(`[ERROR] Couldn't JMP to label "${name}" because it doesn't exists.`);
else
{
compil_executionLine = prog_LocalJumpLabels[name];
}
}
let prog_LocalJumpEnds = null;
let prog_jumpsEnded = 0;
function prog_JMP_END(array)
{
let name = array[1];
if (prog_LocalJumpLabels[name] == null) RaiseError(`[ERROR] Couldn't JMP to label "${name}" because it doesn't exists.`);
else
{
prog_jumpsEnded++;
prog_LocalJumpEnds[prog_jumpsEnded] = compil_executionLine;
compil_executionLine = prog_LocalJumpLabels[name];
}
}
function prog_END(array)
{
if (prog_jumpsEnded != 0)
{
compil_executionLine = prog_LocalJumpEnds[prog_jumpsEnded];
delete prog_LocalJumpEnds[prog_jumpsEnded];
prog_jumpsEnded--;
}
}
function prog_END_EP(array)
{
compil_forceStop = true;
}
function prog_OUT(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't OUT variable "${name}" because it doesn't exists.`);
else
{
AppendOutput(prog_LocalVariables[name]);
}
}
function prog_OUT_NL(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't OUT! variable "${name}" because it doesn't exists.`);
else
{
AppendOutput(prog_LocalVariables[name] + "\n");
}
}
function prog_SIZ(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't SIZ variable "${name}" because it doesn't exists.`);
else
{
if (!prog_LocalVariables[name])
{
compil_executionLine += 1;
}
}
}
function prog_SIZ_NOT(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't SIZ! variable "${name}" because it doesn't exists.`);
else
{
if (prog_LocalVariables[name])
{
compil_executionLine += 1;
}
}
}
function prog_SIN(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't SIN variable "${name}" because it doesn't exists.`);
else
{
if (typeof prog_LocalVariables[name] == "number" && prog_LocalVariables[name] < 0)
{
compil_executionLine += 1;
}
}
}
function prog_SIN_NOT(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't SIN! variable "${name}" because it doesn't exists.`);
else
{
if (typeof prog_LocalVariables[name] == "number" && prog_LocalVariables[name] > 0)
{
compil_executionLine += 1;
}
}
}
function prog_FLP(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't FLP variable "${name}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
prog_LocalVariables[name] *= -1;
break;
default:
RaiseError(`[ERROR] Couldn't FLP variable "${name}" because it's type isn't a number.`);
break;
}
}
}
function prog_TOF(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't TOF variable "${name}" because it doesn't exists.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't TOF variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "string":
prog_LocalVariables[name] = typeof prog_LocalVariables[name2];
break;
default:
RaiseError(`[ERROR] Couldn't TOF variable "${name}" because it's type isn't a string.`);
break;
}
}
}
function prog_LEN(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't LEN variable "${name}" because it doesn't exists.`);
else if (typeof prog_LocalVariables[name] != "number") RaiseError(`[ERROR] Couldn't LEN variable "${name}" because it's type isn't a number.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't LEN variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name2])
{
case "string":
prog_LocalVariables[name] = prog_LocalVariables[name2].length;
break;
default:
RaiseError(`[ERROR] Couldn't LEN variable "${name2}" because it's type isn't a string.`);
break;
}
}
}
function prog_PUT(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't PUT variable "${name}" because it doesn't exists.`);
else if (typeof prog_LocalVariables[name] != "string") RaiseError(`[ERROR] Couldn't PUT variable "${name}" because it's type isn't a string.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't PUT variable "${name2}" because it doesn't exists.`);
else
{
prog_LocalVariables[name] += prog_LocalVariables[name2].toString();
}
}
function prog_CUT(array)
{
let name = array[1];
let name2 = array[2];
let name3 = array[3];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't CUT variable "${name}" because it doesn't exists.`);
else if (typeof prog_LocalVariables[name] != "string") RaiseError(`[ERROR] Couldn't CUT variable "${name}" because it's type isn't a string.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't CUT variable "${name2}" because it doesn't exists.`);
else if (typeof prog_LocalVariables[name2] != "number") RaiseError(`[ERROR] Couldn't CUT variable "${name2}" because it's type isn't a number.`);
else if (prog_LocalVariables[name3] == null) RaiseError(`[ERROR] Couldn't CUT variable "${name3}" because it doesn't exists.`);
else if (typeof prog_LocalVariables[name3] != "number") RaiseError(`[ERROR] Couldn't CUT variable "${name3}" because it's type isn't a number.`);
else
{
prog_LocalVariables[name] = prog_LocalVariables[name].substring(prog_LocalVariables[name2], prog_LocalVariables[name3]);
}
}
function prog_GET(array)
{
let name = array[1];
let name2 = array[2];
let value = array[3];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't GET variable [${name2}] "${value}" to "${name}" because it doesn't exists.`);
else if (typeof prog_LocalVariables[name] != "number") RaiseError(`[ERROR] Couldn't GET variable [${name2}] "${value}" to "${name}" because it's type isn't a number.`);
else
{
if (name2 == 'sys_input') prog_LocalVariables[name] = sys_inputKeycodes[value];
else if (name2 == 'sys_env')
{
if (value == null) RaiseError(`[ERROR] Couldn't GET variable [${name2}] "${value}" to "${name}" because getting value wasn't set.`);
switch(value)
{
case 'Random':
prog_LocalVariables[name] = Math.floor(Math.random() * 1000);
break;
case 'Time':
prog_LocalVariables[name] = parseInt(new Date().getTime() / 1000)
break;
case 'TimeMs':
prog_LocalVariables[name] = new Date().getTime();;
break;
}
}
}
}
function prog_PSH(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't PSH variable "${name}" because it doesn't exists.`);
else
{
prog_LocalStack.push(prog_LocalVariables[name]);
}
}
function prog_POP(array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't POP to variable "${name}" because it doesn't exists.`);
else if (prog_LocalStack.length < 1) RaiseError(`[ERROR] Couldn't POP to variable "${name}" because stack is empty.`);
else
{
prog_LocalVariables[name] = prog_LocalStack.pop();
}
}
function prog_MUL(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't MUL variable "${name}" because it doesn't exists.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't MUL variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
if (!isNaN(prog_LocalVariables[name2])) prog_LocalVariables[name] *= parseInt(prog_LocalVariables[name2]);
else RaiseError(`[ERROR] Couldn't MUL variable "${name}" with "${name2}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof prog_LocalVariables[name2]}) doesn't match.`);
break;
default:
RaiseError(`[ERROR] Couldn't MUL variable "${name}" because it's type isn't a number.`);
break;
}
}
}
function prog_DIV(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't DIV variable "${name}" because it doesn't exists.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't DIV variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
if (!isNaN(prog_LocalVariables[name2])) prog_LocalVariables[name] = Math.floor(prog_LocalVariables[name] / parseInt(prog_LocalVariables[name2]));
else RaiseError(`[ERROR] Couldn't DIV variable "${name}" with "${name2}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof prog_LocalVariables[name2]}) doesn't match.`);
break;
default:
RaiseError(`[ERROR] Couldn't DIV variable "${name}" because it's type isn't a number.`);
break;
}
}
}
function prog_MOD(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't MOD variable "${name}" because it doesn't exists.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't MOD variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
if (!isNaN(prog_LocalVariables[name2])) prog_LocalVariables[name] %= parseInt(prog_LocalVariables[name2]);
else RaiseError(`[ERROR] Couldn't MOD variable "${name}" with "${name2}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof prog_LocalVariables[name2]}) doesn't match.`);
break;
default:
RaiseError(`[ERROR] Couldn't MOD variable "${name}" because it's type isn't a number.`);
break;
}
}
}
function prog_CLR(array)
{
FlushOutput();
}
function prog_CST(array)
{
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't CST variable "${name}" because it doesn't exists.`);
else if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't CST variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
if (!isNaN(prog_LocalVariables[name2])) prog_LocalVariables[name] = parseInt(prog_LocalVariables[name2]);
else RaiseError(`[ERROR] Couldn't CST variable "${name}" with "${name2}" because it's value type (${typeof prog_LocalVariables[name]} =/= ${typeof prog_LocalVariables[name2]}) doesn't match.`);
break;
default:
RaiseError(`[ERROR] Couldn't CST variable "${name}" because it's type isn't a number.`);
break;
}
}
}
async function prog_WSC (array)
{
if (prog_WebSocket != null)
{
RaiseError("[ERROR] Couldn't WSC because there's already one connection started.");
return;
}
let name = array[1];
let name2 = array[2];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't WSC with variable "${name}" because it doesn't exists.`);
if (prog_LocalVariables[name2] == null) RaiseError(`[ERROR] Couldn't WSC with variable "${name2}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
if (typeof prog_LocalVariables[name2] != "string")
{
RaiseError(`[ERROR] Couldn't WSC with variable "${name2}" because it's type isn't a string.`);
return;
}
await new Promise((resolve, reject) =>
{
prog_WebSocket = new WebSocket(prog_LocalVariables[name2]);
prog_WebSocket.onopen = function() {
if (prog_WebSocket == null) return;
prog_WebSocket_messages = [];
prog_LocalVariables[name] = prog_WebSocket.readyState;
prog_WebSocket.onmessage = function (msg)
{
prog_WebSocket_messages.push(msg.data.toString('utf8'));
}
resolve(prog_WebSocket);
};
prog_WebSocket.onerror = function(err) {
prog_LocalVariables[name] = 3;
reject(err);
};
});
break;
default:
RaiseError(`[ERROR] Couldn't WSC with variable "${name}" because it's type isn't a number.`);
break;
}
}
}
function prog_WSE (array)
{
if (prog_WebSocket == null)
{
RaiseError("[ERROR] Couldn't WSE because there's no any connection started.");
return;
}
prog_WebSocket.close();
prog_WebSocket = null;
}
function prog_WSI (array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't WSI with variable "${name}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
prog_LocalVariables[name] = prog_WebSocket_messages.length;
break;
default:
RaiseError(`[ERROR] Couldn't WSI with variable "${name}" because it's type isn't a number.`);
break;
}
}
}
function prog_WSR (array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't WSR with variable "${name}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "string":
let ret = prog_WebSocket_messages.pop();
if (ret == undefined) ret = "";
prog_LocalVariables[name] = ret;
break;
default:
RaiseError(`[ERROR] Couldn't WSR with variable "${name}" because it's type isn't a string.`);
break;
}
}
}
function prog_WSP (array)
{
if (prog_WebSocket == null)
{
RaiseError("[ERROR] Couldn't WSP because there's no any connection started.");
return;
}
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't WSP with variable "${name}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "string":
prog_WebSocket.send(prog_LocalVariables[name]);
break;
default:
RaiseError(`[ERROR] Couldn't WSP with variable "${name}" because it's type isn't a string.`);
break;
}
}
}
function prog_WSS (array)
{
let name = array[1];
if (prog_LocalVariables[name] == null) RaiseError(`[ERROR] Couldn't WSS with variable "${name}" because it doesn't exists.`);
else
{
switch (typeof prog_LocalVariables[name])
{
case "number":
if (prog_WebSocket == null)
{
prog_LocalVariables[name] = 3;
return;
}
prog_LocalVariables[name] = prog_WebSocket.readyState;
break;
default:
RaiseError(`[ERROR] Couldn't WSS with variable "${name}" because it's type isn't a number.`);
break;
}
}
}
// ### SYS CODE ###
let sys_isListenerSet = false;
let sys_inputKeycodes = {};
function sys_HookInput(inputKey)
{
sys_inputKeycodes[inputKey] = 0;
if (!sys_isListenerSet)
{
sys_isListenerSet = true;
window.addEventListener("keydown", function sys_catchInputDown(event)
{
if (event.defaultPrevented) { return; } // Do nothing if event already handled
if (sys_inputKeycodes[event.code] != null) { sys_inputKeycodes[event.code] = 1; }
});
window.addEventListener("keyup", function sys_catchInputUp(event)
{
if (event.defaultPrevented) { return; } // Do nothing if event already handled
if (sys_inputKeycodes[event.code] != null) { sys_inputKeycodes[event.code] = 0; }
});
}
}
function sys_UnhookInput()
{
try{
if (sys_isListenerSet) window.removeEventListener("keydown", sys_catchInputDown);
} catch(e) {}
try {
if (sys_isListenerSet) window.removeEventListener("keyup", sys_catchInputUp);
} catch (e) {}
sys_isListenerSet = false;
sys_inputKeycodes = {};
}
// ################
// ================================
// EXTERNAL CODE: Draggable modal
// ================================
/*
dragElement(document.getElementById("win-draggable-help-modal"));
function dragElement(elmnt) {
var pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.getElementById(elmnt.id + "header")) {
// if present, the header is where you move the DIV from:
document.getElementById(elmnt.id + "header").onmousedown = dragMouseDown;
} else {
// otherwise, move the DIV from anywhere inside the DIV:
elmnt.onmousedown = dragMouseDown;
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup:
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves:
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position:
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position:
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// stop moving when mouse button is released:
document.onmouseup = null;
document.onmousemove = null;
}
}
*/