1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13 (macsyma-module mrgmac macro
)
15 (defun zl-get (sym tag
)
16 (cond ((symbolp sym
) (get sym tag
))
17 ((consp sym
) (getf (cdr sym
) tag
))))
19 (defun define-macro (name lambda-exp
)
20 (cond ((symbolp lambda-exp
)
21 (setq lambda-exp
(symbol-function lambda-exp
))))
22 (setf (macro-function name
) lambda-exp
))
24 (declare-top (special name bas selector
))
26 (defvar *mobjects
* nil
)
28 (defprop mode
(c-mode s-mode a-mode
) mode
)
30 (defmacro c-mode
(&rest l
)
33 (defmacro s-mode
(&rest x
)
35 (cond ((eq 'c
(third x
)) `(first ,(second x
)))
36 ((eq 'sel
(third x
)) `(second ,(second x
)))
37 ((eq 'push
+sto
(third x
)) `(third ,(second x
)))))
39 (defmacro a-mode
(&rest x
)
41 (cond ((eq 'c
(third x
)) `(rplaca (second x
) ,(fourth x
)))
42 ((eq 'sel
(third x
)) `(rplaca (cdr ,(second x
)) ,(fourth x
)))
43 ((eq 'push
+sto
(third x
)) `(rplaca (cddr ,(second x
)) ,(fourth x
)))))
45 (defmacro defmode
(&rest x
)
47 (let ((selector (member 'selector
(cddddr x
) :test
#'eq
)))
48 (define-mode (second x
) (fourth x
))
49 (mapc 'eval
(cddddr x
))
52 (defun define-mode (name desc
)
53 (let ((c (intern (format nil
"C-~A" name
)))
54 (s (intern (format nil
"S-~A" name
)))
55 (a (intern (format nil
"A-~A" name
))))
56 (define-macro c
(defc desc
))
57 (define-macro s
(defs desc
))
58 (define-macro a
(defa desc
))
59 (putprop name
(c-mode c s a
) 'mode
)))
63 (coerce `(lambda (x &optional env
)
64 (declare (ignore env
))
69 (cond ((atom desc
) (list 'quote desc
))
70 ((eq 'selector
(car desc
))
71 (cond ((not (null (cdddr desc
))) (list 'quote
(fourth desc
)))
72 (t (setq bas
(list 'cdr bas
))
74 ((eq (quote atom
) (car desc
))
75 `(list 'c-atom
'',(mapcar 'second
(cdr desc
)) (cons (quote list
) (cdr x
))))
76 ((eq 'cons
(car desc
)) `(list 'cons
,(defc1 (second desc
)) ,(defc1 (third desc
))))
77 ((eq (quote list
) (car desc
))
78 (do ((l (cdr desc
) (cdr l
)) (nl))
79 ((null l
) `(list (quote list
) ,@(nreverse nl
)))
80 (push (defc1 (car l
)) nl
)))
81 ((eq 'struct
(car desc
)) (defc1 (cons (quote list
) (cdr desc
))))
82 (t (list 'quote desc
))))
86 (coerce `(lambda (x &optional env
)
87 (declare (ignore env
))
88 (cond ,@(nreverse (defs1 desc
'(second x
) nil
))))
91 (defun defs1 (desc bas result
)
92 (cond ((atom desc
) result
)
93 ((eq 'selector
(car desc
))
94 (putprop (second desc
)
95 (cons (cons name
(third desc
)) (zl-get (second desc
) 'modes
))
98 (cons (cons (second desc
) (third desc
)) (zl-get name
'sels
))
100 (if selector
(define-macro (second desc
) 'selector
))
101 (cons `((eq ',(second desc
) (third x
)) ,bas
) result
))
102 ((eq (quote atom
) (car desc
))
103 (do ((l (cdr desc
) (cdr l
))) ((null l
))
104 (putprop (cadar l
) (cons (cons name
(caddar l
))
105 (zl-get (cadar l
) 'modes
)) 'modes
)
106 (putprop name
(cons (cons (cadar l
) (caddar l
))
107 (zl-get name
'sels
)) 'sels
)
108 (if selector
(define-macro (cadar l
) 'selector
)))
109 (cons `((member (third x
) ',(mapcar 'second
(cdr desc
)) :test
#'eq
)
110 (list 'zl-get
,bas
(list 'quote
(third x
))))
112 ((eq 'cons
(car desc
))
113 (setq result
(defs1 (second desc
) `(list 'car
,bas
) result
))
114 (defs1 (third desc
) `(list 'cdr
,bas
) result
))
115 ((eq (quote list
) (car desc
))
116 (do ((l (cdr desc
) (cdr l
))) ((null l
))
117 (setq result
(defs1 (car l
) `(list 'car
,bas
) result
)
118 bas
`(list 'cdr
,bas
)))
120 ((eq 'struct
(car desc
)) (defs1 (cons (quote list
) (cdr desc
)) bas result
))
124 (coerce `(lambda (x &optional env
)
125 (declare (ignore env
))
126 (cond ,@(nreverse (defa1 desc
'(second x
) nil nil
))))
129 (defun defa1 (desc bas cdr result
)
130 (cond ((atom desc
) result
)
131 ((eq 'selector
(car desc
))
132 (setq bas
(cond ((not cdr
) `(list 'car
(list 'rplaca
,(third bas
) (fourth x
))))
133 (t `(list 'cdr
(list 'rplacd
,(third bas
) (fourth x
))))))
134 (cons `((eq ',(second desc
) (third x
)) ,bas
) result
))
135 ((eq (quote atom
) (car desc
))
136 (list `(t (list 'cput
(second x
) (fourth x
) (list 'quote
(third x
))))))
137 ((eq 'cons
(car desc
))
138 (setq result
(defa1 (second desc
) `(list 'car
,bas
) nil result
))
139 (defa1 (third desc
) `(list 'cdr
,bas
) t result
))
140 ((eq (quote list
) (car desc
))
141 (do ((l (cdr desc
) (cdr l
))) ((null l
))
142 (setq result
(defa1 (car l
) `(list 'car
,bas
) nil result
)
143 bas
`(list 'cdr
,bas
)))
145 ((eq 'struct
(car desc
)) (defa1 (cons (quote list
) (cdr desc
)) bas cdr result
))
150 (cdr (assoc x
*mobjects
* :test
#'equal
)))
152 (defmacro modedeclare
(&rest l
)
153 `(modeclare-internal ',l
))
155 (defun modedeclare-internal (x)
157 (mapc #'(lambda (v) (push (cons v
(car l
)) *mobjects
*))
161 (defmacro sel
(&rest x
)
163 (let ((s (fsel (mode (second x
)) (cddr x
))))
164 (cond ((null s
) (merror "SEL: ~a ~a" (second x
) (cddr x
)))
165 (t (setq x
(second x
))
168 (setq x
(cons (second (zl-get (car s
) 'mode
)) (rplaca s x
))
170 (rplacd (cddr x
) nil
))))))
172 (defun fsel (m sels
) ;;This has a bug in it.
173 (cond ((null sels
) (list m
))
175 (do ((l (zl-get (car sels
) 'modes
) (cdr l
)))
177 (if (setq m
(fsel (cdar l
) (cdr sels
)))
178 (return (cons (caar l
) (cons (car sels
) m
))))))
180 (if (setq dum
(assoc (car sels
) (zl-get m
'sels
) :test
#'eq
))
181 (cons m
(cons (car sels
) (fsel (cdr dum
) (cdr sels
)))))))
182 (t (do ((l (zl-get m
'sels
) (cdr l
)) (dum))
184 (if (setq dum
(fsel (cdar l
) sels
))
185 (return (cons m
(cons (caar l
) dum
))))))))
187 (defun selector (x env
)
188 (declare (ignore env
))
190 `(sel ,(second x
) ,(car x
))
191 `(push+sto
(sel ,(second x
) ,(car x
)) ,(third x
))))
193 (defmacro push
+sto
(&rest x
)
197 (defmacro sto
(&rest x
)
199 (do ((l (cdr x
) (cddr l
))
202 ((null l
) `(progn ,@(nreverse nl
)))
203 (cond ((atom (car l
))
204 (push `(setq ,(first l
) ,(second l
)) nl
))
205 ((and (eq 'sel
(caar l
)) (setq s
(fsel (mode (cadar l
)) (cddar l
))))
207 (do ((l (cddr s
) (cddr l
)))
209 (setq x
(cons (second (zl-get (first l
) 'mode
)) (rplaca l x
)))
210 (rplacd (cddr x
) nil
))
211 (push (list (third (zl-get (first s
) 'mode
)) x
(second s
) (second l
)) nl
))
212 (t (merror "STO: ~a" (car l
))))))
214 (defmacro cons-exp
(op &rest args
)
215 `(simplify (list (list ,op
) ,@args
)))