comparison lisp/=cl.el @ 957:2619b7a9c11e

entered into RCS
author Jim Blandy <jimb@redhat.com>
date Wed, 12 Aug 1992 12:50:10 +0000
parents 52cd80cb5be1
children 6b409871cc4a
comparison
equal deleted inserted replaced
956:c530dbc9a92a 957:2619b7a9c11e
669 ;;;; typical c[ad]*r functions. 669 ;;;; typical c[ad]*r functions.
670 ;;;; 670 ;;;;
671 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986 671 ;;;; Cesar Quiroz @ UofR DofCSc - Dec. 1986
672 ;;;; (quiroz@cs.rochester.edu) 672 ;;;; (quiroz@cs.rochester.edu)
673 673
674 (defvar *cl-valid-named-list-accessors* 674
675 '(first rest second third fourth fifth sixth seventh eighth ninth tenth)) 675
676 (defvar *cl-valid-nth-offsets* 676 ;;; To make these faster, we define them using defsubst. This directs the
677 '((second . 1) 677 ;;; compiler to open-code these functions.
678 (third . 2)
679 (fourth . 3)
680 (fifth . 4)
681 (sixth . 5)
682 (seventh . 6)
683 (eighth . 7)
684 (ninth . 8)
685 (tenth . 9)))
686
687 (defun byte-compile-named-list-accessors (form)
688 "Generate code for (<accessor> FORM), where <accessor> is one of the named
689 list accessors: first, second, ..., tenth, rest."
690 (let* ((fun (car form))
691 (arg (cadr form))
692 (valid *cl-valid-named-list-accessors*)
693 (offsets *cl-valid-nth-offsets*))
694 (cond
695
696 ;; Check that it's a form we're prepared to handle.
697 ((not (memq fun valid))
698 (error
699 "cl.el internal bug: `%s' not in {first, ..., tenth, rest}"
700 fun))
701
702 ;; Check the number of arguments.
703 ((not (= (length form) 2))
704 (byte-compile-subr-wrong-args form 1))
705
706 ;; If the result will simply be tossed, don't generate any code for
707 ;; it, and indicate that we have already discarded the value.
708 (for-effect
709 (setq for-effect nil))
710
711 ;; Generate code for the call.
712 ((eq fun 'first)
713 (byte-compile-form arg)
714 (byte-compile-out 'byte-car 0))
715 ((eq fun 'rest)
716 (byte-compile-form arg)
717 (byte-compile-out 'byte-cdr 0))
718 (t ;one of the others
719 (byte-compile-constant (cdr (assq fun offsets)))
720 (byte-compile-form arg)
721 (byte-compile-out 'byte-nth 0)))))
722 678
723 ;;; Synonyms for list functions 679 ;;; Synonyms for list functions
724 (defun first (x) 680 (defsubst first (x)
725 "Synonym for `car'" 681 "Synonym for `car'"
726 (car x)) 682 (car x))
727 (put 'first 'byte-compile 'byte-compile-named-list-accessors) 683
728 684 (defsubst second (x)
729 (defun second (x)
730 "Return the second element of the list LIST." 685 "Return the second element of the list LIST."
731 (nth 1 x)) 686 (nth 1 x))
732 (put 'second 'byte-compile 'byte-compile-named-list-accessors) 687
733 688 (defsubst third (x)
734 (defun third (x)
735 "Return the third element of the list LIST." 689 "Return the third element of the list LIST."
736 (nth 2 x)) 690 (nth 2 x))
737 (put 'third 'byte-compile 'byte-compile-named-list-accessors) 691
738 692 (defsubst fourth (x)
739 (defun fourth (x)
740 "Return the fourth element of the list LIST." 693 "Return the fourth element of the list LIST."
741 (nth 3 x)) 694 (nth 3 x))
742 (put 'fourth 'byte-compile 'byte-compile-named-list-accessors) 695
743 696 (defsubst fifth (x)
744 (defun fifth (x)
745 "Return the fifth element of the list LIST." 697 "Return the fifth element of the list LIST."
746 (nth 4 x)) 698 (nth 4 x))
747 (put 'fifth 'byte-compile 'byte-compile-named-list-accessors) 699
748 700 (defsubst sixth (x)
749 (defun sixth (x)
750 "Return the sixth element of the list LIST." 701 "Return the sixth element of the list LIST."
751 (nth 5 x)) 702 (nth 5 x))
752 (put 'sixth 'byte-compile 'byte-compile-named-list-accessors) 703
753 704 (defsubst seventh (x)
754 (defun seventh (x)
755 "Return the seventh element of the list LIST." 705 "Return the seventh element of the list LIST."
756 (nth 6 x)) 706 (nth 6 x))
757 (put 'seventh 'byte-compile 'byte-compile-named-list-accessors) 707
758 708 (defsubst eighth (x)
759 (defun eighth (x)
760 "Return the eighth element of the list LIST." 709 "Return the eighth element of the list LIST."
761 (nth 7 x)) 710 (nth 7 x))
762 (put 'eighth 'byte-compile 'byte-compile-named-list-accessors) 711
763 712 (defsubst ninth (x)
764 (defun ninth (x)
765 "Return the ninth element of the list LIST." 713 "Return the ninth element of the list LIST."
766 (nth 8 x)) 714 (nth 8 x))
767 (put 'ninth 'byte-compile 'byte-compile-named-list-accessors) 715
768 716 (defsubst tenth (x)
769 (defun tenth (x)
770 "Return the tenth element of the list LIST." 717 "Return the tenth element of the list LIST."
771 (nth 9 x)) 718 (nth 9 x))
772 (put 'tenth 'byte-compile 'byte-compile-named-list-accessors) 719
773 720 (defsubst rest (x)
774 (defun rest (x)
775 "Synonym for `cdr'" 721 "Synonym for `cdr'"
776 (cdr x)) 722 (cdr x))
777 (put 'rest 'byte-compile 'byte-compile-named-list-accessors)
778 723
779 (defun endp (x) 724 (defun endp (x)
780 "t if X is nil, nil if X is a cons; error otherwise." 725 "t if X is nil, nil if X is a cons; error otherwise."
781 (if (listp x) 726 (if (listp x)
782 (null x) 727 (null x)
843 (reverse result)) 788 (reverse result))
844 (setq result (cons (car curcons) result)))) 789 (setq result (cons (car curcons) result))))
845 790
846 ;;; The popular c[ad]*r functions and other list accessors. 791 ;;; The popular c[ad]*r functions and other list accessors.
847 792
848 ;;; To implement this efficiently, a new byte compile handler is used to 793 ;;; To implement this efficiently, we define them using defsubst,
849 ;;; generate the minimal code, saving one function call. 794 ;;; which directs the compiler to open-code these functions.
850 795
851 (defun byte-compile-ca*d*r (form) 796 (defsubst caar (X)
852 "Generate code for a (c[ad]+r argument). This realizes the various
853 combinations of car and cdr whose names are supported in this implementation.
854 To use this functionality for a given function,just give its name a
855 'byte-compile property of 'byte-compile-ca*d*r"
856 (let* ((fun (car form))
857 (arg (cadr form))
858 (seq (mapcar (function (lambda (letter)
859 (if (= letter ?a)
860 'byte-car 'byte-cdr)))
861 (cdr (nreverse (cdr (append (symbol-name fun) nil)))))))
862 ;; SEQ is a list of byte-car and byte-cdr in the correct order.
863 (cond
864
865 ;; Is this a function we can handle?
866 ((null seq)
867 (error
868 "cl.el internal bug: `%s' cannot be compiled by byte-compile-ca*d*r"
869 (prin1-to-string form)))
870
871 ;; Are we passing this function the correct number of arguments?
872 ((or (null (cdr form)) (cddr form))
873 (byte-compile-subr-wrong-args form 1))
874
875 ;; Are we evaluating this expression for effect only?
876 (for-effect
877
878 ;; We needn't generate any actual code, as long as we tell the rest
879 ;; of the compiler that we didn't push anything on the stack.
880 (setq for-effect nil))
881
882 ;; Generate code for the function.
883 (t
884 (byte-compile-form arg)
885 (while seq
886 (byte-compile-out (car seq) 0)
887 (setq seq (cdr seq)))))))
888
889 (defun caar (X)
890 "Return the car of the car of X." 797 "Return the car of the car of X."
891 (car (car X))) 798 (car (car X)))
892 (put 'caar 'byte-compile 'byte-compile-ca*d*r) 799
893 800 (defsubst cadr (X)
894 (defun cadr (X)
895 "Return the car of the cdr of X." 801 "Return the car of the cdr of X."
896 (car (cdr X))) 802 (car (cdr X)))
897 (put 'cadr 'byte-compile 'byte-compile-ca*d*r) 803
898 804 (defsubst cdar (X)
899 (defun cdar (X)
900 "Return the cdr of the car of X." 805 "Return the cdr of the car of X."
901 (cdr (car X))) 806 (cdr (car X)))
902 (put 'cdar 'byte-compile 'byte-compile-ca*d*r) 807
903 808 (defsubst cddr (X)
904 (defun cddr (X)
905 "Return the cdr of the cdr of X." 809 "Return the cdr of the cdr of X."
906 (cdr (cdr X))) 810 (cdr (cdr X)))
907 (put 'cddr 'byte-compile 'byte-compile-ca*d*r) 811
908 812 (defsubst caaar (X)
909 (defun caaar (X)
910 "Return the car of the car of the car of X." 813 "Return the car of the car of the car of X."
911 (car (car (car X)))) 814 (car (car (car X))))
912 (put 'caaar 'byte-compile 'byte-compile-ca*d*r) 815
913 816 (defsubst caadr (X)
914 (defun caadr (X)
915 "Return the car of the car of the cdr of X." 817 "Return the car of the car of the cdr of X."
916 (car (car (cdr X)))) 818 (car (car (cdr X))))
917 (put 'caadr 'byte-compile 'byte-compile-ca*d*r) 819
918 820 (defsubst cadar (X)
919 (defun cadar (X)
920 "Return the car of the cdr of the car of X." 821 "Return the car of the cdr of the car of X."
921 (car (cdr (car X)))) 822 (car (cdr (car X))))
922 (put 'cadar 'byte-compile 'byte-compile-ca*d*r) 823
923 824 (defsubst cdaar (X)
924 (defun cdaar (X)
925 "Return the cdr of the car of the car of X." 825 "Return the cdr of the car of the car of X."
926 (cdr (car (car X)))) 826 (cdr (car (car X))))
927 (put 'cdaar 'byte-compile 'byte-compile-ca*d*r) 827
928 828 (defsubst caddr (X)
929 (defun caddr (X)
930 "Return the car of the cdr of the cdr of X." 829 "Return the car of the cdr of the cdr of X."
931 (car (cdr (cdr X)))) 830 (car (cdr (cdr X))))
932 (put 'caddr 'byte-compile 'byte-compile-ca*d*r) 831
933 832 (defsubst cdadr (X)
934 (defun cdadr (X)
935 "Return the cdr of the car of the cdr of X." 833 "Return the cdr of the car of the cdr of X."
936 (cdr (car (cdr X)))) 834 (cdr (car (cdr X))))
937 (put 'cdadr 'byte-compile 'byte-compile-ca*d*r) 835
938 836 (defsubst cddar (X)
939 (defun cddar (X)
940 "Return the cdr of the cdr of the car of X." 837 "Return the cdr of the cdr of the car of X."
941 (cdr (cdr (car X)))) 838 (cdr (cdr (car X))))
942 (put 'cddar 'byte-compile 'byte-compile-ca*d*r) 839
943 840 (defsubst cdddr (X)
944 (defun cdddr (X)
945 "Return the cdr of the cdr of the cdr of X." 841 "Return the cdr of the cdr of the cdr of X."
946 (cdr (cdr (cdr X)))) 842 (cdr (cdr (cdr X))))
947 (put 'cdddr 'byte-compile 'byte-compile-ca*d*r) 843
948 844 (defsubst caaaar (X)
949 (defun caaaar (X)
950 "Return the car of the car of the car of the car of X." 845 "Return the car of the car of the car of the car of X."
951 (car (car (car (car X))))) 846 (car (car (car (car X)))))
952 (put 'caaaar 'byte-compile 'byte-compile-ca*d*r) 847
953 848 (defsubst caaadr (X)
954 (defun caaadr (X)
955 "Return the car of the car of the car of the cdr of X." 849 "Return the car of the car of the car of the cdr of X."
956 (car (car (car (cdr X))))) 850 (car (car (car (cdr X)))))
957 (put 'caaadr 'byte-compile 'byte-compile-ca*d*r) 851
958 852 (defsubst caadar (X)
959 (defun caadar (X)
960 "Return the car of the car of the cdr of the car of X." 853 "Return the car of the car of the cdr of the car of X."
961 (car (car (cdr (car X))))) 854 (car (car (cdr (car X)))))
962 (put 'caadar 'byte-compile 'byte-compile-ca*d*r) 855
963 856 (defsubst cadaar (X)
964 (defun cadaar (X)
965 "Return the car of the cdr of the car of the car of X." 857 "Return the car of the cdr of the car of the car of X."
966 (car (cdr (car (car X))))) 858 (car (cdr (car (car X)))))
967 (put 'cadaar 'byte-compile 'byte-compile-ca*d*r) 859
968 860 (defsubst cdaaar (X)
969 (defun cdaaar (X)
970 "Return the cdr of the car of the car of the car of X." 861 "Return the cdr of the car of the car of the car of X."
971 (cdr (car (car (car X))))) 862 (cdr (car (car (car X)))))
972 (put 'cdaaar 'byte-compile 'byte-compile-ca*d*r) 863
973 864 (defsubst caaddr (X)
974 (defun caaddr (X)
975 "Return the car of the car of the cdr of the cdr of X." 865 "Return the car of the car of the cdr of the cdr of X."
976 (car (car (cdr (cdr X))))) 866 (car (car (cdr (cdr X)))))
977 (put 'caaddr 'byte-compile 'byte-compile-ca*d*r) 867
978 868 (defsubst cadadr (X)
979 (defun cadadr (X)
980 "Return the car of the cdr of the car of the cdr of X." 869 "Return the car of the cdr of the car of the cdr of X."
981 (car (cdr (car (cdr X))))) 870 (car (cdr (car (cdr X)))))
982 (put 'cadadr 'byte-compile 'byte-compile-ca*d*r) 871
983 872 (defsubst cdaadr (X)
984 (defun cdaadr (X)
985 "Return the cdr of the car of the car of the cdr of X." 873 "Return the cdr of the car of the car of the cdr of X."
986 (cdr (car (car (cdr X))))) 874 (cdr (car (car (cdr X)))))
987 (put 'cdaadr 'byte-compile 'byte-compile-ca*d*r) 875
988 876 (defsubst caddar (X)
989 (defun caddar (X)
990 "Return the car of the cdr of the cdr of the car of X." 877 "Return the car of the cdr of the cdr of the car of X."
991 (car (cdr (cdr (car X))))) 878 (car (cdr (cdr (car X)))))
992 (put 'caddar 'byte-compile 'byte-compile-ca*d*r) 879
993 880 (defsubst cdadar (X)
994 (defun cdadar (X)
995 "Return the cdr of the car of the cdr of the car of X." 881 "Return the cdr of the car of the cdr of the car of X."
996 (cdr (car (cdr (car X))))) 882 (cdr (car (cdr (car X)))))
997 (put 'cdadar 'byte-compile 'byte-compile-ca*d*r) 883
998 884 (defsubst cddaar (X)
999 (defun cddaar (X)
1000 "Return the cdr of the cdr of the car of the car of X." 885 "Return the cdr of the cdr of the car of the car of X."
1001 (cdr (cdr (car (car X))))) 886 (cdr (cdr (car (car X)))))
1002 (put 'cddaar 'byte-compile 'byte-compile-ca*d*r) 887
1003 888 (defsubst cadddr (X)
1004 (defun cadddr (X)
1005 "Return the car of the cdr of the cdr of the cdr of X." 889 "Return the car of the cdr of the cdr of the cdr of X."
1006 (car (cdr (cdr (cdr X))))) 890 (car (cdr (cdr (cdr X)))))
1007 (put 'cadddr 'byte-compile 'byte-compile-ca*d*r) 891
1008 892 (defsubst cddadr (X)
1009 (defun cddadr (X)
1010 "Return the cdr of the cdr of the car of the cdr of X." 893 "Return the cdr of the cdr of the car of the cdr of X."
1011 (cdr (cdr (car (cdr X))))) 894 (cdr (cdr (car (cdr X)))))
1012 (put 'cddadr 'byte-compile 'byte-compile-ca*d*r) 895
1013 896 (defsubst cdaddr (X)
1014 (defun cdaddr (X)
1015 "Return the cdr of the car of the cdr of the cdr of X." 897 "Return the cdr of the car of the cdr of the cdr of X."
1016 (cdr (car (cdr (cdr X))))) 898 (cdr (car (cdr (cdr X)))))
1017 (put 'cdaddr 'byte-compile 'byte-compile-ca*d*r) 899
1018 900 (defsubst cdddar (X)
1019 (defun cdddar (X)
1020 "Return the cdr of the cdr of the cdr of the car of X." 901 "Return the cdr of the cdr of the cdr of the car of X."
1021 (cdr (cdr (cdr (car X))))) 902 (cdr (cdr (cdr (car X)))))
1022 (put 'cdddar 'byte-compile 'byte-compile-ca*d*r) 903
1023 904 (defsubst cddddr (X)
1024 (defun cddddr (X)
1025 "Return the cdr of the cdr of the cdr of the cdr of X." 905 "Return the cdr of the cdr of the cdr of the cdr of X."
1026 (cdr (cdr (cdr (cdr X))))) 906 (cdr (cdr (cdr (cdr X)))))
1027 (put 'cddddr 'byte-compile 'byte-compile-ca*d*r)
1028 907
1029 ;;; some inverses of the accessors are needed for setf purposes 908 ;;; some inverses of the accessors are needed for setf purposes
1030 909
1031 (defun setnth (n list newval) 910 (defun setnth (n list newval)
1032 "Set (nth N LIST) to NEWVAL. Returns NEWVAL." 911 "Set (nth N LIST) to NEWVAL. Returns NEWVAL."