-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_meta.js
2264 lines (2134 loc) · 51.2 KB
/
gen_meta.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
/*
* gen_meta.js -- Humus meta-circular code generator
*
* author: Dale Schumacher <[email protected]>
*/
import core from "./core.js";
import Humus from "./humus.js";
var version = '0.8.1 2024-02-03';
var equal = core.equal;
var log = core.log;
// var debug = function (msg) {
// core.debug(msg);
// core.trace('--?-- '+msg);
// };
var trace = core.trace;
var HUM = Humus;
var UNDEF = HUM.UNDEF;
var NIL = HUM.NIL;
var Obj = HUM.Obj;
var Pr = HUM.Pr;
/*
LET tag_beh(cust) = \msg.[ SEND (SELF, msg) TO cust ]
*/
var tag_beh = function (cust) {
return function (msg) {
this.send(Pr(this.self, msg), cust);
};
};
/*
LET join_beh(cust, k_first, k_rest) = \msg.[
CASE msg OF
($k_first, first) : [
BECOME \($k_rest, rest).[
SEND (first, rest) TO cust
]
]
($k_rest, rest) : [
BECOME \($k_first, first).[
SEND (first, rest) TO cust
]
]
END
]
*/
var join_beh = function (cust, head, tail) {
var h = undefined;
var t = undefined;
return function (msg) {
if (Pr.created(msg)) {
var tag = msg.hd;
if ((h === undefined) && (tag === head)) {
h = msg.tl;
} else if ((t == undefined) && (tag === tail)) {
t = msg.tl;
}
if ((h !== undefined) && (t !== undefined)) {
this.send(Pr(h, t), cust)
}
}
};
};
/*
LET fork_beh(cust, head, tail) = \(h_req, t_req).[
SEND (k_head, h_req) TO head
SEND (k_tail, t_req) TO tail
CREATE k_head WITH tag_beh(SELF)
CREATE k_tail WITH tag_beh(SELF)
BECOME join_beh(cust, k_head, k_tail)
]
*/
var fork_beh = function (cust, head, tail) {
return function (msg) {
if (Pr.created(msg)) {
var h_req = msg.hd;
var t_req = msg.tl;
var k_head = this.create(tag_beh(this.self));
var k_tail = this.create(tag_beh(this.self));
this.send(Pr(k_head, h_req), head);
this.send(Pr(k_tail, t_req), tail);
this.become(join_beh(cust, k_head, k_tail));
}
};
};
/*
LET call_pair_beh(head, tail) = \(cust, req).[
SEND (req, req) TO NEW fork_beh(cust, head, tail)
]
*/
var call_pair_beh = function (head, tail) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
this.send(
pair_of(req),
this.create(fork_beh(cust, head, tail))
);
}
};
};
/*
LET pair_of(value) = (value, value)
*/
var pair_of = function (value) {
return Pr(value, value);
};
/*
LET serializer_beh(svc) = \(cust, req).[
CREATE ret WITH tag_beh(SELF)
BECOME serializer_busy_beh(svc, cust, ret, NIL)
SEND (ret, req) TO svc
]
LET serializer_busy_beh(svc, cust, ret, pending) = \msg.[
CASE msg OF
($ret, res) : [
SEND res TO cust
IF $pending = ((cust', req'), rest) [
CREATE ret' WITH tag_beh(SELF)
BECOME serializer_busy_beh(svc, cust', ret', rest)
SEND (ret', req') TO svc
] ELSE [
BECOME serializer_beh(svc)
]
]
(cust', req') : [
LET pending' = $((cust', req'), pending)
BECOME serializer_busy_beh(svc, cust, ret, pending')
]
END
]
*/
var serializer_beh = function (svc) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
var ret = this.create(tag_beh(this.self));
this.become(serializer_busy_beh(svc, cust, ret, NIL));
this.send(Pr(ret, req), svc);
}
};
};
var serializer_busy_beh = function (svc, cust, ret, pending) {
return function (msg) {
if (Pr.created(msg)) {
var h = msg.hd;
var t = msg.tl;
if (h === ret) { // tagged response
this.send(t, cust);
if (Pr.created(pending)
&& Pr.created(pending.hd)) {
var cust_ = pending.hd.hd;
var req_ = pending.hd.tl;
var rest = pending.tl;
var ret_ = this.create(tag_beh(this.self));
this.become(serializer_busy_beh(svc, cust_, ret_, rest));
this.send(Pr(ret_, req_), svc);
} else {
this.become(serializer_beh(svc));
}
} else { // concurrent request
var pending_ = Pr(msg, pending);
this.become(serializer_busy_beh(svc, cust, ret, pending_));
}
}
};
};
/*
CREATE undefined WITH \(cust, _).[ SEND ? TO cust ]
*/
var undefined_beh = function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
trace('undefined_beh: req='+msg.tl);
this.send(UNDEF, cust);
}
};
/*
LET empty_env_beh = \(cust, _).[ SEND ? TO cust ]
*/
var empty_env_beh = function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
trace('undefined_beh: req=' + req);
log('Undefined ' + req);
this.send(UNDEF, cust);
}
};
/*
LET env_beh(ident, value, next) = \(cust, req).[
CASE req OF
(#lookup, $ident) : [ SEND value TO cust ]
_ : [ SEND (cust, req) TO next ]
END
]
*/
var env_beh = function (ident, value, next) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'lookup')
&& (req.tl === ident)) {
this.send(value, cust);
} else {
this.send(msg, next);
}
}
};
};
/*
LET dynamic_env_beh(next) = \(cust, req).[
CASE req OF
(#bind, ident, value) : [
CREATE next' WITH env_beh(ident, value, next)
BECOME dynamic_env_beh(next')
SEND SELF TO cust
]
_ : [ SEND (cust, req) TO next ]
END
]
*/
var dynamic_env_beh = function (next) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'bind')
&& Pr.created(req.tl)) {
var ident = req.tl.hd;
var value = req.tl.tl;
var next_ = this.create(
env_beh(ident, value, next),
ident+':'+value
);
this.become(dynamic_env_beh(next_));
this.send(this.self, cust);
} else {
this.send(msg, next);
}
}
};
};
var repl_env_beh = function (dict, next) { /*** WARNING! 'dict' is a MUTABLE value ***/
return function (msg) {
var cust, req, ident, value;
if (Pr.created(msg)) {
cust = msg.hd;
req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'bind')
&& Pr.created(req.tl)) {
ident = req.tl.hd;
value = req.tl.tl;
dict[ident] = value;
this.send(this.self, cust);
} else if (Pr.created(req)
&& (req.hd === 'lookup')) {
ident = req.tl;
value = dict[ident];
if (value === undefined) {
this.send(msg, next);
} else {
this.send(value, cust);
}
} else {
this.send(msg, next);
}
}
}
};
/*
LET block_env_beh(next, denv) = \(cust, req).[
CASE req OF
#self : [ SEND (cust, req) TO denv ]
(#declare, ident) : [
CREATE next' WITH unbound_env_beh(SELF, ident, NIL, next)
BECOME block_env_beh(next', denv)
SEND SELF TO cust
]
_ : [ SEND (cust, req) TO next ]
END
]
*/
var block_env_beh = function (next, denv) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (req === 'self') {
this.send(msg, denv);
} else if (Pr.created(req)
&& (req.hd === 'declare')) {
var ident = req.tl;
var next_ = this.create(
unbound_env_beh(this.self, ident, NIL, next),
ident+':_'
);
this.become(block_env_beh(next_, denv));
this.send(this.self, cust);
} else {
this.send(msg, next);
}
}
};
};
/*
LET unbound_env_beh(scope, ident, waiting, next) = \(cust, req).[
CASE req OF
(#lookup, $ident) : [ # wait for binding
BECOME unbound_env_beh(scope, ident, (cust, waiting), next)
]
(#bind, $ident, value) : [
BECOME bound_env_beh(scope, ident, value, next)
SEND waiting TO NEW \list.[ # deliver value to waiting
IF $list = (first, rest) [
SEND value TO first
SEND rest TO SELF
]
]
SEND scope TO cust
]
_ : [ SEND (cust, req) TO next ]
END
]
*/
var unbound_env_beh = function (scope, ident, waiting, next) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'lookup')
&& (req.tl === ident)) {
this.become(
unbound_env_beh(scope, ident, Pr(cust, waiting), next)
);
} else if (Pr.created(req)
&& (req.hd === 'bind')
&& Pr.created(req.tl)
&& (req.tl.hd === ident)) {
var value = req.tl.tl;
var dispatch = this.create(
function (list) {
if (Pr.created(list)) {
this.send(value, list.hd);
this.send(list.tl, this.self);
}
},
'dispatch:'+value
);
this.become(
bound_env_beh(scope, ident, value, next)
);
this.send(waiting, dispatch);
this.send(scope, cust);
} else {
this.send(msg, next);
}
}
};
};
/*
LET bound_env_beh(scope, ident, value, next) = \(cust, req).[
CASE req OF
(#lookup, $ident) : [ SEND value TO cust ]
(#bind, $ident, $value) : [ SEND scope TO cust ]
(#bind, $ident, value') : [ SEND ? TO cust ]
_ : [ SEND (cust, req) TO next ]
END
]
*/
var bound_env_beh = function (scope, ident, value, next) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'lookup')
&& (req.tl === ident)) {
this.send(value, cust);
} else if (Pr.created(req)
&& (req.hd === 'bind')
&& Pr.created(req.tl)
&& (req.tl.hd === ident)) {
var value_ = req.tl.tl;
this.send(
((value === value_) ? scope : UNDEF),
cust
);
} else {
this.send(msg, next);
}
}
};
};
/*
LET self_env_beh(self, next) = \(cust, req).[
CASE req OF
#self : [ SEND self TO cust ]
_ : [ SEND (cust, req) TO next ]
END
]
*/
var self_env_beh = function (self, next) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (req === 'self') {
this.send(self, cust);
} else {
this.send(msg, next);
}
}
};
};
/*
LET const_expr_beh(value) = \(cust, #eval, _).[
SEND value TO cust
]
*/
var const_expr_beh = function (value) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
this.send(value, cust);
}
}
};
};
/*
LET ident_expr_beh(ident) = \(cust, #eval, env).[
SEND (cust, #lookup, ident) TO env
]
*/
var ident_expr_beh = function (ident) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
this.send(Pr(cust, Pr('lookup', ident)), env);
}
}
};
};
/*
LET abs_expr_beh(ptrn, body_expr) = \(cust, #eval, env).[
SEND NEW closure_beh(ptrn, body_expr, env) TO cust
]
*/
var abs_expr_beh = function (ptrn, body_expr) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
var fn = this.create(
closure_beh(ptrn, body_expr, env),
'closure'
);
fn.isFunction = true; // mark closure-actors as "functions"
this.send(fn, cust);
}
}
};
};
/*
LET closure_beh(ptrn, body, env) = \(cust, #apply, arg).[
SEND (k_env, #match, arg, NEW dynamic_env_beh(env)) TO ptrn
CREATE k_env WITH \env'.[
CASE env' OF
? : [ SEND ? TO cust ]
_ : [ SEND (cust, #eval, env') TO body ]
END
]
]
*/
var closure_beh = function (ptrn, body, env) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'apply')) {
var arg = req.tl;
var k_env = this.create(
function (env_) {
if (env_ === UNDEF) {
this.send(UNDEF, cust);
} else {
this.send(Pr(cust, Pr('eval', env_)), body);
}
}
);
this.send(
Pr(k_env, Pr('match', Pr(arg, this.create(
dynamic_env_beh(env),
'dyn_env'
)))),
ptrn
);
}
}
};
};
/*
LET app_expr_beh(abs_expr, arg_expr) = \(cust, #eval, env).[
CREATE fork WITH fork_beh(k_app, abs_expr, arg_expr)
SEND pair_of(#eval, env) TO fork
CREATE k_app WITH \(abs, arg).[
SEND (cust, #apply, arg) TO abs
]
]
*/
var app_expr_beh = function (abs_expr, arg_expr) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
var k_app = this.create(
function (abs_arg) {
if (Pr.created(abs_arg)) {
var abs = abs_arg.hd;
var arg = abs_arg.tl;
if ((typeof abs !== 'object') || !abs.isFunction) {
/* avoid "send requires an actor" error */
this.send(UNDEF, cust);
} else {
this.send(Pr(cust, Pr('apply', arg)), abs);
}
}
}
);
var fork = this.create(
fork_beh(k_app, abs_expr, arg_expr)
);
this.send(pair_of(req), fork);
}
}
};
};
/*
LET pair_expr_beh(head_expr, tail_expr) = \(cust, #eval, env).[
CREATE fork WITH fork_beh(cust, head_expr, tail_expr)
SEND pair_of(#eval, env) TO fork
]
*/
var pair_expr_beh = function (head_expr, tail_expr) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
var fork = this.create(
fork_beh(cust, head_expr, tail_expr)
);
this.send(pair_of(req), fork);
}
}
};
};
/*
LET case_expr_beh(value_expr, choices) = \(cust, #eval, env).[
SEND (k_value, #eval, env) TO value_expr
CREATE k_value WITH \value.[
SEND (cust, #match, value, env) TO choices
]
]
*/
var case_expr_beh = function (value_expr, choices) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
var k_value = this.create(
function (value) {
this.send(
Pr(cust, Pr('match', Pr(value, env))),
choices
);
}
)
this.send(Pr(k_value, req), value_expr);
}
}
};
};
/*
LET case_choice_beh(ptrn, expr, next) = \(cust, #match, value, env).[
CREATE env' WITH dynamic_env_beh(env)
SEND (k_match, #match, value, env') TO ptrn
CREATE k_match WITH \env'.[
CASE env' OF
? : [ SEND (cust, #match, value, env) TO next ]
_ : [ SEND (cust, #eval, env') TO expr ]
END
]
]
*/
var case_choice_beh = function (ptrn, expr, next) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'match')
&& Pr.created(req.tl)) {
var value = req.tl.hd;
var env = req.tl.tl;
var env_ = this.create(dynamic_env_beh(env), 'dyn_env');
var k_match = this.create(
function (env_) {
if (env_ === UNDEF) {
this.send(msg, next);
} else {
this.send(Pr(cust, Pr('eval', env_)), expr);
}
}
);
this.send(
Pr(k_match, Pr('match', Pr(value, env_))),
ptrn
);
}
}
};
};
/*
CREATE case_end WITH \(cust, #match, _).[ SEND ? TO cust ]
*/
var case_end_beh = function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'match')) {
this.send(UNDEF, cust); // mismatch
}
}
};
/*
LET if_expr_beh(eqtn, expr, else) = \(cust, #eval, env).[
CREATE env' WITH dynamic_env_beh(env)
SEND (k_env, #unify, env') TO eqtn
CREATE k_env WITH \env'.[
CASE env' OF
? : [ SEND (cust, #eval, env) TO else ]
_ : [ SEND (cust, #eval, env') TO expr ]
END
]
]
*/
var if_expr_beh = function (eqtn, expr, next) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
var env_ = this.create(dynamic_env_beh(env), 'dyn_env');
var k_env = this.create(
function (env_) {
if (env_ === UNDEF) {
this.send(msg, next);
} else {
this.send(Pr(cust, Pr('eval', env_)), expr);
}
}
);
this.send(
Pr(k_env, Pr('unify', env_)),
eqtn
);
}
}
};
};
/*
LET let_expr_beh(eqtn, expr) = \(cust, #eval, env).[
BECOME if_expr_beh(eqtn, expr, undefined)
SEND (cust, #eval, env) TO SELF
]
*/
var let_expr_beh = function (eqtn, expr) {
return function (msg) {
var undef = this.create(const_expr_beh(UNDEF), '?');
this.become(if_expr_beh(eqtn, expr, undef));
this.send(msg, this.self);
};
};
/*
LET block_expr_beh(vars, stmt) = \(cust, #eval, env).[
SEND NEW block_beh(vars, stmt, env) TO cust
]
*/
var block_expr_beh = function (vars, stmt) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
this.send(
this.create(
block_beh(vars, stmt, env),
'block'
),
cust
);
}
}
};
};
/*
CREATE now_expr WITH \(cust, #eval, env).[
SEND <timestamp> TO cust
]
*/
var now_expr_beh = function (msg) { // FIXME: non-standard extension
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
this.send(+(new Date()), cust);
}
}
};
/*
CREATE self_expr WITH \(cust, #eval, env).[
SEND (cust, #self) TO env
]
*/
var self_expr_beh = function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
this.send(Pr(cust, 'self'), env);
}
}
};
/*
LET new_expr_beh(b_expr) = \(cust, #eval, env).[
SEND (k_beh, #eval, env) TO b_expr
CREATE k_beh WITH \beh_fn.[
#
# FIXME: INSTANCE NOT TRACKED, SHOULD ASK SPONSOR TO CREATE NEW ACTOR
#
BECOME serializer_beh(NEW self_beh(SELF, beh_fn, env))
SEND SELF TO cust
]
]
*/
var new_expr_beh = function (b_expr) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'eval')) {
var env = req.tl;
var k_beh = this.create(
function (beh_fn) {
this.become(serializer_beh(
this.create(
self_beh(this.self, beh_fn, env),
'actor'
)
));
this.send(this.self, cust);
},
'new'
)
this.send(Pr(k_beh, req), b_expr);
}
}
};
};
/*
LET eqtn_beh(left_ptrn, right_ptrn) = \(cust, #unify, env).[
SEND (cust, #eq, right_ptrn, env) TO left_ptrn
]
*/
var eqtn_beh = function (left_ptrn, right_ptrn) {
return function (msg) {
if (Pr.created(msg)
&& Pr.created(msg.tl)
&& (msg.tl.hd === 'unify')) {
var cust = msg.hd;
var env = msg.tl.tl;
this.send(
Pr(cust, Pr('eq', Pr(right_ptrn, env))),
left_ptrn
);
}
};
};
/*
LET const_ptrn_beh(value) = \(cust, req).[
CASE req OF
(#match, $value, env) : [ SEND env TO cust ]
(#eq, right, env) : [ SEND (cust, #match, value, env) TO right ]
(#bind, left, env) : [ SEND (cust, #match, value, env) TO left ]
(#pair, pair, env) : [ SEND (cust, #match, value, env) TO pair ]
(#value, _, env) : [ SEND value TO cust ]
_ : [ SEND ? TO cust ]
END
]
*/
var const_ptrn_beh = function (value) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'match')
&& Pr.created(req.tl)
&& equal(req.tl.hd, value)) { // use core.equal() to match structued objects (like Pr)
var env = req.tl.tl;
this.send(env, cust); // match
} else if (Pr.created(req)
&& (req.hd === 'eq')
&& Pr.created(req.tl)) {
var right = req.tl.hd;
var env = req.tl.tl;
this.send(
Pr(cust, Pr('match', Pr(value, env))),
right
);
} else if (Pr.created(req)
&& (req.hd === 'bind')
&& Pr.created(req.tl)) {
var left = req.tl.hd;
var env = req.tl.tl;
this.send(
Pr(cust, Pr('match', Pr(value, env))),
left
);
} else if (Pr.created(req)
&& (req.hd === 'pair')
&& Pr.created(req.tl)) {
var pair = req.tl.hd;
var env = req.tl.tl;
this.send(
Pr(cust, Pr('match', Pr(value, env))),
pair
);
} else if (Pr.created(req)
&& (req.hd === 'value')) {
this.send(value, cust); // value
} else {
this.send(UNDEF, cust); // mismatch
}
}
};
};
/*
LET value_ptrn_beh(expr) = \(cust, cmd, arg, env).[
SEND (k_val, #eval, env) TO expr
CREATE k_val WITH \value.[
SEND (cust, cmd, arg, env) TO NEW const_ptrn_beh(value)
]
]
*/
var value_ptrn_beh = function (expr) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& Pr.created(req.tl)) {
var cmd = req.hd;
var arg = req.tl.hd;
var env = req.tl.tl;
var k_val = this.create(
function (value) {
this.send(
msg,
this.create(
const_ptrn_beh(value),
'is:'+value
)
);
}
);
this.send(
Pr(k_val, Pr('eval', env)),
expr
);
}
}
};
};
/*
LET ident_ptrn_beh(ident) = \(cust, req).[
CASE req OF
(#match, value, env) : [ SEND (cust, #bind, ident, value) TO env ]
(#eq, right, env) : [ SEND (cust, #bind, SELF, env) TO right ]
(#pair, pair, env) : [ SEND (cust, #bind, SELF, env) TO pair ]
_ : [ SEND ? TO cust ]
END
]
*/
var ident_ptrn_beh = function (ident) {
return function (msg) {
if (Pr.created(msg)) {
var cust = msg.hd;
var req = msg.tl;
if (Pr.created(req)
&& (req.hd === 'match')
&& Pr.created(req.tl)) {