Fix bogus and inconsistent translations of max and min
[maxima.git] / src / transl.lisp
blob6c7df7ae0f3fc7ca13a55930b70dcbfbc90f470f
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 $transcompile t
98 "If TRUE TRANSLATE_FILE outputs declarations for the COMPLR.
99 The only use of the switch is to save the space declarations take
100 up in interpreted code.")
102 (defmvar tstack nil " stack of local variable modes ")
104 (defmvar local nil "T if a $local statement is in the body.")
105 (defmvar arrays nil "arrays to declare to `complr'")
106 (defmvar lexprs nil "Lexprs to declare.")
107 (defmvar exprs nil "what else?")
108 (defmvar fexprs nil "Fexprs to declare.")
109 (defmvar tr-progret t)
110 (defmvar inside-mprog nil)
111 (defmvar returns nil "list of `translate'd return forms in the block.")
112 (defmvar return-mode nil "the highest(?) mode of all the returns.")
113 (defmvar need-prog? nil)
114 (defmvar assigns nil "These are very-special variables which have a Maxima
115 assign property which must be called to bind and unbind the variable
116 whenever it is `lambda' bound.")
118 (defmvar specials nil "variables to declare special to the complr.")
120 (defmvar translate-time-evalables
121 '($modedeclare $alias $declare $infix $nofix $declare_translated
122 $matchfix $prefix $postfix $compfile))
124 (defmvar *transl-backtrace* nil
125 " What do you think? ")
127 (defmvar *transl-debug* nil "if T it pushes `backtrace' and `trace' ")
129 (defmvar tr-abort nil "set to T if abortion is requested by any of the
130 sub-parts of the translation. A *THROW would be better, although it
131 wouldn't cause the rest of the translation to continue, which may
132 be useful in translation for MAXIMA-ERROR checking.")
134 (defmvar tr-unique (gensym)
135 "this is just a unque object used for random purposes,
136 such as the second (file end) argument of READ.")
139 (defmvar $tr_warn_undeclared '$compile
140 "When to send warnings about undeclared variables to the TTY")
142 (defmvar $tr_warn_meval '$compfile
143 "If `meval' is called that indicates problems in the translation")
145 (defmvar $tr_warn_fexpr
146 '$compfile
147 "FEXPRS should not normally be output in translated code, all legitimate
148 special program forms are translated.")
150 (defmvar $tr_warn_mode '$all
151 "Warn when variables are assigned values out of their mode.")
153 (defmvar $tr_warn_undefined_variable '$all
154 "Warn when undefined global variables are seen.")
157 (defmvar *warned-un-declared-vars* nil "Warning State variable")
158 (defmvar *warned-fexprs* nil "Warning State variable")
159 (defmvar *warned-mode-vars* nil "Warning State variable")
161 (defmvar $tr_function_call_default '$general
163 FALSE means punt to MEVAL, EXPR means assume lisp fixed arg function.
164 GENERAL, the default gives code good for mexprs and mlexprs but not macros.
165 GENERAL assures variable bindings are correct in compiled code.
166 In GENERAL mode, when translating F(X), if F is a bound variable, then
167 it assumes that APPLY(F,[X]) is meant, and translates a such, with
168 appropriate warning. There is no need to turn this off.
169 APPLY means like APPLY.")
171 (defmvar $tr_array_as_ref t
172 "If true runtime code uses value of the variable as the array.")
174 (defmvar $tr_numer nil
175 "If `true' numer properties are used for atoms which have them, e.g. %pi")
177 (defvar boolean-object-table
178 '(($true . ($boolean . t))
179 ($false . ($boolean . nil))
180 (t . ($boolean . t))
181 (nil . ($boolean . nil))))
183 (defvar mode-init-value-table
184 '(($float . 0.0)
185 ($fixnum . 0)
186 ($number . 0)
187 ($list . '((mlist)))
188 ($boolean . nil)))
190 (defvar tr-lambda-punt-assigns nil
191 "Kludge argument to `tr-lambda' due to lack of keyword argument passing")
193 (or (boundp '*in-compile*)
194 (setq *in-compile* nil))
196 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
199 (defun barfo (msg)
200 (tr-format (intl:gettext "translator: internal error. Message: ~:M~%") msg)
201 (cond (*transl-debug*
202 (break "transl barfo"))
204 (setq tr-abort t)
205 nil)))
207 (defun specialp (var)
208 (cond ((or (optionp var)
209 (tr-get-special var))
210 (if $transcompile (pushnew var specials :test #'eq))
211 t)))
214 ;;; The error message system. Crude as it is.
215 ;;; I tell you how this aught to work:
216 ;;; (1) All state should be in one structure, one state variable.
217 ;;; (2) Should print out short message on-the-fly so that it
218 ;;; gives something to watch, and also so that it says something
219 ;;; if things crash.
220 ;;; (3) Summaries on a cross-referenced per-function and per-item
221 ;;; should be printed at the end, as a table.
222 ;;; e.g.
223 ;;; Undefined Functions used in
224 ;;; FOO BAR, BAZ,BOMB
225 ;;; FOOA P,Q
226 ;;; Undefined Variables ... same thing
227 ;;; Incomprehensible special forms
228 ;;; EV ....
229 ;;; Predicate Mode Targeting failures.
230 ;;; ..... -gjc
232 ;;; The way it works now is to print too little or too much.
233 ;;; Many items are only warned about the first time seen.
234 ;;; However, this isn't too much of a problem when using Emacs
235 ;;; to edit code, because searching for warned-about tokens
236 ;;; is quick and easy.
238 (defmvar *tr-warn-break* t
239 " if in debug mode `warning's signaled go to lisp break loops ")
241 (defmacro tr-warnbreak ()
242 `(and *transl-debug* *tr-warn-break* (break "transl")))
244 (defun tr-warnp (val)
245 (and val
246 (cond (*in-compile*
247 (member val '($all $compile $compfile $translate) :test #'eq))
248 ((or *in-compfile* *in-translate-file*)
249 (member val '($all $compfile $translate) :test #'eq))
250 (*in-translate*
251 (member val '($all $translate) :test #'eq)))))
253 (defvar warned-undefined-variables nil)
255 (defun warn-undefined-variable (form)
256 (and (tr-warnp $tr_warn_undefined_variable)
257 (cond ((member form warned-undefined-variables :test #'eq))
259 (push form warned-undefined-variables)
260 (tr-format (intl:gettext "warning: encountered undefined variable ~:M in translation.~%") form)
261 (tr-warnbreak)))))
263 (defun warn-undeclared (form &optional comment)
264 (and (tr-warnp $tr_warn_undeclared)
265 (cond ((member form *warned-un-declared-vars* :test #'equal) t)
267 (push form *warned-un-declared-vars*)
268 (tr-format (intl:gettext "warning: no type declaration for ~:M; assume type is 'any'.~%") form)
269 (tr-format (intl:gettext "note: 'modedeclare' declares types for translation.~%"))
270 (cond (comment
271 (dolist (v *translation-msgs-files*)
272 (terpri v)
273 (princ comment v))))
274 (tr-warnbreak)
275 nil))))
277 (defun warn-meval (form &optional comment)
278 (cond ((tr-warnp $tr_warn_meval)
279 (tr-format (intl:gettext "warning: emit call to MEVAL for expression: ~:M~%") form)
280 (cond (comment (dolist (v *translation-msgs-files*)
281 (terpri v)
282 (princ comment v))))
283 (tr-warnbreak)
284 'warned)))
287 (defun warn-mode (var mode newmode &optional comment)
288 (cond ((eq mode newmode))
290 (cond ((and (tr-warnp $tr_warn_mode)
291 (not (covers mode newmode))
292 (not (member (list var mode newmode) *warned-mode-vars* :test #'equal)))
293 (push (list var mode newmode) *warned-mode-vars*)
294 (tr-format (intl:gettext "warning: variable ~:M (declared type ~:M) assigned type ~:M.~%") var mode newmode)
295 (cond (comment
296 (dolist (v *translation-msgs-files*)
297 (terpri v)
298 (princ comment v))))
299 (tr-warnbreak))))))
301 (defun warn-fexpr (form &optional comment)
302 (cond ((and (tr-warnp $tr_warn_fexpr)
303 (not (member form *warned-fexprs* :test #'equal)))
304 (push form *warned-fexprs*)
305 (tr-format (intl:gettext "warning: ~:M is a special function without a full Lisp translation.~%") form)
306 (tr-format (intl:gettext "warning: calling ~:M in compiled code might not have the desired effect.~%") form)
307 (cond (comment
308 (dolist (v *translation-msgs-files*)
309 (terpri v)
310 (princ comment v))))
311 (tr-warnbreak))))
314 (defun macsyma-special-macro-p (fcn)
315 (getl-lm-fcn-prop fcn '(macro)))
317 (defun macsyma-special-op-p (f)
318 (getl f '(fsubr fexpr mfexpr* mfexpr*s *fexpr)))
320 (defun possible-predicate-op-p (f)
321 (member f '(mnotequal mequal $equal mgreaterp mgeqp mlessp mleqp) :test #'eq))
323 ;;;***************************************************************;;;
325 ;;; This function is the way to call the TRANSLATOR on an expression with
326 ;;; locally bound internal mode declarations. Result of TR-LAMBDA will be
327 ;;; (MODE . (LAMBDA (...) (DECLARE ...) TRANSLATED-EXP))
329 (defun tr-local-exp (exp &rest vars-modes)
330 (let ((loc (let ((tr-lambda-punt-assigns t))
331 (tr-lambda `((lambda) ((mlist) ,@(do ((l vars-modes (cddr l))
332 (ll nil (cons (car l) ll)))
333 ((null l) ll)
334 (or (variable-p (car l))
335 (bad-var-warn (car l)))))
336 (($modedeclare) ,@vars-modes)
337 ,exp)))))
338 (let ((mode (car loc))
339 (exp (car (last loc)))) ;;; length varies with TRANSCOMPILE.
340 (cons mode exp))))
342 (defun tr-args (form)
343 (mapcar #'(lambda (x) (dconvx (translate x))) form))
345 (defun dtranslate (form) (cdr (translate form)))
347 (defun dconv (x mode)
348 (cond ((eq '$float mode) (dconv-$float x))
349 ((eq '$cre mode) (dconv-$cre x))
350 (t (cdr x))))
352 (defun dconvx (x)
353 (if (member (car x) '(ratexpr pexpr) :test #'eq)
354 (dconv-$cre x)
355 (cdr x)))
357 (defun dconv-$float (x)
358 (cond ((member (car x) '($fixnum $number) :test #'eq)
359 (if (integerp (cdr x)) (float (cdr x)) (list 'float (cdr x))))
360 ((eq '$rational (car x))
361 (cond ((or (atom (cdr x))
362 (not (eq 'quote (cadr x))))
363 `($float ,(cdr x)))
365 (/ (float (cadadr (cdr x))) (float (caddr (caddr x)))))))
366 (t (cdr x))))
368 (defun dconv-$cre (x)
369 (if (eq '$cre (car x))
370 (cdr x)
371 `(ratf ,(cdr x))))
373 (defmvar *$any-modes* '($any $list))
375 (defun covers (mode1 mode2)
376 (cond ((eq mode1 mode2) t)
377 ((eq '$float mode1) (member mode2 '($float $fixnum $rational) :test #'eq))
378 ((eq '$number mode1) (member mode2 '($fixnum $float) :test #'eq))
379 ((member mode1 *$any-modes* :test #'eq) t)))
381 ;;; takes a function name as input.
383 (defun tr-mfun (name &aux (*transl-backtrace* nil))
384 (let ((def-form (consfundef name nil nil)))
385 (cond ((null def-form)
386 (setq tr-abort t))
388 (tr-mdefine-toplevel def-form)))))
390 ;;; DEFUN
391 ;;; All the hair here to deal with macsyma fexprs has been flushed.
392 ;;; Right now this handles MDEFMACRO and MDEFINE. The decisions
393 ;;; of where to put the actual properties and what kind of
394 ;;; defuns to make (LEXPR EXPR for maclisp) are punted to the
395 ;;; macro package.
397 (defun tr-mdefine-toplevel (form &aux (and-restp nil))
398 (destructuring-let (((((name . flags) . args) body) (cdr form))
399 (a-args) kind out-forms)
401 (do ((args args (cdr args))
402 ;; array functions cannot be LEXPR-like. gee.
403 ;; there is no good reason for that, except for efficiency,
404 ;; and I know that efficiency was not a consideration.
405 (full-restricted-flag (or (eq name 'mqapply)
406 (member 'array flags :test #'eq))))
407 ((null args) (setq a-args (nreverse a-args)))
408 (let ((u (car args)))
409 (cond ((atom u)
410 (push u a-args))
411 ((and (not full-restricted-flag)
412 (not and-restp)
413 (eq (caar u) 'mlist)
414 (cdr u) (atom (cadr u)))
415 (push (cadr u) a-args)
416 (setq and-restp t))
418 (push tr-unique a-args)))))
421 (cond ((eq name 'mqapply)
422 ;; don't you love syntax!
423 ;; do a switch-a-roo here. Calling ourselves recursively
424 ;; like this allows all legal forms and also catches
425 ;; errors. However, certain generalizations are also
426 ;; allowed. They won't get passed the interpreter, but
427 ;; interesting things may happen here. Thats what you
428 ;; get from too much syntax, so don't sweat it.
429 (tr-mdefine-toplevel
430 `(,(car form) ,(car args)
431 ((lambda) ((mlist) ,@(cdr args)) ,body))))
432 ((member tr-unique a-args :test #'eq)
433 ;; WHAT IS "BAD" ABOUT THE ARGUMENT LIST HERE ??
434 (tr-format (intl:gettext "error: unhandled argument list in function definition: ~:M~%") `((mlist),@args))
435 (setq tr-abort t)
436 nil)
437 ((member (caar form) '(mdefine mdefmacro) :test #'eq)
438 (setq kind (cond ((eq (caar form) 'mdefmacro) 'macro)
439 ((member 'array flags :test #'eq) 'array)
440 (t 'func)))
441 (let* ((t-form
442 (tr-lambda `((lambda) ((mlist) ,@a-args) ,body)))
443 (desc-header
444 `(,name ,(car t-form) ,(caar form)
445 ,and-restp ,(eq kind 'array))))
446 (cond ((eq kind 'func)
447 (push-pre-transl-form
448 `(defmtrfun-external ,desc-header))
449 (and (not (member (car t-form) '($any nil) :test #'eq))
450 (putprop name (car t-form) 'function-mode)))
451 ((eq kind 'array)
452 (and (not (member (car t-form) '($any nil) :test #'eq))
453 (decmode-arrayfun name (car t-form)))))
455 (cond ((or *in-translate* (not $packagefile))
456 ; These are all properties which tell the
457 ; user that functions are in the environment,
458 ; and that also allow him to SAVE the functions.
459 (push `(defprop ,name t translated) out-forms)
460 (push `(add2lnc ',name $props) out-forms)
461 (cond ((eq '$all $savedef)
462 (push
463 `(add2lnc
464 '((,name ,@flags) ,@args)
465 ,(case kind
466 (array '$arrays)
467 (func '$functions)
468 (macro '$macros))) out-forms)))))
469 (cond ((eq '$all $savedef)
470 ;; For some reason one may want to save the
471 ;; interpreted definition even if in a PACKAGEFILE.
472 ;; not a good idea to use SAVEDEF anyway though.
473 (push `(mdefprop ,name
474 ((lambda) ((mlist) ,@args) ,body)
475 ,(case kind
476 (array 'aexpr)
477 (macro 'mmacro)
478 (func 'mexpr)))
479 out-forms)))
480 ;;once a function has been translated we want to make sure mfunction-call is eliminated.
481 (progn
482 (remprop (car desc-header) 'undefined-warnp)
483 (setf (get (car desc-header) 'once-translated) "I was once translated"))
484 `(progn
485 ,@(nreverse out-forms)
486 (defmtrfun ,desc-header ,@(cdr (cdr t-form))))))
488 (barfo '?)))))
490 (defun translate-function (name)
491 (bind-transl-state
492 (setq *in-translate* t)
493 (let ((lisp-def-form (tr-mfun name))
494 (delete-subr? (and (get name 'translated)
495 (not (get name 'expr)))))
496 (cond (tr-abort
497 (trfail name))
499 (if delete-subr? (remprop name 'subr))
500 (if (mget name 'trace) (macsyma-untrace name))
501 (if (not $savedef) (meval `(($remfunction) ,name)))
502 (let ((lisp-action
503 ; apply EVAL so it is easy to TRACE.
504 ; ERRSET is crude, but...
505 (errset (apply 'eval (list lisp-def-form)))))
506 (cond ((not lisp-action)
507 (trfail name))
508 (t (values name lisp-def-form)))))))))
511 (defun trfail (x)
512 (tr-format (intl:gettext "error: failed to translate ~:@M~%") x)
513 nil)
515 (defun translate-and-eval-macsyma-expression (form)
516 ;; this is the hyper-random entry to the transl package!
517 ;; it is used by MLISP for TRANSLATE:TRUE "::=".
518 (bind-transl-state
519 (setq *in-translate* t)
520 ;; Use TRANSLATOR-EVAL so we don't have to lose badly by tracing EVAL
521 (translator-eval (translate-macexpr-toplevel form))))
523 (defun translator-eval (x)
524 (eval x))
526 ;; This basically tells the msetq def%tr to use defparameter insetad
527 ;; of setq because we're doing a setq at top-level, which isn't
528 ;; specified by ANSI CL.
529 (defvar *macexpr-top-level-form-p* nil)
531 (defun translate-macexpr-toplevel (form &aux (*transl-backtrace* nil) tr-abort)
532 ;; there are very few top-level special cases, I don't
533 ;; think it would help the code any to generalize TRANSLATE
534 ;; to target levels.
536 ;; Except msetq at top-level is special for ANSI CL. See below.
537 (setq form (toplevel-optimize form))
538 (cond ((atom form) nil)
539 ((eq (caar form) '$eval_when)
540 (let ((whens (cadr form))
541 (body (cddr form)) tr-whens)
542 (setq whens (cond (($listp whens) (cdr whens))
543 ((atom whens) (list whens))
545 (tr-format (intl:gettext "error: 'eval_when' argument must be a list or atom; found: ~:M~%") (cadr form))
546 nil)))
547 (setq tr-whens (mapcar 'stripdollar whens))
548 (cond ((member '$translate whens :test #'eq)
549 (mapc 'meval body)))
550 (cond ((member '$loadfile whens :test #'eq)
551 `(progn
552 ,@(mapcar 'translate-macexpr-toplevel body)))
553 ((setq tr-whens (intersect tr-whens '(:compile-toplevel :load-toplevel :execute)))
554 `(eval-when
555 ,tr-whens
556 ,@(mapcar 'translate-macexpr-toplevel body)))
557 ((member '$compile whens :test #'eq)
558 ;; strictly for the knowledgeable user.
559 ;; I.E. so I can use EVAL_WHEN(COMPILE,?SPECIALS:TRUE)
560 `(eval-when
561 #+gcl (compile)
562 #-gcl (:compile-toplevel)
563 ,@(mapcar 'translate-macexpr-toplevel body))))))
564 ((member (caar form) translate-time-evalables :test #'eq)
565 (meval1 form)
566 `(eval-when
567 #+gcl (compile load eval)
568 #-gcl (:compile-toplevel :load-toplevel :execute)
569 (meval* ',form)))
570 ((member (caar form) '(mdefine mdefmacro) :test #'eq)
571 (let ((name (caaadr form))
572 (trl))
573 (tr-format (intl:gettext "note: translating ~:@M~%") name)
574 (setq trl (tr-mdefine-toplevel form))
575 (cond (tr-abort
576 (tr-format (intl:gettext "error: failed to translate ~:@M~%") name)
577 (tr-format (intl:gettext "note: keep going and hope for the best.~%"))
578 `(meval* ',form))
579 (t trl))))
580 ((eq 'mprogn (caar form))
581 ;; note that this ignores the $%% crock.
582 `(progn ,@(mapcar #'translate-macexpr-toplevel (cdr form))))
583 ((eq 'msetq (caar form))
584 ;; Toplevel msetq's should really be defparameter instead of
585 ;; setq for Common Lisp.
586 (let ((*macexpr-top-level-form-p* t))
587 (dtranslate form)))
588 ((eq '$define_variable (caar form))
589 ;; Toplevel msetq's should really be defparameter instead of
590 ;; setq for Common Lisp.
591 (let ((*macexpr-top-level-form-p* t))
592 (dtranslate form)))
594 (let ((t-form (dtranslate form)))
595 (cond (tr-abort
596 `(meval* ',form))
598 t-form))))))
602 (defmvar $tr_optimize_max_loop 100.
603 "The maximum number of times the macro-expansion and optimization
604 pass of the translator will loop in considering a form.
605 This is to catch macro expansion errors, and non-terminating
606 optimization properties.")
608 (defun toplevel-optimize (form)
609 ;; it is vital that optimizations be done within the
610 ;; context of variable meta bindings, declarations, etc.
611 ;; Also: think about calling the simplifier here.
612 (cond ((atom form)
613 (cond ((symbolp form)
614 ;; If this symbol has the constant property, then
615 ;; use its assigned constant value in place of the
616 ;; symbol.
617 (let ((v (getl (mget form '$props) '($constant))))
618 (if v (cadr v) form)))
619 (t form)))
621 (do ((new-form)
622 (kount 0 (1+ kount)))
623 ;; tailrecursion should always arrange for a counter
624 ;; to check for mobylossage.
625 ((> kount $tr_optimize_max_loop)
626 (tr-format (intl:gettext "warning: I've looped ~A times in macro expansion; just give up and return ~:@M~%")
627 $tr_optimize_max_loop (caar form))
628 form)
629 (setq new-form (toplevel-optimize-1 form))
630 (cond ((atom new-form)
631 (return (toplevel-optimize new-form)))
632 ((eq new-form form)
633 (return form))
635 (setq form new-form)))))))
637 (defun toplevel-optimize-1 (form &aux (op (car form)) prop)
638 (cond ((or (atom op)
639 (member 'array op :test #'eq)) form)
640 ((progn (setq op (car op))
641 (setq prop
642 (if $transrun ; crock a minute.
643 (or (get op 'translated-mmacro)
644 (mget op 'mmacro))
645 (or (mget op 'mmacro)
646 (get op 'translated-mmacro)))))
647 (mmacro-apply prop form))
648 ((setq prop ($get op '$optimize))
649 ;; interesting, the MAPPLY here causes the simplification
650 ;; of the form and the result.
651 ;; The optimize property can be used to implement
652 ;; such niceties as the $%% crock.
653 (mapply1 prop (list form) "an optimizer property" nil))
654 ((and ($get op '$transload)
655 (get op 'autoload)
656 ;; check for all reasonable definitions,
657 ;; $OPTIMIZE and MACRO already checked.
658 (not (or (get-lisp-fun-type op)
659 (getl op '(translate mfexpr* mfexpr*s
660 fsubr fexpr *fexpr
661 macro
662 ;; foobar?
664 (mgetl op '(mexpr)))))
665 (load-function op t)
666 ;; to loop.
667 (cons (car form) (cdr form)))
668 (t form)))
670 (defun translate (form)
671 (and *transl-debug* (push form *transl-backtrace*))
672 (setq form (toplevel-optimize form))
673 (and *transl-debug* (pop *transl-backtrace*))
674 (prog2
675 (and *transl-debug* (push form *transl-backtrace*))
676 (if (atom form)
677 (translate-atom form)
678 (translate-form form))
679 ;; hey boy, reclaim that cons, just don't pop it!
680 (and *transl-debug* (pop *transl-backtrace*))))
682 (defun translate-atom (form &aux temp)
683 (cond ((numberp form) (cons (tr-class form) form))
684 ((setq temp (assoc form boolean-object-table :test #'eq))
685 (cdr temp))
686 ((and (setq temp (mget form '$numer)) $tr_numer)
687 `($float . ,temp))
688 ((setq temp (implied-quotep form))
689 `($any . ',temp))
690 ((tboundp form)
691 (specialp form) ;; notes its usage if special.
692 (setq form (teval form))
693 `(,(value-mode form) . ,form))
695 (cond ((not (specialp form))
696 (warn-undefined-variable form)
697 (if $transcompile (pushnew form specials :test #'eq))))
698 ;; note that the lisp analysis code must know that
699 ;; the TRD-MSYMEVAL form is a semantic variable.
700 (let* ((mode (value-mode form))
701 (init-val (assoc mode mode-init-value-table :test #'eq)))
702 (setq init-val (cond (init-val (cdr init-val))
703 (t `',form)))
704 ;; in the compiler TRD-MSYMEVAL doesn't do a darn
705 ;; thing, but it provides dynamic initialization of
706 ;; variables in interpreted code which is translated
707 ;; in-core. In FILE loaded code the DEFVAR will take
708 ;; care of this.
709 (push-defvar form init-val)
710 `(,mode . (trd-msymeval ,form ,init-val))))))
712 (defun translate-form (form &aux temp)
713 (cond ((eq (car form) 'meval) (cons '$any form)) ;;for those lispy macsyma forms
714 ((not (atom (caar form)))
715 ;; this is a check like that in the simplifier. form could
716 ;; result from substitution macros.
717 (translate `((mqapply) ,(caar form) . ,(cdr form))))
718 ((member 'array (cdar form) :test #'eq)
719 ;; dispatch this bad-boy to another module quick.
720 (tr-arraycall form))
721 ;; TRANSLATE properties have priority.
722 ((setq temp (get (caar form) 'translate))
723 (funcall temp form))
724 ((setq temp (get-lisp-fun-type (caar form)))
725 (tr-lisp-function-call form temp))
726 ((setq temp (macsyma-special-macro-p (caar form)))
727 (attempt-translate-random-macro-op form temp))
728 ((setq temp (macsyma-special-op-p (caar form)))
729 ;; a special form not handled yet! foobar!
730 (attempt-translate-random-special-op form temp))
731 ((or (get (caar form) 'noun) (get (caar form) 'operators))
732 ;; puntastical case. the weird ones are presumably taken care
733 ;; of by TRANSLATE properties by now.
734 (tr-infamous-noun-form form))
736 ;; "What does a macsyma function call mean?".
737 ;; By the way, (A:'B,B:'C,C:'D)$ A(3) => D(3)
738 ;; is not supported.
740 (tr-macsyma-user-function-call (caar form) (cdr form) form))))
744 (defmvar $tr_bound_function_applyp t)
746 (defun tr-macsyma-user-function-call (function args form)
747 ;; this needs some work, output load-time code to
748 ;; check for MMACRO properties, etc, to be really
749 ;; foolproof.
750 (cond ((eq $tr_function_call_default '$apply)
751 (translate `(($apply) ,(caar form) ((mlist) ,@(cdr form)))))
752 ((eq $tr_function_call_default '$expr)
753 (tr-lisp-function-call form 'subr))
754 ((eq $tr_function_call_default '$general)
755 (cond
756 ;;; G(F,X):=F(X+1); case.
757 ((and $tr_bound_function_applyp (tboundp function))
758 (let ((new-form `(($apply) ,function ((mlist) ,@args))))
759 (tr-format (intl:gettext "warning: ~:M is a bound variable in ~:M, but it is used as a function.~%") function form)
760 (tr-format (intl:gettext "note: instead I'll translate it as: ~:M~%") new-form)
761 (translate new-form)))
762 ;; MFUNCTION-CALL cleverely punts this question to a FSUBR in the
763 ;; interpreter, and a macro in the compiler. This is good style,
764 ;; if a user is compiling then assume he is less lossage prone.
766 (pushnew (caar form) *untranslated-functions-called*)
767 (call-and-simp
768 (function-mode (caar form))
769 'mfunction-call `(,(caar form) ,@(tr-args args))))))
771 ;; This case used to be the most common, a real loser.
772 (warn-meval form)
773 `(,(function-mode (caar form)) . (meval ',form)))))
776 (defun attempt-translate-random-macro-op (form typel &aux tem)
777 (warn-fexpr form)
778 (setq tem (translate-atoms form))
779 (setf (car tem) (caar tem))
780 `($any . ,tem))
782 (defun attempt-translate-random-special-op (form typel)
783 (warn-fexpr form)
784 `(,(function-mode (caar form)) . (meval ',(translate-atoms form))))
787 (defun tr-lisp-function-call (form type)
788 (let ((op (caar form)) (mode) (args))
789 (setq args (cond ((member type '(subr lsubr expr) :test #'eq)
790 (if $transcompile
791 (case type
792 ((subr) (pushnew op exprs :test #'eq))
793 ((lsubr) (pushnew op lexprs :test #'eq))
794 (t nil)))
795 (mapcar #'(lambda (llis) (dconvx (translate llis)))
796 (cdr form)))
798 (if $transcompile (pushnew op fexprs :test #'eq))
799 (mapcar 'dtranslate (cdr form))))
800 mode (function-mode op))
801 (call-and-simp mode op args)))
803 ;;the once-translated is so that inside translate file where a function
804 ;;has been translated, subsequent calls won't use mfunction call
805 (defun get-lisp-fun-type (fun &aux temp)
806 ;; N.B. this is Functional types. NOT special-forms,
807 ;; lisp special forms are meaningless to macsyma.
808 (cond ((get fun '*lexpr) 'lsubr)
809 ((get fun '*expr) 'subr)
810 ;; *LEXPR & *EXPR gotten from DEFMFUN declarations
811 ;; which is loaded by TrData.
812 ((mget fun '$fixed_num_args_function)
813 'subr)
814 ((mget fun '$variable_num_args_function)
815 'lsubr)
816 ((setq temp (getl fun '(expr subr lsubr)))
817 (car temp))
818 ((get fun 'once-translated))
819 ((get fun 'translated))
820 (t nil)))
822 (defun tr-infamous-noun-form (form)
823 ;; 'F(X,Y) means noun-form. The arguments are evaluated.
824 ;; but the function is cons on, not applied.
825 ;; N.B. for special forms and macros this is totally wrong.
826 ;; But, those cases are filtered out already, presumably.
828 (let ((op (cond ((member 'array (car form) :test #'eq)
829 `(,(caar form) array))
830 (t `(,(caar form)))))
831 (args (tr-args (cdr form))))
832 `($any . (simplify (list ',op ,@args)))))
836 ;;; Some atoms, solely by usage, are self evaluating.
838 (defun implied-quotep (atom)
839 (cond
840 ((stringp atom)
841 (cond
842 ;; I WONDER IF THIS NEXT CONDITION CAN BE CUT OUT ?? !!
843 ((equal atom "**") ;;; foolishness. The PARSER should do this.
844 ;; Losing Fortran hackers.
845 (tr-format "~% `**' is obsolete, use `^' !!!")
846 "^")
847 (t atom)))
848 ((get atom 'implied-quotep) atom)
849 (t nil)))
851 (defun translate-atoms (form)
852 ;; This is an oldy moldy which tries to declare everything
853 ;; special so that calling fexpr's will work in compiled
854 ;; code. What a joke.
855 (cond ((atom form)
856 (cond ((or (numberp form) (member form '(t nil) :test #'eq)) form)
857 ((tboundp form)
858 (if $transcompile
859 (or (specialp form)
860 (pushnew form specials :test #'eq)))
861 form)
863 (if $transcompile (pushnew form specials :test #'eq))
864 form)))
865 ((eq 'mquote (caar form)) form)
866 (t (cons (car form) (mapcar #'translate-atoms (cdr form))))))
869 ;;; the Translation Properties. the heart of TRANSL.
871 ;;; This conses up the call to the function, adding in the
872 ;;; SIMPLIFY i the mode is $ANY. This should be called everywhere.
873 ;;; instead of duplicating the COND everywhere, as is done now in TRANSL.
875 (defun tr-nosimpp (op)
876 (cond ((atom op)
877 (get op 'tr-nosimp))
878 (t nil)))
880 (defun call-and-simp (mode fun args)
881 (cond ((or (not (eq mode '$any))
882 (tr-nosimpp fun))
883 `(,mode ,fun . ,args))
885 `(,mode simplify (,fun . ,args)))))
887 (defmspec $declare_translated (fns)
888 (setq fns (cdr fns))
889 (loop for v in fns
890 when (or (symbolp v) (and (stringp v) (setq v ($verbify v))))
891 do (setf (get v 'once-translated) t)
892 (pushnew v *declared-translated-functions*)
893 else do (merror (intl:gettext "declare_translated: arguments must be symbols or strings; found: ~:M") v)))
895 (def%tr $declare (form)
896 `($any . (meval ',form)))
898 (def%tr $eval_when (form)
899 (tr-format (intl:gettext "error: found 'eval_when' in a function or expression: ~:M~%") form)
900 (tr-format (intl:gettext "note: 'eval_when' can appear only at the top level in a file.~%"))
901 (setq tr-abort t)
902 '($any . nil))
904 (def%tr mdefmacro (form)
905 (tr-format (intl:gettext "warning: globally defining macro ~:M now to ensure correct macro expansions.~%") (caaadr form))
906 ; Define the macro now to ensure that it's defined when it's time
907 ; to expand it. It's a bug that this definition occurs during
908 ; translation without being cleaned it up afterward, but simply
909 ; removing this breaks things.
910 (meval form)
911 `($any . (meval ',form)))
913 (def%tr $local (form)
914 (cond (local
915 (tr-format (intl:gettext "error: there is already a 'local' in this block.~%"))
916 (setq tr-abort t))
918 (setq local t)))
919 (cons nil `(mapply 'mlocal ',(cdr form) '$local)))
922 (def%tr mquote (form)
923 (list (tr-class (cadr form)) 'quote (cadr form)))
926 (defun tr-lambda (form &optional (tr-body #'tr-seq) &rest tr-body-argl
927 &aux
928 (arglist (mparams (cadr form)))
929 (easy-assigns nil))
930 ;; This function is defined to take a simple macsyma lambda expression and
931 ;; return a simple lisp lambda expression. The optional TR-BODY hook
932 ;; can be used for translating other special forms that do lambda binding.
934 ;; Local SPECIAL declarations are not used because
935 ;; the multics lisp compiler does not support them. They are of course
936 ;; a purely syntactic construct that doesn't buy much. I have been
937 ;; advocating the use of DEFINE_VARIABLE in macsyma user programs so
938 ;; that the use of DECLARE(FOO,SPECIAL) will be phased out at that level.
940 (mapc #'tbind arglist)
941 (destructuring-let (((mode . nbody) (apply tr-body (cddr form) tr-body-argl))
942 (local-declares (make-declares arglist t)))
943 ;; -> BINDING of variables with ASSIGN properties may be difficult to
944 ;; do correctly and efficiently if arbitrary code is to be run.
945 (if (or tr-lambda-punt-assigns
946 (do ((l arglist (cdr l)))
947 ((null l) t)
948 (let* ((var (car l))
949 (assign (get var 'assign)))
950 (if assign
951 (cond ((eq assign 'assign-mode-check)
952 (push `(,assign ',var ,(teval var)) easy-assigns))
954 (return nil)))))))
955 ;; Case with EASY or no ASSIGN's
956 (progn ;;-have to undo any local assignments. --wfs
957 `(,mode . (lambda ,(tunbinds arglist)
958 ,local-declares
959 ,@easy-assigns
960 ,@nbody)))
961 ;; Case with arbitrary ASSIGN's.
962 (let ((temps (mapcar #'(lambda (ign) ign (tr-gensym)) arglist)))
963 `(,mode . (lambda ,temps
964 (unwind-protect
965 (progn
966 ;; [1] Check before binding.
967 ,@(mapcan #'(lambda (var val)
968 (let ((assign (get var 'assign)))
969 (if assign
970 (let ((assign-fn (if (symbolp assign) (symbol-function assign) (coerce assign 'function))))
971 (list `(funcall ,assign-fn ',var ,val))))))
972 arglist temps)
973 ;; [2] do the binding.
974 ((lambda ,(tunbinds arglist)
975 ,local-declares
976 ,@nbody)
977 ,@temps))
978 ;; [2] check when unbinding too.
979 ,@(mapcan #'(lambda (var)
980 (let ((assign (get var 'assign)))
981 (if assign
982 (let ((assign-fn (if (symbolp assign) (symbol-function assign) (coerce assign 'function))))
983 (list `(funcall ,assign-fn ',var
984 ;; use DTRANSLATE to
985 ;; catch global
986 ;; scoping if any.
987 ,(dtranslate var)))))))
988 arglist))))))))
991 (defun make-declares (varlist localp &aux (dl) (fx) (fl) specs)
992 (when $transcompile
993 (do ((l varlist (cdr l))
994 (mode) (var))
995 ((null l))
997 ;; When a variable is declared special, be sure to declare it
998 ;; special here.
999 (when (and localp (tr-get-special (car l)))
1000 (push (car l) specs))
1002 (when (or (not localp)
1003 (not (tr-get-special (car l))))
1004 ;; don't output local declarations on special variables.
1005 (setq var (teval (car l)) mode (value-mode var))
1006 (setq specs (cons var specs))
1008 (cond ((eq '$fixnum mode) (pushnew var fx :test #'eq))
1009 ((eq '$float mode) (pushnew var fl :test #'eq)))))
1010 (if fx (pushnew `(fixnum . ,fx) dl :test #'eq))
1011 (if fl (pushnew `(type flonum . ,fl) dl :test #'eq))
1012 (if specs (pushnew `(special . ,specs) dl :test #'eq))
1013 (if dl `(declare . ,dl))))
1015 (defun tr-seq (l)
1016 (do ((mode nil)
1017 (body nil))
1018 ((null l)
1019 (cons mode (nreverse body)))
1020 (let ((exp (translate (pop l))))
1021 (setq mode (car exp))
1022 (push (cdr exp) body))))
1024 (def%tr mprogn (form)
1025 (setq form (tr-seq (cdr form)))
1026 (cons (car form) `(progn ,@(cdr form))))
1029 (def%tr mprog (form)
1030 (let (arglist body val-list)
1031 ;; [1] normalize the MPROG syntax.
1032 (cond (($listp (cadr form))
1033 (setq arglist (cdadr form)
1034 body (cddr form)))
1036 (setq arglist nil
1037 body (cdr form))))
1038 (cond ((null body)
1039 (setq body '(((mquote) $done)))))
1040 (setq val-list (mapcar #'(lambda (u)
1041 (if (atom u) u
1042 (translate (caddr u))))
1043 arglist)
1044 arglist (mapcar #'(lambda (u)
1045 ;; X or ((MSETQ) X Y)
1046 (if (atom u) u (cadr u)))
1047 arglist))
1048 (let ((dup (find-duplicate arglist :test #'eq)))
1049 (when dup
1050 (tr-format (intl:gettext "error: ~M occurs more than once in block variable list") dup)
1051 (setq tr-abort t)))
1052 (unless tr-abort
1053 (setq form
1054 (tr-lambda
1055 ;; [2] call the lambda translator.
1056 `((lambda) ((mlist) ,@arglist) ,@body)
1057 ;; [3] supply our own body translator.
1058 #'tr-mprog-body
1059 val-list
1060 arglist))
1061 (cons (car form) `(,(cdr form) ,@val-list)))))
1063 (defun tr-mprog-body (body val-list arglist
1064 &aux
1065 (inside-mprog t)
1066 (return-mode nil)
1067 (need-prog? nil)
1068 (returns nil) ;; not used but must be bound.
1069 (local nil)
1071 (do ((l nil))
1072 ((null body)
1073 ;; [5] hack the val-list for the mode context.
1074 ;; Perhaps the only use of the function MAP in all of macsyma.
1075 (mapl #'(lambda (val-list arglist)
1076 (cond ((atom (car val-list))
1077 (rplaca val-list
1078 (or (cdr (assoc (value-mode (car arglist))
1079 mode-init-value-table :test #'eq))
1080 `',(car arglist))))
1082 (warn-mode (car arglist)
1083 (value-mode (car arglist))
1084 (car (car val-list))
1085 "in a `block' statement")
1086 (rplaca val-list (cdr (car val-list))))))
1087 val-list arglist)
1088 (setq l (nreverse l))
1089 (cons return-mode
1090 (if need-prog?
1091 `((prog () ,@(delete nil l :test #'equal)))
1092 l)))
1093 ;; [4] translate a form in the body
1094 (let ((form (pop body)))
1095 (cond ((null body)
1096 ;; this is a really bad case.
1097 ;; we don't really know if the return mode
1098 ;; of the expression is for the value of the block.
1099 ;; Some people write RETURN at the end of a block
1100 ;; and some don't. In any case, the people not
1101 ;; use the PROG programming style won't be screwed
1102 ;; by this.
1103 (setq form (translate form))
1104 (setq return-mode (*union-mode (car form) return-mode))
1105 (setq form (cdr form))
1106 (if (and need-prog?
1107 (or (atom form)
1108 (not (eq (car form) 'return))))
1109 ;; put a RETURN on just in case.
1110 (setq form `(return ,form))))
1111 ((symbolp form))
1113 (setq form (dtranslate form))))
1114 (push form l))))
1116 (def%tr mreturn (form)
1117 (if (null inside-mprog)
1118 (tr-format (intl:gettext "warning: 'return' not within 'block' or 'do': ~:M~%") form))
1119 (setq need-prog? t)
1120 (setq form (translate (cadr form)))
1121 (setq return-mode (if return-mode (*union-mode (car form) return-mode)
1122 (car form)))
1123 (setq form `(return ,(cdr form)))
1124 (push form returns) ;; USED by lusing MDO etc not yet re-written.
1125 ;; MODE here should be $PHANTOM or something.
1126 `($any . ,form))
1128 (def%tr mgo (form)
1129 (if (null inside-mprog)
1130 (tr-format (intl:gettext "warning: 'go' not within 'block' or 'do': ~:M~%") form))
1131 (if (not (symbolp (cadr form)))
1132 (tr-format (intl:gettext "warning: 'go' tag must be a symbol: ~:M~%") form))
1133 (setq need-prog? t)
1134 `($any . (go ,(cadr form))))
1136 (def%tr mqapply (form)
1137 (let ((fn (cadr form)) (args (cddr form))
1138 (aryp (member 'array (cdar form) :test #'eq)))
1139 (cond ((atom fn)
1140 ;; I'm guessing (ATOM FN) is a parser error or other Lisp error,
1141 ;; so don't bother to translate the following error message.
1142 (mformat *translation-msgs-files* "translator: MQAPPLY operator must be a cons; found: ~:M" form)
1143 nil)
1144 ((eq (caar fn) 'mquote)
1145 `($any list ',(cons (cadr fn) aryp) ,@(tr-args args)))
1146 ((eq (caar fn) 'lambda)
1147 ;; LAMBDA([X,'Y,[L]],...)(A,B,C) is a bogus form. Don't bother with it.
1148 ;; ((LAMBDA) ((MLIST) ....) ....)
1149 (cond ((member 'bogus (mapcar #'(lambda (arg)
1150 (cond ((or (mquotep arg)
1151 ($listp arg))
1152 'bogus)))
1153 (cdr (cadr fn))) :test #'eq)
1154 (tr-format (intl:gettext "error: quote or list arguments are not allowed in MQAPPLY; found: ~:M~%") form)
1155 (setq tr-abort t)
1156 nil)
1158 (setq fn (tr-lambda fn)
1159 args (tr-args args))
1160 `(,(car fn) ,(cdr fn) ,@args))))
1161 ((not aryp)
1162 `($any simplify (mapply ,(dconvx (translate fn))
1163 (list ,@(tr-args args))
1164 ',fn)))
1166 (warn-meval form)
1167 `($any meval ',form)))))
1171 (def%tr mcond (form)
1172 (prog (dummy mode nl)
1173 (setq dummy (translate (caddr form))
1174 mode (car dummy)
1175 nl (list dummy (translate-predicate (cadr form))))
1176 (do ((l (cdddr form) (cddr l))) ((null l))
1177 (cond ((and (not (atom (cadr l))) (eq 'mcond (caaadr l)))
1178 (setq l (cdadr l))))
1179 (setq dummy (translate (cadr l))
1180 mode (*union-mode mode (car dummy))
1181 nl (cons dummy
1182 (cons (translate-predicate (car l))
1183 nl))))
1184 (setq form nil)
1185 (do ((l nl (cddr l))) ((null l))
1186 (cond ((and (eq t (cadr l)) (null (cdar l))))
1187 (t (setq form
1188 (cons (cons (cadr l)
1189 (cond ((and (not (atom (cdar l)))
1190 (cddar l)
1191 (eq (cadar l) 'progn))
1192 (nreverse
1193 (cons (dconv (cons (caar l)
1194 (car (reverse (cddar l))))
1195 mode)
1196 (cdr (reverse (cddar l))))))
1197 ((and (equal (cdar l) (cadr l))
1198 (atom (cdar l))) nil)
1199 (t (list (cdr (car l))))))
1200 form)))))
1201 ;; Wrap (LET (($PREDERROR T)) ...) around translation of MCOND.
1202 ;; Nested MCOND expressions (e.g. if x > 0 then if y > 0 then ...)
1203 ;; will therefore yield nested (LET (($PREDERROR T)) ... (LET (($PREDERROR T)) ...)).
1204 ;; I suppose only the topmost one is needed, but there is very little harm
1205 ;; in the redundant ones, so I'll let it stand.
1206 (return (cons mode (list 'let '(($prederror t)) (cons 'cond form))))))
1210 ;; The MDO and MDOIN translators should be changed to use the TR-LAMBDA.
1211 ;; Perhaps a mere expansion into an MPROG would be best.
1213 (def%tr mdo (form)
1214 (let (returns assigns return-mode local (inside-mprog t) need-prog?)
1215 (let (mode var init next test action varmode)
1216 (setq var (cond ((cadr form)) (t 'mdo)))
1217 (specialp var)
1218 (tbind var)
1219 (setq init (if (caddr form) (translate (caddr form)) '($fixnum . 1)))
1220 (cond ((not (setq varmode (tr-get-mode var)))
1221 (declvalue var (car init) t)))
1222 (setq next (translate (cond ((cadddr form) (list '(mplus) (cadddr form) var))
1223 ((car (cddddr form)))
1224 (t (list '(mplus) 1 var)))))
1225 (setq form (copy-list form))
1226 ;;to make the end test for thru be numberp if the index is numberp
1227 ;;and to eliminate reevaluation
1228 (cond ((not varmode)
1229 (declvalue var (*union-mode (car init) (car next)) t))
1231 (warn-mode var varmode (*union-mode (car init) (car next)))))
1232 (setq test (translate-predicate
1233 (list '(mor)
1234 (cond ((null (cadr (cddddr form))) nil)
1235 ((and (cadddr form)
1236 (mnegp ($numfactor (simplify (cadddr form)))))
1237 (list '(mlessp) var (cadr (cddddr form))))
1238 (t (list '(mgreaterp) var (cadr (cddddr form)))))
1239 (caddr (cddddr form)))))
1240 (setq action (translate (cadddr (cddddr form)))
1241 mode (cond ((null returns) '$any)
1242 (t return-mode)))
1243 (setq var (tunbind (cond ((cadr form)) (t 'mdo))))
1244 `(,mode do ((,var ,(cdr init) ,(cdr next)))
1245 (,test '$done) . ((declare (special ,var)) .
1246 ,(cond ((atom (cdr action)) nil)
1247 ((eq 'progn (cadr action)) (cddr action))
1248 (t (list (cdr action)))))))))
1250 (def%tr mdoin (form)
1251 (let (returns assigns return-mode local (inside-mprog t) need-prog?)
1252 (prog (mode var init action)
1253 (setq var (tbind (cadr form))) (tbind 'mdo)
1254 (specialp var)
1255 (setq init (dtranslate (caddr form)))
1256 (cond ((or (cadr (cddddr form)) (caddr (cddddr form)))
1257 (tunbind 'mdo) (tunbind (cadr form))
1258 (return `($any meval '((mdoin) . ,(cdr form))))))
1259 (setq action (translate (cadddr (cddddr form)))
1260 mode (cond ((null returns) '$any)
1261 (t return-mode)))
1262 (tunbind 'mdo) (tunbind (cadr form))
1263 (return
1264 `(,mode do ((,var) (mdo (cdr ,init) (cdr mdo)))
1265 ((null mdo) '$done) .
1266 ((declare (special ,var)) (setq ,var (car mdo)) .
1267 ,(cond ((atom (cdr action)) nil)
1268 ((eq 'progn (cadr action)) (cddr action))
1269 (t (list (cdr action))))))))))
1272 (defun lambda-wrap1 (tn val form)
1273 (if (or (atom val)
1274 (eq (car val) 'quote))
1275 (subst val tn form)
1276 `((lambda (,tn) ,form) ,val)))
1278 (def%tr msetq (form)
1279 (let ((var (cadr form))
1280 (val (caddr form))
1281 assign
1282 mode)
1283 (cond ((atom var)
1284 (setq mode (value-mode var) val (translate val))
1285 (cond ((not (tboundp var))
1286 (pushnew var specials :test #'eq)))
1287 (warn-mode var mode (car val))
1288 (if (eq '$any mode)
1289 (setq mode (car val) val (cdr val))
1290 (setq val (dconv val mode)))
1291 (cons mode
1292 (if (setq assign (get var 'assign))
1293 (let ((tn (tr-gensym))
1294 (assign-fn (if (symbolp assign) (symbol-function assign) (coerce assign 'function))))
1295 (lambda-wrap1 tn val `(let nil
1296 (declare (special ,var ,(teval var)))
1297 (funcall ,assign-fn ',var ,tn)
1298 (setq ,(teval var) ,tn))))
1299 `(let nil (declare (special ,(teval var)))
1300 (if (not (boundp ',(teval var)))
1301 (add2lnc ',(teval var) $values))
1302 (,(if *macexpr-top-level-form-p*
1303 'defparameter
1304 'setq)
1305 ,(teval var) ,val)))))
1306 ((member 'array (car var) :test #'eq)
1307 (tr-arraysetq var val))
1309 (unless (safe-get (caar var) 'mset_extension_operator)
1310 (tr-format (intl:gettext "warning: no assignment operator known for ~:M~%") var)
1311 (tr-format (intl:gettext "note: just keep going and hope for the best.~%")))
1312 (setq val (translate val))
1313 `(,(car val) mset ',(translate-atoms var) ,(cdr val))))))
1315 (def%tr $max (x) (translate-$max-$min x))
1316 (def%tr $min (x) (translate-$max-$min x))
1317 (def%tr %max (x) (translate-$max-$min x))
1318 (def%tr %min (x) (translate-$max-$min x))
1320 (defun translate-$max-$min (form)
1321 (let ((mode) (arglist) (op (stripdollar (caar form))))
1322 (setq arglist
1323 (mapcar (lambda (l)
1324 (setq l (translate l))
1325 (cond ((null mode)
1326 (setq mode (car l)))
1327 ((eq mode (car l)))
1329 (setq mode '$any)))
1331 (cdr form)))
1332 ; To match the interpreted case, and to make sure we use the
1333 ; correct mode for the return value, we do not apply float
1334 ; contagion to the arguments and we use a special translation
1335 ; to call MAX or MIN only when every argument has the same
1336 ; mode (either all fixnum or all float). CLHS says that
1337 ; implementations have choices they can make about what MAX
1338 ; and MIN return when the arguments are a mix of float and
1339 ; rational types.
1340 ; Example: if an implementation decides to apply float contagion
1341 ; to the arguments of MAX (MIN), then it can return either an
1342 ; integer or a float if the greatest (least) argument was an
1343 ; integer.
1344 (if (member mode '($fixnum $float) :test #'eq)
1345 `(,mode ,(if (eq 'min op) 'min 'max) . ,(mapcar 'cdr arglist))
1346 `($any ,(if (eq 'min op) '$lmin '$lmax)
1347 (list '(mlist) . ,(mapcar 'dconvx arglist))))))
1350 ;;; mode acessing, binding, handling. Super over-simplified.
1352 (defun tr-class (x)
1353 (cond ((integerp x) '$fixnum)
1354 ((floatp x) '$float)
1355 ((member x '(t nil) :test #'eq) '$boolean)
1356 ((atom x) '$any)
1357 ((eq 'rat (caar x)) '$rational)
1358 (t '$any)))
1360 (defun *union-mode (mode1 mode2)
1361 (cond ((eq mode1 mode2) mode1)
1362 ((null mode1) mode2)
1363 ((null mode2) mode1)
1364 ((member mode2 *$any-modes* :test #'eq) '$any)
1365 ((member mode1 *$any-modes* :test #'eq) '$any)
1366 ((eq '$fixnum mode1) mode2)
1367 ((eq '$float mode1)
1368 (if (eq '$number mode2) '$number '$float))
1369 ((eq '$rational mode1)
1370 (if (eq '$float mode2) '$float '$any))
1371 ((eq '$number mode1)
1372 (if (eq '$rational mode2) '$any '$number))
1373 (t '$any)))
1375 (defun value-mode (var)
1376 (cond ((tr-get-mode var))
1378 (warn-undeclared var)
1379 '$any)))
1381 (defun decmode-arrayfun (f m)
1382 (putprop f m 'arrayfun-mode))
1384 (defun array-mode (ar)
1385 (cond ((get ar 'array-mode)) (t '$any)))
1387 (defun arrayfun-mode (ar)
1388 (cond ((get ar 'arrayfun-mode)) (t '$any)))
1390 (defun function-mode (f)
1391 (cond ((get f 'function-mode)) (t '$any)))
1393 (defun function-mode-@ (f)
1394 (ass-eq-ref (tr-get-val-modes f) 'function-mode '$any))
1396 (defun array-mode-@ (f)
1397 (ass-eq-ref (tr-get-val-modes f) 'array-mode '$any))
1400 (defvar $tr_bind_mode_hook nil
1401 "A hack to allow users to key the modes of variables
1402 off of variable spelling, and other things like that.")
1404 ;; TBIND, below, copies the MODE, VAL-MODES, and SPECIAL properties
1405 ;; into the a table named TSTACK, and then removes those properties.
1406 ;; So if TBIND has been called, we will need to look for those
1407 ;; properties in TSTACK instead of the symbol property list.
1409 (defstruct (tstack-slot (:conc-name tstack-slot-))
1410 mode
1411 tbind
1412 val-modes
1413 ;; an alist telling second order info
1414 ;; about APPLY(VAR,[X]), ARRAYAPPLY(F,[X]) etc.
1415 special)
1417 (defun tr-get-mode (a)
1418 (if (get a 'tbind)
1419 (let ((my-slot (cdr (assoc a tstack))))
1420 (tstack-slot-mode my-slot))
1421 (get a 'mode)))
1423 #-gcl (defun (setf tr-get-mode) (b a)
1424 (if (get a 'tbind)
1425 (let ((my-slot (cdr (assoc a tstack))))
1426 (setf (tstack-slot-mode my-slot) b))
1427 (setf (get a 'mode) b)))
1429 #+gcl (defsetf tr-get-mode (a) (b)
1430 `(if (get ,a 'tbind)
1431 (let ((my-slot (cdr (assoc ,a tstack))))
1432 (setf (tstack-slot-mode my-slot) ,b))
1433 (setf (get ,a 'mode) ,b)))
1435 (defun tr-get-val-modes (a)
1436 (if (get a 'tbind)
1437 (let ((my-slot (cdr (assoc a tstack))))
1438 (tstack-slot-val-modes my-slot))
1439 (get a 'val-modes)))
1441 #-gcl (defun (setf tr-get-val-modes) (b a)
1442 (if (get a 'tbind)
1443 (let ((my-slot (cdr (assoc a tstack))))
1444 (setf (tstack-slot-val-modes my-slot) b))
1445 (setf (get a 'val-modes) b)))
1447 #+gcl (defsetf tr-get-val-modes (a) (b)
1448 `(if (get ,a 'tbind)
1449 (let ((my-slot (cdr (assoc ,a tstack))))
1450 (setf (tstack-slot-val-modes my-slot) ,b))
1451 (setf (get ,a 'val-modes) ,b)))
1453 (defun tr-get-special (a)
1454 (if (get a 'tbind)
1455 (let ((my-slot (cdr (assoc a tstack))))
1456 (tstack-slot-special my-slot))
1457 (get a 'special)))
1459 #-gcl (defun (setf tr-get-special) (b a)
1460 (if (get a 'tbind)
1461 (let ((my-slot (cdr (assoc a tstack))))
1462 (setf (tstack-slot-special my-slot) b))
1463 (setf (get a 'special) b)))
1465 #+gcl (defsetf tr-get-special (a) (b)
1466 `(if (get ,a 'tbind)
1467 (let ((my-slot (cdr (assoc ,a tstack))))
1468 (setf (tstack-slot-special my-slot) ,b))
1469 (setf (get ,a 'special) ,b)))
1471 ;;; should be a macro (TBINDV <var-list> ... forms)
1472 ;;; so that TUNBIND is assured, and also so that the stupid ASSQ doesn't
1473 ;;; have to be done on the darn TSTACK. This will have to wait till
1474 ;;; the basic special form translation properties are rewritten.
1476 (defun variable-p (var)
1477 (and var (symbolp var) (not (eq var t))))
1479 (defun bad-var-warn (var)
1480 (tr-format (intl:gettext "warning: ~:M cannot be used as a variable.~%") var))
1482 (defun tbind (var &aux old)
1483 (cond ((variable-p var)
1484 (setq old (make-tstack-slot :mode (get var 'mode)
1485 :tbind (get var 'tbind)
1486 :val-modes (get var 'val-modes)
1487 :special (get var 'special)))
1488 (push (cons var old) tstack)
1489 (cond ((not (specialp var))
1490 ;; It is the lisp convention in use to inherit
1491 ;; specialness from higher context.
1492 ;; Spurious MODEDECLARATIONS get put in the environment
1493 ;; when code is MEVAL'd since there is no way to stack
1494 ;; the mode properties. Certainly nobody is willing
1495 ;; to hack MEVAL in JPG;MLISP
1496 (remprop var 'val-modes)
1497 (remprop var 'mode)
1498 (remprop var 'special)))
1499 (putprop var var 'tbind)
1500 (if $tr_bind_mode_hook
1501 (let ((mode? (mapply $tr_bind_mode_hook
1502 (list var)
1503 '$tr_bind_mode_hook)))
1504 (if mode? (tr-declare-varmode var mode?))))
1505 var)
1507 (bad-var-warn var))))
1509 (defun tunbind (var &aux (old (assoc var tstack :test #'eq)))
1510 (when (variable-p var)
1511 (prog1
1512 (teval var)
1513 (cond (old
1514 (setq tstack (delete old tstack :test #'eq)) ; POP should be all we need.
1515 (setq old (cdr old))
1516 (putprop1 var (tstack-slot-mode old) 'mode)
1517 (putprop1 var (tstack-slot-tbind old) 'tbind)
1518 (putprop1 var (tstack-slot-val-modes old) 'val-modes)
1519 (putprop1 var (tstack-slot-special old) 'special))))))
1521 (defun putprop1 (name value key)
1522 ;; leaves property list clean after unwinding, this
1523 ;; is an efficiency/storage issue only.
1524 (if value
1525 (putprop name value key)
1526 (progn
1527 (remprop name key)
1528 nil)))
1530 (defun tunbinds (l)
1531 (do ((nl))
1532 ((null l) nl)
1533 (setq nl (cons (tunbind (caar tstack)) nl)
1534 l (cdr l))))
1536 (defun tboundp (var)
1537 ;; really LEXICAL-VARP.
1538 (and (symbolp var) (get var 'tbind) (not (tr-get-special var))))
1540 (defun teval (var)
1541 (or (and (symbolp var) (get var 'tbind)) var))
1543 ;; Local Modes:
1544 ;; Mode: LISP
1545 ;; Comment Col: 40
1546 ;; END: