-
Notifications
You must be signed in to change notification settings - Fork 0
/
all-cps.ss
1294 lines (1215 loc) · 48.3 KB
/
all-cps.ss
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
(load "chez-init.ss")
(define apply-k
(lambda (k . vals)
(apply k vals)))
; #datatypes
; _____ _ _
; | __ \ | | | |
; | | | | __ _| |_ __ _| |_ _ _ _ __ ___ ___
; | | | |/ _` | __/ _` | __| | | | '_ \ / _ \/ __|
; | |__| | |_| | || |_| | |_| |_| | |_| | __/\__ \
; |_____/ \__,_|\__\__,_|\__|\__, | .__/ \___||___/
; __/ | |
; |___/|_|
(define-datatype expression expression?
[var-exp
(id symbol?)]
[lit-exp
(id (lambda (x)
(or (number? x) (string? x) (boolean? x) (null? x) (vector? x))))]
[quoted-exp
(id (lambda x #t))]
[if-else-exp
(condition expression?)
(true expression?)
(false expression?)]
[if-exp
(condition expression?)
(true expression?)]
[let-exp
(vars (list-of symbol?))
(declarations (list-of expression?))
(body (list-of expression?))]
[named-let-exp
(name symbol?)
(vars (list-of symbol?))
(declarations (list-of expression?))
(body (list-of expression?))]
[let*-exp
(vars (list-of symbol?))
(declarations (list-of expression?))
(body (list-of expression?))]
[letrec-exp
(vars (list-of symbol?))
(declarations (list-of expression?))
(body (list-of expression?))]
[lambda-list-exp
(id (list-of symbol?))
(body (list-of expression?))]
[lambda-ref-exp
(id (list-of symbol?))
(refs (list-of number?))
;(refs (list-of symbol?))
(body (list-of expression?))]
[lambda-single-exp
(id symbol?)
(body (list-of expression?))]
[lambda-improper-exp
(id (list-of symbol?))
(other symbol?)
(body (list-of expression?))]
[set!-exp
(var symbol?)
(expr expression?)]
[cond-exp
(lefts (list-of expression?))
(rights (list-of expression?))
(else expression?)]
[or-exp
(conditions (list-of expression?))]
[begin-exp
(exps (list-of expression?))]
[case-exp
(expr expression?)
(lefts (list-of expression?))
(rights (list-of expression?))
(else expression?)]
[while-exp
(condition expression?)
(exprs (list-of expression?))]
[app-exp
(rator expression?)
(rand (list-of expression?))]
[define-exp
(var symbol?)
(expr expression?)]
)
(define-datatype environment environment?
(empty-env-record)
(extended-env-record
(syms (list-of symbol?))
(vals (list-of box?))
(env environment?)))
; datatype for procedures. At first there is only one
; kind of procedure, but more kinds will be added later.
(define-datatype proc-val proc-val?
[prim-proc
(name symbol?)]
[closure
(ids (list-of symbol?))
(bodies (list-of expression?))
(env environment?)]
[closure-single-arg
(id symbol?)
(bodies (list-of expression?))
(env environment?)]
[closure-improper-args
(ids (list-of symbol?))
(other symbol?)
(bodies (list-of expression?))
(env environment?)]
[closure-ref
(ids (list-of symbol?))
(refs (list-of number?))
(bodies (list-of expression?))
(env environment?)])
; environment type definitions
(define scheme-value?
(lambda (x) #t))
; #parser
; _____
; | __ \
; | |__| |_ _ _ __ ___ ___ _ __
; | ___/ _` | '__/ __|/ _ \ '__|
; | | | |_| | | \__ \ __/ |
; |_| \__,_|_| |___/\___|_|
; Procedures to make the parser a little bit saner.
(define 1st car)
(define 2nd cadr)
(define 3rd caddr)
(define 4th cadddr)
; Gets everything but the last element of an improper list
; Ex: improper-lst = '(a b . c)
; result = (a b)
(define get-all-but-last-improper
(lambda (improper-lst)
(let loop [[lst improper-lst]
[result '()]]
(if (pair? lst)
(loop (cdr lst) (append result (list (car lst))))
result))))
; Gets the last element of an improper list
; Ex: improper-lst = '(a b . c)
; result = c
(define get-last-improper
(lambda (improper-lst)
(let loop [[lst improper-lst]]
(if (pair? lst)
(loop (cdr lst))
lst))))
; Takes an object to check and a list. Returns true if obj is in the list, false otherwise
(define member?
(lambda (obj list)
(not (not (member obj list)))))
; Takes a list-of expression and extracts the lefts of each expression except the last
(define get-lefts
(lambda (exp)
(let loop [[exp exp]
[result '()]]
(cond
[(= (length exp) 1) result]
;[else (loop (cdr exp) (cons (caar exp) result))]
[else (loop (cdr exp) (append result (list (caar exp))))]
))))
; Takes a list-of expression and extracts the rights of each expression except the last
(define get-rights
(lambda (exp)
(let loop [[exp exp]
[result '()]]
(cond
[(= (length exp) 1) result]
[else (loop (cdr exp) (append result (list (cadar exp))))]
))))
(define get-ids
(lambda (args)
(map (lambda (x)
(if (list? x)
(cadr x)
x)) args)))
(define get-refs
(lambda (args)
(let loop [[args args]
[result '()]
[count 0]]
(cond
[(null? args) (reverse result)]
[(list? (car args)) ; assume ref
(loop (cdr args) (cons count result) (+ count 1))]
[else
(loop (cdr args) result (+ count 1))]
))))
(define parse-exp
(lambda (datum)
(cond
[(symbol? datum) (var-exp datum)]
[(and (list? datum) (eqv? (1st datum) 'quote)) (quoted-exp (2nd datum))]
[(or (number? datum) (string? datum) (boolean? datum) (null? datum) (vector? datum)) (lit-exp datum)]
[(eqv? (1st datum) 'lambda)
(cond
[(list? (2nd datum)) ; args is proper list
(if (valid-lambda-list-exp? datum)
(lambda-list-exp
(2nd datum)
(map parse-exp (cddr datum)))
(lambda-ref-exp
(get-ids (2nd datum))
(get-refs (2nd datum))
(map parse-exp (cddr datum)))
)]
[(symbol? (2nd datum)) ; single arg
(if (valid-lambda-single-exp? datum)
(lambda-single-exp
(2nd datum)
(map parse-exp (cddr datum))))]
[else ; improper list
(if (valid-lambda-improper-exp? datum)
(lambda-improper-exp
(get-all-but-last-improper (2nd datum))
(get-last-improper (2nd datum))
(map parse-exp (cddr datum))))]
)]
[(eqv? (1st datum) 'if)
(if (valid-if? datum)
(if (eqv? (length datum) 4)
(if-else-exp ; if-then-else
(parse-exp (2nd datum))
(parse-exp (3rd datum))
(parse-exp (4th datum)))
(if-exp ; if-then
(parse-exp (2nd datum))
(parse-exp (3rd datum)))))]
[(eqv? (1st datum) 'cond)
(cond-exp
(map parse-exp (get-lefts (cdr datum)))
(map parse-exp (get-rights (cdr datum)))
(parse-exp (cadar (last-pair datum))))]
[(eqv? (1st datum) 'let)
(if (symbol? (2nd datum)) ; named let
(if (valid-named-let-exp? datum)
(named-let-exp
(2nd datum)
(map 1st (3rd datum))
(map (lambda (x) (parse-exp (2nd x))) (3rd datum))
(map parse-exp (cdddr datum))))
(if (valid-let-exp? datum)
(let-exp ; normal let
(map 1st (2nd datum))
(map (lambda (x) (parse-exp (2nd x))) (2nd datum))
(map parse-exp (cddr datum)))))]
[(eqv? (1st datum) 'let*)
(if (valid-let*-exp? datum)
(let*-exp
(map 1st (2nd datum))
(map (lambda (x) (parse-exp (2nd x))) (2nd datum))
(map parse-exp (cddr datum))))]
[(eqv? (1st datum) 'letrec)
(if (valid-letrec-exp? datum)
(letrec-exp
(map 1st (2nd datum))
(map (lambda (x) (parse-exp (2nd x))) (2nd datum))
(map parse-exp (cddr datum))))]
[(eqv? (1st datum) 'set!)
(if (valid-set!-exp? datum)
(set!-exp
(2nd datum)
(parse-exp (3rd datum))))]
[(eqv? (1st datum) 'or)
(or-exp (map parse-exp (cdr datum)))]
[(eqv? (1st datum) 'begin)
(begin-exp
(map parse-exp (cdr datum)))]
[(eqv? (1st datum) 'case)
(case-exp
(parse-exp (2nd datum))
(map quoted-exp (get-lefts (cddr datum)))
(map parse-exp (get-rights (cddr datum)))
(parse-exp (cadar (last-pair datum))))]
[(eqv? (1st datum) 'while)
(while-exp
(parse-exp (2nd datum))
(map parse-exp (cddr datum)))]
[(eqv? (1st datum) 'define)
(define-exp
(2nd datum)
(parse-exp (3rd datum)))]
[(valid-app-exp? datum)
(app-exp
(parse-exp (1st datum))
(map parse-exp (cdr datum)))]
[else (eopl:error 'parse-exp "bad expression: ~s" datum)])))
; #environment
; ______ _ _
; | ____| |_| | |
; | |__ _ ____ ___ _ __ ___ _ __ _ __ ___ ___ _ __ | |_
; | __| | '_ \ \ / / | '__/ _ \| '_ \| '_ ` _ \ / _ \ '_ \| __|
; | |____| | | \ V /| | | | |_| | | | | | | | | | __/ | | | |_
; |______|_| |_|\_/ |_|_| \___/|_| |_|_| |_| |_|\___|_| |_|\__|
; Environment definitions for CSSE 304 Scheme interpreter. Based on EoPL section 2.3
; produces a representation of the empty environment
(define empty-env
(lambda ()
(empty-env-record)))
(define extend-env
(lambda (syms vals env)
(begin ;(display '(*** extend-env ***)) (newline) (display '-syms:) (display syms) (newline) (display '-vals:) (display vals) (newline) (display '-env:) (display env) (newline) (newline)
(extended-env-record syms (map box vals) env))))
(define extend-env-with-refs
(lambda (syms boxed-vals env)
(begin ;(display '(*** extend-env ***)) (newline) (display '-syms:) (display syms) (newline) (display '-vals:) (display vals) (newline) (display '-env:) (display env) (newline) (newline)
(extended-env-record syms boxed-vals env))))
(define list-find-position
(lambda (sym los)
(list-index (lambda (xsym) (eqv? sym xsym)) los)))
(define list-index
(lambda (pred ls)
(cond
((null? ls) #f)
((pred (car ls)) 0)
(else (let ((list-index-r (list-index pred (cdr ls))))
(if (number? list-index-r)
(+ 1 list-index-r)
#f))))))
;(define apply-env
; (lambda (env sym succeed fail) ; succeed and fail are procedures applied if the var is or isn't found, respectively.
; (cases environment env
; (empty-env-record ()
; (fail))
; (extended-env-record (syms vals env)
;(let ((pos (list-find-position sym syms)))
; (if (number? pos)
; (succeed (deref (list-ref vals pos)))
; (apply-env env sym succeed fail)))))))
(define apply-env
(lambda (env sym succeed fail)
(deref (apply-env-ref env sym succeed fail))))
(define (apply-env-ref env sym succeed fail)
(cases environment env
(empty-env-record ()
(fail))
(extended-env-record (syms vals env)
(let ((pos (list-find-position sym syms)))
(if (number? pos)
(succeed (list-ref vals pos))
(apply-env-ref env sym succeed fail))))))
(define (deref ref)
(unbox ref))
(define (set-ref! ref value)
(set-box! ref value))
(define (reset-global-env)
(set! global-env init-env))
; #syntax expander
; _____ _ ______ _
; / ____| | | | ____| | |
; | (___ _ _ _ __ | |_ __ ___ __ | |__ __ ___ __ __ _ _ __ __| | ___ _ __
; \___ \| | | | '_ \| __/ _` \ \/ / | __| \ \/ / '_ \ / _` | '_ \ / _` |/ _ \ '__|
; ____) | |_| | | | | || (_| |> < | |____ > <| |_) | (_| | | | | (_| | __/ |
; |_____/ \__, |_| |_|\__\__,_/_/\_\ |______/_/\_\ .__/ \__,_|_| |_|\__,_|\___|_|
; __/ | | |
; |___/ |_|
(define syntax-expand
(lambda (exp)
(cases expression exp
[var-exp (id) exp]
[lit-exp (id) exp]
[quoted-exp (id) exp]
[if-else-exp (condition true false)
(if-else-exp
(syntax-expand condition)
(syntax-expand true)
(syntax-expand false))]
[if-exp (condition true)
(if-else-exp
(syntax-expand condition)
(syntax-expand true)
(app-exp (var-exp 'void) '()))]
[named-let-exp (name vars declarations bodies)
(syntax-expand
(letrec-exp (list name) (list (lambda-list-exp vars bodies)) (list (app-exp (var-exp name) declarations))))]
[let-exp (vars declarations bodies)
(syntax-expand
(app-exp
(lambda-list-exp
vars
bodies)
declarations))]
[let*-exp (vars declarations bodies)
(syntax-expand
(if (= (length vars) 0)
(let-exp
'()
'()
bodies)
(let-exp
(list (car vars))
(list (car declarations))
(list (let*-exp
(cdr vars)
(cdr declarations)
bodies)))))]
[letrec-exp (vars declarations bodies)
(let [[random-symbols (make-list-of-random-symbols (length vars))]]
(syntax-expand
(let-exp
vars
(map lit-exp (make-list-of #f (length vars)))
(list (let-exp
random-symbols
declarations
(append
(map (lambda (var temp)
(set!-exp var (var-exp temp))) vars random-symbols)
bodies
))))))]
[lambda-list-exp (id bodies)
(lambda-list-exp
id
(map syntax-expand bodies))]
[lambda-ref-exp (ids refs bodies)
(lambda-ref-exp
ids
refs
(map syntax-expand bodies))]
[lambda-single-exp (id bodies) exp]
[lambda-improper-exp (id other bodies) exp]
[set!-exp (var expr)
(set!-exp
var
(syntax-expand expr))]
[app-exp (rator rands)
(begin ;(display exp) (newline) (display rands)
(app-exp
(syntax-expand rator)
(map syntax-expand rands)))]
[cond-exp (conditions expressions else)
(syntax-expand (cond
[(and (null? conditions) (null? expressions))
else]
[else
(if-else-exp
(car conditions)
(car expressions)
(cond-exp
(cdr conditions)
(cdr expressions)
else))]))]
[begin-exp (exps)
(app-exp (lambda-list-exp
'()
(map syntax-expand exps))
'())]
[case-exp (expr lefts rights else)
(syntax-expand
(if (null? lefts)
(begin-exp (list expr else))
(let [[temp (generate-random-symbol)]]
(let-exp
(list temp)
(list expr)
(list
(let loop [[lefts lefts] [rights rights]]
(if-else-exp
(app-exp (var-exp 'member?) (list (var-exp temp) (car lefts)))
(car rights)
(if (not (null? (cdr lefts)))
(loop (cdr lefts) (cdr rights))
else))))))))]
[while-exp (condition exprs)
(begin ;(display condition) (newline) (display exprs)
(let [[temp (generate-random-symbol)]]
(syntax-expand
(let-exp
(list 'x)
(list (lambda-list-exp
(list temp)
(list (if-exp
condition
(begin-exp
(append exprs
(list (app-exp (var-exp temp) (list (var-exp temp))))))))))
(list (app-exp (var-exp 'x) (list (var-exp 'x))))))))]
[or-exp (exps)
(syntax-expand
(let [[temp (generate-random-symbol)]]
(cond
[(null? exps) (lit-exp #f)]
[(null? (cdr exps)) (car exps)]
[else
(let-exp (list temp) (list (car exps))
(list (if-else-exp (var-exp temp)
(var-exp temp)
(or-exp (cdr exps)))))]
)))
]
[define-exp (var expr)
(define-exp
var
(syntax-expand expr))
]
)))
(define member?
(lambda (obj list)
(not (not (member obj list)))))
(define make-list-of
(lambda (obj size)
(let loop [[size size]
[result '()]]
(if (eq? size 0)
result
(loop (- size 1) (cons obj result))))))
(define make-list-of-random-symbols
(lambda (size)
(let loop [[size size]
[result '()]]
(if (eq? size 0)
result
(loop (- size 1) (cons (generate-random-symbol) result))))))
; generates a random symbol
(define generate-random-symbol
(lambda ()
(string->symbol (string-append "temp" (number->string (random 1000000000))))))
; #interpreter
; _____ _ _
; |_ _| | | | |
; | | _ __ | |_ ___ _ __ _ __ _ __ ___| |_ ___ _ __
; | | | '_ \| __/ _ \ '__| '_ \| '__/ _ \ __/ _ \ '__|
; _| |_| | | | || __/ | | |_| | | | __/ || __/ |
; |_____|_| |_|\__\___|_| | .__/|_| \___|\__\___|_|
; | |
; |_|
(define map-cps
(lambda (proc-cps L k)
(if (null? L)
(apply-k k '())
(proc-cps (car L) (lambda (mapped-car)
(map-cps proc-cps (cdr L) (lambda (mapped-cdr)
(apply-k k (cons mapped-car mapped-cdr))))))
)))
; top-level-eval evaluates a form in the global environment
(define top-level-eval
(lambda (form)
; later we may add things that are not expressions.
;(eval-exp form (empty-env))
(eval-exp-cps form (empty-env) (lambda (k) k))
))
(define eval-bodies
(lambda (bodies env)
(let eval-bodies ([bodies bodies] [env env])
(if (null? (cdr bodies))
(eval-exp (car bodies) env)
(begin (eval-exp (car bodies) env) (eval-bodies (cdr bodies) env))))))
(define eval-bodies-cps
(lambda (bodies env k)
(let eval-bodies ([bodies bodies] [env env])
(if (null? (cdr bodies))
(eval-exp-cps (car bodies) env k)
(eval-exp-cps (car bodies) env (lambda (evalled-car)
(eval-bodies-cps (cdr bodies) env k)))))))
; eval-exp is the main component of the interpreter
(define eval-exp
(lambda (exp env)
(cases expression exp
[lit-exp (datum) datum]
[var-exp (id)
(apply-env
env
id; look up its value.
(lambda (x) x) ; procedure to call if id is in the environment
(lambda () ; procedure to call if id is not in env
(apply-env-ref
global-env ; was init-env
id
(lambda (x) x)
(lambda ()
(eopl:error 'apply-env ; procedure to call if id not in env
"variable not found in environment: ~s"
id)
))
))]
[quoted-exp (id) id]
[if-else-exp (condition true false)
(if (eval-exp condition env)
(eval-exp true env)
(eval-exp false env))]
;[if-exp (condition true)
; (if (eval-exp condition env)
; (eval-exp true env)
; (void))]
[app-exp (rator rands)
(let ([proc-value (eval-exp rator env)]
[args (eval-rands rands env)])
(apply-proc proc-value args))
]
;[let-exp (vars declarations bodies)
; (let [[new-env
; (extend-env
; vars
; (map (lambda (x) (eval-exp x env)) declarations)
; env)]]
; (eval-bodies bodies new-env))] ; evaluate bodies in order, return last value
[lambda-list-exp (id bodies)
(closure id bodies env)]
[lambda-single-exp (id bodies)
(closure-single-arg id bodies env)]
[lambda-improper-exp (ids other bodies)
(closure-improper-args ids other bodies env)]
[lambda-ref-exp (ids refs bodies)
(closure-ref ids refs bodies env)]
[set!-exp (var expr)
(set-ref!
(apply-env-ref env
var
(lambda (x) x)
(lambda ()
(apply-env-ref
global-env
var
(lambda (x) x)
(lambda ()
(eopl:error 'set! "Variable not previously defined: ~s" var)))))
(eval-exp expr env))]
[define-exp (var expr)
(begin (set! global-env (extend-env
(list var)
(list (eval-exp expr env))
global-env))
)
]
[else (eopl:error 'eval-exp "Bad abstract syntax: ~a" exp)])))
(define eval-exp-cps
(lambda (exp env k)
(cases expression exp
[lit-exp (datum) (apply-k k datum)]
[var-exp (id)
(apply-k k (apply-env
env
id; look up its value.
(lambda (x) x) ; procedure to call if id is in the environment
(lambda () ; procedure to call if id is not in env
(apply-env-ref
global-env ; was init-env
id
(lambda (x) x)
(lambda ()
(eopl:error 'apply-env ; procedure to call if id not in env
"variable not found in environment: ~s"
id)
))
)))]
[quoted-exp (id) (apply-k k id)]
[if-else-exp (condition true false)
;(if (eval-exp condition env)
; (eval-exp true env)
; (eval-exp false env))
(eval-exp-cps condition env (lambda (is-cond-true)
(if is-cond-true
(eval-exp-cps true env k)
(eval-exp-cps false env k))))]
;[if-exp (condition true)
; (if (eval-exp condition env)
; (eval-exp true env)
; (void))]
[app-exp (rator rands)
;(let ([proc-value (eval-exp rator env)]
; [args (eval-rands rands env)])
; (apply-proc proc-value args))
(eval-exp-cps rator env (lambda (proc-value)
(eval-rands-cps rands env (lambda (args)
(apply-proc-cps proc-value args k)))))]
;[let-exp (vars declarations bodies)
; (let [[new-env
; (extend-env
; vars
; (map (lambda (x) (eval-exp x env)) declarations)
; env)]]
; (eval-bodies bodies new-env))] ; evaluate bodies in order, return last value
[lambda-list-exp (id bodies) ; HEREE
(apply-k k (closure id bodies env))]
[lambda-single-exp (id bodies)
(apply-k k (closure-single-arg id bodies env))]
[lambda-improper-exp (ids other bodies)
(apply-k k (closure-improper-args ids other bodies env))]
[lambda-ref-exp (ids refs bodies)
(apply-k k (closure-ref ids refs bodies env))]
[set!-exp (var expr)
(eval-exp-cps expr env (lambda (evalled-expr)
(apply-k k (set-ref!
(apply-env-ref env
var
(lambda (x) x)
(lambda ()
(apply-env-ref
global-env
var
(lambda (x) x)
(lambda ()
(eopl:error 'set! "Variable not previously defined: ~s" var)))))
evalled-expr))))
]
[define-exp (var expr)
(eval-exp-cps expr env (lambda (evalled-exp)
(apply-k k (set! global-env (extend-env
(list var)
(list evalled-exp)
global-env)))))
]
[else (eopl:error 'eval-exp "Bad abstract syntax: ~a" exp)])))
; evaluate the list of operands, putting results into a list
(define eval-rands
(lambda (rands env)
(map (lambda (x) (eval-exp x env)) rands)))
(define eval-rands-cps
(lambda (rands env k)
(map-cps (lambda (x k)
(eval-exp-cps x env k)) rands k)))
(define pick-from-list
(lambda (ls refs)
(map (lambda (x)
(list-ref ls x)) refs)))
(define pick-all-except
(lambda (ls refs)
(let loop [[ls ls]
[refs refs]
[count 0]
[result '()]]
(cond
[(null? ls) (reverse result)]
[(null? refs)
(loop (cdr ls) refs (+ count 1) (cons (car ls) result))]
[(= count (car refs))
(loop (cdr ls) (cdr refs) (+ count 1) result)]
[else
(loop (cdr ls) refs (+ count 1) (cons (car ls) result))]
))))
; Apply a procedure to its arguments.
; At this point, we only have primitive procedures.
; User-defined procedures will be added later.
(define apply-proc
(lambda (proc-value args)
(cases proc-val proc-value
[prim-proc (op)
(apply-prim-proc op args)]
[closure (ids bodies env)
(begin ;(display '(*** apply-proc to closure ***)) (newline) (display '-ids:) (display ids) (newline) (display '-bodies:) (display bodies) (newline) (display '-environment:) (display env) (newline) (newline)
(let [[new-env
(extend-env
ids
args
env)]]
(eval-bodies bodies new-env)))]
[closure-single-arg (id bodies env)
(begin ;(display '(*** apply-proc to closure-single-arg ***)) (newline) (display '-id:) (display id) (newline) (display '-bodies:) (display bodies) (newline) (display '-environment:) (display env) (newline) (display '-args:) (display args) (newline) (newline)
(let [[new-env
(extend-env
(list id)
(list args)
env)]]
(eval-bodies bodies new-env))
)]
[closure-improper-args (ids other bodies env)
(begin ;(display '(*** apply-proc to closure-improper-args ***)) (newline) (display '-ids:) (display ids) (newline) (display '-other:) (display other) (newline) (display '-bodies:) (display bodies) (newline) (display '-environment:) (display env) (newline) (display '-args:) (display args) (newline) (newline)
(let* [[new-ids (append ids (list other))]
[new-env
(extend-env
new-ids
(group-improper-args args (length new-ids))
env)]]
(eval-bodies bodies new-env))
)]
; You will add other cases
[else (error 'apply-proc
"Attempt to apply bad procedure: ~s"
proc-value)])))
(define apply-proc-cps
(lambda (proc-value args k)
(cases proc-val proc-value
[prim-proc (op)
(apply-prim-proc-cps op args k)]
[closure (ids bodies env)
(begin ;(display '(*** apply-proc to closure ***)) (newline) (display '-ids:) (display ids) (newline) (display '-bodies:) (display bodies) (newline) (display '-environment:) (display env) (newline) (newline)
(let [[new-env
(extend-env
ids
args
env)]]
(eval-bodies-cps bodies new-env k)))]
[closure-single-arg (id bodies env)
(begin ;(display '(*** apply-proc to closure-single-arg ***)) (newline) (display '-id:) (display id) (newline) (display '-bodies:) (display bodies) (newline) (display '-environment:) (display env) (newline) (display '-args:) (display args) (newline) (newline)
(let [[new-env
(extend-env
(list id)
(list args)
env)]]
(eval-bodies-cps bodies new-env k))
)]
[closure-improper-args (ids other bodies env) ; uhhh
(begin ;(display '(*** apply-proc to closure-improper-args ***)) (newline) (display '-ids:) (display ids) (newline) (display '-other:) (display other) (newline) (display '-bodies:) (display bodies) (newline) (display '-environment:) (display env) (newline) (display '-args:) (display args) (newline) (newline)
(let* [[new-ids (append ids (list other))]
[new-env
(extend-env
new-ids
(group-improper-args args (length new-ids))
env)]]
(eval-bodies-cps bodies new-env k))
)]
; You will add other cases
[else (error 'apply-proc
"Attempt to apply bad procedure: ~s"
proc-value)])))
(define *prim-proc-names* '(+ - * / add1 sub1 zero? not cons = < > <= >= car cdr caar
cadr cdar cddr caaar caadr cadar caddr cdaar cdadr cddar
cdddr list append null? assq eq? eqv? equal? atom? length list->vector
list? pair? procedure? vector->list vector make-vector
vector-ref vector? number? symbol? set-car! set-cdr!
vector-set! display newline list-tail map apply member? quotient
newline display void))
(define init-env ; for now, our initial global environment only contains
(extend-env ; procedure names. Recall that an environment associates
*prim-proc-names* ; a value (not an expression) with an identifier.
;(map box
(map prim-proc
*prim-proc-names*)
;)
(empty-env)))
(define global-env init-env)
; Some helpers to group arguments for improper lambdas
(define split
(lambda (args count)
(let loop [[result '()]
[curr-count count]]
(cond
[(eq? curr-count (length args)) result]
[else
(loop (append result (list (list-ref args curr-count))) (+ curr-count 1))]
))))
(define group-improper-args
(lambda (args length-ids)
(let loop [[result '()]
[count 0]]
(cond
[(eq? count (- length-ids 1)) (append result (list (split args count)))]
[else (loop (append result (list (list-ref args count))) (+ count 1))]
))))
; Usually an interpreter must define each
; built-in procedure individually. We are "cheating" a little bit.
; TODO: add error statements if the interpreted code attempts to apply a proc to an incorrect number of args
(define apply-prim-proc
(lambda (prim-proc args)
(case prim-proc
[(+) (apply + args)]
[(-) (apply - args)]
[(*) (apply * args)]
[(/) (apply / args)]
[(add1) (+ (1st args) 1)]
[(sub1) (- (1st args) 1)]
[(zero?) (if (valid-zero? args) (zero? (car args)))]
[(not) (apply not args)]
[(cons) (cons (1st args) (2nd args))]
[(=) (apply = args)]
[(<) (apply < args)]
[(>) (apply > args)]
[(<=) (apply <= args)]
[(>=) (apply >= args)]
[(car) (apply car args)]
[(cdr) (apply cdr args)]
[(caar) (apply caar args)]
[(cadr) (apply cadr args)]
[(cdar) (apply cdar args)]
[(cddr) (apply cddr args)]
[(caaar) (apply caaar args)]
[(caadr) (apply caadr args)]
[(cadar) (apply cadar args)]
[(caddr) (apply caddr args)]
[(cdaar) (apply cdaar args)]
[(cdadr) (apply cdadr args)]
[(cddar) (apply cddar args)]
[(cdddr) (apply cdddr args)]
[(list) (apply list args)]
[(append) (apply append args)]
[(null?) (apply null? args)]
[(assq) (apply assq args)]
[(eq?) (apply eq? args)]
[(eqv?) (apply eqv? args)]
[(equal?) (apply equal? args)]
[(atom?) (apply atom? args)]
[(length) (apply length args)]
[(list->vector) (apply list->vector args)]
[(list?) (apply list? args)]
[(pair?) (apply pair? args)]
[(procedure?) (proc-val? (car args))]
[(vector->list) (apply vector->list args)]
[(vector) (apply vector args)]
[(make-vector) (apply make-vector args)]
[(vector-ref) (apply vector-ref args)]
[(vector?) (apply vector? args)]
[(number?) (apply number? args)]
[(symbol?) (apply symbol? args)]
[(set-car!) (apply set-car! args)]
[(set-cdr!) (apply set-cdr! args)]
[(vector-set!) (apply vector-set! args)]
[(display) (apply display args)]
[(newline) (apply newline args)]
[(map) (map (lambda (x) (apply-proc (car args) (list x))) (cadr args))]
[(list-tail) (list-tail (car args) (cadr args))]
[(apply) (apply-proc (car args) (cadr args))]
[(member?) (apply member? (car args) (cdr args))]
[(quotient) (apply quotient args)]
[(newline) (apply newline args)]
[(display) (apply display args)]
[(void) (void)]
[else (error 'apply-prim-proc
"Bad primitive procedure name: ~s"
prim-op)])))
(define apply-prim-proc-cps
(lambda (prim-proc args k)
(case prim-proc
[(+) (apply-k k (apply + args))]
[(-) (apply-k k (apply - args))]
[(*) (apply-k k (apply * args))]
[(/) (apply-k k (apply / args))]
[(add1) (apply-k k (+ (1st args) 1))]
[(sub1) (apply-k k (- (1st args) 1))]
[(zero?) (apply-k k (zero? (1st args)))]
[(not) (apply-k k (apply not args))]
[(cons) (apply-k k (cons (1st args) (2nd args)))]
[(=) (apply-k k (apply = args))]
[(<) (apply-k k (apply < args))]
[(>) (apply-k k (apply > args))]
[(<=) (apply-k k (apply <= args))]
[(>=) (apply-k k (apply >= args))]
[(car) (apply-k k (apply car args))]
[(cdr) (apply-k k (apply cdr args))]
[(caar) (apply-k k (apply caar args))]
[(cadr) (apply-k k (apply cadr args))]
[(cdar) (apply-k k (apply cdar args))]
[(cddr) (apply-k k (apply cddr args))]
[(caaar) (apply-k k (apply caaar args))]
[(caadr) (apply-k k (apply caadr args))]
[(cadar) (apply-k k (apply cadar args))]