|
366 | 366 | ['Boolean (if val #t #f)]
|
367 | 367 | ['Integer val]
|
368 | 368 | ['Void (void)]
|
369 |
| - ['Any `(inject ,(arithmetic-shift val -2) Integer)] |
| 369 | + ['Any `(tagged ,(arithmetic-shift val -2) Integer)] |
370 | 370 | [else (error (format "don't know how to display type ~a" ty))]))
|
371 | 371 |
|
372 | 372 | ));; class interp-R1
|
|
685 | 685 | (define interp-R3
|
686 | 686 | (class interp-R2
|
687 | 687 | (super-new)
|
688 |
| - (inherit primitives seq-C display-by-type) |
| 688 | + (inherit primitives seq-C display-by-type interp-op) |
689 | 689 | (inherit-field result)
|
690 | 690 |
|
691 | 691 | (define/public (non-apply-ast)
|
|
698 | 698 | (match ast
|
699 | 699 | [`(define (,f [,xs : ,ps] ...) : ,rt ,body)
|
700 | 700 | (cons f `(lambda ,xs ,body))]
|
701 |
| - [`(function-ref ,f) |
702 |
| - (lookup f env)] |
703 |
| - [`(app ,f ,args ...) |
704 |
| - (define new-args (map (interp-scheme env) args)) |
705 |
| - (let ([f-val ((interp-scheme env) f)]) |
706 |
| - (match f-val |
707 |
| - [`(lambda (,xs ...) ,body) |
708 |
| - (define new-env (append (map cons xs new-args) env)) |
709 |
| - ((interp-scheme new-env) body)] |
710 |
| - [else (error "interp-scheme, expected function, not" f-val)]))] |
711 | 701 | [`(program (type ,ty) ,ds ... ,body)
|
712 | 702 | ((interp-scheme env) `(program ,@ds ,body))]
|
713 | 703 | [`(program ,ds ... ,body)
|
714 | 704 | (let ([top-level (map (interp-scheme '()) ds)])
|
715 | 705 | ((interp-scheme top-level) body))]
|
716 |
| - [`(,f ,args ...) #:when (not (set-member? |
717 |
| - (non-apply-ast) f)) |
718 |
| - ((interp-scheme env) `(app ,f ,@args))] |
719 |
| - [else ((super interp-scheme env) ast)]))) |
| 706 | + [`(,fun ,args ...) #:when (not (set-member? (non-apply-ast) fun)) |
| 707 | + (define new-args (map (interp-scheme env) args)) |
| 708 | + (define fun-val ((interp-scheme env) fun)) |
| 709 | + (match fun-val |
| 710 | + [`(lambda (,xs ...) ,body) |
| 711 | + (define new-env (append (map cons xs new-args) env)) |
| 712 | + ((interp-scheme new-env) body)] |
| 713 | + [else (error "interp-scheme, expected function, not" fun-val)])] |
| 714 | + [else ((super interp-scheme env) ast)] |
| 715 | + ))) |
| 716 | + |
| 717 | + (define/public (interp-F env) |
| 718 | + (lambda (ast) |
| 719 | + (verbose "R3/interp-F" ast) |
| 720 | + (define result |
| 721 | + (match ast |
| 722 | + ;; For R3 |
| 723 | + [`(define (,f [,xs : ,ps] ...) : ,rt ,body) |
| 724 | + (cons f `(lambda ,xs ,body))] |
| 725 | + [`(function-ref ,f) |
| 726 | + (lookup f env)] |
| 727 | + [`(app ,fun ,args ...) |
| 728 | + (define arg-vals (map (interp-F env) args)) |
| 729 | + (define fun-val ((interp-F env) fun)) |
| 730 | + (match fun-val |
| 731 | + [`(lambda (,xs ...) ,body) |
| 732 | + (define new-env (append (map cons xs arg-vals) env)) |
| 733 | + ((interp-F new-env) body)] |
| 734 | + [else (error "interp-F, expected function, not" fun-val)])] |
| 735 | + [`(program (type ,ty) ,ds ... ,body) |
| 736 | + ((interp-F env) `(program ,@ds ,body))] |
| 737 | + [`(program ,ds ... ,body) |
| 738 | + (let ([top-level (map (interp-F '()) ds)]) |
| 739 | + ((interp-F top-level) body))] |
| 740 | + ;; For R2 |
| 741 | + [`(has-type ,e ,t) ((interp-F env) e)] |
| 742 | + [#t #t] |
| 743 | + [#f #f] |
| 744 | + [`(and ,e1 ,e2) |
| 745 | + (match ((interp-F env) e1) |
| 746 | + [#t (match ((interp-F env) e2) |
| 747 | + [#t #t] [#f #f])] |
| 748 | + [#f #f])] |
| 749 | + [`(if ,cnd ,thn ,els) |
| 750 | + (if ((interp-F env) cnd) |
| 751 | + ((interp-F env) thn) |
| 752 | + ((interp-F env) els))] |
| 753 | + ;; For R1 |
| 754 | + [(? symbol?) |
| 755 | + (lookup ast env)] |
| 756 | + [(? integer?) ast] |
| 757 | + [`(let ([,x ,e]) ,body) |
| 758 | + (let ([v ((interp-F env) e)]) |
| 759 | + ((interp-F (cons (cons x v) env)) body))] |
| 760 | + [`(program ,e) ((interp-F '()) e)] |
| 761 | + [`(,op ,args ...) #:when (set-member? (primitives) op) |
| 762 | + (apply (interp-op op) (map (interp-F env) args))] |
| 763 | + )) |
| 764 | + (verbose "R3/interp-F" ast result) |
| 765 | + result |
| 766 | + )) |
720 | 767 |
|
721 | 768 | (define/override (interp-C env)
|
722 | 769 | (lambda (ast)
|
|
727 | 774 | [`(function-ref ,f)
|
728 | 775 | (lookup f env)]
|
729 | 776 | [`(app ,f ,args ...)
|
730 |
| - (define new-args (map (interp-C env) args)) |
| 777 | + (define arg-vals (map (interp-C env) args)) |
731 | 778 | (define f-val ((interp-C env) f))
|
732 | 779 | (match f-val
|
733 | 780 | [`(lambda (,xs ...) ,ss ...)
|
734 |
| - (define new-env (append (map cons xs new-args) env)) |
| 781 | + (define new-env (append (map cons xs arg-vals) env)) |
735 | 782 | (define result-env ((seq-C new-env) ss))
|
736 | 783 | (lookup result result-env)]
|
737 | 784 | [else (error "interp-C, expected a funnction, not" f-val)])]
|
|
823 | 870 | (define interp-R4
|
824 | 871 | (class interp-R3
|
825 | 872 | (super-new)
|
| 873 | + (inherit non-apply-ast) |
826 | 874 | (inherit-field result)
|
827 | 875 |
|
828 | 876 | (define/override (interp-scheme env)
|
|
831 | 879 | (match ast
|
832 | 880 | [`(lambda: ([,xs : ,Ts] ...) : ,rT ,body)
|
833 | 881 | `(lambda ,xs ,body ,env)]
|
834 |
| - [`(app ,f ,args ...) |
835 |
| - (define new-args (map (interp-scheme env) args)) |
836 |
| - (let ([f-val ((interp-scheme env) f)]) |
837 |
| - (match f-val |
838 |
| - [`(lambda (,xs ...) ,body ,lam-env) |
839 |
| - (define new-env (append (map cons xs new-args) lam-env)) |
840 |
| - ((interp-scheme new-env) body)] |
841 |
| - [else (error "interp-scheme, expected function, not" f-val)]))] |
842 | 882 | [`(define (,f [,xs : ,ps] ...) : ,rt ,body)
|
843 | 883 | (mcons f `(lambda ,xs ,body))]
|
844 | 884 | [`(program (type ,ty) ,ds ... ,body)
|
|
851 | 891 | [`(lambda ,xs ,body)
|
852 | 892 | `(lambda ,xs ,body ,top-level)])))
|
853 | 893 | ((interp-scheme top-level) body))]
|
854 |
| - [else ((super interp-scheme env) ast)]))))) ;; end interp-R4 |
| 894 | + [`(,fun ,args ...) #:when (not (set-member? (non-apply-ast) fun)) |
| 895 | + (define arg-vals (map (interp-scheme env) args)) |
| 896 | + (define fun-val ((interp-scheme env) fun)) |
| 897 | + (match fun-val |
| 898 | + [`(lambda (,xs ...) ,body ,lam-env) |
| 899 | + (define new-env (append (map cons xs arg-vals) lam-env)) |
| 900 | + ((interp-scheme new-env) body)] |
| 901 | + [else (error "interp-scheme, expected function, not" fun-val)])] |
| 902 | + [else ((super interp-scheme env) ast)] |
| 903 | + ))) |
| 904 | + |
| 905 | + (define/override (interp-F env) |
| 906 | + (lambda (ast) |
| 907 | + (verbose "R4/interp-F" ast) |
| 908 | + (match ast |
| 909 | + [`(lambda: ([,xs : ,Ts] ...) : ,rT ,body) |
| 910 | + `(lambda ,xs ,body ,env)] |
| 911 | + [`(define (,f [,xs : ,ps] ...) : ,rt ,body) |
| 912 | + (mcons f `(lambda ,xs ,body))] |
| 913 | + [`(program (type ,ty) ,ds ... ,body) |
| 914 | + ((interp-F env) `(program ,@ds ,body))] |
| 915 | + [`(program ,ds ... ,body) |
| 916 | + (let ([top-level (map (interp-F '()) ds)]) |
| 917 | + ;; Use set-cdr! on define lambda's for mutual recursion |
| 918 | + (for/list ([b top-level]) |
| 919 | + (set-mcdr! b (match (mcdr b) |
| 920 | + [`(lambda ,xs ,body) |
| 921 | + `(lambda ,xs ,body ,top-level)]))) |
| 922 | + ((interp-F top-level) body))] |
| 923 | + [`(app ,fun ,args ...) |
| 924 | + (define arg-vals (map (interp-F env) args)) |
| 925 | + (define fun-val ((interp-F env) fun)) |
| 926 | + (match fun-val |
| 927 | + [`(lambda (,xs ...) ,body ,lam-env) |
| 928 | + (define new-env (append (map cons xs arg-vals) lam-env)) |
| 929 | + ((interp-F new-env) body)] |
| 930 | + [else (error "interp-F, expected function, not" fun-val)])] |
| 931 | + [else ((super interp-F env) ast)] |
| 932 | + ))) |
| 933 | + |
| 934 | + )) ;; end interp-R4 |
855 | 935 |
|
856 | 936 |
|
857 | 937 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
862 | 942 | (super-new)
|
863 | 943 | (inherit-field result)
|
864 | 944 |
|
865 |
| - |
866 | 945 | (define/override (primitives)
|
867 | 946 | (set-union (super primitives)
|
868 | 947 | (set 'boolean? 'integer? 'vector? 'procedure?)))
|
|
871 | 950 | (match op
|
872 | 951 | ['boolean? (lambda (v)
|
873 | 952 | (match v
|
874 |
| - [`(inject ,v1 Boolean) #t] |
| 953 | + [`(tagged ,v1 Boolean) #t] |
875 | 954 | [else #f]))]
|
876 | 955 | ['integer? (lambda (v)
|
877 | 956 | (match v
|
878 |
| - [`(inject ,v1 Integer) #t] |
| 957 | + [`(tagged ,v1 Integer) #t] |
879 | 958 | [else #f]))]
|
880 | 959 | ['vector? (lambda (v)
|
881 | 960 | (match v
|
882 |
| - [`(inject ,v1 (Vector ,ts ...)) #t] |
| 961 | + [`(tagged ,v1 (Vector ,ts ...)) #t] |
883 | 962 | [else #f]))]
|
884 | 963 | ['procedure? (lambda (v)
|
885 | 964 | (match v
|
886 |
| - [`(inject ,v1 (,ts ... -> ,rt)) #t] |
| 965 | + [`(tagged ,v1 (,ts ... -> ,rt)) #t] |
887 | 966 | [else #f]))]
|
888 | 967 | [else (super interp-op op)]
|
889 | 968 | ))
|
|
893 | 972 | (verbose "R6/interp-scheme" ast)
|
894 | 973 | (match ast
|
895 | 974 | [`(inject ,e ,t)
|
896 |
| - `(inject ,((interp-scheme env) e) ,t)] |
| 975 | + `(tagged ,((interp-scheme env) e) ,t)] |
897 | 976 | [`(project ,e ,t2)
|
898 | 977 | (define v ((interp-scheme env) e))
|
899 | 978 | (match v
|
900 |
| - [`(inject ,v1 ,t1) |
| 979 | + [`(tagged ,v1 ,t1) |
901 | 980 | (cond [(equal? t1 t2)
|
902 | 981 | v1]
|
903 | 982 | [else
|
|
913 | 992 | (verbose "R6/interp-C" ast)
|
914 | 993 | (match ast
|
915 | 994 | [`(inject ,e ,t)
|
916 |
| - `(inject ,((interp-C env) e) ,t)] |
| 995 | + `(tagged ,((interp-C env) e) ,t)] |
917 | 996 | [`(project ,e ,t2)
|
918 | 997 | (define v ((interp-C env) e))
|
919 | 998 | (match v
|
920 |
| - [`(inject ,v1 ,t1) |
| 999 | + [`(tagged ,v1 ,t1) |
921 | 1000 | (cond [(equal? t1 t2)
|
922 | 1001 | v1]
|
923 | 1002 | [else
|
|
0 commit comments