| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil; Package: C -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; |
| 5 |
;;;; Copyright (c) 1984, Taiichi Yuasa and Masami Hagiya. |
| 6 |
;;;; Copyright (c) 1990, Giuseppe Attardi. |
| 7 |
;;;; |
| 8 |
;;;; See file 'LICENSE' for the copyright details. |
| 9 |
|
| 10 |
;;;; CMPMAP Map functions. |
| 11 |
|
| 12 |
(in-package "COMPILER") |
| 13 |
|
| 14 |
(defun expand-mapcar (whole) |
| 15 |
(when (< (length whole) 3) |
| 16 |
(cmpwarn "Too few arguments to function ~A in form: ~A" (first whole) whole) |
| 17 |
(return-from expand-mapcar |
| 18 |
`(si:simple-program-error |
| 19 |
"Too few arguments to function ~A in form: ~A" ',(first whole) ',whole))) |
| 20 |
(let ((which (first whole))) |
| 21 |
(when (eq which 'FUNCALL) |
| 22 |
(setf whole (rest whole) |
| 23 |
which (first whole)) |
| 24 |
(when (consp which) |
| 25 |
(if (eq (first which) 'FUNCTION) |
| 26 |
(setf which (second which)) |
| 27 |
(return-from expand-mapcar whole)))) |
| 28 |
(let* ((function (gensym)) |
| 29 |
(fun-with `(with ,function = ,(second whole))) |
| 30 |
(args (cddr whole)) |
| 31 |
iterators for-statements |
| 32 |
(in-or-on :IN) |
| 33 |
(do-or-collect :COLLECT) |
| 34 |
(list-1-form nil) |
| 35 |
(finally-form nil)) |
| 36 |
(case which |
| 37 |
(MAPCAR) |
| 38 |
(MAPLIST (setf in-or-on :ON)) |
| 39 |
(MAPC (setf do-or-collect :DO)) |
| 40 |
(MAPL (setf in-or-on :ON do-or-collect :DO)) |
| 41 |
(MAPCAN (setf do-or-collect 'NCONC)) |
| 42 |
(MAPCON (setf in-or-on :ON do-or-collect 'NCONC))) |
| 43 |
(when (eq in-or-on :ON) |
| 44 |
(setf args (mapcar #'(lambda (arg) `(ext:checked-value list ,arg)) args))) |
| 45 |
(when (eq do-or-collect :DO) |
| 46 |
(let ((var (gensym))) |
| 47 |
(setf list-1-form `(with ,var = ,(first args)) |
| 48 |
args (list* var (rest args)) |
| 49 |
finally-form `(finally (return ,var))))) |
| 50 |
(loop for arg in (reverse args) |
| 51 |
do (let ((var (gensym))) |
| 52 |
(setf iterators (cons var iterators) |
| 53 |
for-statements (list* :for var in-or-on arg for-statements)))) |
| 54 |
`(loop ,@fun-with |
| 55 |
,@list-1-form |
| 56 |
,@for-statements |
| 57 |
,do-or-collect (funcall ,function ,@iterators) |
| 58 |
,@finally-form)))) |
| 59 |
|
| 60 |
(define-compiler-macro mapcar (&whole whole &rest r) |
| 61 |
(expand-mapcar whole)) |
| 62 |
|
| 63 |
(define-compiler-macro mapc (&whole whole &rest r) |
| 64 |
(expand-mapcar whole)) |
| 65 |
|
| 66 |
(define-compiler-macro mapcan (&whole whole &rest r) |
| 67 |
(expand-mapcar whole)) |
| 68 |
|
| 69 |
(define-compiler-macro maplist (&whole whole &rest r) |
| 70 |
(expand-mapcar whole)) |
| 71 |
|
| 72 |
(define-compiler-macro mapl (&whole whole &rest r) |
| 73 |
(expand-mapcar whole)) |
| 74 |
|
| 75 |
(define-compiler-macro mapcon (&whole whole &rest r) |
| 76 |
(expand-mapcar whole)) |