-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmeta-linklet.rkt
More file actions
1036 lines (929 loc) · 41.6 KB
/
meta-linklet.rkt
File metadata and controls
1036 lines (929 loc) · 41.6 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
997
998
999
1000
#lang racket
(require (only-in racket/unsafe/undefined
check-not-unsafe-undefined
unsafe-undefined))
(require (only-in racket/linklet
correlated?
correlated-e
correlated-source
correlated-line
correlated-column
correlated-position
correlated-span
correlated-property-symbol-keys
correlated-property
correlated->datum
datum->correlated))
(require (for-syntax racket/base))
;;;
;;; ... src/racket/expander/common/make-match.rkt
;;;
;; Yet another pattern matcher along the lines of `syntax-rules`, but
;; intended for relatively simple and small patterns.
;;
;; The `define-match` form generated here has the following syntax to
;; match the result of <s-expr> against <pattern>:
;;
;; (define-match <m-id> <s-expr> <guard> <try> '<pattern>)
;;
;; <guard> = <epsilon> | #:when <expr> | #:unless <expr>
;; <try> = <epsilon> | #:try
;;
;; <pattern> = <id> ; matches anything
;; | id:<id> ; matches only identifiers
;; | (<pattern> ...) ; zero or more
;; | (<pattern> ...+) ; one or more
;; | (<pattern> . <pattern>)
;;
;; Note that the ' before <pattern> doesn't produce a symbol or list;
;; it's just a literal to textually highlight the pattern.
;;
;; The <m-id> bound by `define-match` is used as either
;;
;; (<m-id>)
;;
;; to check whether the match succeeded (which makes sense only if a
;; guard or `#:try` is included) or
;;
;; (<m-id> '<pattern-id>)
;;
;; to access the value for a match. Again, the ' here does not produce
;; a symbol, but serves only as visual highlighting.
;;
;; Unlike `syntax-rules`/`syntax-case`/`syntax-parse`, there's no
;; template system and no help in making sure your uses of variables
;; before `...` expect the right shape. For example, with
;;
;; (define-match m s '(a ...))
;;
;; then `(m 'a)` will always produce a list of matches of `a`.
;;
;; If a pattern doesn't match and there's no `#:try`, then a syntax
;; error is reported.
;;
;; The `define-define-match` form is a macro-generating macro so that
;; it can be used with different underlying notions of syntax, as
;; specific by the `rt-syntax?`, etc., macro arguments.
(define-syntax-rule (define-define-match define-match
rt-syntax? rt-syntax-e rt-raise-syntax-error)
(...
(begin
(define-for-syntax (extract-pattern-ids pattern)
(cond
[(identifier? pattern)
(if (or (eq? '... (syntax-e pattern))
(eq? '...+ (syntax-e pattern)))
null
(list pattern))]
[(symbol? pattern)
(if (or (eq? '... pattern)
(eq? '...+ pattern))
null
(list pattern))]
[(syntax? pattern) (extract-pattern-ids (syntax-e pattern))]
[(pair? pattern)
(append (extract-pattern-ids (car pattern))
(extract-pattern-ids (cdr pattern)))]
[else null]))
;; This pattern compiler has bad time complexity for complex
;; patterns, because it keeps recomputing the set of pattern
;; variables, but we're only going to use it on simple patterns
(define-for-syntax (identifier-pattern? pattern)
(regexp-match? #rx"^id(:|$)" (symbol->string pattern)))
(define-for-syntax (compile-pattern pattern already-checked?)
(cond
[(symbol? pattern)
(if (identifier-pattern? pattern)
(if already-checked?
#'s
#`(if (or (and (rt-syntax? s)
(symbol? (rt-syntax-e s)))
(symbol? s))
s
(rt-raise-syntax-error #f "not an identifier" orig-s s)))
#'s)]
[else
#`(let ([s (if (rt-syntax? s) (rt-syntax-e s) s)])
#,(cond
[(and (list? pattern)
(= (length pattern) 2)
(or (eq? '... (cadr pattern))
(eq? '...+ (cadr pattern))))
(with-syntax ([(pattern-id ...) (extract-pattern-ids (car pattern))])
#`(let ([flat-s (to-syntax-list s)])
(cond
[#,(if already-checked? #'#f #'(not flat-s))
(rt-raise-syntax-error #f "bad syntax" orig-s)]
[#,(if (and (eq? '...+ (cadr pattern)) (not already-checked?)) #'(null? flat-s) #'#f)
(rt-raise-syntax-error #f "bad syntax" orig-s)]
[else
#,(if (and (symbol? (car pattern))
(or (not (identifier-pattern? (car pattern)))
already-checked?))
#`flat-s
#`(for/lists (pattern-id ...) ([s (in-list flat-s)])
#,(compile-pattern (car pattern) already-checked?)))])))]
[(pair? pattern)
(with-syntax ([(a-pattern-id ...) (generate-temporaries (extract-pattern-ids (car pattern)))]
[(d-pattern-id ...) (generate-temporaries (extract-pattern-ids (cdr pattern)))])
#`(if #,(if already-checked? #'#t #'(pair? s))
(let-values ([(a-pattern-id ...) (let ([s (car s)]) #,(compile-pattern (car pattern)
already-checked?))]
[(d-pattern-id ...) (let ([s (cdr s)]) #,(compile-pattern (cdr pattern)
already-checked?))])
(values a-pattern-id ... d-pattern-id ...))
(rt-raise-syntax-error #f "bad syntax" orig-s)))]
[(null? pattern)
(if already-checked?
#'(values)
#'(if (null? s)
(values)
(rt-raise-syntax-error #f "bad syntax" orig-s)))]
[(or (keyword? pattern)
(boolean? pattern))
(if already-checked?
#'(values)
#`(if (eq? '#,pattern s)
(values)
(rt-raise-syntax-error #f "bad syntax" orig-s)))]
[else
(raise-syntax-error 'define-match "bad pattern" pattern)]))]))
(define-for-syntax (compile-pattern-check pattern)
(cond
[(symbol? pattern)
(if (identifier-pattern? pattern)
#`(or (and (rt-syntax? s)
(symbol? (rt-syntax-e s)))
(symbol? s))
#'#t)]
[else
#`(let ([s (if (rt-syntax? s) (rt-syntax-e s) s)])
#,(cond
[(and (list? pattern)
(= (length pattern) 2)
(or (eq? '... (cadr pattern))
(eq? '...+ (cadr pattern))))
(with-syntax ([(pattern-id ...) (extract-pattern-ids (car pattern))])
#`(let ([flat-s (to-syntax-list s)])
(cond
[(not flat-s) #f]
[#,(if (eq? '...+ (cadr pattern)) #'(null? flat-s) #'#f) #f]
[else #,(if (and (symbol? (car pattern))
(not (identifier-pattern? (car pattern))))
#`#t
#`(for/and ([s (in-list flat-s)])
#,(compile-pattern-check (car pattern))))])))]
[(pair? pattern)
(with-syntax ([(a-pattern-id ...) (extract-pattern-ids (car pattern))]
[(d-pattern-id ...) (extract-pattern-ids (cdr pattern))])
#`(and (pair? s)
(let ([s (car s)]) #,(compile-pattern-check (car pattern)))
(let ([s (cdr s)]) #,(compile-pattern-check (cdr pattern)))))]
[(null? pattern)
#'(null? s)]
[(or (keyword? pattern)
(boolean? pattern))
#`(eq? '#,pattern s)]
[else
(raise-syntax-error 'define-match "bad pattern" pattern)]))]))
(define (to-syntax-list s)
(cond
[(list? s) s]
[(pair? s)
(define r (to-syntax-list (cdr s)))
(and r (cons (car s) r))]
[(rt-syntax? s) (to-syntax-list (rt-syntax-e s))]
[else #f]))
(define-syntax (define-match stx)
(syntax-case stx (quote)
[(_ id expr 'pattern)
#'(do-define-match id expr 'pattern #:when #t #:try? #f)]
[(_ id expr #:try 'pattern)
#'(do-define-match id expr 'pattern #:when #t #:try? #t)]
[(_ id expr #:when guard-expr 'pattern)
#'(do-define-match id expr 'pattern #:when guard-expr #:try? #f)]
[(_ id expr #:when guard-expr #:try 'pattern)
#'(do-define-match id expr 'pattern #:when guard-expr #:try? #t)]
[(_ id expr #:unless guard-expr 'pattern)
#'(do-define-match id expr 'pattern #:when (not guard-expr) #:try? #f)]
[(_ id expr #:unless guard-expr #:try 'pattern)
#'(do-define-match id expr 'pattern #:when (not guard-expr) #:try? #t)]))
(define-syntax (do-define-match stx)
(syntax-case stx (quote)
[(_ id expr 'pattern #:when guard-expr #:try? try?)
(let ([pattern-ids (extract-pattern-ids #'pattern)]
[try? (syntax-e #'try?)])
(with-syntax ([(pattern-id ...) pattern-ids]
[(pattern-result-id ...) (generate-temporaries pattern-ids)]
[(false-result ...) (map (lambda (x) #'#f) pattern-ids)]
[matcher (compile-pattern (syntax->datum #'pattern) try?)])
#`(begin
(define-values (ok? pattern-result-id ...)
(let ([s expr])
(if (and guard-expr
#,(if try?
(compile-pattern-check (syntax->datum #'pattern))
#'#t))
(let ([orig-s s])
(let-values ([(pattern-result-id ...) matcher])
(values #t pattern-result-id ...)))
(values #f false-result ...))))
(define-syntax id
(syntax-rules (quote pattern-id ...)
[(m) ok?]
[(m (quote pattern-id))
pattern-result-id]
...)))))])))))
;;;
;;; .../racket/src/expander/host/correlate.rkt
;;;
(define (correlated->list e)
(let loop ([e e])
(cond
[(list? e) e]
[(pair? e) (cons (car e) (loop (cdr e)))]
[(null? e) null]
[(syntax? e) (loop (syntax-e e))]
[else (error 'correlated->list "not a list")])))
(define (correlated-cadr e)
(car (correlated-e (cdr (correlated-e e)))))
(define (correlate src-e s-exp)
(define e (datum->correlated s-exp src-e))
(define maybe-n (syntax-property src-e 'inferred-name))
(if maybe-n
(syntax-property e 'inferred-name maybe-n)
e))
(define-define-match define-correlated-match
syntax? syntax-e (lambda (false str e) (error str)))
;;;
;;; src/expander/syntax/datum-map.rkt
;;;
;; `(datum-map v f)` walks over `v`, traversing objects that
;; `datum->syntax` traverses to convert content to syntax objects.
;;
;; `(f tail? d)` is called on each datum `d`, where `tail?`
;; indicates that the value is a pair/null in a `cdr` --- so that it
;; doesn't need to be wrapped for `datum->syntax`, for example;
;; the `tail?` argument is actually #f or a fixnum for a lower bound
;; on `cdr`s that have been taken
;;
;; `gf` is like `f`, but `gf` is used when the argument might be
;; syntax; if `gf` is provided, `f` can assume that its argument
;; is not syntax
;;
;; If a `seen` argument is provided, then it should be an `eq?`-based
;; hash table, and cycle checking is enabled; when a cycle is
;; discovered, the procedure attached to 'cycle-fail in the initial
;; table is called
;;
;; If a `known-pairs` argument is provided, then it should be an
;; `eq?`-based hash table to map pairs that can be returned as-is
;; in a `tail?` position
;; The inline version uses `f` only in an application position to
;; help avoid allocating a closure. It also covers only the most common
;; cases, defering to the general (not inlined) function for other cases.
(define fx+ +)
(define fx> >)
(define (datum-map s f [gf f] [seen #f] [known-pairs #f])
(let loop ([tail? #f] [s s] [prev-depth 0])
(define depth (fx+ 1 prev-depth)) ; avoid cycle-checking overhead for shallow cases
(cond
[(and seen (depth . fx> . 32))
(datum-map-slow tail? s (lambda (tail? s) (gf tail? s)) seen known-pairs)]
[(null? s) (f tail? s)]
[(pair? s)
(f tail? (cons (loop #f (car s) depth)
(loop 1 (cdr s) depth)))]
[(symbol? s) (f #f s)]
[(boolean? s) (f #f s)]
[(number? s) (f #f s)]
[(or (vector? s) (box? s) (prefab-struct-key s) (hash? s))
(datum-map-slow tail? s (lambda (tail? s) (gf tail? s)) seen known-pairs)]
[else (gf #f s)])))
(define (datum-map-slow tail? s f seen known-pairs)
(let loop ([tail? tail?] [s s] [prev-seen seen])
(define seen
(cond
[(and prev-seen (datum-has-elements? s))
(cond
[(hash-ref prev-seen s #f)
((hash-ref prev-seen 'cycle-fail) s)]
[else (hash-set prev-seen s #t)])]
[else prev-seen]))
(cond
[(null? s) (f tail? s)]
[(pair? s)
(cond
[(and known-pairs
tail?
(hash-ref known-pairs s #f))
s]
[else
(f tail? (cons (loop #f (car s) seen)
(loop (if tail? (fx+ 1 tail?) 1) (cdr s) seen)))])]
[(or (symbol? s) (boolean? s) (number? s))
(f #f s)]
[(vector? s)
(f #f (vector->immutable-vector
(for/vector #:length (vector-length s) ([e (in-vector s)])
(loop #f e seen))))]
[(box? s)
(f #f (box-immutable (loop #f (unbox s) seen)))]
#;[(immutable-prefab-struct-key s)
=> (lambda (key)
(f #f
(apply make-prefab-struct
key
(for/list ([e (in-vector (struct->vector s) 1)])
(loop #f e seen)))))]
[(and (hash? s) (immutable? s))
(f #f
(hash-map/copy s
(lambda (k v)
(values k (loop #f v seen)))
#:kind 'immutable))]
[else (f #f s)])))
(define (datum-has-elements? d)
(or (pair? d)
(vector? d)
(box? d)
#;(immutable-prefab-struct-key d)
(and (hash? d) (immutable? d) (positive? (hash-count d)))))
;;;
;;; src/expander/run/correlated-to-host-syntax.rkt
;;;
(define (correlated->host-syntax v)
(datum-map v
(lambda (tail? v)
(cond
[(correlated? v)
(define e (correlated->host-syntax (correlated-e v)))
(define s (datum->syntax #f
e
(vector (correlated-source v)
(correlated-line v)
(correlated-column v)
(correlated-position v)
(correlated-span v))))
(define keys (correlated-property-symbol-keys v))
(for/fold ([s s]) ([key (in-list keys)])
(syntax-property s key (correlated-property v key)))]
[else v]))))
;;;
;;; src/expander/run/linklet.rkt
;;;
(struct linklet (name) #:transparent)
(struct source-linklet linklet (src) #:transparent)
(struct compiled-linklet linklet (proc ; takes self instance plus instance arguments to run the linklet body
importss ; list [length is 1 less than proc arity] of list of symbols
exports) ; list of symbols
#:transparent)
(struct instance (name ; for debugging, typically a module name + phase
data ; any value (e.g., a namespace)
variables) ; symbol -> value
#:transparent)
(define (make-instance name [data #f] [mode #f] . content)
(define i (instance name data (make-hasheq)))
(let loop ([content content])
(cond
[(null? content) (void)]
[else
(unless (symbol? (car content))
(raise-argument-error 'make-instance
"symbol?"
(car content)))
(when (null? (cdr content))
(raise-arguments-error 'make-instance
"missing variable value"
"variable" (car content)))
(instance-set-variable-value! i (car content) (cadr content) mode)
(loop (cddr content))]))
i)
(define (instance-variable-names i)
; todo: filter unset variables
(hash-keys (instance-variables i)))
; helper
(define (instance-variable-box i sym can-create?)
(or (hash-ref (instance-variables i) sym #f)
(if can-create?
(let ([b (box undefined)])
(hash-set! (instance-variables i) sym b)
b)
(error 'link "missing binding: ~s" sym))))
(define (instance-set-variable-value! i sym val [constant? #f])
; todo: this doesn't handle the mode
(set-box! (instance-variable-box i sym #t) val))
(define (instance-unset-variable! i sym)
(set-box! (instance-variable-box i sym #t) undefined))
(define (instance-variable-value i sym [fail-k (lambda () (error "instance variable not found:" sym))])
(define b (hash-ref (instance-variables i) sym #f))
(cond
[(and b
(not (eq? (unbox b) undefined)))
(unbox b)]
[(procedure? fail-k) (fail-k)]
[else fail-k]))
(define (instance-describe-variable! i sym desc)
(void))
;; ----------------------------------------
(define undefined (gensym 'undefined))
(define (check-not-undefined val sym)
(if (eq? val undefined)
(check-not-unsafe-undefined unsafe-undefined sym)
val))
;; ----------------------------------------
(define (primitive-table name)
(cond
[(eq? name '#%bootstrap-linklet) #f]
#;[(eq? name '#%linklet) (linklet-operations=> reflect-hash)]
[else
(define mod-name `(quote ,name))
(define-values (vars trans) (module->exports mod-name))
(for/hasheq ([sym (in-list (map car (cdr (assv 0 vars))))])
(values sym
(dynamic-require mod-name sym)))]))
;; Bootstrap implementation doesn't support bytecode:
(define (primitive->compiled-position v) #f)
(define (compiled-position->primitive pos) #f)
(define (primitive-in-category? name cat-sym) #f)
(define (primitive-lookup sym) #f)
;; ----------------------------------------
(struct variable-reference (instance primitive-varref))
(define (variable-reference->instance vr [ref-site? #f])
(and (or ref-site?
;; It would be better to have a `variable-reference-anonymous?` predicate:
; TODO
#;(with-handlers ([exn:fail? (lambda (exn) #f)])
(variable-reference->module-declaration-inspector
(variable-reference-primitive-varref vr))))
;; Always returning ref-site instance; that's good enough to
;; bootstrap:
(variable-reference-instance vr)))
(define variable-reference-constant?*
(let ([variable-reference-constant?
(lambda (vr)
(variable-reference-constant? (variable-reference-primitive-varref vr)))])
variable-reference-constant?))
(define variable-reference-from-unsafe?*
(let ([variable-reference-from-unsafe?
(lambda (vr)
(variable-reference-from-unsafe? (variable-reference-primitive-varref vr)))])
variable-reference-from-unsafe?))
;; ----------------------------------------
; https://github.com/racket/racket/ ... /racket/src/expander/boot/runtime-primitive.rkt#L11
;; Instances that are built into the runtime system, but
;; not including '#%linklet
(define runtime-instances
'(#%kernel
#%paramz
#%foreign
#%unsafe
#%flfxnum
#%extfl
#%network
#%place
#%futures
#%terminal))
(define cu-namespace (make-empty-namespace))
(namespace-attach-module (current-namespace) ''#%builtin cu-namespace)
(parameterize ([current-namespace cu-namespace])
(for ([name (in-list runtime-instances)])
(namespace-require `',name))
(namespace-require ''#%linklet)
(namespace-set-variable-value! 'check-not-undefined check-not-undefined)
(namespace-set-variable-value! 'instance-variable-box instance-variable-box)
(namespace-set-variable-value! 'variable-reference variable-reference)
(namespace-set-variable-value! 'variable-reference? variable-reference? #t)
(namespace-set-variable-value! 'variable-reference->instance variable-reference->instance #t)
(namespace-set-variable-value! 'variable-reference-constant? variable-reference-constant?* #t)
(namespace-set-variable-value! 'variable-reference-from-unsafe? variable-reference-from-unsafe?* #t)
;; Needed when the host is RacketCS:
#;(namespace-set-variable-value! 'fasl->s-exp/intern (lambda (v)
(fasl->s-exp v #:datum-intern? #t))))
;; ----------------------------------------
;; (linklet [[imported-id/renamed ...] ...]
;; [exported-id/renamed ...]
;; defn-or-expr ...)
;; imported-id/renamed = imported-id
;; | (external-imported-id internal-imported-id)
;; exported-id/renamed = exported-id
;; | (internal-exported-id external-exported-id)
;; Compile a `linklet` to a plain `lambda`. Also, convert from the
;; notion of correlated that works for `compile-linklet` to the notion
;; of host syntax objects that works for `compile`.
(define (desugar-linklet c)
(define imports (list-ref c 1))
(define exports (list-ref c 2))
(define bodys (list-tail c 3))
(define inst-names (for/list ([import (in-list imports)]
[i (in-naturals)])
(string->symbol (format "in_~a" i))))
(define import-box-bindings
(for/list ([inst-imports (in-list imports)]
[inst (in-list inst-names)]
#:when #t
[name (in-list inst-imports)])
(define ext (if (symbol? name) name (car name))) ; external name
(define int (if (symbol? name) name (cadr name))) ; internal name
`[(,int) (instance-variable-box ,inst ',ext #f)]))
(define export-box-bindings
(for/list ([name (in-list exports)])
(define int (if (symbol? name) name (car name)))
(define ext (if (symbol? name) name (cadr name)))
`[(,int) (instance-variable-box self-inst ',ext #t)]))
(define box-bindings (append import-box-bindings export-box-bindings))
(define import-box-syms (apply seteq (map caar import-box-bindings)))
(define box-syms (set-union import-box-syms
(apply seteq (map caar export-box-bindings))))
(define (desugar e)
(displayln e)
(cond
[(symbol? e) (if (set-member? box-syms e)
(if (set-member? import-box-syms e)
`(unbox ,e)
`(check-not-undefined (unbox ,e) ',e))
e)]
[(pair? e)
(case (car e)
[(quote) e]
[(set!)
(match e
[(list 'set! var rhs)
(if (set-member? box-syms var)
`(set-box! ,var ,(desugar rhs))
`(set! ,var ,(desugar rhs)))])]
[(define-values)
(match e
[(list 'define-values (list ids ...) rhs)
(define tmps (map gensym ids))
`(define-values ,(for/list ([id (in-list ids)]
#:when (not (set-member? box-syms id)))
id)
(let-values ([,tmps (let-values ([,ids ,(desugar rhs)])
(values ,@ids))])
(begin
,@(for/list ([id (in-list ids)]
[tmp (in-list tmps)]
#:when (set-member? box-syms id))
`(set-box! ,id ,tmp))
(values ,@(for/list ([id (in-list ids)]
[tmp (in-list tmps)]
#:when (not (set-member? box-syms id)))
tmp)))))])]
[(lambda)
(match e
[(list 'lambda formals body)
`(lambda ,formals ,(desugar body))])]
[(case-lambda)
(match e
[(list 'case-lambda [list formals body] ...)
`(case-lambda ,@(for/list ([formals (in-list formals)]
[body (in-list body)])
`[,formals ,(desugar body)]))])]
[(#%variable-reference)
(if (and (pair? (cdr e))
(set-member? box-syms (cadr e)))
;; Using a plain `#%variable-reference` (for now) means
;; that all imported and exported variables count as
;; mutable:
'(variable-reference self-inst (#%variable-reference))
;; Preserve info about a local identifier:
`(variable-reference self-inst ,e))]
[else (map desugar e)])]
[else e]))
#;(define (desugar e)
(displayln e)
(cond
[(correlated? e)
(correlate e (desugar (correlated-e e)))]
[(symbol? e) (if (set-member? box-syms e)
(if (set-member? import-box-syms e)
`(unbox ,e)
`(check-not-undefined (unbox ,e) ',e))
e)]
[(pair? e)
(case (let ([a (car e)]) (if (correlated? a) (correlated-e a) a))
[(quote) e]
[(set!)
(define-correlated-match m e '(set! var rhs))
(if (set-member? box-syms (correlated-e (m 'var)))
`(set-box! ,(m 'var) ,(desugar (m 'rhs)))
`(set! ,(m 'var) ,(desugar (m 'rhs))))]
[(define-values)
(define-correlated-match m e '(define-values (id ...) rhs))
(define ids (m 'id))
(define tmps (map gensym (map correlated-e ids)))
`(define-values ,(for/list ([id (in-list ids)]
#:when (not (set-member? box-syms (correlated-e id))))
id)
(let-values ([,tmps (let-values ([,ids ,(desugar (m 'rhs))])
(values ,@ids))])
(begin
,@(for/list ([id (in-list ids)]
[tmp (in-list tmps)]
#:when (set-member? box-syms (correlated-e id)))
`(set-box! ,id ,tmp))
(values ,@(for/list ([id (in-list ids)]
[tmp (in-list tmps)]
#:when (not (set-member? box-syms (correlated-e id))))
tmp)))))]
[(lambda)
(define-correlated-match m e '(lambda formals body))
`(lambda ,(m 'formals) ,(desugar (m 'body)))]
[(case-lambda)
(define-correlated-match m e '(case-lambda [formals body] ...))
`(case-lambda ,@(for/list ([formals (in-list (m 'formals))]
[body (in-list (m 'body))])
`[,formals ,(desugar body)]))]
[(#%variable-reference)
(if (and (pair? (correlated-e (cdr (correlated-e e))))
(set-member? box-syms (correlated-e (correlated-cadr e))))
;; Using a plain `#%variable-reference` (for now) means
;; that all imported and exported variables count as
;; mutable:
'(variable-reference self-inst (#%variable-reference))
;; Preserve info about a local identifier:
`(variable-reference self-inst ,e))]
[else (map desugar (correlated->list e))])]
[else e]))
(define (last-is-definition? bodys)
(define p (car (reverse bodys)))
(and (pair? p) (eq? (correlated-e (car p)) 'define-values)))
(correlated->host-syntax
`(lambda (self-inst ,@inst-names)
(let-values ,box-bindings
,(cond
[(null? bodys) '(void)]
[else
`(begin
,@(for/list ([body (in-list bodys)])
(desugar body))
,@(if (last-is-definition? bodys)
'((void))
null))])))))
;; #:pairs? #f -> list of list of symbols
;; #:pairs? #t -> list of list of (cons ext-symbol int-symbol)
(define (extract-import-variables-from-expression c #:pairs? pairs?)
(for/list ([is (in-list (unmarshal (list-ref c 1)))])
(for/list ([i (in-list is)])
(cond
[pairs? (if (symbol? i)
(cons i i)
(cons (car i) (cadr i)))]
[else (if (symbol? i)
i
(car i))]))))
;; #:pairs? #f -> list of symbols
;; #:pairs? #t -> list of (cons ext-symbol int-symbol)
(define (extract-export-variables-from-expression c #:pairs? pairs?)
(for/list ([e (in-list (unmarshal (list-ref c 2)))])
(cond
[pairs? (if (symbol? e)
(cons e e)
(cons (cadr e) (car e)))]
[else (if (symbol? e)
e
(cadr e))])))
;; ----------------------------------------
(define orig-eval (current-eval))
(define orig-compile (current-compile))
(define linklet-compile-to-s-expr (make-parameter #f #f 'linklet-compile-to-s-expr))
;; Compile to a serializable form
(define (compile-linklet c
[info #f]
[import-keys #f]
[get-import (lambda (key) (values #f #f))]
[options '(serializable)])
(define l
(cond
[(linklet-compile-to-s-expr)
(source-linklet (marshal (correlated->datum/lambda-name c)))]
[else
(define plain-c (desugar-linklet c))
(parameterize ([current-namespace cu-namespace]
[current-eval orig-eval]
[current-compile orig-compile])
;; Use a vector to list the exported variables
;; with the compiled bytecode
(compiled-linklet (compile plain-c)
(marshal (extract-import-variables-from-expression c #:pairs? #f))
(marshal (extract-export-variables-from-expression c #:pairs? #f))))]))
(if import-keys
(values l import-keys) ; no imports added or removed
l))
;; ----------------------------------------
(struct path-bytes (bstr) #:prefab)
(struct unreadable (str) #:prefab)
(struct void-value () #:prefab)
(struct srcloc-parts (source line column position span) #:prefab)
(define (marshal c)
(datum-map c (lambda (tail? c)
(cond
[(path? c) (path-bytes (path->bytes c))]
[(and (symbol? c)
(symbol-unreadable? c)) (unreadable (symbol->string c))]
[(void? c) (void-value)]
[(srcloc? c) (srcloc-parts (marshal (srcloc-source c))
(marshal (srcloc-line c))
(marshal (srcloc-column c))
(marshal (srcloc-position c))
(marshal (srcloc-span c)))]
[else c]))))
(define (unmarshal c)
(datum-map c
(lambda (tail? c)
(cond
[(path-bytes? c) (bytes->path (path-bytes-bstr c))]
[(unreadable? c) (string->unreadable-symbol (unreadable-str c))]
[(void-value? c) (void)]
[(srcloc-parts? c) (srcloc (marshal (srcloc-parts-source c))
(marshal (srcloc-parts-line c))
(marshal (srcloc-parts-column c))
(marshal (srcloc-parts-position c))
(marshal (srcloc-parts-span c)))]
[else c]))))
;; Like `correlated->datum`, but preserves 'inferred-name information
;; by encoding it as a symbol in a `lambda` or `case-lambda` body.
;; Remove any existing symbol in the name position that might
;; otherwise be confused for the name. This conversion avoids parsing
;; expressions in general by relying on the fact that bindings are
;; renamed to avoid shadowing, `lambda`, `case-lambda`, or `quote`.
(define (correlated->datum/lambda-name c)
(define (strip-potential-name-from-body body)
(define-correlated-match m body #:try '(begin (quote _) body bodys ...))
(cond
[(and (m)
(eq? 'begin (m 'begin))
(eq? 'quote (m 'quote)))
(strip-potential-name-from-body
(if (null? (m 'bodys))
(m 'body)
`(begin ,@(m 'bodys))))]
[else body]))
(let correlated->datum/lambda-name ([c c])
(cond
[(and (pair? c)
(eq? (car c) 'lambda))
(define-correlated-match m c '(lambda args body))
`(lambda ,(correlated->datum (m 'args))
,(correlated->datum/lambda-name
(strip-potential-name-from-body (m 'body))))]
[(and (pair? c)
(eq? (car c) 'case-lambda))
(define-correlated-match m c '(case-lambda [argss bodys] ...))
`(case-lambda
,@(for/list ([args (in-list (m 'argss))]
[body (in-list (m 'bodys))])
`[,(correlated->datum args)
,(correlated->datum/lambda-name
(strip-potential-name-from-body body))]))]
[(and (pair? c)
(eq? (car c) 'quote))
(correlated->datum c)]
[(pair? c)
(cons (correlated->datum/lambda-name (car c))
(correlated->datum/lambda-name (cdr c)))]
[(and (correlated? c)
(let ([e (correlated-e c)])
(and (pair? e)
(or (eq? 'lambda (car e))
(eq? 'case-lambda (car e)))))
(correlated-property c 'inferred-name))
=> (lambda (name)
(cond
[(void? name)
;; Don't try to hide the name after all
(correlated->datum/lambda-name (correlated-e c))]
[else
;; Encode `name` as a symbol in the function body:
(define lam (correlated->datum/lambda-name (correlated-e c)))
(cond
[(eq? 'lambda (car lam))
(define-correlated-match m lam '(lambda args body))
`(lambda ,(m 'args) (begin (quote ,name) ,(m 'body)))]
[else
(define-correlated-match m lam '(case-lambda [argss bodys] ...))
(cond
[(null? (m 'argss))
;; give up on naming an empty `case-lambda`
lam]
[else
`(case-lambda
[,(car (m 'argss)) (begin (quote ,name) ,(car (m 'bodys)))]
,@(cddr lam))])])]))]
[(correlated? c)
(correlated->datum/lambda-name (correlated-e c))]
[else
(correlated->datum c)])))
(define (instantiate-linklet linklet ; a linklet?
import-instances ; a list of instances
[target-instance #f] ; #f or an instance
[use-prompt? #f]) ; ignored by webracket
; Type check linklet
(unless (linklet? linklet)
(raise-argument-error 'instantiate-linklet "linklet?" linklet))
(unless (compiled-linklet? linklet)
(raise-argument-error 'instantiate-linklet "compiled-linklet?" linklet))
; Type check import-instances
(let loop ([l import-instances])
(unless (null? l)
(if (and (pair? l) (instance? (car l)))
(loop (cdr l))
(raise-argument-error 'instantiate-linklet "(listof instance?)" import-instances))))
; Type check the target instance
(unless (or (not target-instance) (instance? target-instance))
(raise-argument-error 'instantiate-linklet "(or/c instance? #f)" target-instance))
;; From now on, we can assume the linklet is a compiled-linklet.
; Check that the length of import-instances match compiled-linklets-importss
(unless (= (length (compiled-linklet-importss linklet))
(length import-instances))
(raise-argument-error 'instantiate-linklet
"the number of import instances does not match the expected number of imports"))
; Two cases:
; 1) If `target-instance` is an existing instance, then
; the instance is used and modified for the linklet definitions and expressions, and
; the result is the value of the last expression in the linklet.
; 2) If target-instance is #f or not provided,
; the result is a fresh instance for the linklet.
(cond
[target-instance
;; Instantiate into the given instance and return the result of the linklet body.
(apply (compiled-linklet-proc linklet) target-instance import-instances)]
[else
;; Make a fresh instance, recur, and return the instance
(let ([i (make-instance (linklet-name linklet))])
(instantiate-linklet linklet import-instances i use-prompt?)
i)]))
(module+ test
(require rackunit)
(define (make-test-compiled-linklet name proc import-count [exports '()])
(compiled-linklet name proc (build-list import-count (lambda (_) '())) exports))
(define simple-linklet
(make-test-compiled-linklet
'simple
(lambda (self)
(instance-set-variable-value! self 'v 'fresh)
'simple-result)
0
'(v)))
(define target-linklet
(make-test-compiled-linklet
'target
(lambda (self)
(instance-set-variable-value! self 'v 'target)
'target-result)
0
'(v)))
(define one-import-linklet
(make-test-compiled-linklet
'needs-import
(lambda (self imported)
(values self imported))
1))
(test-case "raises when linklet argument is not a linklet"
(check-exn exn:fail:contract?
(lambda () (instantiate-linklet 42 '()))))
(test-case "raises when linklet is not compiled"
(check-exn exn:fail:contract?
(lambda () (instantiate-linklet (linklet 'plain) '()))))
(test-case "raises when import list contains non-instances"
(check-exn exn:fail:contract?
(lambda () (instantiate-linklet one-import-linklet (list 'not-an-instance)))))
(test-case "raises when target-instance is not an instance"
(check-exn exn:fail:contract?
(lambda () (instantiate-linklet simple-linklet '() 'not-an-instance))))
(test-case "raises when import count does not match compiled linklet"
(check-exn exn:fail:contract?
(lambda () (instantiate-linklet one-import-linklet '()))))
(test-case "creates a fresh instance when no target is provided"
(define result (instantiate-linklet simple-linklet '()))
(check-true (instance? result))
(check-equal? (instance-name result) 'simple)