source: trunk/j/src/org/armedbear/lisp/source-transform.lisp @ 7468

Last change on this file since 7468 was 7468, checked in by piso, 17 years ago

Initial checkin.

File size: 2.6 KB
Line 
1;;; source-transform.lisp
2;;;
3;;; Copyright (C) 2004 Peter Graves
4;;; $Id: source-transform.lisp,v 1.1 2004-08-21 03:22:09 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20(in-package "SYSTEM")
21
22(defun source-transform (name)
23  (let ((info (function-info name)))
24    (and info (getf info :source-transform))))
25
26(defun (setf source-transform) (transform name)
27  (let ((info (function-info name)))
28    (setf info (%putf info :source-transform transform))
29    (setf (function-info name) info))
30  transform)
31
32(defmacro define-source-transform (name lambda-list &rest body)
33  (let* ((form (gensym))
34         (env (gensym))
35         (body (sys::parse-defmacro lambda-list form body name 'defmacro
36                                    :environment env))
37         (expander
38          (if (symbolp name)
39              `(lambda (,form) (block ,name ,body))
40              `(lambda (,form) (block ,(cadr name) ,body)))))
41    `(progn
42       (setf (source-transform ',name) ,expander)
43       ',name)))
44
45(defun expand-source-transform-1 (form)
46  (let ((expander nil)
47        (newdef nil))
48    (cond ((atom form)
49           (values form nil))
50          ((and (consp (car form))
51                (eq (caar form) 'SETF)
52                (setf expander (source-transform (car form))))
53           (values (setq newdef (funcall expander form))
54                   (not (eq newdef form))))
55          ((and (symbolp (car form))
56                (setf expander (source-transform (car form))))
57           (values (setq newdef (funcall expander form))
58                   (not (eq newdef form))))
59          (t
60           (values form nil)))))
61
62(defun expand-source-transform (form)
63  (let ((expanded-p nil))
64    (loop
65      (multiple-value-bind (expansion exp-p) (expand-source-transform-1 form)
66        (if exp-p
67            (setf form expansion
68                  expanded-p t)
69            (return))))
70    (values form expanded-p)))
71
72(provide :source-transform)
Note: See TracBrowser for help on using the repository browser.