Remove useless SPECIALP calls
[maxima.git] / src / transl.lisp
blob27eac8bbc0f7e1c6c0fb8e9f8f2ee7877ec64777
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
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 ;;; GJC 9:29am Saturday, 5 April 1980 ;;;
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 (in-package :maxima)
14 (macsyma-module transl)
16 ;;; File directory.
18 ;;; TRANSL Driver. Basic translation properties.
19 ;;; TRANSS User-interaction, FILE-I/O etc.
20 ;;; TRANS1 Translation of JPG;MLISP and other FSUBRS.
21 ;;; which take call-by-name parameters.
22 ;;; TRANS2 LISTS, ARRAYs, other random operators.
23 ;;; TRANS3 LAMBDA. CLOSURES. also used by fsubr call-by-name
24 ;;; compatibility package.
25 ;;; TRANS4 operators, ".", "^^" some functions such as GAMMA.
26 ;;; TRANS5 FSUBRS from COMM, and others, these are mere MACRO
27 ;;; FSUBRS.
28 ;;; TRANSF floating point intensive properties. BIGFLOAT stuff.
29 ;;; TROPER Basic OPERATORS.
30 ;;; TRUTIL transl utilities.
31 ;;; TRMODE definition of MODEDECLARE. run time error checking code.
32 ;;; TRDATA this is the MODE data for the "built-in" functions.
33 ;;; TRANSM This defines the macro DEF%TR. When compiled on MC
34 ;;; DEF%TR produces autoload definitions for TRANS1 thru L.
35 ;;; PROCS macro's needed.
36 ;;; TRANSQ these are macros for translated code. Loaded by TPRELU
37 ;;; this is compile-time only.
38 ;;; MDEFUN contains the macro which defines macsyma functions.
39 ;;; runtime and compile-time.
40 ;;; ACALL is some run time support for translated code, array calls.
41 ;;; FCALL run-time translated function call support for uncompiled
42 ;;; code. Many FSUBRS which are macros in TRANSQ.
43 ;;; EVALW EVAL-WHEN definition for interpreter.
44 ;;; MLOAD This has a hack hook into BATCH, which is needed to do
45 ;;; TRANSLATE_FILE I/O. when using old-i/o SUPRV.
48 ;;; Functions and literals have various MODE properties;;; >
49 ;;; (at user level set up by $MODEDECLARE), such as "$FLOAT" and "$ANY".
50 ;;; The main problem solved by this translator (and the reason that
51 ;;; it works on forms from the "inside out" as an evaluator would do
52 ;;; (expect for macro forms)), is the problem of type (MODE) dependent
53 ;;; function calling and mode conversion. The function TRANSLATE
54 ;;; returns a list where the CAR of the list is the MODE of the
55 ;;; expression and the CDR is the expression to be evaluated by
56 ;;; the lisp evaluator to give the equivalent result of evaluating
57 ;;; the given macsyma expression with the macsyma evaluator.
58 ;;; One doesn't know the MODE of an expression until seeing the modes
59 ;;; of all its parts. See "*UNION-MODE"
61 ;;; weak points in the code
62 ;;; [1] duplication of functionality in the translators for
63 ;;; MPLUS MTIMES etc.
64 ;;; [3] primitive mode scheme. lack of even the most primitive general
65 ;;; type coercion code. Most FORTRAN compilers are better than this.
66 ;;; [4] for a compiler, this code SUCKS when it comes to error checking
67 ;;; of the code it is munging. It doesn't even do a WNA check of system
68 ;;; functions!
69 ;;; [5]
70 ;;; The duplication of the code which handles lambda binding, in MDO, MDOIN
71 ;;; TR-LAMBDA, and MPROG, is very stupid. For macsyma this is one of
72 ;;; the hairier things. Declarations must be handled, ASSIGN properties...
73 ;;; -> Binding of ASSIGN properties should be handled with he "new"
74 ;;; UNWIND-PROTECT instead of at each RETURN, and at "hope" points such as
75 ;;; the ERRLIST. {Why wasn't this obvious need for UNWIND-PROTECT made
76 ;;; known to the lisp implementers by the macsyma implementers? Why did it
77 ;;; have to wait for the lisp machine group? Isn't this just a generalization
78 ;;; of special binding?}
79 ;;; [6] the DCONVX idea here is obscurely coded, incomplete, and totally
80 ;;; undocumented. It was probably an attempt to hack efficient
81 ;;; internal representations (internal to a given function), for some macsyma
82 ;;; data constructs, and yet still be sure that fully general legal data
83 ;;; frobs are seen outside of the functions. Note: this can be done
84 ;;; simply by type coercion and operator folding.
86 ;;; General comments on the structure of the code.
87 ;;; A function named TR-<something> means that it translates
88 ;;; something having to do with that something.
89 ;;; N.B. It does not mean that that is the translate property for <something>.
92 (defvar *untranslated-functions-called* nil)
94 (defmvar *declared-translated-functions* nil
95 "List of functions which are believed to be translated.")
97 (defmvar tstack nil " stack of local variable modes ")
99 (defmvar local nil "T if a $local statement is in the body.")
100 (defmvar tr-progret t)
101 (defmvar inside-mprog nil)
102 (defmvar returns nil "list of `translate'd return forms in the block.")
103 (defmvar return-mode nil "the highest(?) mode of all the returns.")
104 (defmvar need-prog? nil)
105 (defmvar assigns nil "These are very-special variables which have a Maxima
106 assign property which must be called to bind and unbind the variable
107 whenever it is `lambda' bound.")
109 (defmvar translate-time-evalables
110 '($modedeclare $alias $declare $infix $nofix $declare_translated
111 $matchfix $prefix $postfix $compfile))
113 (defmvar *transl-backtrace* nil
114 " What do you think? ")
116 (defmvar *transl-debug* nil "if T it pushes `backtrace' and `trace' ")
118 (defmvar tr-abort nil "set to T if abortion is requested by any of the
119 sub-parts of the translation. A *THROW would be better, although it
120 wouldn't cause the rest of the translation to continue, which may
121 be useful in translation for MAXIMA-ERROR checking.")
123 (defmvar tr-unique (gensym)
124 "this is just a unque object used for random purposes,
125 such as the second (file end) argument of READ.")
128 (defmvar $tr_warn_undeclared '$compile
129 "When to send warnings about undeclared variables to the TTY")
131 (defmvar $tr_warn_meval '$compfile
132 "If `meval' is called that indicates problems in the translation")
134 (defmvar $tr_warn_fexpr
135 '$compfile
136 "FEXPRS should not normally be output in translated code, all legitimate
137 special program forms are translated.")
139 (defmvar $tr_warn_mode '$all
140 "Warn when variables are assigned values out of their mode.")
142 (defmvar $tr_warn_undefined_variable '$all
143 "Warn when undefined global variables are seen.")
146 (defmvar *warned-un-declared-vars* nil "Warning State variable")
147 (defmvar *warned-fexprs* nil "Warning State variable")
148 (defmvar *warned-mode-vars* nil "Warning State variable")
150 (defmvar $tr_function_call_default '$general
152 FALSE means punt to MEVAL, EXPR means assume lisp fixed arg function.
153 GENERAL, the default gives code good for mexprs and mlexprs but not macros.
154 GENERAL assures variable bindings are correct in compiled code.
155 In GENERAL mode, when translating F(X), if F is a bound variable, then
156 it assumes that APPLY(F,[X]) is meant, and translates a such, with
157 appropriate warning. There is no need to turn this off.
158 APPLY means like APPLY.")
160 (defmvar $tr_array_as_ref t
161 "If true runtime code uses value of the variable as the array.")
163 (defmvar $tr_numer nil
164 "If `true' numer properties are used for atoms which have them, e.g. %pi")
166 (defvar boolean-object-table
167 '(($true . ($boolean . t))
168 ($false . ($boolean . nil))
169 (t . ($boolean . t))
170 (nil . ($boolean . nil))))
172 (defvar mode-init-value-table
173 '(($float . 0.0)
174 ($fixnum . 0)
175 ($number . 0)
176 ($list . '((mlist)))
177 ($boolean . nil)))
179 (defvar tr-lambda-punt-assigns nil
180 "Kludge argument to `tr-lambda' due to lack of keyword argument passing")
182 (or (boundp '*in-compile*)
183 (setq *in-compile* nil))
185 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
188 (defun barfo (msg)
189 (tr-format (intl:gettext "translator: internal error. Message: ~:M~%") msg)
190 (cond (*transl-debug*
191 (break "transl barfo"))
193 (setq tr-abort t)
194 nil)))
196 (defun specialp (var)
197 (or (optionp var)
198 (tr-get-special var)))
201 ;;; The error message system. Crude as it is.
202 ;;; I tell you how this aught to work:
203 ;;; (1) All state should be in one structure, one state variable.
204 ;;; (2) Should print out short message on-the-fly so that it
205 ;;; gives something to watch, and also so that it says something
206 ;;; if things crash.
207 ;;; (3) Summaries on a cross-referenced per-function and per-item
208 ;;; should be printed at the end, as a table.
209 ;;; e.g.
210 ;;; Undefined Functions used in
211 ;;; FOO BAR, BAZ,BOMB
212 ;;; FOOA P,Q
213 ;;; Undefined Variables ... same thing
214 ;;; Incomprehensible special forms
215 ;;; EV ....
216 ;;; Predicate Mode Targeting failures.
217 ;;; ..... -gjc
219 ;;; The way it works now is to print too little or too much.
220 ;;; Many items are only warned about the first time seen.
221 ;;; However, this isn't too much of a problem when using Emacs
222 ;;; to edit code, because searching for warned-about tokens
223 ;;; is quick and easy.
225 (defmvar *tr-warn-break* t
226 " if in debug mode `warning's signaled go to lisp break loops ")
228 (defmacro tr-warnbreak ()
229 `(and *transl-debug* *tr-warn-break* (break "transl")))
231 (defun tr-warnp (val)
232 (and val
233 (cond (*in-compile*
234 (member val '($all $compile $compfile $translate) :test #'eq))
235 ((or *in-compfile* *in-translate-file*)
236 (member val '($all $compfile $translate) :test #'eq))
237 (*in-translate*
238 (member val '($all $translate) :test #'eq)))))
240 (defvar warned-undefined-variables nil)
242 (defun warn-undefined-variable (form)
243 (and (tr-warnp $tr_warn_undefined_variable)
244 (cond ((member form warned-undefined-variables :test #'eq))
246 (push form warned-undefined-variables)
247 (tr-format (intl:gettext "warning: encountered undefined variable ~:M in translation.~%") form)
248 (tr-warnbreak)))))
250 (defun warn-undeclared (form &optional comment)
251 (and (tr-warnp $tr_warn_undeclared)
252 (cond ((member form *warned-un-declared-vars* :test #'equal) t)
254 (push form *warned-un-declared-vars*)
255 (tr-format (intl:gettext "warning: no type declaration for ~:M; assume type is 'any'.~%") form)
256 (tr-format (intl:gettext "note: 'modedeclare' declares types for translation.~%"))
257 (cond (comment
258 (dolist (v *translation-msgs-files*)
259 (terpri v)
260 (princ comment v))))
261 (tr-warnbreak)
262 nil))))
264 (defun warn-meval (form &optional comment)
265 (cond ((tr-warnp $tr_warn_meval)
266 (tr-format (intl:gettext "warning: emit call to MEVAL for expression: ~:M~%") form)
267 (cond (comment (dolist (v *translation-msgs-files*)
268 (terpri v)
269 (princ comment v))))
270 (tr-warnbreak)
271 'warned)))
274 (defun warn-mode (var mode newmode &optional comment)
275 (cond ((eq mode newmode))
277 (cond ((and (tr-warnp $tr_warn_mode)
278 (not (covers mode newmode))
279 (not (member (list var mode newmode) *warned-mode-vars* :test #'equal)))
280 (push (list var mode newmode) *warned-mode-vars*)
281 (tr-format (intl:gettext "warning: variable ~:M (declared type ~:M) assigned type ~:M.~%") var mode newmode)
282 (cond (comment
283 (dolist (v *translation-msgs-files*)
284 (terpri v)
285 (princ comment v))))
286 (tr-warnbreak))))))
288 (defun warn-fexpr (form &optional comment)
289 (cond ((and (tr-warnp $tr_warn_fexpr)
290 (not (member form *warned-fexprs* :test #'equal)))
291 (push form *warned-fexprs*)
292 (tr-format (intl:gettext "warning: ~:M is a special function without a full Lisp translation.~%") form)
293 (tr-format (intl:gettext "warning: calling ~:M in compiled code might not have the desired effect.~%") form)
294 (cond (comment
295 (dolist (v *translation-msgs-files*)
296 (terpri v)
297 (princ comment v))))
298 (tr-warnbreak))))
301 (defun macsyma-special-macro-p (fcn)
302 (getl-lm-fcn-prop fcn '(macro)))
304 (defun macsyma-special-op-p (f)
305 (getl f '(fsubr fexpr mfexpr* mfexpr*s *fexpr)))
307 (defun possible-predicate-op-p (f)
308 (member f '(mnotequal mequal $equal mgreaterp mgeqp mlessp mleqp) :test #'eq))
310 ;;;***************************************************************;;;
312 ;;; This function is the way to call the TRANSLATOR on an expression with
313 ;;; locally bound internal mode declarations. Result of TR-LAMBDA will be
314 ;;; (MODE . (LAMBDA (...) (DECLARE ...) TRANSLATED-EXP))
316 (defun tr-local-exp (exp &rest vars-modes)
317 (let ((loc (let ((tr-lambda-punt-assigns t))
318 (tr-lambda `((lambda) ((mlist) ,@(do ((l vars-modes (cddr l))
319 (ll nil (cons (car l) ll)))
320 ((null l) ll)
321 (or (variable-p (car l))
322 (bad-var-warn (car l)))))
323 (($modedeclare) ,@vars-modes)
324 ,exp)))))
325 (let ((mode (car loc))
326 (exp (car (last loc))))
327 (cons mode exp))))
329 (defun tr-args (form)
330 (mapcar #'(lambda (x) (dconvx (translate x))) form))
332 (defun dtranslate (form) (cdr (translate form)))
334 (defun dconv (x mode)
335 (cond ((eq '$float mode) (dconv-$float x))
336 ((eq '$cre mode) (dconv-$cre x))
337 (t (cdr x))))
339 (defun dconvx (x)
340 (if (member (car x) '(ratexpr pexpr) :test #'eq)
341 (dconv-$cre x)
342 (cdr x)))
344 (defun dconv-$float (x)
345 (cond ((member (car x) '($fixnum $number) :test #'eq)
346 (if (integerp (cdr x)) (float (cdr x)) (list 'float (cdr x))))
347 ((eq '$rational (car x))
348 (cond ((or (atom (cdr x))
349 (not (eq 'quote (cadr x))))
350 `($float ,(cdr x)))
352 (/ (float (cadadr (cdr x))) (float (caddr (caddr x)))))))
353 (t (cdr x))))
355 (defun dconv-$cre (x)
356 (if (eq '$cre (car x))
357 (cdr x)
358 `(ratf ,(cdr x))))
360 (defmvar *$any-modes* '($any $list))
362 (defun covers (mode1 mode2)
363 (cond ((eq mode1 mode2) t)
364 ((eq '$float mode1) (member mode2 '($float $fixnum $rational) :test #'eq))
365 ((eq '$number mode1) (member mode2 '($fixnum $float) :test #'eq))
366 ((member mode1 *$any-modes* :test #'eq) t)))
368 ;;; takes a function name as input.
370 (defun tr-mfun (name &aux (*transl-backtrace* nil))
371 (let ((def-form (consfundef name nil nil)))
372 (cond ((null def-form)
373 (setq tr-abort t))
375 (tr-mdefine-toplevel def-form)))))
377 ;;; DEFUN
378 ;;; All the hair here to deal with macsyma fexprs has been flushed.
379 ;;; Right now this handles MDEFMACRO and MDEFINE. The decisions
380 ;;; of where to put the actual properties and what kind of
381 ;;; defuns to make (LEXPR EXPR for maclisp) are punted to the
382 ;;; macro package.
384 (defun tr-mdefine-toplevel (form &aux (and-restp nil))
385 (destructuring-let (((((name . flags) . args) body) (cdr form))
386 (a-args) kind out-forms)
388 (do ((args args (cdr args))
389 ;; array functions cannot be LEXPR-like. gee.
390 ;; there is no good reason for that, except for efficiency,
391 ;; and I know that efficiency was not a consideration.
392 (full-restricted-flag (or (eq name 'mqapply)
393 (member 'array flags :test #'eq))))
394 ((null args) (setq a-args (nreverse a-args)))
395 (let ((u (car args)))
396 (cond ((atom u)
397 (push u a-args))
398 ((and (not full-restricted-flag)
399 (not and-restp)
400 (eq (caar u) 'mlist)
401 (cdr u) (atom (cadr u)))
402 (push (cadr u) a-args)
403 (setq and-restp t))
405 (push tr-unique a-args)))))
408 (cond ((eq name 'mqapply)
409 ;; don't you love syntax!
410 ;; do a switch-a-roo here. Calling ourselves recursively
411 ;; like this allows all legal forms and also catches
412 ;; errors. However, certain generalizations are also
413 ;; allowed. They won't get passed the interpreter, but
414 ;; interesting things may happen here. Thats what you
415 ;; get from too much syntax, so don't sweat it.
416 (tr-mdefine-toplevel
417 `(,(car form) ,(car args)
418 ((lambda) ((mlist) ,@(cdr args)) ,body))))
419 ((member tr-unique a-args :test #'eq)
420 ;; WHAT IS "BAD" ABOUT THE ARGUMENT LIST HERE ??
421 (tr-format (intl:gettext "error: unhandled argument list in function definition: ~:M~%") `((mlist),@args))
422 (setq tr-abort t)
423 nil)
424 ((member (caar form) '(mdefine mdefmacro) :test #'eq)
425 (setq kind (cond ((eq (caar form) 'mdefmacro) 'macro)
426 ((member 'array flags :test #'eq) 'array)
427 (t 'func)))
428 (let* ((t-form
429 (tr-lambda `((lambda) ((mlist) ,@a-args) ,body)))
430 (desc-header
431 `(,name ,(car t-form) ,(caar form)
432 ,and-restp ,(eq kind 'array))))
433 (cond ((eq kind 'func)
434 (push-pre-transl-form
435 `(defmtrfun-external ,desc-header))
436 (and (not (member (car t-form) '($any nil) :test #'eq))
437 (putprop name (car t-form) 'function-mode)))
438 ((eq kind 'array)
439 (and (not (member (car t-form) '($any nil) :test #'eq))
440 (decmode-arrayfun name (car t-form)))))
442 (cond ((or *in-translate* (not $packagefile))
443 ; These are all properties which tell the
444 ; user that functions are in the environment,
445 ; and that also allow him to SAVE the functions.
446 (push `(defprop ,name t translated) out-forms)
447 (push `(add2lnc ',name $props) out-forms)
448 (cond ((eq '$all $savedef)
449 (push
450 `(add2lnc
451 '((,name ,@flags) ,@args)
452 ,(case kind
453 (array '$arrays)
454 (func '$functions)
455 (macro '$macros))) out-forms)))))
456 (cond ((eq '$all $savedef)
457 ;; For some reason one may want to save the
458 ;; interpreted definition even if in a PACKAGEFILE.
459 ;; not a good idea to use SAVEDEF anyway though.
460 (push `(mdefprop ,name
461 ((lambda) ((mlist) ,@args) ,body)
462 ,(case kind
463 (array 'aexpr)
464 (macro 'mmacro)
465 (func 'mexpr)))
466 out-forms)))
467 ;;once a function has been translated we want to make sure mfunction-call is eliminated.
468 (progn
469 (remprop (car desc-header) 'undefined-warnp)
470 (setf (get (car desc-header) 'once-translated) "I was once translated"))
471 `(progn
472 ,@(nreverse out-forms)
473 (defmtrfun ,desc-header ,@(cdr (cdr t-form))))))
475 (barfo '?)))))
477 (defun translate-function (name)
478 (bind-transl-state
479 (setq *in-translate* t)
480 (let ((lisp-def-form (tr-mfun name))
481 (delete-subr? (and (get name 'translated)
482 (not (get name 'expr)))))
483 (cond (tr-abort
484 (trfail name))
486 (if delete-subr? (remprop name 'subr))
487 (if (mget name 'trace) (macsyma-untrace name))
488 (if (not $savedef) (meval `(($remfunction) ,name)))
489 (let ((lisp-action
490 ; apply EVAL so it is easy to TRACE.
491 ; ERRSET is crude, but...
492 (errset (apply 'eval (list lisp-def-form)))))
493 (cond ((not lisp-action)
494 (trfail name))
495 (t name))))))))
498 (defun trfail (x)
499 (tr-format (intl:gettext "error: failed to translate ~:@M~%") x)
500 nil)
502 (defun translate-and-eval-macsyma-expression (form)
503 ;; this is the hyper-random entry to the transl package!
504 ;; it is used by MLISP for TRANSLATE:TRUE "::=".
505 (bind-transl-state
506 (setq *in-translate* t)
507 ;; Use TRANSLATOR-EVAL so we don't have to lose badly by tracing EVAL
508 (translator-eval (translate-macexpr-toplevel form))))
510 (defun translator-eval (x)
511 (eval x))
513 ;; This basically tells the msetq def%tr to use defparameter insetad
514 ;; of setq because we're doing a setq at top-level, which isn't
515 ;; specified by ANSI CL.
516 (defvar *macexpr-top-level-form-p* nil)
518 (defun translate-macexpr-toplevel (form &aux (*transl-backtrace* nil) tr-abort)
519 ;; there are very few top-level special cases, I don't
520 ;; think it would help the code any to generalize TRANSLATE
521 ;; to target levels.
523 ;; Except msetq at top-level is special for ANSI CL. See below.
524 (setq form (toplevel-optimize form))
525 (cond ((atom form) nil)
526 ((eq (caar form) '$eval_when)
527 (let ((whens (cadr form))
528 (body (cddr form)) tr-whens)
529 (setq whens (cond (($listp whens) (cdr whens))
530 ((atom whens) (list whens))
532 (tr-format (intl:gettext "error: 'eval_when' argument must be a list or atom; found: ~:M~%") (cadr form))
533 nil)))
534 (setq tr-whens (mapcar 'stripdollar whens))
535 (cond ((member '$translate whens :test #'eq)
536 (mapc 'meval body)))
537 (cond ((member '$loadfile whens :test #'eq)
538 `(progn
539 ,@(mapcar 'translate-macexpr-toplevel body)))
540 ((setq tr-whens (intersect tr-whens '(:compile-toplevel :load-toplevel :execute)))
541 `(eval-when
542 ,tr-whens
543 ,@(mapcar 'translate-macexpr-toplevel body)))
544 ((member '$compile whens :test #'eq)
545 ;; strictly for the knowledgeable user.
546 `(eval-when
547 #+gcl (compile)
548 #-gcl (:compile-toplevel)
549 ,@(mapcar 'translate-macexpr-toplevel body))))))
550 ((member (caar form) translate-time-evalables :test #'eq)
551 (meval1 form)
552 `(eval-when
553 #+gcl (compile load eval)
554 #-gcl (:compile-toplevel :load-toplevel :execute)
555 (meval* ',form)))
556 ((member (caar form) '(mdefine mdefmacro) :test #'eq)
557 (let ((name (caaadr form))
558 (trl))
559 (tr-format (intl:gettext "note: translating ~:@M~%") name)
560 (setq trl (tr-mdefine-toplevel form))
561 (cond (tr-abort
562 (tr-format (intl:gettext "error: failed to translate ~:@M~%") name)
563 (tr-format (intl:gettext "note: keep going and hope for the best.~%"))
564 `(meval* ',form))
565 (t trl))))
566 ((eq 'mprogn (caar form))
567 ;; note that this ignores the $%% crock.
568 `(progn ,@(mapcar #'translate-macexpr-toplevel (cdr form))))
569 ((eq 'msetq (caar form))
570 ;; Toplevel msetq's should really be defparameter instead of
571 ;; setq for Common Lisp.
572 (let ((*macexpr-top-level-form-p* t))
573 (dtranslate form)))
574 ((eq '$define_variable (caar form))
575 ;; Toplevel msetq's should really be defparameter instead of
576 ;; setq for Common Lisp.
577 (let ((*macexpr-top-level-form-p* t))
578 (dtranslate form)))
580 (let ((t-form (dtranslate form)))
581 (cond (tr-abort
582 `(meval* ',form))
584 t-form))))))
588 (defmvar $tr_optimize_max_loop 100.
589 "The maximum number of times the macro-expansion and optimization
590 pass of the translator will loop in considering a form.
591 This is to catch macro expansion errors, and non-terminating
592 optimization properties.")
594 (defun toplevel-optimize (form)
595 ;; it is vital that optimizations be done within the
596 ;; context of variable meta bindings, declarations, etc.
597 ;; Also: think about calling the simplifier here.
598 (cond ((atom form)
599 (cond ((symbolp form)
600 ;; If this symbol has the constant property, then
601 ;; use its assigned constant value in place of the
602 ;; symbol.
603 (let ((v (getl (mget form '$props) '($constant))))
604 (if v (cadr v) form)))
605 (t form)))
607 (do ((new-form)
608 (kount 0 (1+ kount)))
609 ;; tailrecursion should always arrange for a counter
610 ;; to check for mobylossage.
611 ((> kount $tr_optimize_max_loop)
612 (tr-format (intl:gettext "warning: I've looped ~A times in macro expansion; just give up and return ~:@M~%")
613 $tr_optimize_max_loop (caar form))
614 form)
615 (setq new-form (toplevel-optimize-1 form))
616 (cond ((atom new-form)
617 (return (toplevel-optimize new-form)))
618 ((eq new-form form)
619 (return form))
621 (setq form new-form)))))))
623 (defun toplevel-optimize-1 (form &aux (op (car form)) prop)
624 (cond ((or (atom op)
625 (member 'array op :test #'eq)) form)
626 ((progn (setq op (car op))
627 (setq prop
628 (if $transrun ; crock a minute.
629 (or (get op 'translated-mmacro)
630 (mget op 'mmacro))
631 (or (mget op 'mmacro)
632 (get op 'translated-mmacro)))))
633 (mmacro-apply prop form))
634 ((setq prop ($get op '$optimize))
635 ;; interesting, the MAPPLY here causes the simplification
636 ;; of the form and the result.
637 ;; The optimize property can be used to implement
638 ;; such niceties as the $%% crock.
639 (mapply1 prop (list form) "an optimizer property" nil))
640 ((and ($get op '$transload)
641 (get op 'autoload)
642 ;; check for all reasonable definitions,
643 ;; $OPTIMIZE and MACRO already checked.
644 (not (or (get-lisp-fun-type op)
645 (getl op '(translate mfexpr* mfexpr*s
646 fsubr fexpr *fexpr
647 macro
648 ;; foobar?
650 (mgetl op '(mexpr)))))
651 (load-function op t)
652 ;; to loop.
653 (cons (car form) (cdr form)))
654 (t form)))
656 (defun translate (form)
657 (and *transl-debug* (push form *transl-backtrace*))
658 (setq form (toplevel-optimize form))
659 (and *transl-debug* (pop *transl-backtrace*))
660 (prog2
661 (and *transl-debug* (push form *transl-backtrace*))
662 (if (atom form)
663 (translate-atom form)
664 (translate-form form))
665 ;; hey boy, reclaim that cons, just don't pop it!
666 (and *transl-debug* (pop *transl-backtrace*))))
668 (defun translate-atom (form &aux temp)
669 (cond ((numberp form) (cons (tr-class form) form))
670 ((setq temp (assoc form boolean-object-table :test #'eq))
671 (cdr temp))
672 ((and (setq temp (mget form '$numer)) $tr_numer)
673 `($float . ,temp))
674 ((setq temp (implied-quotep form))
675 `($any . ',temp))
676 ((tboundp form)
677 (setq form (teval form))
678 `(,(value-mode form) . ,form))
680 (cond ((not (specialp form))
681 (warn-undefined-variable form)))
682 ;; note that the lisp analysis code must know that
683 ;; the TRD-MSYMEVAL form is a semantic variable.
684 (let* ((mode (value-mode form))
685 (init-val (assoc mode mode-init-value-table :test #'eq)))
686 (setq init-val (cond (init-val (cdr init-val))
687 (t `',form)))
688 ;; in the compiler TRD-MSYMEVAL doesn't do a darn
689 ;; thing, but it provides dynamic initialization of
690 ;; variables in interpreted code which is translated
691 ;; in-core. In FILE loaded code the DEFVAR will take
692 ;; care of this.
693 (push-defvar form init-val)
694 `(,mode . (trd-msymeval ,form ,init-val))))))
696 (defun translate-form (form &aux temp)
697 (cond ((eq (car form) 'meval) (cons '$any form)) ;;for those lispy macsyma forms
698 ((not (atom (caar form)))
699 ;; this is a check like that in the simplifier. form could
700 ;; result from substitution macros.
701 (translate `((mqapply) ,(caar form) . ,(cdr form))))
702 ((member 'array (cdar form) :test #'eq)
703 ;; dispatch this bad-boy to another module quick.
704 (tr-arraycall form))
705 ;; TRANSLATE properties have priority.
706 ((setq temp (get (caar form) 'translate))
707 (funcall temp form))
708 ((setq temp (get-lisp-fun-type (caar form)))
709 (tr-lisp-function-call form temp))
710 ((setq temp (macsyma-special-macro-p (caar form)))
711 (attempt-translate-random-macro-op form temp))
712 ((setq temp (macsyma-special-op-p (caar form)))
713 ;; a special form not handled yet! foobar!
714 (attempt-translate-random-special-op form temp))
715 ((or (get (caar form) 'noun) (get (caar form) 'operators))
716 ;; puntastical case. the weird ones are presumably taken care
717 ;; of by TRANSLATE properties by now.
718 (tr-infamous-noun-form form))
720 ;; "What does a macsyma function call mean?".
721 ;; By the way, (A:'B,B:'C,C:'D)$ A(3) => D(3)
722 ;; is not supported.
724 (tr-macsyma-user-function-call (caar form) (cdr form) form))))
728 (defmvar $tr_bound_function_applyp t)
730 (defun tr-macsyma-user-function-call (function args form)
731 ;; this needs some work, output load-time code to
732 ;; check for MMACRO properties, etc, to be really
733 ;; foolproof.
734 (cond ((eq $tr_function_call_default '$apply)
735 (translate `(($apply) ,(caar form) ((mlist) ,@(cdr form)))))
736 ((eq $tr_function_call_default '$expr)
737 (tr-lisp-function-call form 'subr))
738 ((eq $tr_function_call_default '$general)
739 (cond
740 ;;; G(F,X):=F(X+1); case.
741 ((and $tr_bound_function_applyp (tboundp function))
742 (let ((new-form `(($apply) ,function ((mlist) ,@args))))
743 (tr-format (intl:gettext "warning: ~:M is a bound variable in ~:M, but it is used as a function.~%") function form)
744 (tr-format (intl:gettext "note: instead I'll translate it as: ~:M~%") new-form)
745 (translate new-form)))
746 ;; MFUNCTION-CALL cleverely punts this question to a FSUBR in the
747 ;; interpreter, and a macro in the compiler. This is good style,
748 ;; if a user is compiling then assume he is less lossage prone.
750 (pushnew (caar form) *untranslated-functions-called*)
751 (call-and-simp
752 (function-mode (caar form))
753 'mfunction-call `(,(caar form) ,@(tr-args args))))))
755 ;; This case used to be the most common, a real loser.
756 (warn-meval form)
757 `(,(function-mode (caar form)) . (meval ',form)))))
760 (defun attempt-translate-random-macro-op (form typel &aux tem)
761 (warn-fexpr form)
762 (setq tem (translate-atoms form))
763 (setf (car tem) (caar tem))
764 `($any . ,tem))
766 (defun attempt-translate-random-special-op (form typel)
767 (warn-fexpr form)
768 `(,(function-mode (caar form)) . (meval ',(translate-atoms form))))
771 (defun tr-lisp-function-call (form type)
772 (let ((op (caar form)) (mode) (args))
773 (setq args (cond ((member type '(subr lsubr expr) :test #'eq)
774 (mapcar #'(lambda (llis) (dconvx (translate llis)))
775 (cdr form)))
777 (mapcar 'dtranslate (cdr form))))
778 mode (function-mode op))
779 (call-and-simp mode op args)))
781 ;;the once-translated is so that inside translate file where a function
782 ;;has been translated, subsequent calls won't use mfunction call
783 (defun get-lisp-fun-type (fun &aux temp)
784 ;; N.B. this is Functional types. NOT special-forms,
785 ;; lisp special forms are meaningless to macsyma.
786 (cond ((get fun '*lexpr) 'lsubr)
787 ((get fun '*expr) 'subr)
788 ;; *LEXPR & *EXPR gotten from DEFMFUN declarations
789 ;; which is loaded by TrData.
790 ((mget fun '$fixed_num_args_function)
791 'subr)
792 ((mget fun '$variable_num_args_function)
793 'lsubr)
794 ((setq temp (getl fun '(expr subr lsubr)))
795 (car temp))
796 ((get fun 'once-translated))
797 ((get fun 'translated))
798 (t nil)))
800 (defun tr-infamous-noun-form (form)
801 ;; 'F(X,Y) means noun-form. The arguments are evaluated.
802 ;; but the function is cons on, not applied.
803 ;; N.B. for special forms and macros this is totally wrong.
804 ;; But, those cases are filtered out already, presumably.
806 (let ((op (cond ((member 'array (car form) :test #'eq)
807 `(,(caar form) array))
808 (t `(,(caar form)))))
809 (args (tr-args (cdr form))))
810 `($any . (simplify (list ',op ,@args)))))
814 ;;; Some atoms, solely by usage, are self evaluating.
816 (defun implied-quotep (atom)
817 (cond
818 ((stringp atom)
819 (cond
820 ;; I WONDER IF THIS NEXT CONDITION CAN BE CUT OUT ?? !!
821 ((equal atom "**") ;;; foolishness. The PARSER should do this.
822 ;; Losing Fortran hackers.
823 (tr-format "~% `**' is obsolete, use `^' !!!")
824 "^")
825 (t atom)))
826 ((get atom 'implied-quotep) atom)
827 (t nil)))
829 (defun translate-atoms (form)
830 (cond ((atom form)
831 (cond ((or (numberp form) (member form '(t nil) :test #'eq)) form)
832 ((tboundp form)
833 form)
835 form)))
836 ((eq 'mquote (caar form)) form)
837 (t (cons (car form) (mapcar #'translate-atoms (cdr form))))))
840 ;;; the Translation Properties. the heart of TRANSL.
842 ;;; This conses up the call to the function, adding in the
843 ;;; SIMPLIFY i the mode is $ANY. This should be called everywhere.
844 ;;; instead of duplicating the COND everywhere, as is done now in TRANSL.
846 (defun tr-nosimpp (op)
847 (cond ((atom op)
848 (get op 'tr-nosimp))
849 (t nil)))
851 (defun call-and-simp (mode fun args)
852 (cond ((or (not (eq mode '$any))
853 (tr-nosimpp fun))
854 `(,mode ,fun . ,args))
856 `(,mode simplify (,fun . ,args)))))
858 (defmspec $declare_translated (fns)
859 (setq fns (cdr fns))
860 (loop for v in fns
861 when (or (symbolp v) (and (stringp v) (setq v ($verbify v))))
862 do (setf (get v 'once-translated) t)
863 (pushnew v *declared-translated-functions*)
864 else do (merror (intl:gettext "declare_translated: arguments must be symbols or strings; found: ~:M") v)))
866 (def%tr $declare (form)
867 `($any . (meval ',form)))
869 (def%tr $eval_when (form)
870 (tr-format (intl:gettext "error: found 'eval_when' in a function or expression: ~:M~%") form)
871 (tr-format (intl:gettext "note: 'eval_when' can appear only at the top level in a file.~%"))
872 (setq tr-abort t)
873 '($any . nil))
875 (def%tr mdefmacro (form)
876 (tr-format (intl:gettext "warning: globally defining macro ~:M now to ensure correct macro expansions.~%") (caaadr form))
877 ; Define the macro now to ensure that it's defined when it's time
878 ; to expand it. It's a bug that this definition occurs during
879 ; translation without being cleaned it up afterward, but simply
880 ; removing this breaks things.
881 (meval form)
882 `($any . (meval ',form)))
884 (def%tr $local (form)
885 (cond (local
886 (tr-format (intl:gettext "error: there is already a 'local' in this block.~%"))
887 (setq tr-abort t))
889 (setq local t)))
890 (cons nil `(mapply 'mlocal ',(cdr form) '$local)))
893 (def%tr mquote (form)
894 (list (tr-class (cadr form)) 'quote (cadr form)))
897 (defun tr-lambda (form &optional (tr-body #'tr-seq) &rest tr-body-argl
898 &aux
899 (arglist (mparams (cadr form)))
900 (easy-assigns nil))
901 ;; This function is defined to take a simple macsyma lambda expression and
902 ;; return a simple lisp lambda expression. The optional TR-BODY hook
903 ;; can be used for translating other special forms that do lambda binding.
905 ;; Local SPECIAL declarations are not used because
906 ;; the multics lisp compiler does not support them. They are of course
907 ;; a purely syntactic construct that doesn't buy much. I have been
908 ;; advocating the use of DEFINE_VARIABLE in macsyma user programs so
909 ;; that the use of DECLARE(FOO,SPECIAL) will be phased out at that level.
911 (mapc #'tbind arglist)
912 (destructuring-let (((mode . nbody) (apply tr-body (cddr form) tr-body-argl))
913 (local-declares (make-declares arglist t)))
914 ;; -> BINDING of variables with ASSIGN properties may be difficult to
915 ;; do correctly and efficiently if arbitrary code is to be run.
916 (if (or tr-lambda-punt-assigns
917 (do ((l arglist (cdr l)))
918 ((null l) t)
919 (let* ((var (car l))
920 (assign (get var 'assign)))
921 (if assign
922 (cond ((eq assign 'assign-mode-check)
923 (push `(,assign ',var ,(teval var)) easy-assigns))
925 (return nil)))))))
926 ;; Case with EASY or no ASSIGN's
927 (progn ;;-have to undo any local assignments. --wfs
928 `(,mode . (lambda ,(tunbinds arglist)
929 ,local-declares
930 ,@easy-assigns
931 ,@nbody)))
932 ;; Case with arbitrary ASSIGN's.
933 (let ((temps (mapcar #'(lambda (ign) ign (tr-gensym)) arglist)))
934 `(,mode . (lambda ,temps
935 (unwind-protect
936 (progn
937 ;; [1] Check before binding.
938 ,@(mapcan #'(lambda (var val)
939 (let ((assign (get var 'assign)))
940 (if assign
941 (let ((assign-fn (if (symbolp assign) (symbol-function assign) (coerce assign 'function))))
942 (list `(funcall ,assign-fn ',var ,val))))))
943 arglist temps)
944 ;; [2] do the binding.
945 ((lambda ,(tunbinds arglist)
946 ,local-declares
947 ,@nbody)
948 ,@temps))
949 ;; [2] check when unbinding too.
950 ,@(mapcan #'(lambda (var)
951 (let ((assign (get var 'assign)))
952 (if assign
953 (let ((assign-fn (if (symbolp assign) (symbol-function assign) (coerce assign 'function))))
954 (list `(funcall ,assign-fn ',var
955 ;; use DTRANSLATE to
956 ;; catch global
957 ;; scoping if any.
958 ,(dtranslate var)))))))
959 arglist))))))))
962 (defun make-declares (varlist localp &aux (dl) (fx) (fl) specs)
963 (do ((l varlist (cdr l))
964 (mode) (var))
965 ((null l))
967 ;; When a variable is declared special, be sure to declare it
968 ;; special here.
969 (when (and localp (tr-get-special (car l)))
970 (push (car l) specs))
972 (when (or (not localp)
973 (not (tr-get-special (car l))))
974 ;; don't output local declarations on special variables.
975 (setq var (teval (car l)) mode (value-mode var))
976 (setq specs (cons var specs))
978 (cond ((eq '$fixnum mode) (pushnew var fx :test #'eq))
979 ((eq '$float mode) (pushnew var fl :test #'eq)))))
980 (if fx (pushnew `(fixnum . ,fx) dl :test #'eq))
981 (if fl (pushnew `(type flonum . ,fl) dl :test #'eq))
982 (if specs (pushnew `(special . ,specs) dl :test #'eq))
983 (if dl `(declare . ,dl)))
985 (defun tr-seq (l)
986 (do ((mode nil)
987 (body nil))
988 ((null l)
989 (cons mode (nreverse body)))
990 (let ((exp (translate (pop l))))
991 (setq mode (car exp))
992 (push (cdr exp) body))))
994 (def%tr mprogn (form)
995 (setq form (tr-seq (cdr form)))
996 (cons (car form) `(progn ,@(cdr form))))
999 (def%tr mprog (form)
1000 (let (arglist body val-list)
1001 ;; [1] normalize the MPROG syntax.
1002 (cond (($listp (cadr form))
1003 (setq arglist (cdadr form)
1004 body (cddr form)))
1006 (setq arglist nil
1007 body (cdr form))))
1008 (cond ((null body)
1009 (setq body '(((mquote) $done)))))
1010 (setq val-list (mapcar #'(lambda (u)
1011 (if (atom u) u
1012 (translate (caddr u))))
1013 arglist)
1014 arglist (mapcar #'(lambda (u)
1015 ;; X or ((MSETQ) X Y)
1016 (if (atom u) u (cadr u)))
1017 arglist))
1018 (let ((dup (find-duplicate arglist :test #'eq)))
1019 (when dup
1020 (tr-format (intl:gettext "error: ~M occurs more than once in block variable list") dup)
1021 (setq tr-abort t)))
1022 (unless tr-abort
1023 (setq form
1024 (tr-lambda
1025 ;; [2] call the lambda translator.
1026 `((lambda) ((mlist) ,@arglist) ,@body)
1027 ;; [3] supply our own body translator.
1028 #'tr-mprog-body
1029 val-list
1030 arglist))
1031 (cons (car form) `(,(cdr form) ,@val-list)))))
1033 (defun tr-mprog-body (body val-list arglist
1034 &aux
1035 (inside-mprog t)
1036 (return-mode nil)
1037 (need-prog? nil)
1038 (returns nil) ;; not used but must be bound.
1039 (local nil)
1041 (do ((l nil))
1042 ((null body)
1043 ;; [5] hack the val-list for the mode context.
1044 ;; Perhaps the only use of the function MAP in all of macsyma.
1045 (mapl #'(lambda (val-list arglist)
1046 (cond ((atom (car val-list))
1047 (rplaca val-list
1048 (or (cdr (assoc (value-mode (car arglist))
1049 mode-init-value-table :test #'eq))
1050 `',(car arglist))))
1052 (warn-mode (car arglist)
1053 (value-mode (car arglist))
1054 (car (car val-list))
1055 "in a `block' statement")
1056 (rplaca val-list (cdr (car val-list))))))
1057 val-list arglist)
1058 (setq l (nreverse l))
1059 (cons return-mode
1060 (if need-prog?
1061 `((prog () ,@(delete nil l :test #'equal)))
1062 l)))
1063 ;; [4] translate a form in the body
1064 (let ((form (pop body)))
1065 (cond ((null body)
1066 ;; this is a really bad case.
1067 ;; we don't really know if the return mode
1068 ;; of the expression is for the value of the block.
1069 ;; Some people write RETURN at the end of a block
1070 ;; and some don't. In any case, the people not
1071 ;; use the PROG programming style won't be screwed
1072 ;; by this.
1073 (setq form (translate form))
1074 (setq return-mode (*union-mode (car form) return-mode))
1075 (setq form (cdr form))
1076 (if (and need-prog?
1077 (or (atom form)
1078 (not (eq (car form) 'return))))
1079 ;; put a RETURN on just in case.
1080 (setq form `(return ,form))))
1081 ((symbolp form))
1083 (setq form (dtranslate form))))
1084 (push form l))))
1086 (def%tr mreturn (form)
1087 (if (null inside-mprog)
1088 (tr-format (intl:gettext "warning: 'return' not within 'block' or 'do': ~:M~%") form))
1089 (setq need-prog? t)
1090 (setq form (translate (cadr form)))
1091 (setq return-mode (if return-mode (*union-mode (car form) return-mode)
1092 (car form)))
1093 (setq form `(return ,(cdr form)))
1094 (push form returns) ;; USED by lusing MDO etc not yet re-written.
1095 ;; MODE here should be $PHANTOM or something.
1096 `($any . ,form))
1098 (def%tr mgo (form)
1099 (if (null inside-mprog)
1100 (tr-format (intl:gettext "warning: 'go' not within 'block' or 'do': ~:M~%") form))
1101 (if (not (symbolp (cadr form)))
1102 (tr-format (intl:gettext "warning: 'go' tag must be a symbol: ~:M~%") form))
1103 (setq need-prog? t)
1104 `($any . (go ,(cadr form))))
1106 (def%tr mqapply (form)
1107 (let ((fn (cadr form)) (args (cddr form))
1108 (aryp (member 'array (cdar form) :test #'eq)))
1109 (cond ((atom fn)
1110 ;; I'm guessing (ATOM FN) is a parser error or other Lisp error,
1111 ;; so don't bother to translate the following error message.
1112 (tr-format "translator: MQAPPLY operator must be a cons; found: ~:M" form)
1113 nil)
1114 ((eq (caar fn) 'mquote)
1115 `($any list ',(cons (cadr fn) aryp) ,@(tr-args args)))
1116 ((eq (caar fn) 'lambda)
1117 ;; LAMBDA([X,'Y,[L]],...)(A,B,C) is a bogus form. Don't bother with it.
1118 ;; ((LAMBDA) ((MLIST) ....) ....)
1119 (cond ((member 'bogus (mapcar #'(lambda (arg)
1120 (cond ((or (mquotep arg)
1121 ($listp arg))
1122 'bogus)))
1123 (cdr (cadr fn))) :test #'eq)
1124 (tr-format (intl:gettext "error: quote or list arguments are not allowed in MQAPPLY; found: ~:M~%") form)
1125 (setq tr-abort t)
1126 nil)
1128 (setq fn (tr-lambda fn)
1129 args (tr-args args))
1130 `(,(car fn) ,(cdr fn) ,@args))))
1131 ((not aryp)
1132 `($any simplify (mapply ,(dconvx (translate fn))
1133 (list ,@(tr-args args))
1134 ',fn)))
1136 (warn-meval form)
1137 `($any meval ',form)))))
1141 (def%tr mcond (form)
1142 (prog (dummy mode nl)
1143 (setq dummy (translate (caddr form))
1144 mode (car dummy)
1145 nl (list dummy (translate-predicate (cadr form))))
1146 (do ((l (cdddr form) (cddr l))) ((null l))
1147 (cond ((and (not (atom (cadr l))) (eq 'mcond (caaadr l)))
1148 (setq l (cdadr l))))
1149 (setq dummy (translate (cadr l))
1150 mode (*union-mode mode (car dummy))
1151 nl (cons dummy
1152 (cons (translate-predicate (car l))
1153 nl))))
1154 (setq form nil)
1155 (do ((l nl (cddr l))) ((null l))
1156 (cond ((and (eq t (cadr l)) (null (cdar l))))
1157 (t (setq form
1158 (cons (cons (cadr l)
1159 (cond ((and (not (atom (cdar l)))
1160 (cddar l)
1161 (eq (cadar l) 'progn))
1162 (nreverse
1163 (cons (dconv (cons (caar l)
1164 (car (reverse (cddar l))))
1165 mode)
1166 (cdr (reverse (cddar l))))))
1167 ((and (equal (cdar l) (cadr l))
1168 (atom (cdar l))) nil)
1169 (t (list (cdr (car l))))))
1170 form)))))
1171 ;; Wrap (LET (($PREDERROR T)) ...) around translation of MCOND.
1172 ;; Nested MCOND expressions (e.g. if x > 0 then if y > 0 then ...)
1173 ;; will therefore yield nested (LET (($PREDERROR T)) ... (LET (($PREDERROR T)) ...)).
1174 ;; I suppose only the topmost one is needed, but there is very little harm
1175 ;; in the redundant ones, so I'll let it stand.
1176 (return (cons mode (list 'let '(($prederror t)) (cons 'cond form))))))
1180 ;; The MDO and MDOIN translators should be changed to use the TR-LAMBDA.
1181 ;; Perhaps a mere expansion into an MPROG would be best.
1183 (def%tr mdo (form)
1184 (let (returns assigns return-mode local (inside-mprog t) need-prog?)
1185 (let (mode var init next test action varmode)
1186 (setq var (cond ((cadr form)) (t 'mdo)))
1187 (tbind var)
1188 (setq init (if (caddr form) (translate (caddr form)) '($fixnum . 1)))
1189 (cond ((not (setq varmode (tr-get-mode var)))
1190 (declvalue var (car init) t)))
1191 (setq next (translate (cond ((cadddr form) (list '(mplus) (cadddr form) var))
1192 ((car (cddddr form)))
1193 (t (list '(mplus) 1 var)))))
1194 (setq form (copy-list form))
1195 ;;to make the end test for thru be numberp if the index is numberp
1196 ;;and to eliminate reevaluation
1197 (cond ((not varmode)
1198 (declvalue var (*union-mode (car init) (car next)) t))
1200 (warn-mode var varmode (*union-mode (car init) (car next)))))
1201 (setq test (translate-predicate
1202 (list '(mor)
1203 (cond ((null (cadr (cddddr form))) nil)
1204 ((and (cadddr form)
1205 (mnegp ($numfactor (simplify (cadddr form)))))
1206 (list '(mlessp) var (cadr (cddddr form))))
1207 (t (list '(mgreaterp) var (cadr (cddddr form)))))
1208 (caddr (cddddr form)))))
1209 (setq action (translate (cadddr (cddddr form)))
1210 mode (cond ((null returns) '$any)
1211 (t return-mode)))
1212 (setq var (tunbind (cond ((cadr form)) (t 'mdo))))
1213 `(,mode do ((,var ,(cdr init) ,(cdr next)))
1214 (,test '$done) . ((declare (special ,var)) .
1215 ,(cond ((atom (cdr action)) nil)
1216 ((eq 'progn (cadr action)) (cddr action))
1217 (t (list (cdr action)))))))))
1219 (def%tr mdoin (form)
1220 (let (returns assigns return-mode local (inside-mprog t) need-prog?)
1221 (prog (mode var init action)
1222 (setq var (tbind (cadr form))) (tbind 'mdo)
1223 (setq init (dtranslate (caddr form)))
1224 (cond ((or (cadr (cddddr form)) (caddr (cddddr form)))
1225 (tunbind 'mdo) (tunbind (cadr form))
1226 (return `($any meval '((mdoin) . ,(cdr form))))))
1227 (setq action (translate (cadddr (cddddr form)))
1228 mode (cond ((null returns) '$any)
1229 (t return-mode)))
1230 (tunbind 'mdo) (tunbind (cadr form))
1231 (return
1232 `(,mode do ((,var) (mdo (cdr ,init) (cdr mdo)))
1233 ((null mdo) '$done) .
1234 ((declare (special ,var)) (setq ,var (car mdo)) .
1235 ,(cond ((atom (cdr action)) nil)
1236 ((eq 'progn (cadr action)) (cddr action))
1237 (t (list (cdr action))))))))))
1240 (defun lambda-wrap1 (tn val form)
1241 (if (or (atom val)
1242 (eq (car val) 'quote))
1243 (subst val tn form)
1244 `((lambda (,tn) ,form) ,val)))
1246 (def%tr msetq (form)
1247 (let ((var (cadr form))
1248 (val (caddr form))
1249 assign
1250 mode)
1251 (cond ((atom var)
1252 (setq mode (value-mode var) val (translate val))
1253 (warn-mode var mode (car val))
1254 (if (eq '$any mode)
1255 (setq mode (car val) val (cdr val))
1256 (setq val (dconv val mode)))
1257 (cons mode
1258 (if (setq assign (get var 'assign))
1259 (let ((tn (tr-gensym))
1260 (assign-fn (if (symbolp assign) (symbol-function assign) (coerce assign 'function))))
1261 (lambda-wrap1 tn val `(let nil
1262 (declare (special ,var ,(teval var)))
1263 (funcall ,assign-fn ',var ,tn)
1264 (setq ,(teval var) ,tn))))
1265 `(let nil (declare (special ,(teval var)))
1266 (if (not (boundp ',(teval var)))
1267 (add2lnc ',(teval var) $values))
1268 (,(if *macexpr-top-level-form-p*
1269 'defparameter
1270 'setq)
1271 ,(teval var) ,val)))))
1272 ((member 'array (car var) :test #'eq)
1273 (tr-arraysetq var val))
1275 (unless (safe-get (caar var) 'mset_extension_operator)
1276 (tr-format (intl:gettext "warning: no assignment operator known for ~:M~%") var)
1277 (tr-format (intl:gettext "note: just keep going and hope for the best.~%")))
1278 (setq val (translate val))
1279 `(,(car val) mset ',(translate-atoms var) ,(cdr val))))))
1281 (def%tr $max (x) (translate-$max-$min x))
1282 (def%tr $min (x) (translate-$max-$min x))
1283 (def%tr %max (x) (translate-$max-$min x))
1284 (def%tr %min (x) (translate-$max-$min x))
1286 (defun translate-$max-$min (form)
1287 (let ((mode) (arglist) (op (stripdollar (caar form))))
1288 (setq arglist
1289 (mapcar (lambda (l)
1290 (setq l (translate l))
1291 (cond ((null mode)
1292 (setq mode (car l)))
1293 ((eq mode (car l)))
1295 (setq mode '$any)))
1297 (cdr form)))
1298 ; To match the interpreted case, and to make sure we use the
1299 ; correct mode for the return value, we do not apply float
1300 ; contagion to the arguments and we use a special translation
1301 ; to call MAX or MIN only when every argument has the same
1302 ; mode (either all fixnum or all float). CLHS says that
1303 ; implementations have choices they can make about what MAX
1304 ; and MIN return when the arguments are a mix of float and
1305 ; rational types.
1306 ; Example: if an implementation decides to apply float contagion
1307 ; to the arguments of MAX (MIN), then it can return either an
1308 ; integer or a float if the greatest (least) argument was an
1309 ; integer.
1310 (if (member mode '($fixnum $float) :test #'eq)
1311 `(,mode ,(if (eq 'min op) 'min 'max) . ,(mapcar 'cdr arglist))
1312 `($any ,(if (eq 'min op) '$lmin '$lmax)
1313 (list '(mlist) . ,(mapcar 'dconvx arglist))))))
1316 ;;; mode acessing, binding, handling. Super over-simplified.
1318 (defun tr-class (x)
1319 (cond ((integerp x) '$fixnum)
1320 ((floatp x) '$float)
1321 ((member x '(t nil) :test #'eq) '$boolean)
1322 ((atom x) '$any)
1323 ((eq 'rat (caar x)) '$rational)
1324 (t '$any)))
1326 (defun *union-mode (mode1 mode2)
1327 (cond ((eq mode1 mode2) mode1)
1328 ((null mode1) mode2)
1329 ((null mode2) mode1)
1330 ((member mode2 *$any-modes* :test #'eq) '$any)
1331 ((member mode1 *$any-modes* :test #'eq) '$any)
1332 ((eq '$fixnum mode1) mode2)
1333 ((eq '$float mode1)
1334 (if (eq '$number mode2) '$number '$float))
1335 ((eq '$rational mode1)
1336 (if (eq '$float mode2) '$float '$any))
1337 ((eq '$number mode1)
1338 (if (eq '$rational mode2) '$any '$number))
1339 (t '$any)))
1341 (defun value-mode (var)
1342 (cond ((tr-get-mode var))
1344 (warn-undeclared var)
1345 '$any)))
1347 (defun decmode-arrayfun (f m)
1348 (putprop f m 'arrayfun-mode))
1350 (defun array-mode (ar)
1351 (cond ((get ar 'array-mode)) (t '$any)))
1353 (defun arrayfun-mode (ar)
1354 (cond ((get ar 'arrayfun-mode)) (t '$any)))
1356 (defun function-mode (f)
1357 (cond ((get f 'function-mode)) (t '$any)))
1359 (defun function-mode-@ (f)
1360 (ass-eq-ref (tr-get-val-modes f) 'function-mode '$any))
1362 (defun array-mode-@ (f)
1363 (ass-eq-ref (tr-get-val-modes f) 'array-mode '$any))
1366 (defvar $tr_bind_mode_hook nil
1367 "A hack to allow users to key the modes of variables
1368 off of variable spelling, and other things like that.")
1370 ;; TBIND, below, copies the MODE, VAL-MODES, and SPECIAL properties
1371 ;; into the a table named TSTACK, and then removes those properties.
1372 ;; So if TBIND has been called, we will need to look for those
1373 ;; properties in TSTACK instead of the symbol property list.
1375 (defstruct (tstack-slot (:conc-name tstack-slot-))
1376 mode
1377 tbind
1378 val-modes
1379 ;; an alist telling second order info
1380 ;; about APPLY(VAR,[X]), ARRAYAPPLY(F,[X]) etc.
1381 special)
1383 (defun tr-get-mode (a)
1384 (if (get a 'tbind)
1385 (let ((my-slot (cdr (assoc a tstack))))
1386 (tstack-slot-mode my-slot))
1387 (get a 'mode)))
1389 #-gcl (defun (setf tr-get-mode) (b a)
1390 (if (get a 'tbind)
1391 (let ((my-slot (cdr (assoc a tstack))))
1392 (setf (tstack-slot-mode my-slot) b))
1393 (setf (get a 'mode) b)))
1395 #+gcl (defsetf tr-get-mode (a) (b)
1396 `(if (get ,a 'tbind)
1397 (let ((my-slot (cdr (assoc ,a tstack))))
1398 (setf (tstack-slot-mode my-slot) ,b))
1399 (setf (get ,a 'mode) ,b)))
1401 (defun tr-get-val-modes (a)
1402 (if (get a 'tbind)
1403 (let ((my-slot (cdr (assoc a tstack))))
1404 (tstack-slot-val-modes my-slot))
1405 (get a 'val-modes)))
1407 #-gcl (defun (setf tr-get-val-modes) (b a)
1408 (if (get a 'tbind)
1409 (let ((my-slot (cdr (assoc a tstack))))
1410 (setf (tstack-slot-val-modes my-slot) b))
1411 (setf (get a 'val-modes) b)))
1413 #+gcl (defsetf tr-get-val-modes (a) (b)
1414 `(if (get ,a 'tbind)
1415 (let ((my-slot (cdr (assoc ,a tstack))))
1416 (setf (tstack-slot-val-modes my-slot) ,b))
1417 (setf (get ,a 'val-modes) ,b)))
1419 (defun tr-get-special (a)
1420 (if (get a 'tbind)
1421 (let ((my-slot (cdr (assoc a tstack))))
1422 (tstack-slot-special my-slot))
1423 (get a 'special)))
1425 #-gcl (defun (setf tr-get-special) (b a)
1426 (if (get a 'tbind)
1427 (let ((my-slot (cdr (assoc a tstack))))
1428 (setf (tstack-slot-special my-slot) b))
1429 (setf (get a 'special) b)))
1431 #+gcl (defsetf tr-get-special (a) (b)
1432 `(if (get ,a 'tbind)
1433 (let ((my-slot (cdr (assoc ,a tstack))))
1434 (setf (tstack-slot-special my-slot) ,b))
1435 (setf (get ,a 'special) ,b)))
1437 ;;; should be a macro (TBINDV <var-list> ... forms)
1438 ;;; so that TUNBIND is assured, and also so that the stupid ASSQ doesn't
1439 ;;; have to be done on the darn TSTACK. This will have to wait till
1440 ;;; the basic special form translation properties are rewritten.
1442 (defun variable-p (var)
1443 (and var (symbolp var) (not (eq var t))))
1445 (defun bad-var-warn (var)
1446 (tr-format (intl:gettext "warning: ~:M cannot be used as a variable.~%") var))
1448 (defun tbind (var &aux old)
1449 (cond ((variable-p var)
1450 (setq old (make-tstack-slot :mode (get var 'mode)
1451 :tbind (get var 'tbind)
1452 :val-modes (get var 'val-modes)
1453 :special (get var 'special)))
1454 (push (cons var old) tstack)
1455 (cond ((not (specialp var))
1456 ;; It is the lisp convention in use to inherit
1457 ;; specialness from higher context.
1458 ;; Spurious MODEDECLARATIONS get put in the environment
1459 ;; when code is MEVAL'd since there is no way to stack
1460 ;; the mode properties. Certainly nobody is willing
1461 ;; to hack MEVAL in JPG;MLISP
1462 (remprop var 'val-modes)
1463 (remprop var 'mode)
1464 (remprop var 'special)))
1465 (putprop var var 'tbind)
1466 (if $tr_bind_mode_hook
1467 (let ((mode? (mapply $tr_bind_mode_hook
1468 (list var)
1469 '$tr_bind_mode_hook)))
1470 (if mode? (tr-declare-varmode var mode?))))
1471 var)
1473 (bad-var-warn var))))
1475 (defun tunbind (var &aux (old (assoc var tstack :test #'eq)))
1476 (when (variable-p var)
1477 (prog1
1478 (teval var)
1479 (cond (old
1480 (setq tstack (delete old tstack :test #'eq)) ; POP should be all we need.
1481 (setq old (cdr old))
1482 (putprop1 var (tstack-slot-mode old) 'mode)
1483 (putprop1 var (tstack-slot-tbind old) 'tbind)
1484 (putprop1 var (tstack-slot-val-modes old) 'val-modes)
1485 (putprop1 var (tstack-slot-special old) 'special))))))
1487 (defun putprop1 (name value key)
1488 ;; leaves property list clean after unwinding, this
1489 ;; is an efficiency/storage issue only.
1490 (if value
1491 (putprop name value key)
1492 (progn
1493 (remprop name key)
1494 nil)))
1496 (defun tunbinds (l)
1497 (do ((nl))
1498 ((null l) nl)
1499 (setq nl (cons (tunbind (caar tstack)) nl)
1500 l (cdr l))))
1502 (defun tboundp (var)
1503 ;; really LEXICAL-VARP.
1504 (and (symbolp var) (get var 'tbind) (not (tr-get-special var))))
1506 (defun teval (var)
1507 (or (and (symbolp var) (get var 'tbind)) var))
1509 ;; Local Modes:
1510 ;; Mode: LISP
1511 ;; Comment Col: 40
1512 ;; END: