-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpreter.js
2507 lines (2292 loc) · 90.9 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
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
'use strict'
// inspired by https://gist.github.com/eyecatchup/d0e1fb062343d45fbb5800dd0dc3d4d9
function wrapListener(pFunction_listener) {
if (!pFunction_listener.wrapped) {
pFunction_listener.wrapped = function () {
try{
pFunction_listener.apply(this, arguments)
} catch(err) {
console.error('JavaScript Error at line ' + (err.lineNumber - 13) + ' in this listener (' + pFunction_listener.evtType + '): ' + pFunction_listener.toString())
//~ console.error('JavaScript Error (location): ', pFunction_listener.location)
throw err
}
}
}
}
const addEventListener_orig = EventTarget.prototype.addEventListener
EventTarget.prototype.addEventListener = function (ps_evtType, pFunction_listener, options, location) {
pFunction_listener.location = (new Error()).stack
pFunction_listener.evtType = ps_evtType
wrapListener(pFunction_listener)
addEventListener_orig.call(this, ps_evtType, pFunction_listener.wrapped, options)
}
const removeEventListener_orig = EventTarget.prototype.removeEventListener
EventTarget.prototype.removeEventListener = function (ps_evtType, pFunction_listener, options) {
removeEventListener_orig.call(this, ps_evtType, pFunction_listener.wrapped || pFunction_listener, options)
}
// end of part inspired by https://gist.github.com/eyecatchup/d0e1fb062343d45fbb5800dd0dc3d4d9
const namespaceSet = new Set()
const cancellationSet = new Set()
const frozenLiveboxSet = new Set()
const cancellableFrameSet = new Set()
const dynamicParallelSet = new Set()
const BREAK = 0
const RESTART = 1
const TOGGLE_PAUSE = 2
const PAUSE = 3
const RESUME = 4
let stop
let timeout0 = false
let frameFunction = undefined
// continuous
//-----------
let gs_prepContinuous = ''
let old_timestamp = undefined
let continuousEvents
clearContinuousEvents()
function clearContinuousEvents() {
continuousEvents = new Map()
continuousEvents.old_get = continuousEvents.get
continuousEvents.get = (key) => continuousEvents.old_get(key) || []
}
function send(type, val) {
if (! continuousEvents.has(type)) continuousEvents.set(type, [])
continuousEvents.get(type).push(val)
}
function prep_goAssign(pFrame_) {
return function goAssign(ps_variable, p_val, pFrame = pFrame_) {
const l_namespace = getNamespace(pFrame, ps_variable)
;; $__ErrorChecking(pFrame, l_namespace===undefined, 'set undefined variable (for goAssign) "' + ps_variable + '"')
const l_livebox = l_namespace.get(ps_variable)
l_livebox.setval(pFrame, [p_val], true)
runBurst()
}
}
const continuousActionDict = {
send: new Map(),
adapt: new Map(),
play: new Map(),
emit: new Map(),
}
let continuousActionNotEmptyAnymore = false
let continuousActionEmpty = true
function addContinuousAction(type, key, fct, frame) {
if (continuousActionDict.send.size === 0
&& continuousActionDict.adapt.size === 0
&& continuousActionDict.play.size === 0
&& continuousActionDict.emit.size === 0) {
continuousActionNotEmptyAnymore = true
}
continuousActionDict[type].set(key,{fct:fct,frame:frame})
continuousActionEmpty = false
}
function delContinuousAction(type, key) {
continuousActionDict[type].delete(key)
if (continuousActionDict.send.size === 0
&& continuousActionDict.adapt.size === 0
&& continuousActionDict.play.size === 0
&& continuousActionDict.emit.size === 0) {
continuousActionEmpty = true
}
}
let mainFrame
let globalFrameTree
let g_superInstantNumber = 0
let Expression = null
//===================================================================================================
//
// Errors, debug
//
//===================================================================================================
//===================================================================================================
let f_location
const localLog = console.debug
const localGroup = console.group
const localGroupEnd = console.groupEnd
let g_debug = 0
const condLog = function (debugLevel, ...param) {
if (g_debug >= debugLevel) localLog(...param)
}
const condLogGroup = function (debugLevel, ...param) {
if (g_debug >= debugLevel) localGroup(...param)
}
const condLogGroupEnd = function (debugLevel, ...param) {
if (g_debug >= debugLevel) localGroupEnd(...param)
}
function $$$__BugChecking(pb_bugCondition, ps_message, pn_line, p_otherInfo, ps_messageForOtherInfo) {
if (pb_bugCondition) {
if (p_otherInfo!==undefined) console.warn( ps_messageForOtherInfo, p_otherInfo )
console.error('%c Bug in funcSug interpreter %c ' + ps_message + ' **at** line ' + pn_line, 'background-color: #ffb0b0; color: #800000')
throw '-- END OF BUG --'
//~ throw 'Bug: ' + ps_message + ' **at** line ' + pn_line
}
}
function $__ErrorChecking(pFrameOrCode, pb_errorCondition, ps_message, p_otherInfo, ps_messageForOtherInfo) {
if (pb_errorCondition) {
if (p_otherInfo!==undefined) console.warn( ps_messageForOtherInfo, p_otherInfo )
const lExpr_code = (pFrameOrCode.constructor===Frame) ? pFrameOrCode.code : pFrameOrCode
console.error('Error (funcSug): %c' + ps_message + '%c --IN-- %c' + expressionToString(lExpr_code), 'color: blue', 'color: red', 'color: blue', 'color: red', 'color: blue', 'color: red', 'color: blue')
throw '-- END OF ERROR --'
//~ throw 'Error (prog): ' + ps_message + ' --IN--' + expressionToString(lExpr_code)
}
}
function locationToString(loc) {
const oldStartLine = loc.start.line
const oldEndLine = loc.end.line
const startSource = (f_location) ? f_location(loc.start.line).source : loc.source
const startLine = (f_location) ? f_location(loc.start.line).line : loc.start.line
const endSource = (f_location) ? f_location(loc.end.line).source : loc.source
const endLine = (f_location) ? f_location(loc.end.line).line : loc.end.line
return 'source ' + startSource + ', line ' + startLine + ' column ' + loc.start.column + '%c to %csource ' + endSource + ', line ' + endLine + ' column ' + loc.end.column
}
function expressionToString(expr) {
return ' "... ' + expr.text + ' ..."%c --AT--> %c' + locationToString(expr.location)
}
//===================================================================================================
//
// Utils
//
//===================================================================================================
//===================================================================================================
function cartesianProduct(...p_arrays) { // inspired by https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript
if (p_arrays.length==0) return [[]]
if (p_arrays.length==1) return p_arrays[0].map(elt=>[elt])
return p_arrays.reduce(
(previousResult, currentArray) => previousResult.flatMap(
previousResultElt => currentArray.map(
currentArrayElt => [previousResultElt, currentArrayElt].flat()
)
)
)
}
//===================================================================================================
//
// variables
//
//===================================================================================================
//===================================================================================================
function makeNewDetachedNamespace(pFrame) {
const l_namespace = new Map()
l_namespace.frame = pFrame
namespaceSet.add(l_namespace)
return l_namespace
}
function makeNewAttachedNamespace(pFrame) {
;; $$$__BugChecking(pFrame.namespace!==undefined, 'pFrame.namespace already set', new Error().lineNumber)
pFrame.namespace = makeNewDetachedNamespace(pFrame)
pFrame.namespace.attached = true
}
function getNamespace(pFrame, label) {
;; $$$__BugChecking(pFrame===undefined, 'pFrame===undefined', new Error().lineNumber)
if ( pFrame.namespace!==undefined && pFrame.namespace.has(label) ) return pFrame.namespace
if ( pFrame.historicParent ) return getNamespace(pFrame.namespaceParent || pFrame.historicParent, label)
}
function getLivebox(pFrame, label) {
const l_namespace = getNamespace(pFrame, label)
return l_namespace.get(label)
}
function Livebox(p_namespace, p_label, pb_split) {
this.lbNamespace = p_namespace
this.lbLabel = p_label
this.lb_split = pb_split
this.precBip = false
this.precBeep = false
this.currBip = false
this.currBeep = false
this.precMultival = []
this.currMultival = []
}
Livebox.prototype.getMultival = function() {
return this.precMultival
}
function getCommonStart(ps_text1, ps_text2) {
let i = 0
while (ps_text1.charAt(i) === ps_text2.charAt(i) && i < ps_text1.length) i += 1
return ps_text1.slice(0,i)
}
Livebox.prototype.setval = function(pFrame, val, pb_multiple) {
this.currBip = true
this.currBeep = true
// delete values in direct line
for (const otherVal of [...this.currMultival]) {
//~ const lb_specialVariable = typeof this.lbLabel === 'string' && this.lbLabel.charAt(0) === '*'
const lb_specialVariable = this.lb_split
let ls_common
if (lb_specialVariable) {
//~ if ( isPrecedingFrame(otherVal.frame, pFrame) ) {
ls_common = getCommonStart(otherVal.frame.pathOfExecution, pFrame.pathOfExecution)
while (['0','1','2','3','4','5','6','7','8','9'].includes(ls_common.charAt(ls_common.length - 1))) {
ls_common = ls_common.slice(0,-1)
}
}
if ( !lb_specialVariable || ls_common.charAt(ls_common.length - 1) == ';') {
const l_otherValIndex = this.currMultival.indexOf(otherVal)
this.currMultival.splice(l_otherValIndex, 1)
}
}
// add new val
if (pb_multiple) {
for (const valElt of val) {
this.currMultival.push({frame:pFrame, val:valElt})
}
} else {
this.currMultival.push({frame:pFrame, val:val})
}
}
function makeNewVariable(pFrame, p_label, pb_split) {
if (pFrame.namespace===undefined) makeNewAttachedNamespace(pFrame)
;; $$$__BugChecking(pFrame.namespace.has( p_label ), 'variable already defined', new Error().lineNumber, [pFrame, p_label])
pFrame.namespace.set( p_label, new Livebox(pFrame.namespace, p_label, pb_split) )
}
function variableExists(pFrame, p_label) {
if (pFrame.namespace===undefined) return false
return pFrame.namespace.has( p_label )
}
//===================================================================================================
//
// Desc instruction
//
//===================================================================================================
//===================================================================================================
const gDict_instructions = {
lambda: { // lambda <paramExpression> <expressionToExecute>
nbArg:2,
exec: function(pFrame, p_content) {
//~ ;; $__ErrorChecking(pFrame, p_content[1].type !== 'expression', 'parameters not a list')
;; $__ErrorChecking(pFrame, p_content[2].type !== 'expression', 'body not an expression')
//~ ;; $__ErrorChecking(pFrame, p_content[1].content.some(elt=>(elt.type !== 'identifier')), 'some parameter is not an identifier')
pFrame.toReturn_multival.push({type: 'lambda', frame:pFrame, param:p_content[1], body:p_content[2]})
pFrame.terminated = true
}
},
//===========================================================
foreach: { // foreach <variable> <expressionToExecute>
nbArg:2,
exec: function(pFrame, p_content) {
const lPARAM_variable = p_content[1]
const lPARAM_expressionToExecute = p_content[2]
;; $__ErrorChecking(pFrame, p_content[2].type !== 'expression', 'expressionToExecute not an expression')
if (pFrame.instrPointer==1) {
pFrame.addChild(lPARAM_variable, 'foreachVariable')
} else if (pFrame.instrPointer==2) {
const l_argsLabel = pFrame.childReturnedMultivals.foreachVariable
;; $__ErrorChecking(pFrame, l_argsLabel.length>1, 'multiple foreach-ed variable')
for (const label of l_argsLabel) {
const l_namespace = getNamespace(pFrame, label)
;; $__ErrorChecking(pFrame, l_namespace===undefined, 'undefined foreach-ed variable')
const l_livebox = l_namespace.get(label)
const l_multival = l_livebox.getMultival()
// exec
//==========
let ln_valCnt = 0
for (const val of l_multival) {
const l_childFrame = pFrame.addChild(lPARAM_expressionToExecute, 'expressionToExecute'+ln_valCnt)
l_childFrame.injectParametersWithValues(
[new Expression('identifier', label, label)],
[[val]],
{frame:pFrame, foreach: true}
)
l_childFrame.injectParametersWithValues(
[new Expression('identifier', label+'_N', label+'_N')],
[[ln_valCnt]],
{frame:pFrame, foreach: true}
)
ln_valCnt += 1
}
pFrame.valCnt = ln_valCnt
}
} else {
for (const key in pFrame.childReturnedMultivals) {
if (key.startsWith('expressionToExecute')) {
pFrame.toReturn_multival.push(...pFrame.childReturnedMultivals[key])
}
}
pFrame.terminated = true
}
}
},
//===========================================================
foreachIn: { // foreachIn <variable> <multival <expressionToExecute>
nbArg:3,
exec: function(pFrame, p_content) {
const lPARAM_variable = p_content[1]
const lPARAM_multival = p_content[2]
const lPARAM_expressionToExecute = p_content[3]
;; $__ErrorChecking(pFrame, lPARAM_expressionToExecute.type !== 'expression', 'expressionToExecute not an expression')
if (pFrame.instrPointer==1) {
pFrame.addChild(lPARAM_variable, 'foreachVariable')
pFrame.addChild(lPARAM_multival, 'multival')
} else if (pFrame.instrPointer==2) {
const l_argsLabel = pFrame.childReturnedMultivals.foreachVariable
const l_multival = pFrame.childReturnedMultivals.multival
//~ const label = lPARAM_variable.content
for (const label of l_argsLabel) {
// exec
//==========
let ln_valCnt = 0
for (const val of l_multival) {
const l_childFrame = pFrame.addChild(lPARAM_expressionToExecute, 'expressionToExecute'+ln_valCnt)
l_childFrame.injectParametersWithValues(
[new Expression('identifier', label, label)],
[[val]],
{frame:pFrame, foreach: true}
)
l_childFrame.injectParametersWithValues(
[new Expression('identifier', label+'_N', label+'_N')],
[[ln_valCnt]],
{frame:pFrame, foreach: true}
)
ln_valCnt += 1
}
pFrame.valCnt = ln_valCnt
}
} else {
for (const key in pFrame.childReturnedMultivals) {
if (key.startsWith('expressionToExecute')) {
pFrame.toReturn_multival.push(...pFrame.childReturnedMultivals[key])
}
}
pFrame.terminated = true
}
}
},
//===========================================================
call: { // call <function> <param> ... <param>
nbArg: (n=> (n>=1) ),
exec: function(pFrame, p_content) {
if (pFrame.instrPointer==1) {
;; $$$__BugChecking(p_content[1]===undefined, 'p_content[1]===undefined', new Error().lineNumber)
pFrame.addChild(p_content[1], 'calledFunction')
} else if (pFrame.instrPointer==2) {
pFrame.lambda = pFrame.childReturnedMultivals.calledFunction
;; $__ErrorChecking(pFrame, pFrame.lambda.length!==1, 'multiple lambdas')
;; $__ErrorChecking(pFrame, pFrame.lambda.some(elt=>(elt.type!=='lambda')), 'not lambda', pFrame.lambda)
;; $__ErrorChecking(pFrame, typeof pFrame.lambda[0].param.content !== 'string' && pFrame.lambda.some(elt=> ( elt.param.content.length !== p_content.length-2 ) ), 'arguments number differs from parameters number')
const l_params = pFrame.lambda[0].param.content
if (p_content.length==3 && p_content[2].content[0] == '*') {
// spread syntax
} else {
for (let i=2;i<=p_content.length-1;i++) {
;; $$$__BugChecking(p_content[i]===undefined, 'p_content[i]===undefined', new Error().lineNumber)
//~ if(l_params[i-2][0] !== '_') pFrame.addChild(p_content[i], 'param' + (i-1))
if (
typeof l_params === 'string' && l_params[0] !== '_' || typeof l_params !== 'string' && l_params[i-2].content[0] !== '_'
) {
pFrame.addChild(p_content[i], 'param' + (i-1))
}
}
}
} else if (pFrame.instrPointer==3) {
// get args
//==========
const l_params = pFrame.lambda[0].param.content
const l_argsResults = []
let cpt = 0
for (let i=1;i<p_content.length-1;i++) {
//~ l_argsResults[i-1] = (l_params[i-1].content[0] !== '_') ? pFrame.childReturnedMultivals['param'+i] : [p_content[i+1]]
l_argsResults[i-1] = (
typeof l_params === 'string' && l_params[0] !== '_' || typeof l_params !== 'string' && l_params[i-1].content[0] !== '_'
) ? pFrame.childReturnedMultivals['param'+i] : [p_content[i+1]]
cpt += 1
}
// exec
//==========
pFrame.addChild(pFrame.lambda[0].body, 'bodyToExecute')
pFrame.injectParametersWithValues(pFrame.lambda[0].param.content, l_argsResults, pFrame.lambda[0])
} else {
pFrame.toReturn_multival = pFrame.childReturnedMultivals.bodyToExecute
pFrame.terminated = true
}
}
},
//===========================================================
ext: { // ext <inputExpression> <jsStringToExecute> <outputExpression> [<jsStringForCancellation>] [<jsStringForResume>]
nbArg: (n=> (2<=n && n<=5) ),
exec: function(pFrame, p_content) {
const lPARAM_inputExpression = p_content[1]
const lPARAM_jsStringToExecute = p_content[2]
const lPARAM_outputExpression = p_content[3]
const ls_jsStringForCancellation = (p_content.length>=5) ? p_content[4].content : ''
const ls_jsStringForResume = (p_content.length>=6) ? p_content[5].content : ''
const l_inputContent = lPARAM_inputExpression.content
if (pFrame.instrPointer==1) {
// input
//------
;; $__ErrorChecking(pFrame, lPARAM_inputExpression.type!=='expression', 'inputExpression not an expression')
for (let i=0;i<l_inputContent.length;i++) {
;; $$$__BugChecking(l_inputContent[i]===undefined, 'l_inputContent[i]===undefined', new Error().lineNumber)
const lExpr_inputProg = new Expression('expression', [
new Expression('identifier', 'get', 'get'),
l_inputContent[i]
], '.get ' + l_inputContent[i].content)
lExpr_inputProg.location = pFrame.code.location
pFrame.addChild(lExpr_inputProg, 'input' + i)
}
// jsStringToExecute
//------------------
pFrame.addChild(lPARAM_jsStringToExecute, 'jsStringToExecute')
// outputExpression
//-----------------
if (lPARAM_outputExpression) pFrame.addChild(lPARAM_outputExpression, 'outputExpression')
} else {
// get input
//==========
const l_argsResults = []
for (let i=0;i<l_inputContent.length;i++) {
l_argsResults[i] = pFrame.childReturnedMultivals['input' + i]
}
const l_argOutput = pFrame.childReturnedMultivals.outputExpression
;; $__ErrorChecking(pFrame, l_argOutput?.length>1, 'multiple output')
const l_theCombinations = cartesianProduct(...l_argsResults)
// get jsStringToExecute
//======================
const l_arg_jsStringToExecute = pFrame.childReturnedMultivals.jsStringToExecute
;; $__ErrorChecking(pFrame, l_arg_jsStringToExecute?.length>1, 'multiple jsStringToExecute')
// output
//========
const l_outputLabel = l_argOutput?.[0]
const l_namespace = getNamespace(pFrame, l_outputLabel)
;; $__ErrorChecking(pFrame, l_outputLabel!==undefined && l_namespace===undefined, 'undefined output variable ' + l_outputLabel)
const l_outputLivebox = (l_outputLabel) ? l_namespace.get(l_outputLabel) : undefined
if (l_outputLabel) l_namespace.set( l_outputLivebox, new Livebox(l_namespace, l_outputLabel) )
// replace in jsStringToExecute
//=============================
let ls_jsStringToExecute = `
"use strict"
const output=arguments[0]
const error=arguments[1]
const SAVES=arguments[2]
const goAssign=arguments[3]
const goBreak=arguments[3]
const goBip=arguments[3]
const sugAssign=arguments[3]
const sugBreak=arguments[3]
const sugBip=arguments[3]
`
let cptArg = 4
for (const vari of l_inputContent) {
ls_jsStringToExecute += 'const ' + vari.content + '=arguments[' + cptArg + ']\n'
cptArg += 1
}
ls_jsStringToExecute += l_arg_jsStringToExecute[0]
//~ ls_jsStringToExecute += '\n//# sourceURL=ext' + + '.js'
// cancellation preparation
//=========================
const lList_saveExternals=[]
const l_cancelObj={
externalObjects: lList_saveExternals,
pathOfExecution: pFrame.pathOfExecution,
execCancel: ls_jsStringForCancellation,
execPause: ls_jsStringForCancellation,
execResume: ls_jsStringForResume,
code: pFrame.code
}
if(ls_jsStringForCancellation !== '') cancellationSet.add(l_cancelObj)
// exec
//======
let promises = []
for (const argmts of l_theCombinations) {
const lFunction = new Function(ls_jsStringToExecute)
lFunction.location = pFrame.code.location
promises.push(
new Promise(
//~ (resolve,reject) => { (new Function(ls_jsStringToExecute))(resolve,reject,lList_saveExternals,prep_goAssign(pFrame),...argmts) }
(resolve,reject) => { lFunction(resolve,reject,lList_saveExternals,prep_goAssign(pFrame),...argmts) }
)
)
promises[promises.length-1].catch(err=>{
console.warn('Text where Error is', ls_jsStringToExecute)
console.error('Error (Js in funcSug): %c' + '"ext" instruction' + '%c --IN-- %c' + expressionToString(pFrame.code), 'color: blue', 'color: red', 'color: blue', 'color: red', 'color: blue', 'color: red', 'color: blue')
throw err
})
promises[promises.length-1].then(res => {
l_outputLivebox.currMultival.push({frame:pFrame, val:res})
l_outputLivebox.precMultival.push(res) // useful ?
runBurst()
}).catch(err=>{
console.error('javascript ERROR: ' + expressionToString(lPARAM_jsStringToExecute), 'color: #008000')
console.error(err)
})
}
Promise.all(promises).then(res => {
l_outputLivebox.currBip = true // useful ?
l_outputLivebox.currBeep = true // useful ?
l_outputLivebox.precBip = true
l_outputLivebox.precBeep = true
if(ls_jsStringForCancellation !== '') cancellationSet.delete(l_cancelObj)
runBurst()
}).catch(err=>{
console.error('javascript ERROR: ' + expressionToString(lPARAM_jsStringToExecute), 'color: #008000')
console.error(err)
})
pFrame.toReturn_multival = []
pFrame.terminated = true
}
}
},
//===========================================================
short: { // short <inputExpression> <jsStringToExecute>
nbArg: 2,
exec: function(pFrame, p_content) {
const lPARAM_inputExpression = p_content[1]
const lPARAM_jsStringToExecute = p_content[2]
const l_inputContent = lPARAM_inputExpression.content
if (pFrame.instrPointer==1) {
// input
//------
;; $__ErrorChecking(pFrame, lPARAM_inputExpression.type!=='expression', 'inputExpression not an expression')
for (let i=0;i<l_inputContent.length;i++) {
;; $$$__BugChecking(l_inputContent[i]===undefined, 'l_inputContent[i]===undefined', new Error().lineNumber)
const lExpr_inputProg = new Expression('expression', [
new Expression('identifier', 'get', 'get'),
l_inputContent[i]
], '.get ' + l_inputContent[i].content)
lExpr_inputProg.location = pFrame.code.location
pFrame.addChild(lExpr_inputProg, 'input' + i)
}
// jsStringToExecute
//------------------
pFrame.addChild(lPARAM_jsStringToExecute, 'jsStringToExecute')
} else {
// get input
//==========
const l_argsResults = []
for (let i=0;i<l_inputContent.length;i++) {
l_argsResults[i] = pFrame.childReturnedMultivals['input' + i]
}
const l_theCombinations = cartesianProduct(...l_argsResults)
// get jsStringToExecute
//======================
const l_arg_jsStringToExecute = pFrame.childReturnedMultivals.jsStringToExecute
;; $__ErrorChecking(pFrame, l_arg_jsStringToExecute?.length>1, 'multiple jsStringToExecute')
// replace in jsStringToExecute
//=============================
let ls_jsStringToExecute = `
"use strict"
function v(elt) {return elt.baseVal.value}
`
let cptArg = 0
for (const vari of l_inputContent) {
ls_jsStringToExecute += 'const ' + vari.content + '=arguments[' + cptArg + ']\n'
cptArg += 1
}
ls_jsStringToExecute += l_arg_jsStringToExecute[0]
// exec
//======
pFrame.toReturn_multival = []
for (const argmts of l_theCombinations) {
try {
pFrame.toReturn_multival.push( (new Function(ls_jsStringToExecute))(...argmts) )
} catch (err) {
console.warn('Text where Error is', ls_jsStringToExecute)
console.error('Error (Js in funcSug): %c' + '"short" instruction' + '%c --IN-- %c' + expressionToString(pFrame.code), 'color: blue', 'color: red', 'color: blue', 'color: red', 'color: blue', 'color: red', 'color: blue')
throw err
}
}
pFrame.terminated = true
}
}
},
//===========================================================
continuous: { // continuous <type> <key> <jsStringToExecute>
nbArg: 3,
exec: function(pFrame, p_content) {
const lPARAM_type = p_content[1]
const lPARAM_key = p_content[2]
const lPARAM_jsStringToExecute = p_content[3]
if (pFrame.instrPointer==1) {
;; $__ErrorChecking(pFrame, lPARAM_type.type!=='identifier', 'lPARAM_type not an identifier', lPARAM_type)
// key
//------
;; $__ErrorChecking(pFrame, lPARAM_key.type!=='identifier', 'lPARAM_key not an identifier', lPARAM_type)
const lExpr_key = new Expression('expression', [
new Expression('identifier', 'get', 'get'),
lPARAM_key
], '.get ' + lPARAM_key.content)
lExpr_key.location = pFrame.code.location
pFrame.addChild(lExpr_key, 'key')
// jsStringToExecute
//------------------
pFrame.addChild(lPARAM_jsStringToExecute, 'jsStringToExecute')
} else if (pFrame.instrPointer==2) {
// get key
//==========
const l_arg_key = pFrame.childReturnedMultivals.key
;; $__ErrorChecking(pFrame, l_arg_key?.length>1, 'continuous: multiple key')
// get jsStringToExecute
//======================
const l_arg_jsStringToExecute = pFrame.childReturnedMultivals.jsStringToExecute
;; $__ErrorChecking(pFrame, l_arg_jsStringToExecute?.length>1, 'continuous: multiple jsStringToExecute')
// replace in jsStringToExecute
//=============================
let ls_jsStringToExecute = `
"use strict"
function v(elt) {return elt.baseVal.value}
`
ls_jsStringToExecute += gs_prepContinuous
ls_jsStringToExecute += ';const ' + lPARAM_key.content + '=arguments[0]\n'
ls_jsStringToExecute += 'const delta=arguments[1]\n'
if (lPARAM_type.content === 'send') ls_jsStringToExecute += 'const send=arguments[2]\n'
if (lPARAM_type.content === 'adapt') ls_jsStringToExecute += 'const events=arguments[2]\nconst goAssign=arguments[3]\nconst goBreak=arguments[3]\nconst goBip=arguments[3]\nconst sugAssign=arguments[3]\nconst sugBreak=arguments[3]\nconst sugBip=arguments[3]\n'
ls_jsStringToExecute += l_arg_jsStringToExecute[0]
// cancellation preparation
//=========================
const l_cancelObj={
externalObjects: 'continuous',
pathOfExecution: pFrame.pathOfExecution,
type: lPARAM_type.content,
key: pFrame.childReturnedMultivals.key[0]
}
cancellationSet.add(l_cancelObj)
// exec
//======
try {
addContinuousAction(
lPARAM_type.content,
pFrame.childReturnedMultivals.key[0],
new Function(ls_jsStringToExecute),
pFrame,
)
} catch(err) {
//~ console.warn('code text where the error is:', ls_jsStringToExecute)
console.error('Error (Js in funcSug): %c' + '"continuous" instruction' + '%c --IN-- %c' + expressionToString(pFrame.code), 'color: blue', 'color: red', 'color: blue', 'color: red', 'color: blue', 'color: red', 'color: blue')
throw err
}
pFrame.toReturn_multival = []
//~ pFrame.terminated = true
pFrame.awake = false
} else {
pFrame.awake = false
}
}
},
//===========================================================
setDebug: { // setDebug <level>
nbArg:1,
postExec: function(pFrame, p_content) {
for (const val of pFrame.childReturnedMultivals.arg1) {
g_debug = val
}
pFrame.toReturn_multival = []
}
},
//===========================================================
awaitForever: { // awaitForever
nbArg:0,
exec: function(pFrame, p_content) {
pFrame.awake = false
}
},
//===========================================================
await: { // await <variable> <type>
nbArg:2,
exec: function(pFrame, p_content) {
const lPARAM_variable = p_content[1]
const lPARAM_type = p_content[2]
if (pFrame.instrPointer==1) {
pFrame.addChild(lPARAM_variable, 'awaitedVariable')
} else {
const l_argsLabel = pFrame.childReturnedMultivals.awaitedVariable
;; $__ErrorChecking(pFrame, l_argsLabel.length>1, 'multiple awaited variable')
for (const label of l_argsLabel) {
const l_namespace = getNamespace(pFrame, label)
;; $__ErrorChecking(pFrame, l_namespace===undefined, 'undefined awaited variable')
const l_livebox = l_namespace.get(label)
if (l_livebox.precBip && lPARAM_type.content=='bip') {
pFrame.toReturn_multival.push(...l_livebox.getMultival())
pFrame.awake = true
} else if (l_livebox.precBeep && lPARAM_type.content=='beep') {
pFrame.toReturn_multival.push(...l_livebox.getMultival())
l_livebox.currBeep = false
pFrame.awake = true
} else {
pFrame.awake = false
}
if (pFrame.awake) pFrame.terminated = true
}
}
}
},
//===========================================================
awaitInNamespace: { // await <namespace> <variable> <type>
nbArg:3,
exec: function(pFrame, p_content) {
const lPARAM_namespace = p_content[1]
const lPARAM_variable = p_content[2]
const lPARAM_type = p_content[3]
if (pFrame.instrPointer==1) {
pFrame.addChild(lPARAM_namespace, 'namespace')
pFrame.addChild(lPARAM_variable, 'awaitedVariable')
} else {
const l_argsNamespace = pFrame.childReturnedMultivals.namespace
const l_argsLabel = pFrame.childReturnedMultivals.awaitedVariable
;; $__ErrorChecking(pFrame, l_argsNamespace.length>1, 'multiple namespace (awaitNamespace)')
;; $__ErrorChecking(pFrame, l_argsLabel.length>1, 'multiple awaited variable')
for (const label of l_argsLabel) {
const l_namespace = l_argsNamespace[0]
;; $__ErrorChecking(pFrame, l_namespace===undefined, 'undefined awaited variable')
const l_livebox = l_namespace.get(label)
if (l_livebox === undefined) {
pFrame.awake = false
} else if (l_livebox.precBip && lPARAM_type.content=='bip') {
pFrame.toReturn_multival.push(...l_livebox.getMultival())
pFrame.awake = true
} else if (l_livebox.precBeep && lPARAM_type.content=='beep') {
pFrame.toReturn_multival.push(...l_livebox.getMultival())
l_livebox.currBeep = false
pFrame.awake = true
} else {
pFrame.awake = false
}
if (pFrame.awake) pFrame.terminated = true
}
}
}
},
//===========================================================
awaitBool: { // 'awaitBool' <condition>
nbArg:1,
exec: function(pFrame, p_content) {
let lastResult
if (pFrame.instrPointer%2==1) {
;; $$$__BugChecking(p_content[1]===undefined, 'p_content[1]===undefined', new Error().lineNumber)
pFrame.addChild(p_content[1], 'awaitedExpression')
} else {
const l_firstargResult = pFrame.childReturnedMultivals.awaitedExpression
const lb_thereIsTrue = l_firstargResult.some(x=>x)
if (lb_thereIsTrue) {
pFrame.awake = true
} else {
pFrame.awake = false
}
if (pFrame.awake) pFrame.terminated = true
}
}
},
//===========================================================
prepContinuous: { // 'prepContinuous' <functionsDefinition>
nbArg:1,
exec: function(pFrame, p_content) {
;; $$$__BugChecking(p_content[1]===undefined, 'p_content[1]===undefined', new Error().lineNumber)
gs_prepContinuous += p_content[1].content
pFrame.terminated = true
}
},
//===========================================================
print: {
nbArg:1,
postExec: function(pFrame, p_content) {
for (const val of pFrame.childReturnedMultivals.arg1) {
console.log(val)
}
if (pFrame.childReturnedMultivals.arg1.length === 0) console.log('No value')
pFrame.toReturn_multival = []
}
},
//===========================================================
Namespace: {
nbArg:0,
postExec: function(pFrame, p_content) {
const lMap_newNamespace = makeNewDetachedNamespace(pFrame)
pFrame.toReturn_multival = [lMap_newNamespace]
}
},
//===========================================================
combineMicrostep: {
nbArg:0,
postExec: function(pFrame, p_content) {
timeout0 = false
pFrame.toReturn_multival = []
}
},
//===========================================================
separateMicrostep: {
nbArg:0,
postExec: function(pFrame, p_content) {
timeout0 = true
pFrame.toReturn_multival = []
}
},
//===========================================================
'var': {
nbArg:1,
postExec: function(pFrame, p_content) {
for (const label of pFrame.childReturnedMultivals.arg1) {
;; $__ErrorChecking(pFrame, variableExists(pFrame.parent, label), 'variable already exists in this namespace', label)
variableExists(pFrame.parent, label)
makeNewVariable(pFrame.parent, label)
}
pFrame.toReturn_multival = []
}
},
//===========================================================
'varmul': {
nbArg:1,
postExec: function(pFrame, p_content) {
for (const label of pFrame.childReturnedMultivals.arg1) {
;; $__ErrorChecking(pFrame, variableExists(pFrame.parent, label), 'variable already exists in this namespace', label)
variableExists(pFrame.parent, label)
makeNewVariable(pFrame.parent, label, true)
}
pFrame.toReturn_multival = []
}
},
//===========================================================
freeze: {
nbArg:1,
postExec: function(pFrame, p_content) {
for (const label of pFrame.childReturnedMultivals.arg1) {
;; $__ErrorChecking(pFrame, typeof label === 'string' && label[0]==='_' , 'freeze underscore variable "' + label + '"')
// get variable
//-------------
const l_namespace = getNamespace(pFrame, label)
;; $__ErrorChecking(pFrame, l_namespace===undefined, 'freeze undefined variable "' + label + '"')
const l_livebox = l_namespace.get(label)
frozenLiveboxSet.add(l_livebox)
}
pFrame.toReturn_multival = []
}
},
//===========================================================
unfreeze: {
nbArg:1,
postExec: function(pFrame, p_content) {
for (const label of pFrame.childReturnedMultivals.arg1) {
;; $__ErrorChecking(pFrame, typeof label === 'string' && label[0]==='_' , 'unfreeze underscore variable "' + label + '"')
// get variable
//-------------
const l_namespace = getNamespace(pFrame, label)
;; $__ErrorChecking(pFrame, l_namespace===undefined, 'unfreeze undefined variable "' + label + '"')
const l_livebox = l_namespace.get(label)
frozenLiveboxSet.delete(l_livebox)
}
pFrame.toReturn_multival = []
}
},
//===========================================================
next: {
nbArg:1,
postExec: function(pFrame, p_content) {
for (const label of pFrame.childReturnedMultivals.arg1) {
;; $__ErrorChecking(pFrame, typeof label === 'string' && label[0]==='_' , 'next underscore variable "' + label + '"')
// get variable
//-------------
const l_namespace = getNamespace(pFrame, label)
;; $__ErrorChecking(pFrame, l_namespace===undefined, 'next undefined variable "' + label + '"')
const l_livebox = l_namespace.get(label)
l_livebox.precMultival = l_livebox.currMultival.map(elt=>elt.val)
l_livebox.precBip = l_livebox.currBip
l_livebox.precBeep = l_livebox.currBeep
l_livebox.currBip = false
}
pFrame.toReturn_multival = []
}
},
//===========================================================
aggregsum: {
nbArg:1,
postExec: function(pFrame, p_content) {
for (const label of pFrame.childReturnedMultivals.arg1) {
;; $__ErrorChecking(pFrame, typeof label === 'string' && label[0]==='_' , 'get underscore variable (aggregsum) "' + label + '"')
// get variable
//-------------
const l_namespace = getNamespace(pFrame, label)
;; $__ErrorChecking(pFrame, l_namespace===undefined, 'aggregsum undefined variable "' + label + '"')
const l_livebox = l_namespace.get(label)
l_livebox.setval(pFrame, l_livebox.getMultival().reduce((x, y) => x+y, 0))
}
pFrame.toReturn_multival = []
}
},
//===========================================================
aggregprod: {
nbArg:1,
postExec: function(pFrame, p_content) {
for (const label of pFrame.childReturnedMultivals.arg1) {
;; $__ErrorChecking(pFrame, typeof label === 'string' && label[0]==='_' , 'get underscore variable (aggregprod) "' + label + '"')
// get variable
//-------------
const l_namespace = getNamespace(pFrame, label)