Rename *ll* and *ul* to ll and ul in method-by-limits
[maxima.git] / src / mmacro.lisp
blob0e21cfb1291dcd1c4a5a4bf9ac0e640b4fe9a29e
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1980 Massachusetts Institute of Technology ;;;
9 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
11 (in-package :maxima)
13 (macsyma-module mmacro)
15 ;; Exported functions are MDEFMACRO, $MACROEXPAND, $MACROEXPAND1, MMACRO-APPLY
16 ;; MMACROEXPANDED, MMACROEXPAND and MMACROEXPAND1
19 ;; $MACROS declared in jpg;mlisp >
22 (defmvar $macroexpansion ()
23 "Governs the expansion of Maxima Macros. The following settings are
24 available: FALSE means to re-expand the macro every time it gets called.
25 EXPAND means to remember the expansion for each individual call do that it
26 won't have to be re-expanded every time the form is evaluated. The form will
27 still grind and display as if the expansion had not taken place. DISPLACE
28 means to completely replace the form with the expansion. This is more space
29 efficient than EXPAND but grinds and displays the expansion instead of the
30 call."
31 modified-commands '($macroexpand)
32 :setting-list (() $expand $displace))
35 ;;; LOCAL MACRO ;;;
37 (defmacro copy1cons (name) `(cons (car ,name) (cdr ,name)))
39 ;;; DEFINING A MACRO ;;;
41 (defmspec mdefmacro (form) (setq form (cdr form))
42 (cond ((or (null (cdr form)) (cdddr form))
43 (merror (intl:gettext "macro definition: must have exactly two arguments; found: ~M")
44 `((mdefmacro) ,@form))
46 (t (mdefmacro1 (car form) (cadr form)))))
49 (defun mdefmacro1 (fun body)
50 (let ((name) (args))
51 (cond ((or (atom fun)
52 (not (atom (caar fun)))
53 (member 'array (cdar fun) :test #'eq)
54 (mopp (setq name ($verbify (caar fun))))
55 (member name '($all $% $%% mqapply) :test #'eq))
56 (merror (intl:gettext "macro definition: illegal definition: ~M") ;ferret out all the
57 fun)) ; illegal forms
58 ((not (eq name (caar fun))) ;efficiency hack I guess
59 (rplaca (car fun) name))) ; done in jpg;mlisp
60 (setq args (cdr fun)) ; (in MDEFINE).
61 (let ((dup (find-duplicate args :test #'eq :key #'mparam)))
62 (when dup
63 (merror (intl:gettext "macro definition: ~M occurs more than once in the parameter list") (mparam dup))))
64 (mredef-check name)
65 (do ((a args (cdr a)) (mlexprp))
66 ((null a)
67 (remove1 (ncons name) 'mexpr t $functions t) ;do all arg checking,
68 (cond (mlexprp (mputprop name t 'mlexprp)) ; then remove MEXPR defn
69 (t nil)))
70 (cond ((mdefparam (car a)))
71 ((and (mdeflistp a)
72 (mdefparam (cadr (car a))))
73 (setq mlexprp t))
74 (t
75 (merror (intl:gettext "macro definition: bad argument: ~M")
76 (car a)))))
77 (remove-transl-fun-props name)
78 (add2lnc `((,name) ,@args) $macros)
79 (mputprop name (mdefine1 args body) 'mmacro)
81 (cond ($translate (translate-and-eval-macsyma-expression
82 `((mdefmacro) ,fun ,body))))
83 `((mdefmacro simp) ,fun ,body)))
88 ;;; EVALUATING A MACRO CALL ;;;
91 (defun mmacro-apply (defn form)
92 (mmacroexpansion-check form
93 (if (and (atom defn)
94 (not (symbolp defn)))
95 ;; added this clause for NIL. MAPPLY
96 ;; doesn't really handle applying interpreter
97 ;; closures and subrs very well.
98 (apply defn (cdr form))
99 (mapply1 defn (cdr form) (caar form) form))))
104 ;;; MACROEXPANSION HACKERY ;;;
107 ;; does any reformatting necessary according to the current setting of
108 ;; $MACROEXPANSION. Note that it always returns the expansion returned
109 ;; by displace, for future displacing.
111 (defun mmacroexpansion-check (form expansion)
112 (case $macroexpansion
113 (( () )
114 (cond ((eq (caar form) 'mmacroexpanded)
115 (mmacro-displace form expansion))
116 (t expansion)))
117 (($expand)
118 (cond ((not (eq (caar form) 'mmacroexpanded))
119 (displace form `((mmacroexpanded)
120 ,expansion
121 ,(copy1cons form)))))
122 expansion)
123 (($displace)
124 (mmacro-displace form expansion))
125 (t (mtell (intl:gettext "warning: unrecognized value of 'macroexpansion'.")))))
128 (defun mmacro-displace (form expansion)
129 (displace form (cond ((atom expansion) `((mprogn) ,expansion))
130 (t expansion))))
133 ;; Handles memo-ized forms. Reformats them if $MACROEXPANSION has changed.
134 ;; Format is ((MMACROEXPANDED) <expansion> <original form>)
136 (defmspec mmacroexpanded (form)
137 (meval (mmacroexpansion-check form (cadr form))))
140 ;;; MACROEXPANDING FUNCTIONS ;;;
143 (defmspec $macroexpand (form) (setq form (cdr form))
144 (cond ((or (null form) (cdr form))
145 (merror (intl:gettext "macroexpand: must have exactly one argument; found: ~M")
146 `(($macroexpand) ,@form)))
147 (t (mmacroexpand (car form)))))
149 (defmspec $macroexpand1 (form) (setq form (cdr form))
150 (cond ((or (null form) (cdr form))
151 (merror (intl:gettext "macroexpand1: must have exactly one argument; found: ~M")
152 `(($macroexpand1) ,@form)))
153 (t (mmacroexpand1 (car form)))))
156 ;; Expands the top-level form repeatedly until it is no longer a macro
157 ;; form. Has to copy the form each time because if macros are displacing
158 ;; the form given to mmacroexpand1 will get bashed each time. Recursion
159 ;; is used instead of iteration so the user gets a pdl overflow error
160 ;; if he tries to expand recursive macro definitions that never terminate.
162 (defun mmacroexpand (form)
163 (let ((test-form (if (atom form) form (copy1cons form)))
164 (expansion (mmacroexpand1 form)))
165 (cond ((equal expansion test-form)
166 expansion)
167 (t (mmacroexpand expansion)))))
170 ;; only expands the form once. If the form is not a valid macro
171 ;; form it just gets returned (eq'ness is preserved). Note that if the
172 ;; macros are displacing, the returned form is also eq to the given
173 ;; form (which has been bashed).
175 (defun mmacroexpand1 (form)
176 (let ((funname) (macro-defn))
177 (cond ((or (atom form)
178 (atom (car form))
179 (member 'array (cdar form) :test #'eq)
180 (not (symbolp (setq funname (mop form)))))
181 form)
182 ((eq funname 'mmacroexpanded)
183 (mmacroexpansion-check form (cadr form)))
184 ((setq macro-defn
185 (or (and $transrun
186 (get (caar form) 'translated-mmacro))
187 (mget (caar form) 'mmacro)))
188 (mmacro-apply macro-defn form))
189 (t form))))
191 ;;; SIMPLIFICATION ;;;
193 (defprop mdefmacro simpmdefmacro operators)
195 ;; emulating simpmdef (for mdefine) in jm;simp
196 (defun simpmdefmacro (x ignored simp-flag)
197 (declare (ignore ignored simp-flag))
198 (cons '(mdefmacro simp) (cdr x)))
200 (defun displace (x y)
201 (setf (car x) (car y))
202 (setf (cdr x) (cdr y))