-
Notifications
You must be signed in to change notification settings - Fork 7
/
bit.scm
1889 lines (1661 loc) · 56.1 KB
/
bit.scm
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
; Copyright (C) 1995 Danny Dube, Universite de Montreal. All rights reserved.
; Les fonctions utilitaires generales
; Suppose que les deux arguments sont deja des ensembles de symboles
(define symbol-set-union
(lambda (ss1 ss2)
(cond ((null? ss1)
ss2)
((memq (car ss1) ss2)
(symbol-set-union (cdr ss1) ss2))
(else
(cons (car ss1) (symbol-set-union (cdr ss1) ss2))))))
(define symbol-set-intersection
(lambda (ss1 ss2)
(cond ((null? ss1)
'())
((memq (car ss1) ss2)
(cons (car ss1) (symbol-set-intersection (cdr ss1) ss2)))
(else
(symbol-set-intersection (cdr ss1) ss2)))))
(define foldr
(lambda (binop start l)
(if (null? l)
start
(binop (car l) (foldr binop start (cdr l))))))
(define foldr1
(lambda (binop l)
(if (null? (cdr l))
(car l)
(binop (car l) (foldr1 binop (cdr l))))))
(define filter
(lambda (pred? l)
(cond ((null? l) '())
((pred? (car l)) (cons (car l) (filter pred? (cdr l))))
(else (filter pred? (cdr l))))))
(define formals->varlist
(lambda (formals)
(cond ((symbol? formals)
(list formals))
((null? formals)
'())
(else
(cons (car formals) (formals->varlist (cdr formals)))))))
(define prefix?
(lambda (s1 s2)
(let ((l1 (string-length s1))
(l2 (string-length s2)))
(if (> l1 l2)
#f
(let loop ((i 0))
(cond ((= i l1)
#t)
((char=? (string-ref s1 i) (string-ref s2 i))
(loop (+ i 1)))
(else
#f)))))))
(define unprefix
(lambda (s1 s2)
(let ((l1 (string-length s1)))
(cond ((= l1 (string-length s2))
(string-append s1 "a"))
((char=? #\a (string-ref s2 l1))
(string-append s1 "b"))
(else
(string-append s1 "a"))))))
; Initialiser les variables globales du programme
(define init-glob-vars
(lambda ()
(set! safe-name-memv '<memv>)
(set! safe-name-make-promise '<make-promise>)
(set! safe-name-list->vector '<list->vector>)
(set! safe-name-list '<list>)
(set! safe-name-append2 '<append2>)
(set! safe-name-cons '<cons>)
(set! gen-sym-pref #f)
(set! gen-sym-number 0)
(set! libcprims #f)
(set! libalias #f)
(set! libclos #f)
(set! libpublics #f)
(set! libnames #f)
(set! apply1-cprim-no #f)
(set! dirreq #f)
(set! allreq #f)
(set! req-clos-nodes #f)
(set! const-counter 0)
(set! const-alist '())
(set! top-counter 0)
(set! top-alist '())
(set! const-desc-string #f)
(set! glob-counter 0)
(set! glob-hidden '())
(set! glob-public '())
(set! glob-source '())
(set! glob-v '#())
(set! glob-v-len 0)
(set! phys-glob-no 0)
(set! program-bytecode #f)
(set! label-counter 0)
(set! label-v '#())
(set! label-v-len 0)
(set! flat-program-bytecode #f)
(set! final-program-bytecode #f)
(set! glob-var-init-codes #f)))
; Lire le programme source
(define read-source
(lambda ()
(let loop ()
(let ((exp (read)))
(if (eof-object? exp) '()
(cons exp (loop)))))))
; Trouver des noms pour memv, make-promise, list->vector, list,
; append2 et cons. De cette facon, trans-case & cie plus loin
; pourront inserer un symbole representant une fonction
; du systeme aux endroits generes par les macros-expansion
(define safe-name-memv '<memv>)
(define safe-name-make-promise '<make-promise>)
(define safe-name-list->vector '<list->vector>)
(define safe-name-list '<list>)
(define safe-name-append2 '<append2>)
(define safe-name-cons '<cons>)
; Cette fonction ne fonctionne que pour les struc. non-circ.
(define find-all-symbols
(lambda (d)
(cond ((symbol? d)
(list d))
((pair? d)
(symbol-set-union (find-all-symbols (car d))
(find-all-symbols (cdr d))))
((vector? d)
(let loop ((pos (- (vector-length d) 1)))
(if (< pos 0)
'()
(symbol-set-union (find-all-symbols (vector-ref d pos))
(loop (- pos 1))))))
(else
'()))))
; Trouver un prefixe unique pour avoir un gen-sym correct
(define find-uniq-prefix
(lambda (ss)
(let loop ((pref "") (names (map symbol->string ss)))
(if (null? names)
pref
(let ((name (car names)))
(if (prefix? pref name)
(loop (unprefix pref name) (cdr names))
(loop pref (cdr names))))))))
; La fonction gen-sym
(define gen-sym-pref #f)
(define gen-sym-number 0)
(define gen-sym
(lambda ()
(let* ((str-num (number->string gen-sym-number))
(sym-name (string-append gen-sym-pref str-num))
(sym (string->symbol sym-name)))
(set! gen-sym-number (+ gen-sym-number 1))
sym)))
; Traduction des expressions Scheme en expressions plus simples
; Il ne reste que des references de var., des quotes, des literaux
; s'evaluant a eux-memes, des appels de procedure, des lambda exp.,
; des if, des set!, des begins, des defines se rapportant a des
; var. glob. uniquement
; A ne pas inserer dans la liste des translators
(define trans-define
(lambda (l)
(if (symbol? (cadr l))
l
(let ((procname (caadr l))
(formals (cdadr l))
(exps (cddr l)))
`(define ,procname (lambda ,formals ,@exps))))))
; A ne pas inserer dans la liste des translators
(define flatten-begin
(lambda (expbegin)
(cons 'begin
(let loop ((l (cdr expbegin)) (flat '()))
(if (null? l)
flat
(let ((tete (car l))
(queue (cdr l)))
(if (and (pair? tete) (eq? (car tete) 'begin))
(loop (cdr tete) (loop queue flat))
(cons tete (loop queue flat)))))))))
; A ne pas inserer dans la liste des translators
(define extract-define
(lambda (lexp)
(let ((tete (car lexp))
(reste (cdr lexp)))
(if (and (pair? tete) (eq? (car tete) 'define))
(let ((result (extract-define reste)))
(cons (cons tete (car result)) (cdr result)))
(cons '() lexp)))))
; A ne pas inserer dans la liste des translators
(define trans-body
(lambda (l)
(let* ((flatbody (flatten-begin l))
(result (extract-define (cdr flatbody)))
(rawdefines (car result))
(exps (cdr result))
(defines (map trans-define rawdefines))
(decls (map cdr defines))
(body `(begin ,@exps)))
(if (null? decls)
body
`(letrec ,decls ,body)))))
; A ne pas inserer dans la liste des translators
(define trans-lambda
(lambda (l)
(list 'lambda
(cadr l)
(trans-body (cons 'begin (cddr l))))))
; A ne pas inserer dans la liste des translators
(define trans-begin
(lambda (l)
(let ((flat (flatten-begin l)))
(if (null? (cddr flat))
(cadr flat)
flat))))
; A ne pas inserer dans la liste des translators
(define trans-normal-let
(lambda (l)
(let* ((bindings (cadr l))
(body (cddr l))
(vars (map car bindings))
(inits (map cadr bindings)))
`((lambda ,vars ,@body) ,@inits))))
; A ne pas inserer dans la liste des translators
(define trans-let-loop
(lambda (l)
(let* ((loop-name (cadr l))
(bindings (caddr l))
(body (cdddr l))
(vars (map car bindings))
(inits (map cadr bindings)))
`((letrec ((,loop-name (lambda ,vars ,@body)))
,loop-name)
,@inits))))
(define trans-let
(lambda (l)
(if (symbol? (cadr l))
(trans-let-loop l)
(trans-normal-let l))))
(define trans-let*
(lambda (l)
(let ((bindings (cadr l))
(body (cddr l)))
(if (or (null? bindings) (null? (cdr bindings)))
`(let ,bindings ,@body)
(let ((prem (car bindings))
(reste (cdr bindings)))
`(let (,prem) (let* ,reste ,@body)))))))
(define trans-letrec
(lambda (l)
(let ((bindings (cadr l))
(body (cddr l)))
(if (null? bindings)
`(let () ,@body)
(let* ((vars (map car bindings))
(inits (map cadr bindings))
(falsebind (map (lambda (v) `(,v #f)) vars))
(set!s (map (lambda (v i) `(set! ,v ,i)) vars inits)))
`(let ,falsebind
,@set!s
(let () ,@body)))))))
(define trans-and
(lambda (l)
(cond ((null? (cdr l))
#t)
((null? (cddr l))
(cadr l))
(else
`(if ,(cadr l) (and ,@(cddr l)) #f)))))
(define trans-or
(lambda (l)
(cond ((null? (cdr l))
#f)
((null? (cddr l))
(cadr l))
(else
(let* ((e-hd (cadr l))
(e-tl (cddr l))
(tmp (gen-sym)))
`(let ((,tmp ,e-hd))
(if ,tmp
,tmp
(or ,@e-tl))))))))
(define trans-cond
(lambda (l)
(if (null? (cdr l))
#f
(let* ((clause (cadr l))
(autres (cddr l))
(newcond (cons 'cond autres)))
(cond ((eq? (car clause) 'else)
(cons 'begin (cdr clause)))
((null? (cdr clause))
(list 'or (car clause) newcond))
((eq? (cadr clause) '=>)
(let* ((test (car clause))
(recipient (caddr clause))
(tmp (gen-sym)))
`(let ((,tmp ,test))
(if ,tmp
(,recipient ,tmp)
,newcond))))
(else
(let* ((test (car clause))
(actions (cdr clause))
(conseq (cons 'begin actions)))
`(if ,test ,conseq ,newcond))))))))
(define trans-case
(lambda (l)
(let* ((tmp-key (gen-sym))
(trans-test
(lambda (test)
(if (eq? test 'else) 'else `(,safe-name-memv ,tmp-key ',test))))
(key (cadr l))
(clauses (cddr l))
(tests (map car clauses))
(expr-lists (map cdr clauses))
(memv-tests (map trans-test tests))
(cond-clauses (map cons memv-tests expr-lists)))
`(let ((,tmp-key ,key)) (cond ,@cond-clauses)))))
(define trans-do
(lambda (l)
(let* ((normalize-step (lambda (v sf)
(if (null? sf) v (car sf))))
(bindings (cadr l))
(testnsequence (caddr l))
(commands (cdddr l))
(vars (map car bindings))
(inits (map cadr bindings))
(steps-fac (map cddr bindings))
(steps (map normalize-step vars steps-fac))
(test (car testnsequence))
(sequence (cdr testnsequence))
(loop-var (gen-sym))
(loop-call (cons loop-var steps))
(loop-bindings (map list vars inits)))
`(let ,loop-var ,loop-bindings
(if ,test
(begin
#f
,@sequence)
(begin
,@commands
,loop-call))))))
(define trans-delay
(lambda (exp)
(let ((delexp (cadr exp)))
`(,safe-name-make-promise (lambda () ,delexp)))))
; A ne pas inclure dans la liste des translators
(define detect-unquote
(lambda (exp level)
(cond ((vector? exp)
(let loop ((pos (- (vector-length exp) 1)))
(cond ((< pos 0)
#f)
((detect-unquote (vector-ref exp pos) level)
#t)
(else
(loop (- pos 1))))))
((pair? exp)
(let ((tete (car exp)))
(cond ((eq? tete 'quasiquote)
(detect-unquote (cadr exp) (+ level 1)))
((or (eq? tete 'unquote) (eq? tete 'unquote-splicing))
(if (= level 1)
#t
(detect-unquote (cadr exp) (- level 1))))
(else
(or (detect-unquote tete level)
(detect-unquote (cdr exp) level))))))
(else
#f))))
(define trans-quasiquote
(lambda (l)
(let loop ((exp (cadr l)) (level 1))
(cond ((not (detect-unquote exp level))
(list 'quote exp))
((vector? exp)
(list safe-name-list->vector
(loop (vector->list exp) level)))
((pair? exp)
(let ((tete (car exp)))
(cond ((eq? tete 'quasiquote)
(list safe-name-list
''quasiquote
(loop (cadr exp) (+ level 1))))
((eq? tete 'unquote)
(if (= level 1)
(cadr exp)
(list safe-name-list
''unquote
(loop (cadr exp) (- level 1)))))
((and (pair? tete)
(eq? (car tete) 'unquote-splicing)
(= level 1))
(if (null? (cdr exp))
(cadr tete)
(list safe-name-append2
(cadr tete)
(loop (cdr exp) level))))
((eq? tete 'unquote-splicing)
(list safe-name-list
''unquote-splicing
(loop (cadr exp) (- level 1))))
(else
(list safe-name-cons
(loop (car exp) level)
(loop (cdr exp) level))))))))))
(define translators
(list (cons 'let trans-let)
(cons 'let* trans-let*)
(cons 'letrec trans-letrec)
(cons 'and trans-and)
(cons 'or trans-or)
(cons 'cond trans-cond)
(cons 'case trans-case)
(cons 'do trans-do)
(cons 'delay trans-delay)
(cons 'quasiquote trans-quasiquote)))
(define trans-sub
(lambda (exp)
(if (or (boolean? exp)
(symbol? exp)
(char? exp)
(number? exp)
(string? exp))
exp
(let ((tete (car exp)))
(cond ((eq? tete 'quote)
exp)
((eq? tete 'lambda)
(let ((new-lambda (trans-lambda exp)))
(list 'lambda
(cadr new-lambda)
(trans-sub (caddr new-lambda)))))
((eq? tete 'if)
(cons 'if (map trans-sub (cdr exp))))
((eq? tete 'set!)
(list 'set! (cadr exp) (trans-sub (caddr exp))))
((eq? tete 'begin)
(trans-begin (cons 'begin (map trans-sub (cdr exp)))))
((eq? tete 'define)
(let ((new-define (trans-define exp)))
(list 'define
(cadr new-define)
(trans-sub (caddr new-define)))))
(else
(let ((ass (assq tete translators)))
(if ass
(trans-sub ((cdr ass) exp))
(let ((new-exp (map trans-sub exp)))
(if (and (pair? (car new-exp))
(eq? (caar new-exp) 'lambda)
(null? (cadar new-exp)))
(caddar new-exp)
new-exp))))))))))
; Operations sur les nodes
(define make-cte-node
(lambda (cte) (vector 0 cte #f)))
(define make-ref-node
(lambda (symbol) (vector 1 symbol #f #f #f)))
(define make-ref-node-full
(lambda (symbol loc val glob?) (vector 1 symbol loc val glob?)))
(define make-lambda-node
(lambda (formals body) (vector 2 formals #f body #f)))
(define make-if-node
(lambda (test conseq altern) (vector 3 test conseq altern)))
(define make-set!-node
(lambda (symbol exp) (vector 4 symbol #f exp #f)))
(define make-begin-node
(lambda (lexp) (vector 5 lexp)))
(define make-def-node
(lambda (symbol exp) (vector 6 symbol #f exp)))
(define make-call-node
(lambda (op larg) (vector 7 op larg)))
(define make-globdesc-node
(lambda (symbol lib? nbaff) (vector 8 symbol lib? nbaff #f #f #f)))
(define node-type
(lambda (node)
(vector-ref node 0)))
(define cte-node? (lambda (node) (= (node-type node) 0)))
(define ref-node? (lambda (node) (= (node-type node) 1)))
(define lambda-node? (lambda (node) (= (node-type node) 2)))
(define if-node? (lambda (node) (= (node-type node) 3)))
(define set!-node? (lambda (node) (= (node-type node) 4)))
(define begin-node? (lambda (node) (= (node-type node) 5)))
(define def-node? (lambda (node) (= (node-type node) 6)))
(define call-node? (lambda (node) (= (node-type node) 7)))
(define globdesc-node? (lambda (node) (= (node-type node) 8)))
(define getter1 (lambda (node) (vector-ref node 1)))
(define getter2 (lambda (node) (vector-ref node 2)))
(define getter3 (lambda (node) (vector-ref node 3)))
(define getter4 (lambda (node) (vector-ref node 4)))
(define getter5 (lambda (node) (vector-ref node 5)))
(define getter6 (lambda (node) (vector-ref node 6)))
(define get-cte getter1)
(define get-no getter2)
(define get-symbol getter1)
(define get-loc getter2)
(define get-val getter3)
(define get-glob? getter4)
(define get-formals getter1)
(define get-fdesc getter2)
(define get-body getter3)
(define get-label getter4)
(define get-test getter1)
(define get-conseq getter2)
(define get-altern getter3)
(define get-exp getter3)
(define get-lexp getter1)
(define get-op getter1)
(define get-larg getter2)
(define get-lib? getter2)
(define get-nbaff getter3)
(define get-init getter4)
(define get-libno getter5)
(define get-srcno getter6)
(define setter1 (lambda (node val) (vector-set! node 1 val)))
(define setter2 (lambda (node val) (vector-set! node 2 val)))
(define setter3 (lambda (node val) (vector-set! node 3 val)))
(define setter4 (lambda (node val) (vector-set! node 4 val)))
(define setter5 (lambda (node val) (vector-set! node 5 val)))
(define setter6 (lambda (node val) (vector-set! node 6 val)))
(define set-no! setter2)
(define set-glob?! setter4)
(define set-loc! setter2)
(define set-fdesc! setter2)
(define set-nbaff! setter3)
(define set-init! setter4)
(define set-op! setter1)
(define set-val! setter3)
(define set-libno! setter5)
(define set-srcno! setter6)
(define set-label! setter4)
; Transformation du code source en noeuds fonctionnels.
(define lnode #f)
(define exp->node
(lambda (exp)
(cond ((or (boolean? exp) (char? exp) (number? exp) (string? exp))
(make-cte-node exp))
((symbol? exp)
(make-ref-node exp))
(else ; pair
(let ((tete (car exp)))
(cond ((eq? tete 'quote)
(make-cte-node (cadr exp)))
((eq? tete 'lambda)
(make-lambda-node (cadr exp) (exp->node (caddr exp))))
((eq? tete 'if)
(make-if-node (exp->node (cadr exp))
(exp->node (caddr exp))
(exp->node
(if (null? (cdddr exp)) #f (cadddr exp)))))
((eq? tete 'set!)
(make-set!-node (cadr exp) (exp->node (caddr exp))))
((eq? tete 'begin)
(make-begin-node (map exp->node (cdr exp))))
((eq? tete 'define)
(make-def-node (cadr exp) (exp->node (caddr exp))))
(else ; procedure call
(make-call-node (exp->node (car exp))
(map exp->node (cdr exp))))))))))
; Ramassage des variables globales definies, modifiees ou lues
(define extract-glob-names
(let ((action-v
(vector
(lambda (node loop env) ; cte
'())
(lambda (node loop env) ; ref
(let ((refsym (get-symbol node)))
(if (memq refsym env)
'()
(list refsym))))
(lambda (node loop env) ; lambda
(loop (get-body node)
(symbol-set-union env (formals->varlist
(get-formals node)))))
(lambda (node loop env) ; if
(let ((test-globs (loop (get-test node) env))
(conseq-globs (loop (get-conseq node) env))
(altern-globs (loop (get-altern node) env)))
(symbol-set-union (symbol-set-union test-globs conseq-globs)
altern-globs)))
(lambda (node loop env) ; set!
(let* ((set!sym (get-symbol node))
(l (if (memq set!sym env) '() (list set!sym))))
(symbol-set-union l (loop (get-exp node) env))))
(lambda (node loop env) ; begin
(let* ((lnode (get-lexp node))
(llglob (map (lambda (node) (loop node env)) lnode)))
(foldr1 symbol-set-union llglob)))
(lambda (node loop env) ; def
(symbol-set-union (list (get-symbol node))
(loop (get-exp node) env)))
(lambda (node loop env) ; call
(let* ((lnode (cons (get-op node) (get-larg node)))
(llglob (map (lambda (node) (loop node env)) lnode)))
(foldr1 symbol-set-union llglob))))))
(lambda (node)
(let loop ((node node) (env '()))
((vector-ref action-v (node-type node)) node loop env)))))
; Chargement de la librairie
(define libcprims #f)
(define libalias #f)
(define libclos #f)
(define libpublics #f)
(define libnames #f)
(define apply1-cprim-no #f)
(define read-lib
(lambda (libname)
(let ((port (open-input-file libname)))
(let loop1 ((n 0))
(if (= n 4)
(begin
(close-input-port port)
'())
(let loop2 ()
(let* ((datum (read port)))
(if datum
(let ((reste (loop2)))
(cons (cons datum (car reste)) (cdr reste)))
(cons '() (loop1 (+ n 1)))))))))))
(define get-lib-cprims
(lambda (libpart1)
(map (lambda (def)
(let ((name (car def))
(no (cdr def)))
(if (eq? name 'apply1)
(set! apply1-cprim-no no))
(cons name no)))
libpart1)))
(define get-lib-alias
(lambda (libpart2 libpart3 libpart4)
(let* ((defs (append libpart2 libpart3 libpart4))
(defals (filter (lambda (def)
(and (not (symbol? def))
(symbol? (caddr def))))
defs)))
(map (lambda (defal) (cons (cadr defal) (caddr defal))) defals))))
(define get-lib-clos
(lambda (libpart2 libpart3 libpart4)
(let* ((defs (append libpart2 libpart3 libpart4))
(defcls (filter (lambda (def)
(and (not (symbol? def))
(not (symbol? (caddr def)))))
defs)))
(map (lambda (defcl) (cons (cadr defcl) (caddr defcl))) defcls))))
(define get-lib-publics
(lambda (libpart3 libpart4)
(map (lambda (sym-or-def)
(if (symbol? sym-or-def)
sym-or-def
(cadr sym-or-def)))
(append libpart3 libpart4))))
(define get-lib-names
(lambda (libpart1 libpart2 libpart3 libpart4)
(let* ((cprim-names (map car libpart1))
(defs (append libpart2 libpart3 libpart4))
(truedefs (filter (lambda (def) (not (symbol? def))) defs)))
(append cprim-names (map cadr truedefs)))))
(define load-lib
(lambda ()
(let* ((alllib (read-lib "librairie.scm"))
(libpart1 (list-ref alllib 0))
(libpart2 (list-ref alllib 1))
(libpart3 (list-ref alllib 2))
(libpart4 (list-ref alllib 3)))
(set! libcprims (get-lib-cprims libpart1))
(set! libalias (get-lib-alias libpart2 libpart3 libpart4))
(set! libclos (get-lib-clos libpart2 libpart3 libpart4))
(set! libpublics (get-lib-publics libpart3 libpart4))
(set! libnames (get-lib-names libpart1 libpart2 libpart3 libpart4)))))
; Capture de la librairie necessaire
(define dirreq #f)
(define allreq #f)
(define req-clos-nodes #f)
(define grab-lib
(lambda (dirreq)
(let loop ((toadd dirreq) (added '()) (clos-nodes '()))
(cond ((null? toadd)
(set! allreq added)
(set! req-clos-nodes clos-nodes))
((memq (car toadd) added)
(loop (cdr toadd) added clos-nodes))
(else
(let* ((newfun (car toadd))
(ass-cprim (assq newfun libcprims)))
(if ass-cprim
(loop (cdr toadd)
(cons newfun added)
clos-nodes)
(let ((ass-alias (assq newfun libalias)))
(if ass-alias
(loop (cons (cdr ass-alias) (cdr toadd))
(cons newfun added)
clos-nodes)
(let ((ass-clos (assq newfun libclos)))
(let* ((code (cdr ass-clos))
(sub-code (trans-sub code))
(node (exp->node sub-code))
(node-globs (extract-glob-names node))
(new-clos (cons newfun node)))
(loop (append node-globs (cdr toadd))
(cons newfun added)
(cons new-clos clos-nodes)))))))))))))
; Ramassage et codage des constantes du programme source
(define const-counter 0)
(define const-alist '())
; Chaque ass: (original numero . desc)
(define top-counter 0)
(define top-alist '())
; Chaque ass: (const-no . top-const-no)
(define const-desc-string #f)
(define const-no
(lambda (d)
(let ((ass (assoc d const-alist)))
(if ass
(cadr ass)
(begin
(cond ((or (null? d) (boolean? d) (char? d) (number? d))
(set! const-alist
(cons (cons d (cons const-counter d)) const-alist)))
((pair? d)
(let* ((leftno (const-no (car d)))
(rightno (const-no (cdr d)))
(desc (cons leftno rightno)))
(set! const-alist (cons (cons d (cons const-counter desc))
const-alist))))
((string? d)
(set! const-alist
(cons (cons d (cons const-counter d)) const-alist)))
((symbol? d)
(let* ((nom (symbol->string d))
(nomno (const-no nom))
(desc (string->symbol (number->string nomno))))
(set! const-alist (cons (cons d (cons const-counter desc))
const-alist))))
((vector? d)
(let* ((listd (vector->list d))
(listno (map const-no listd))
(desc (list->vector listno)))
(set! const-alist (cons (cons d (cons const-counter desc))
const-alist)))))
(set! const-counter (+ const-counter 1))
(- const-counter 1))))))
(define top-const-no
(lambda (d)
(if (or (null? d) (boolean? d) (char? d) (number? d))
#f
(let* ((cno (const-no d))
(ass (assv cno top-alist)))
(if ass
(cdr ass)
(begin
(set! top-alist (cons (cons cno top-counter) top-alist))
(set! top-counter (+ top-counter 1))
(- top-counter 1)))))))
(define code-abs-number
(lambda (n)
(let* ((msb (quotient n 256))
(lsb (modulo n 256))
(msc (integer->char msb))
(lsc (integer->char lsb)))
(string msc lsc))))
(define code-one-const
(lambda (desc)
(cond ((null? desc)
"0") ; 0 pour EMPTY
((pair? desc)
(string-append "1" ; 1 pour PAIR
(code-abs-number (car desc))
(code-abs-number (cdr desc))))
((boolean? desc)
(if desc "2t" "2f")) ; 2 pour BOOLEAN
((char? desc)
(string #\3 desc)) ; 3 pour CHAR
((string? desc)
(string-append "4" ; 4 pour STRING
(code-abs-number (string-length desc))
desc))
((symbol? desc)
(string-append "5" ; 5 pour SYMBOL
(code-abs-number
(string->number (symbol->string desc)))))
((number? desc)
(string-append "6" ; 6 pour NUMBER
(if (< desc 0) "-" "+")
(code-abs-number (abs desc))))
((vector? desc)
(let* ((listref (vector->list desc))
(listcodes (map code-abs-number listref))
(listallcodes
(cons "7" ; 7 pour VECTOR
(cons (code-abs-number (vector-length desc))
listcodes))))
(apply string-append listallcodes))))))
(define code-in-const
(lambda (nbconst const-alist)
(let* ((right-alist (reverse const-alist))
(listdesc (map cddr right-alist))
(listcodes (map code-one-const listdesc))
(listallcodes (cons (code-abs-number nbconst) listcodes)))
(apply string-append listallcodes))))
(define code-top-const
(lambda (nbtop top-alist)
(let* ((right-alist (reverse top-alist))
(listtop (map car right-alist))
(listcodes (map code-abs-number listtop))
(listallcodes (cons (code-abs-number nbtop) listcodes)))
(apply string-append listallcodes))))
(define code-const
(lambda ()
(string-append (code-in-const const-counter const-alist)
(code-top-const top-counter top-alist))))
; Enregistrement des variables globales
(define glob-counter 0)
(define glob-hidden '()) ; Variables cachees de la librairie
(define glob-public '()) ; Variables visibles de la librairie
(define glob-source '()) ; Variables introduites par le source
; Chaque assoc: (nom . numero)
(define glob-v '#())
(define glob-v-len 0)
(define glob-var-no
(lambda (name lib?)
(or
(cond ((memq name libpublics)
(let ((ass (assq name glob-public)))
(if ass
(cdr ass)
(let ((newass (cons name glob-counter)))
(set! glob-public (cons newass glob-public))
#f))))
(lib?
(let ((ass (assq name glob-hidden)))
(if ass
(cdr ass)
(let ((newass (cons name glob-counter)))
(set! glob-hidden (cons newass glob-hidden))
#f))))
(else
(let ((ass (assq name glob-source)))
(if ass
(cdr ass)
(let ((newass (cons name glob-counter)))
(set! glob-source (cons newass glob-source))
#f)))))
(begin
(if (= glob-counter glob-v-len)
(let* ((newlen (+ (* 2 glob-v-len) 1))
(newv (make-vector newlen)))
(let loop ((pos 0))
(if (< pos glob-v-len)
(begin
(vector-set! newv pos (vector-ref glob-v pos))
(loop (+ pos 1)))))
(set! glob-v newv)
(set! glob-v-len newlen)))
(vector-set! glob-v glob-counter (make-globdesc-node name lib? 0))
(set! glob-counter (+ glob-counter 1))
(- glob-counter 1)))))
; Localisation des variables
; Un resultat (glob . name) signifie que name est globale
; Un resultat (lex #frame . #offset) donne le numero de frame et la
; position sur ce frame
; Un resultat (lex #frame . #f) indique que name est seule sur son frame
(define where-var
(lambda (name env)
(if (null? env)
(cons 'glob name)
(let* ((locals (car env))
(membership (memq name locals)))
(if membership
(let* ((nblocals (length locals))
(pos (- nblocals (length membership)))
(declpos (if (= nblocals 1) #f pos)))
(cons 'lex (cons 0 declpos)))
(let ((result (where-var name (cdr env))))
(if (eq? (car result) 'glob)
result
(let ((frame (cadr result))
(offset (cddr result)))
(cons 'lex (cons (+ frame 1) offset))))))))))