-
Notifications
You must be signed in to change notification settings - Fork 49
/
Operators.js
5464 lines (4898 loc) · 152 KB
/
Operators.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 {
csconsole,
dump,
csgeo,
csmouse,
csctx,
csplay,
cspause,
csstop,
csw,
csh,
globalInstance,
CindyJS,
cscompiled,
shutdownHooks,
simtime,
simunit,
setSpeed,
canvas,
dropped,
dropPoint,
} from "Setup";
import { nada, instanceInvocationArguments, window, document } from "expose";
import { cskey, cskeycode, multipos, move, multiid, addAutoCleaningEventListener, scheduleUpdate } from "Events";
import { CSNumber } from "libcs/CSNumber";
import { List } from "libcs/List";
import { Json } from "libcs/Json";
import { Dict } from "libcs/Dict";
import { General } from "libcs/General";
import { evaluator, niceprint, eval_helper, myfunctions } from "libcs/Essentials";
import { namespace } from "libcs/Namespace";
import { Accessor } from "libcs/Accessors";
import { textRendererCanvas, setTextRendererCanvas, textRendererHtml, setTextRendererHtml } from "libcs/OpDrawing";
import { imageFromValue } from "libcs/OpImageDrawing";
import { evaluate, printStackTrace, evaluateAndVal, evaluateAndHomog, analyse } from "libcs/Evaluator";
import { CSad } from "libcs/CSad";
import { tools, setActiveTool } from "libcs/Tools";
import { csport, csgstorage } from "libgeo/GeoState";
import { addElement, removeElement } from "libgeo/GeoBasics";
import { stateContinueFromHere } from "libgeo/Tracing";
import { geoOps, geoAliases, geoMacros } from "libgeo/GeoOps";
import { masses, springs } from "liblab/LabBasics";
import { labObjects } from "liblab/LabObjects";
import { PSLQ } from "libcs/PSLQ";
//*******************************************************
// and here are the definitions of the operators
//*******************************************************
evaluator.version$0 = function (args, modifs) {
const ver = ["CindyJS"].concat(version);
return List.turnIntoCSList(ver.map(General.wrap));
};
evaluator.clearconsole$0 = function (args, modifs) {
csconsole.clear();
};
evaluator.err$1 = function (args, modifs) {
//OK
let varname = "",
s;
if (args[0].ctype === "variable") {
varname = args[0].name;
s = namespace.getvar(args[0].name);
} else {
s = args[0];
}
s = varname + " ===> " + niceprint(evaluate(s));
printStackTrace(s);
return nada;
};
evaluator.errc$1 = function (args, modifs) {
//OK
let s;
if (args[0].ctype === "variable") {
// var s=evaluate(args[0].value[0]);
s = evaluate(namespace.getvar(args[0].name));
console.log(args[0].name + " ===> " + niceprint(s));
} else {
s = evaluate(args[0]);
console.log(" ===> " + niceprint(s));
}
return nada;
};
evaluator.print$1 = function (args, modifs) {
csconsole.out(niceprint(evaluate(args[0]), modifs), true);
return nada;
};
evaluator.println$1 = function (args, modifs) {
csconsole.out(niceprint(evaluate(args[0], modifs)));
return nada;
};
evaluator.assert$2 = function (args, modifs) {
const v0 = evaluate(args[0]);
if (v0.ctype === "boolean") {
if (v0.value === false) return evaluator.println$1([args[1]], modifs);
} else {
printStackTrace("Condition for assert is not boolean");
}
return nada;
};
evaluator.dump$1 = function (args, modifs) {
dump(args[0]);
return nada;
};
evaluator.repeat$2 = function (args, modifs) {
//OK
return evaluator.repeat$3([args[0], null, args[1]], modifs);
};
evaluator.repeat$3 = function (args, modifs) {
//OK
function handleModifs() {
let erg;
if (modifs.start !== undefined) {
erg = evaluate(modifs.start);
if (erg.ctype === "number") {
startb = true;
start = erg.value.real;
}
}
if (modifs.step !== undefined) {
erg = evaluate(modifs.step);
if (erg.ctype === "number") {
stepb = true;
step = erg.value.real;
}
}
if (modifs.stop !== undefined) {
erg = evaluate(modifs.stop);
if (erg.ctype === "number") {
stopb = true;
stop = erg.value.real;
}
}
if (startb && !stopb && !stepb) {
stop = step * n + start;
}
if (!startb && stopb && !stepb) {
start = -step * (n - 1) + stop;
stop += step;
}
if (!startb && !stopb && stepb) {
stop = step * n + start;
}
if (startb && stopb && !stepb) {
step = (stop - start) / (n - 1);
stop += step;
}
if (startb && !stopb && stepb) {
stop = step * n + start;
}
if (!startb && stopb && stepb) {
start = -step * (n - 1) + stop;
stop += step;
}
if (startb && stopb && stepb) {
stop += step;
}
}
const v1 = evaluateAndVal(args[0]);
let lauf = "#";
if (args[1] !== null) {
if (args[1].ctype === "variable") {
lauf = args[1].name;
}
}
if (v1.ctype !== "number") {
return nada;
}
var n = Math.round(v1.value.real); //TODO: conversion to real!!!
var step = 1;
var start = 1;
var stop = n + 1;
var startb = false;
var stopb = false;
var stepb = false;
handleModifs();
if ((start <= stop && step > 0) || (start >= stop && step < 0))
if (startb && stopb && stepb) {
n = Math.floor((stop - start) / step);
}
namespace.newvar(lauf);
let erg = nada;
for (let i = 0; i < n; i++) {
namespace.setvar(lauf, {
ctype: "number",
value: {
real: i * step + start,
imag: 0,
},
});
erg = evaluate(args[2]);
}
namespace.removevar(lauf);
return erg;
};
evaluator.while$2 = function (args, modifs) {
//OK
const prog = args[1];
const test = args[0];
let bo = evaluate(test);
let erg = nada;
while (bo.ctype !== "list" && bo.value) {
erg = evaluate(prog);
bo = evaluate(test);
}
return erg;
};
evaluator.apply$2 = function (args, modifs) {
return evaluator.apply$3([args[0], null, args[1]], modifs);
};
evaluator.apply$3 = function (args, modifs) {
return evaluator.apply$4([args[0], args[1], null, args[2]], modifs);
};
evaluator.apply$4 = function (args, modifs) {
const v1 = evaluateAndVal(args[0]);
if (!(v1.ctype === "list" || v1.ctype === "JSON")) {
return nada;
}
let valueVar = "#";
if (args[1] !== null) {
if (args[1].ctype === "variable") {
valueVar = args[1].name;
}
}
let keyVar;
if (args[2] !== null) {
if (args[2].ctype === "variable") {
keyVar = args[2].name;
namespace.newvar(keyVar);
}
}
const li = v1.value;
let erg = v1.ctype === "list" ? [] : {};
namespace.newvar(valueVar);
if (keyVar !== undefined) {
if (v1.ctype === "list") {
for (let i = 0; i < li.length; i++) {
namespace.setvar(keyVar, CSNumber.real(i + 1));
namespace.setvar(valueVar, li[i]);
erg[i] = evaluate(args[3]);
}
} else {
// JSON
let res;
for (let k in li) {
namespace.setvar(keyVar, General.string(k));
namespace.setvar(valueVar, li[k]);
erg[k] = evaluate(args[3]);
}
}
namespace.removevar(keyVar);
} else {
// no key var
if (v1.ctype === "list") {
erg = [];
for (let i = 0; i < li.length; i++) {
namespace.setvar(valueVar, li[i]);
erg[i] = evaluate(args[3]);
}
} else {
// JSON
for (let k in li) {
// iterate over values
namespace.setvar(valueVar, li[k]);
erg[k] = evaluate(args[3]);
}
}
}
namespace.removevar(valueVar);
return v1.ctype === "list" ? List.turnIntoCSList(erg) : Json.turnIntoCSJson(erg);
};
evaluator.forall$2 = function (args, modifs) {
//OK
return evaluator.forall$3([args[0], null, args[1]], modifs);
};
evaluator.forall$3 = function (args, modifs) {
//OK
const v1 = evaluateAndVal(args[0]);
if (!(v1.ctype === "list" || v1.ctype === "JSON")) {
return nada;
}
let runVar = "#";
if (args[1] !== null) {
if (args[1].ctype === "variable") {
runVar = args[1].name;
}
}
const li = v1.value;
namespace.newvar(runVar);
let res;
if (v1.ctype === "list") {
for (let i = 0; i < li.length; i++) {
namespace.setvar(runVar, li[i]);
res = evaluate(args[2]);
}
} else {
// JSON
res = Json._helper.forall(li, runVar, args[2], modifs);
}
namespace.removevar(runVar);
return res;
};
evaluator.select$2 = function (args, modifs) {
//OK
return evaluator.select$3([args[0], null, args[1]], modifs);
};
evaluator.select$3 = function (args, modifs) {
//OK
const v1 = evaluateAndVal(args[0]);
if (!(v1.ctype === "list" || v1.ctype === "JSON")) {
return nada;
}
let lauf = "#";
if (args[1] !== null) {
if (args[1].ctype === "variable") {
lauf = args[1].name;
}
}
const li = v1.value;
let erg, ret, res;
namespace.newvar(lauf);
if (v1.ctype === "list") {
erg = [];
let ct = 0;
for (let i = 0; i < li.length; i++) {
namespace.setvar(lauf, li[i]);
res = evaluate(args[2]);
if (res.ctype === "boolean") {
if (res.value === true) {
erg[ct] = li[i];
ct++;
}
}
}
ret = {
ctype: "list",
value: erg,
};
} else {
// JSON
erg = {};
for (const k in li) {
namespace.setvar(lauf, li[k]);
res = evaluate(args[2]);
if (res.ctype === "boolean") {
if (res.value === true) {
erg[k] = li[k];
}
}
}
ret = {
ctype: "JSON",
value: erg,
};
}
namespace.removevar(lauf);
return ret;
};
evaluator.flatten$1 = function (args, modifs) {
function recurse(lst, level) {
if (level === -1 || lst.ctype !== "list") return lst;
return [].concat.apply(
[],
lst.value.map(function (elt) {
return recurse(elt, level - 1);
})
);
}
const lst = evaluateAndVal(args[0]);
if (lst.ctype !== "list") return lst;
let levels = modifs.levels;
if (levels === undefined) {
levels = 1;
} else {
levels = evaluate(levels);
if (levels.ctype === "number") levels = levels.value.real;
else if (levels.ctype === "string" && levels.value === "all") levels = -2;
else levels = 1;
}
return {
ctype: "list",
value: recurse(lst, levels),
};
};
function infix_semicolon(args, modifs) {
//OK
const u0 = args[0].ctype === "void";
const u1 = args[1].ctype === "void";
if (u0 && u1) {
return nada;
}
if (!u0 && u1) {
return evaluate(args[0]);
}
if (!u0 && !u1) {
evaluate(args[0]); //Wegen sideeffects
}
if (!u1) {
return evaluate(args[1]);
}
return nada;
}
evaluator.createvar$1 = function (args, modifs) {
//OK
if (args[0].ctype === "variable") {
const v = args[0].name;
namespace.newvar(v);
}
return nada;
};
evaluator.local = function (args, modifs) {
//VARIADIC!
for (let i = 0; i < args.length; i++) {
if (args[i].ctype === "variable") {
const v = args[i].name;
namespace.newvar(v);
}
}
return nada;
};
evaluator.removevar$1 = function (args, modifs) {
//OK
const ret = evaluate(args[0]);
if (args[0].ctype === "variable") {
const v = args[0].name;
namespace.removevar(v);
}
return ret;
};
evaluator.release = function (args, modifs) {
//VARIADIC!
if (args.length === 0) return nada;
const ret = evaluate(args[args.length - 1]);
for (let i = 0; i < args.length; i++) {
if (args[i].ctype === "variable") {
const v = args[i].name;
namespace.removevar(v);
}
}
return ret;
};
evaluator.regional = function (args, modifs) {
//VARIADIC!
for (let i = 0; i < args.length; i++) {
if (args[i].ctype === "variable") {
const v = args[i].name;
namespace.newvar(v);
namespace.pushVstack(v);
}
}
return nada;
};
evaluator.genList = function (args, modifs) {
//VARIADIC!
return List.turnIntoCSList(args.map(evaluate));
};
evaluator.genJSON = function (args, modifs) {
const res = {};
for (let i = 0; i < args.length; i++) {
const atom = Json.GenFromUserDataEl(args[i]);
// discard values that have no key
if (!atom.key) {
continue;
}
res[atom.key] = atom.val;
}
return {
ctype: "JSON",
value: res,
};
};
eval_helper.assigntake = function (data, what) {
//TODO: Bin nicht ganz sicher obs das so tut
const lhs = data.args[0];
const where = evaluate(lhs);
const ind = evaluateAndVal(data.args[1]);
let rhs = nada;
// TODO fix ind must be number
if (where.ctype === "list" || where.ctype === "string") {
let ind1 = Math.floor(ind.value.real);
if (ind1 < 0) {
ind1 = where.value.length + ind1 + 1;
}
if (ind1 > 0 && ind1 <= where.value.length) {
if (where.ctype === "list") {
const lst = where.internal_usage === "userData" ? where.value : where.value.slice();
lst[ind1 - 1] = evaluate(what);
rhs = List.turnIntoCSList(lst);
// update colon op
if (where.userData) rhs.userData = where.userData;
} else {
// string
let str = where.value;
str = str.substring(0, ind1 - 1) + niceprint(evaluate(what)) + str.substring(ind1, str.length);
rhs = General.string(str);
}
}
}
if (where.ctype === "JSON") {
const key = niceprint(ind);
if (!niceprint.errorTypes.includes(key)) {
rhs = Json._helper.ShallowClone(where);
rhs.value[key] = evaluate(what);
}
}
infix_assign([lhs, rhs]);
};
eval_helper.assigndot = function (data, what) {
const where = evaluate(data.obj);
const field = data.key;
if (where.ctype === "geo" && typeof field === "string") {
Accessor.setField(where.value, field, evaluateAndVal(what));
}
if (where.ctype === "JSON" && typeof field === "string") {
Json.setField(where.value, field, evaluateAndVal(what));
}
return nada;
};
eval_helper.assigndotNoVal = function (data, what) {
const where = evaluate(data.obj);
const field = data.key;
if (where.ctype === "geo" && typeof field === "string") {
Accessor.setField(where.value, field, what);
}
if (where.ctype === "JSON" && typeof field === "string") {
Json.setField(where.value, field, what);
}
return nada;
};
eval_helper.assigncolon = function (data, what) {
const lhs = data.obj;
const where = evaluate(lhs);
let key = General.string(niceprint(evaluate(data.key)));
if (key.value === "_?_") key = nada;
if (where.ctype === "geo" && key) {
Accessor.setuserData(where.value, key, evaluateAndVal(what));
} else if (where.ctype === "JSON" && key) {
Json.setField(where.value, key.value, evaluateAndVal(what));
} else if (where.ctype === "list" || (where.ctype === "string" && key)) {
const rhs = { ...where };
if (!rhs.userData) rhs.userData = {};
if (where.internal_usage !== "userData") {
rhs.userData = { ...rhs.userData };
}
const val = evaluateAndVal(what);
if (val.ctype === "list") {
val.internal_usage = "userData";
}
rhs.userData[key.value] = val;
infix_assign([lhs, rhs]);
} else {
if (!(key && key.ctype === "string")) console.log("Key is undefined");
else
console.log(
"User data " + key.value + " can only be assigned to geo objects and lists, not to " + where.ctype
);
}
return nada;
};
eval_helper.assigncolonNoVal = function (data, what) {
const lhs = data.obj;
const where = evaluate(lhs);
let key = General.string(niceprint(evaluate(data.key)));
if (key.value === "_?_") key = nada;
if (where.ctype === "geo" && key) {
Accessor.setuserData(where.value, key, evaluate(what));
} else if (where.ctype === "JSON" && key) {
Json.setField(where.value, key.value, what);
} else if (where.ctype === "list" || (where.ctype === "string" && key)) {
// copy object
const rhs = {};
for (const i in where) rhs[i] = where[i];
if (!rhs.userData) rhs.userData = {};
else {
// avoid reference copy
const tmpObj = {};
for (const j in rhs.userData) tmpObj[j] = rhs.userData[j];
rhs.userData = tmpObj;
}
rhs.userData[key.value] = what;
infix_assign([lhs, rhs]);
} else {
if (!(key && key.ctype === "string")) console.log("Key is undefined");
else
console.log(
"User data " + key.value + " can only be assigned to geo objects and lists, not to " + where.ctype
);
}
return nada;
};
evaluator.keys$1 = function (args, modifs) {
const obj = evaluate(args[0]);
const ctype = obj.ctype;
if (ctype === "geo" || ctype === "list" || ctype === "JSON") {
let keys = [];
let data;
if (ctype === "geo") {
data = obj.value.userData;
} else if (ctype === "list") {
data = obj.userData;
} else {
// JSON
data = obj.value;
}
if (data) {
keys = Object.keys(data).map(General.string).sort();
}
return List.turnIntoCSList(keys);
}
return nada;
};
evaluator.values$1 = function (args, modifs) {
const obj = evaluate(args[0]);
let ctype = obj.ctype;
if (ctype === "list") return obj;
if (ctype === "geo" || ctype === "JSON") {
let values = [];
let data;
if (ctype === "geo") {
data = obj.value.userData;
} else {
// JSON
data = obj.value;
}
if (data) {
values = Object.keys(data)
.sort()
.map((key) => data[key]);
}
return List.turnIntoCSList(values);
}
return nada;
};
eval_helper.assignlist = function (vars, vals) {
let n = vars.length;
const m = vals.length;
if (m < n) n = m;
for (let i = 0; i < n; i++) {
const name = vars[i];
const val = vals[i];
infix_assign([name, val], []);
}
};
function infix_assign(args, modifs) {
const u0 = args[0].ctype === "undefined";
const u1 = args[1].ctype === "undefined";
const v1 = evaluate(args[1]);
if (u0 || u1) {
return nada;
}
if (args[0].ctype === "variable") {
// restore normal list behavior in case it was extracted of a userData list
if (v1.ctype === "list") {
v1.internal_usage = undefined;
}
namespace.setvar(args[0].name, v1);
} else if (args[0].ctype === "infix") {
if (args[0].oper === "_") {
// Copy on write
eval_helper.assigntake(args[0], v1);
} else {
printStackTrace("Can't use infix expression as lvalue");
}
} else if (args[0].ctype === "field") {
eval_helper.assigndot(args[0], v1);
} else if (args[0].ctype === "userdata") {
eval_helper.assigncolon(args[0], v1);
} else if (args[0].ctype === "function" && args[0].oper === "genList") {
if (v1.ctype === "list") {
eval_helper.assignlist(args[0].args, v1.value);
} else {
printStackTrace("Expected list in rhs of assignment");
}
} else {
printStackTrace("Left hand side of assignment is not a recognized lvalue");
}
return v1;
}
function infix_define(args, modifs, self) {
const u0 = args[0].ctype === "undefined";
const u1 = args[1].ctype === "undefined";
if (u0 || u1) {
return nada;
}
if (args[0].ctype === "function") {
const fname = args[0].oper;
const ar = args[0].args;
const body = args[1];
let generation = 1;
if (myfunctions.hasOwnProperty(fname)) {
const previous = myfunctions[fname];
if (previous.definer === self) {
// Redefinition using the same piece of code changes nothing.
// This needs some work once we have closures.
return nada;
}
generation = previous.generation + 1;
}
myfunctions[fname] = {
oper: fname,
body,
arglist: ar,
definer: self,
generation,
};
} else if (args[0].ctype === "variable") {
namespace.setvar(args[0].name, args[1]);
} else if (args[0].ctype === "field") {
eval_helper.assigndotNoVal(args[0], args[1]);
} else if (args[0].ctype === "userdata") {
eval_helper.assigncolonNoVal(args[0], args[1]);
}
return nada;
}
function postfix_undefine(args, modifs) {
if (args[1].ctype !== "void") {
return nada;
}
if (args[0].ctype === "function") {
delete myfunctions[args[0].oper];
}
return nada;
}
evaluator.if$2 = function (args, modifs) {
//OK
return evaluator.if$3(args, modifs);
};
evaluator.if$3 = function (args, modifs) {
//OK
const u0 = args[0].ctype === "undefined";
const u1 = args[1].ctype === "undefined";
const v0 = evaluateAndVal(args[0]);
if (v0.ctype === "boolean") {
if (v0.value === true) {
return evaluate(args[1]);
} else if (args.length === 3) {
return evaluate(args[2]);
}
} else if (v0.ctype !== "undefined") {
printStackTrace("Condition for if is not boolean");
}
return nada;
};
function comp_equals(args, modifs) {
const v0 = evaluate(args[0]);
const v1 = evaluate(args[1]);
if (v0.ctype === v1.ctype) {
if (v0.ctype === "number") {
return General.bool(v0.value.real === v1.value.real && v0.value.imag === v1.value.imag);
}
if (v0.ctype === "string") {
return General.bool(v0.value === v1.value);
}
if (v0.ctype === "boolean") {
return General.bool(v0.value === v1.value);
}
if (v0.ctype === "list") {
return List.equals(v0, v1);
}
if (v0.ctype === "geo") {
return General.bool(v0.value === v1.value);
}
}
return {
ctype: "boolean",
value: false,
};
}
function comp_notequals(args, modifs) {
return General.not(comp_equals(args, modifs));
}
function comp_almostequals(args, modifs) {
const v0 = evaluateAndVal(args[0]);
const v1 = evaluateAndVal(args[1]);
if (v0.ctype === "number" && v1.ctype === "number") {
return {
ctype: "boolean",
value: CSNumber._helper.isAlmostEqual(v0, v1),
};
}
if (v0.ctype === "string" && v1.ctype === "string") {
return {
ctype: "boolean",
value: v0.value === v1.value,
};
}
if (v0.ctype === "boolean" && v1.ctype === "boolean") {
return {
ctype: "boolean",
value: v0.value === v1.value,
};
}
if (v0.ctype === "list" && v1.ctype === "list") {
const erg = List.almostequals(v0, v1);
return erg;
}
return {
ctype: "boolean",
value: false,
};
}
evaluator.and$2 = infix_and;
function infix_and(args, modifs) {
const v0 = evaluateAndVal(args[0]);
const v1 = evaluateAndVal(args[1]);
if (v0.ctype === "boolean" && v1.ctype === "boolean") {
return {
ctype: "boolean",
value: v0.value && v1.value,
};
}
return nada;
}
evaluator.or$2 = infix_or;
function infix_or(args, modifs) {
const v0 = evaluateAndVal(args[0]);
const v1 = evaluateAndVal(args[1]);
if (v0.ctype === "boolean" && v1.ctype === "boolean") {
return {
ctype: "boolean",
value: v0.value || v1.value,
};
}
return nada;
}
evaluator.xor$2 = function (args, modifs) {
const v0 = evaluateAndVal(args[0]);
const v1 = evaluateAndVal(args[1]);
if (v0.ctype === "boolean" && v1.ctype === "boolean") {
return {
ctype: "boolean",
value: v0.value !== v1.value,
};
}
return nada;
};
evaluator.not$1 = function (args, modifs) {
const v = evaluateAndVal(args[0]);
if (v.ctype === "boolean") {
return {
ctype: "boolean",
value: !v.value,
};
}
return nada;
};
function prefix_not(args, modifs) {
const v1 = evaluateAndVal(args[1]);
if (args[0].ctype === "void" && v1.ctype === "boolean") {
return {
ctype: "boolean",
value: !v1.value,
};
}
return nada;
}
function postfix_numb_degree(args, modifs) {
const v0 = evaluateAndVal(args[0]);
if (v0.ctype === "number" && args[1].ctype === "void") {
return General.withUsage(CSNumber.realmult(Math.PI / 180, v0), "Angle");
}
return nada;
}