Merge branch 'master' into gcl_cleanup
[maxima/cygwin.git] / src / transl.lisp
bloba94ae3b66094eb7a3a048377858183b76dece423
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1980 Massachusetts Institute of Technology ;;;
9 ;;; 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 *tr-free-vars-to-capture* '())
168 (defvar boolean-object-table
169 '(($true . ($boolean . t))
170 ($false . ($boolean . nil))
171 (t . ($boolean . t))
172 (nil . ($boolean . nil))))
174 (defvar mode-init-value-table
175 '(($float . 0.0)
176 ($fixnum . 0)
177 ($number . 0)
178 ($list . '((mlist)))
179 ($boolean . nil)))
181 (defvar tr-lambda-punt-assigns nil
182 "Kludge argument to `tr-lambda' due to lack of keyword argument passing")
184 (or (boundp '*in-compile*)
185 (setq *in-compile* nil))
187 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
189 (defun tr-abort ()
190 (setq tr-abort t)
191 nil)
193 (defun barfo (msg)
194 (tr-format (intl:gettext "Internal translator error: ~M~%") msg)
195 (cond (*transl-debug*
196 (break "transl barfo"))
198 (tr-abort)
199 nil)))
201 (defun specialp (var)
202 (or (optionp var)
203 (tr-get-special var)))
206 ;;; The error message system. Crude as it is.
207 ;;; I tell you how this aught to work:
208 ;;; (1) All state should be in one structure, one state variable.
209 ;;; (2) Should print out short message on-the-fly so that it
210 ;;; gives something to watch, and also so that it says something
211 ;;; if things crash.
212 ;;; (3) Summaries on a cross-referenced per-function and per-item
213 ;;; should be printed at the end, as a table.
214 ;;; e.g.
215 ;;; Undefined Functions used in
216 ;;; FOO BAR, BAZ,BOMB
217 ;;; FOOA P,Q
218 ;;; Undefined Variables ... same thing
219 ;;; Incomprehensible special forms
220 ;;; EV ....
221 ;;; Predicate Mode Targeting failures.
222 ;;; ..... -gjc
224 ;;; The way it works now is to print too little or too much.
225 ;;; Many items are only warned about the first time seen.
226 ;;; However, this isn't too much of a problem when using Emacs
227 ;;; to edit code, because searching for warned-about tokens
228 ;;; is quick and easy.
230 (defmvar *tr-warn-break* t
231 " if in debug mode `warning's signaled go to lisp break loops ")
233 (defmacro tr-warnbreak ()
234 `(and *transl-debug* *tr-warn-break* (break "transl")))
236 (defun tr-warnp (val)
237 (and val
238 (cond (*in-compile*
239 (member val '($all $compile $compfile $translate) :test #'eq))
240 ((or *in-compfile* *in-translate-file*)
241 (member val '($all $compfile $translate) :test #'eq))
242 (*in-translate*
243 (member val '($all $translate) :test #'eq)))))
245 (defvar warned-undefined-variables nil)
247 (defun warn-undefined-variable (form)
248 (and (tr-warnp $tr_warn_undefined_variable)
249 (cond ((member form warned-undefined-variables :test #'eq))
251 (push form warned-undefined-variables)
252 (tr-format (intl:gettext "warning: encountered undefined variable ~:M in translation.~%") form)
253 (tr-warnbreak)))))
255 (defun warn-undeclared (form &optional comment)
256 (and (tr-warnp $tr_warn_undeclared)
257 (cond ((member form *warned-un-declared-vars* :test #'equal) t)
259 (push form *warned-un-declared-vars*)
260 (tr-format (intl:gettext "warning: no type declaration for ~:M; assume type is 'any'.~%") form)
261 (tr-format (intl:gettext "note: 'modedeclare' declares types for translation.~%"))
262 (cond (comment
263 (dolist (v *translation-msgs-files*)
264 (terpri v)
265 (princ comment v))))
266 (tr-warnbreak)
267 nil))))
269 (defun warn-meval (form &optional comment)
270 (cond ((tr-warnp $tr_warn_meval)
271 (tr-format (intl:gettext "warning: emit call to MEVAL for expression: ~:M~%") form)
272 (cond (comment (dolist (v *translation-msgs-files*)
273 (terpri v)
274 (princ comment v))))
275 (tr-warnbreak)
276 'warned)))
279 (defun warn-mode (var mode newmode &optional comment)
280 (cond ((eq mode newmode))
282 (cond ((and (tr-warnp $tr_warn_mode)
283 (not (covers mode newmode))
284 (not (member (list var mode newmode) *warned-mode-vars* :test #'equal)))
285 (push (list var mode newmode) *warned-mode-vars*)
286 (tr-format (intl:gettext "warning: variable ~:M (declared type ~:M) assigned type ~:M.~%") var mode newmode)
287 (cond (comment
288 (dolist (v *translation-msgs-files*)
289 (terpri v)
290 (princ comment v))))
291 (tr-warnbreak))))))
293 (defun warn-fexpr (form &optional comment)
294 (cond ((and (tr-warnp $tr_warn_fexpr)
295 (not (member form *warned-fexprs* :test #'equal)))
296 (push form *warned-fexprs*)
297 (tr-format (intl:gettext "warning: ~:M is a special function without a full Lisp translation.~%") form)
298 (tr-format (intl:gettext "warning: calling ~:M in compiled code might not have the desired effect.~%") form)
299 (cond (comment
300 (dolist (v *translation-msgs-files*)
301 (terpri v)
302 (princ comment v))))
303 (tr-warnbreak))))
306 (defun macsyma-special-macro-p (fcn)
307 (getl-lm-fcn-prop fcn '(macro)))
309 (defun macsyma-special-op-p (f)
310 (getl f '(fsubr fexpr mfexpr* mfexpr*s *fexpr)))
312 (defun possible-predicate-op-p (f)
313 (member f '(mnotequal mequal $equal mgreaterp mgeqp mlessp mleqp) :test #'eq))
315 ;;;***************************************************************;;;
317 ;;; This function is the way to call the TRANSLATOR on an expression with
318 ;;; locally bound internal mode declarations. Result of TR-LAMBDA will be
319 ;;; (MODE . (LAMBDA (...) (DECLARE ...) TRANSLATED-EXP))
321 (defun tr-local-exp (exp &rest vars-modes)
322 (let ((loc (let ((tr-lambda-punt-assigns t))
323 (tr-lambda `((lambda) ((mlist) ,@(do ((l vars-modes (cddr l))
324 (ll nil (cons (car l) ll)))
325 ((null l) ll)
326 (or (variable-p (car l))
327 (bad-var-warn (car l)))))
328 (($modedeclare) ,@vars-modes)
329 ,exp)))))
330 (let ((mode (car loc))
331 (exp (car (last loc))))
332 (cons mode exp))))
334 (defun tr-args (form)
335 (mapcar #'(lambda (x) (dconvx (translate x))) form))
337 (defun dtranslate (form) (cdr (translate form)))
339 (defun dconv (x mode)
340 (cond ((eq '$float mode) (dconv-$float x))
341 ((eq '$cre mode) (dconv-$cre x))
342 (t (cdr x))))
344 (defun dconvx (x)
345 (if (member (car x) '(ratexpr pexpr) :test #'eq)
346 (dconv-$cre x)
347 (cdr x)))
349 (defun dconv-$float (x)
350 (cond ((member (car x) '($fixnum $number) :test #'eq)
351 (if (integerp (cdr x)) (float (cdr x)) (list 'float (cdr x))))
352 ((eq '$rational (car x))
353 (cond ((or (atom (cdr x))
354 (not (eq 'quote (cadr x))))
355 `($float ,(cdr x)))
357 (/ (float (cadadr (cdr x))) (float (caddr (caddr x)))))))
358 (t (cdr x))))
360 (defun dconv-$cre (x)
361 (if (eq '$cre (car x))
362 (cdr x)
363 `(ratf ,(cdr x))))
365 (defmvar *$any-modes* '($any $list))
367 (defun covers (mode1 mode2)
368 (cond ((eq mode1 mode2) t)
369 ((eq '$float mode1) (member mode2 '($float $fixnum $rational) :test #'eq))
370 ((eq '$number mode1) (member mode2 '($fixnum $float) :test #'eq))
371 ((member mode1 *$any-modes* :test #'eq) t)))
373 ;;; takes a function name as input.
375 (defun tr-mfun (name &aux (*transl-backtrace* nil))
376 (let ((def-form (consfundef name nil nil)))
377 (cond ((null def-form)
378 (tr-abort))
380 (tr-mdefine-toplevel def-form)))))
382 ;;; DEFUN
383 ;;; All the hair here to deal with macsyma fexprs has been flushed.
384 ;;; Right now this handles MDEFMACRO and MDEFINE. The decisions
385 ;;; of where to put the actual properties and what kind of
386 ;;; defuns to make (LEXPR EXPR for maclisp) are punted to the
387 ;;; macro package.
389 (defun tr-mdefine-toplevel (form &aux (and-restp nil))
390 (destructuring-let (((((name . flags) . args) body) (cdr form))
391 (a-args) kind out-forms)
393 (do ((args args (cdr args))
394 ;; array functions cannot be LEXPR-like. gee.
395 ;; there is no good reason for that, except for efficiency,
396 ;; and I know that efficiency was not a consideration.
397 (full-restricted-flag (or (eq name 'mqapply)
398 (member 'array flags :test #'eq))))
399 ((null args) (setq a-args (nreverse a-args)))
400 (let ((u (car args)))
401 (cond ((atom u)
402 (push u a-args))
403 ((and (not full-restricted-flag)
404 (not and-restp)
405 (eq (caar u) 'mlist)
406 (cdr u) (atom (cadr u)))
407 (push (cadr u) a-args)
408 (setq and-restp t))
410 (push tr-unique a-args)))))
413 (cond ((eq name 'mqapply)
414 ;; don't you love syntax!
415 ;; do a switch-a-roo here. Calling ourselves recursively
416 ;; like this allows all legal forms and also catches
417 ;; errors. However, certain generalizations are also
418 ;; allowed. They won't get passed the interpreter, but
419 ;; interesting things may happen here. Thats what you
420 ;; get from too much syntax, so don't sweat it.
422 ;; the allowed generalizations aren't necessarily subscripted
423 ;; functions, but we'll act like they are when determining
424 ;; the free vars to capture.
425 ;; don't sweat this either.
426 (let ((*tr-free-vars-to-capture* (union (cdar args) *tr-free-vars-to-capture*)))
427 (tr-mdefine-toplevel
428 `(,(car form) ,(car args)
429 ((lambda) ((mlist) ,@(cdr args)) ,body)))))
430 ((member tr-unique a-args :test #'eq)
431 ;; WHAT IS "BAD" ABOUT THE ARGUMENT LIST HERE ??
432 (tr-format (intl:gettext "error: unhandled argument list in function definition: ~:M~%") `((mlist),@args))
433 (tr-abort)
434 nil)
435 ((member (caar form) '(mdefine mdefmacro) :test #'eq)
436 (setq kind (cond ((eq (caar form) 'mdefmacro) 'macro)
437 ((member 'array flags :test #'eq) 'array)
438 (t 'func)))
439 (let* ((t-form
440 (tr-lambda `((lambda) ((mlist) ,@a-args) ,body)))
441 (desc-header
442 `(,name ,(car t-form) ,(caar form)
443 ,and-restp ,(eq kind 'array))))
444 (cond ((eq kind 'func)
445 (push-pre-transl-form
446 `(defmtrfun-external ,desc-header))
447 (and (not (member (car t-form) '($any nil) :test #'eq))
448 (putprop name (car t-form) 'function-mode)))
449 ((eq kind 'array)
450 (and (not (member (car t-form) '($any nil) :test #'eq))
451 (decmode-arrayfun name (car t-form)))))
453 (cond ((or *in-translate* (not $packagefile))
454 ; These are all properties which tell the
455 ; user that functions are in the environment,
456 ; and that also allow him to SAVE the functions.
457 (push `(defprop ,name t translated) out-forms)
458 (push `(add2lnc ',name $props) out-forms)
459 (cond ((eq '$all $savedef)
460 (push
461 `(add2lnc
462 '((,name ,@flags) ,@args)
463 ,(case kind
464 (array '$arrays)
465 (func '$functions)
466 (macro '$macros))) out-forms)))))
467 (cond ((eq '$all $savedef)
468 ;; For some reason one may want to save the
469 ;; interpreted definition even if in a PACKAGEFILE.
470 ;; not a good idea to use SAVEDEF anyway though.
471 (push `(mdefprop ,name
472 ((lambda) ((mlist) ,@args) ,body)
473 ,(case kind
474 (array 'aexpr)
475 (macro 'mmacro)
476 (func 'mexpr)))
477 out-forms)))
478 ;;once a function has been translated we want to make sure mfunction-call is eliminated.
479 (progn
480 (remprop (car desc-header) 'undefined-warnp)
481 (setf (get (car desc-header) 'once-translated) "I was once translated"))
482 `(progn
483 ,@(nreverse out-forms)
484 (defmtrfun ,desc-header ,@(cdr (cdr t-form))))))
486 (barfo '?)))))
488 (defun translate-function (name)
489 (bind-transl-state
490 (setq *in-translate* t)
491 (let ((lisp-def-form (tr-mfun name))
492 (delete-subr? (and (get name 'translated)
493 (not (get name 'expr)))))
494 (cond (tr-abort
495 (trfail name))
497 (if delete-subr? (remprop name 'subr))
498 (if (mget name 'trace) (macsyma-untrace name))
499 (if (not $savedef) (meval `(($remfunction) ,name)))
500 (handler-case (eval lisp-def-form)
501 (error (e)
502 (tr-abort)
503 (trfail name e)
504 (return-from translate-function nil)))
505 name)))))
507 (defun punt-to-meval (form &optional (mode '$any))
508 (cons mode `(meval ',form)))
510 (defun trfail (x &optional msg)
511 (tr-format (intl:gettext "Error: failed to translate ~:@M~%") x)
512 (when msg
513 (tr-format (intl:gettext "Message: ~A~%") msg))
514 nil)
516 (defun translate-and-eval-macsyma-expression (form)
517 ;; this is the hyper-random entry to the transl package!
518 ;; it is used by MLISP for TRANSLATE:TRUE "::=".
519 (bind-transl-state
520 (setq *in-translate* t)
521 ;; Use TRANSLATOR-EVAL so we don't have to lose badly by tracing EVAL
522 (translator-eval (translate-macexpr-toplevel form))))
524 (defun translator-eval (x)
525 (eval x))
527 ;; This basically tells the msetq def%tr to use defparameter instead
528 ;; of setq because we're doing a setq at top-level, which isn't
529 ;; specified by ANSI CL.
530 (defvar *macexpr-top-level-form-p* nil)
532 (defun translate-macexpr-toplevel (form &aux (*transl-backtrace* nil) tr-abort)
533 ;; there are very few top-level special cases, I don't
534 ;; think it would help the code any to generalize TRANSLATE
535 ;; to target levels.
537 ;; Except msetq at top-level is special for ANSI CL. See below.
538 (setq form (toplevel-optimize form))
539 (cond ((atom form) nil)
540 ((eq (caar form) '$eval_when)
541 (let ((whens (cadr form))
542 (body (cddr form)) tr-whens)
543 (setq whens (cond (($listp whens) (cdr whens))
544 ((atom whens) (list whens))
546 (tr-format (intl:gettext "error: 'eval_when' argument must be a list or atom; found: ~:M~%") (cadr form))
547 nil)))
548 (setq tr-whens (mapcar 'stripdollar whens))
549 (cond ((member '$translate whens :test #'eq)
550 (mapc 'meval body)))
551 (cond ((member '$loadfile whens :test #'eq)
552 `(progn
553 ,@(mapcar 'translate-macexpr-toplevel body)))
554 ((setq tr-whens (intersect tr-whens '(:compile-toplevel :load-toplevel :execute)))
555 `(eval-when
556 ,tr-whens
557 ,@(mapcar 'translate-macexpr-toplevel body)))
558 ((member '$compile whens :test #'eq)
559 ;; strictly for the knowledgeable user.
560 `(eval-when
561 (:compile-toplevel)
562 ,@(mapcar 'translate-macexpr-toplevel body))))))
563 ((member (caar form) translate-time-evalables :test #'eq)
564 (meval1 form)
565 `(eval-when
566 (:compile-toplevel :load-toplevel :execute)
567 (meval* ',form)))
568 ((member (caar form) '(mdefine mdefmacro) :test #'eq)
569 (let ((name (caaadr form))
570 (trl))
571 (tr-format (intl:gettext "note: translating ~:@M~%") name)
572 (setq trl (tr-mdefine-toplevel form))
573 (cond (tr-abort
574 (tr-format (intl:gettext "error: failed to translate ~:@M~%") name)
575 (tr-format (intl:gettext "note: keep going and hope for the best.~%"))
576 `(meval* ',form))
577 (t trl))))
578 ((eq 'mprogn (caar form))
579 ;; note that this ignores the $%% crock.
580 `(progn ,@(mapcar #'translate-macexpr-toplevel (cdr form))))
581 ((eq 'msetq (caar form))
582 ;; Toplevel msetq's should really be defparameter instead of
583 ;; setq for Common Lisp.
584 (let ((*macexpr-top-level-form-p* t))
585 (dtranslate form)))
586 ((eq '$define_variable (caar form))
587 ;; Toplevel msetq's should really be defparameter instead of
588 ;; setq for Common Lisp.
589 (let ((*macexpr-top-level-form-p* t))
590 (dtranslate form)))
592 (let ((t-form (dtranslate form)))
593 (cond (tr-abort
594 `(meval* ',form))
596 t-form))))))
600 (defmvar $tr_optimize_max_loop 100.
601 "The maximum number of times the macro-expansion and optimization
602 pass of the translator will loop in considering a form.
603 This is to catch macro expansion errors, and non-terminating
604 optimization properties.")
606 (defun toplevel-optimize (form)
607 ;; it is vital that optimizations be done within the
608 ;; context of variable meta bindings, declarations, etc.
609 ;; Also: think about calling the simplifier here.
610 (cond ((atom form)
611 (cond ((symbolp form)
612 ;; If this symbol has the constant property, then
613 ;; use its assigned constant value in place of the
614 ;; symbol.
615 (let ((v (getl (mget form '$props) '($constant))))
616 (if v (cadr v) form)))
617 (t form)))
619 (do ((new-form)
620 (kount 0 (1+ kount)))
621 ;; tailrecursion should always arrange for a counter
622 ;; to check for mobylossage.
623 ((> kount $tr_optimize_max_loop)
624 (tr-format (intl:gettext "warning: I've looped ~A times in macro expansion; just give up and return ~:@M~%")
625 $tr_optimize_max_loop (caar form))
626 form)
627 (setq new-form (toplevel-optimize-1 form))
628 (cond ((atom new-form)
629 (return (toplevel-optimize new-form)))
630 ((eq new-form form)
631 (return form))
633 (setq form new-form)))))))
635 (defun toplevel-optimize-1 (form &aux (op (car form)) prop)
636 (cond ((or (atom op)
637 (member 'array op :test #'eq)) form)
638 ((progn (setq op (car op))
639 (setq prop
640 (if $transrun ; crock a minute.
641 (or (get op 'translated-mmacro)
642 (mget op 'mmacro))
643 (or (mget op 'mmacro)
644 (get op 'translated-mmacro)))))
645 (mmacro-apply prop form))
646 ((setq prop ($get op '$optimize))
647 ;; interesting, the MAPPLY here causes the simplification
648 ;; of the form and the result.
649 ;; The optimize property can be used to implement
650 ;; such niceties as the $%% crock.
651 (mapply1 prop (list form) "an optimizer property" nil))
652 ((and ($get op '$transload)
653 (get op 'autoload)
654 ;; check for all reasonable definitions,
655 ;; $OPTIMIZE and MACRO already checked.
656 (not (or (get-lisp-fun-type op)
657 (getl op '(translate mfexpr* mfexpr*s
658 fsubr fexpr *fexpr
659 macro
660 ;; foobar?
662 (mgetl op '(mexpr)))))
663 (load-function op t)
664 ;; to loop.
665 (cons (car form) (cdr form)))
666 (t form)))
668 (defun translate (form)
669 (and *transl-debug* (push form *transl-backtrace*))
670 (setq form (toplevel-optimize form))
671 (and *transl-debug* (pop *transl-backtrace*))
672 (prog2
673 (and *transl-debug* (push form *transl-backtrace*))
674 (if (atom form)
675 (translate-atom form)
676 (translate-form form))
677 ;; hey boy, reclaim that cons, just don't pop it!
678 (and *transl-debug* (pop *transl-backtrace*))))
680 (defun translate-atom (form &aux temp)
681 (cond ((numberp form) (cons (tr-class form) form))
682 ((setq temp (assoc form boolean-object-table :test #'eq))
683 (cdr temp))
684 ((and (setq temp (mget form '$numer)) $tr_numer)
685 `($float . ,temp))
686 ((implied-quotep form)
687 `($any . ',form))
688 ((self-evaluating-lisp-object-p form)
689 `($any . ,form))
690 ((tboundp form)
691 (setq form (teval form))
692 `(,(value-mode form) . ,form))
694 (cond ((not (specialp form))
695 (warn-undefined-variable form)))
696 ;; note that the lisp analysis code must know that
697 ;; the TRD-MSYMEVAL form is a semantic variable.
698 (let* ((mode (value-mode form))
699 (init-val (assoc mode mode-init-value-table :test #'eq)))
700 (setq init-val (cond (init-val (cdr init-val))
701 (t `',form)))
702 ;; in the compiler TRD-MSYMEVAL doesn't do a darn
703 ;; thing, but it provides dynamic initialization of
704 ;; variables in interpreted code which is translated
705 ;; in-core. In FILE loaded code the DEFVAR will take
706 ;; care of this.
707 (push-defvar form init-val)
708 `(,mode . (trd-msymeval ,form ,init-val))))))
710 (defun translate-form (form &aux temp)
711 (cond ((eq (car form) 'meval) (cons '$any form)) ;;for those lispy macsyma forms
712 ((not (atom (caar form)))
713 ;; this is a check like that in the simplifier. form could
714 ;; result from substitution macros.
715 (translate `((mqapply) ,(caar form) . ,(cdr form))))
716 ((member 'array (cdar form) :test #'eq)
717 ;; dispatch this bad-boy to another module quick.
718 (tr-arraycall form))
719 ;; TRANSLATE properties have priority.
720 ((setq temp (get (caar form) 'translate))
721 (funcall temp form))
722 ((setq temp (get-lisp-fun-type (caar form)))
723 (tr-lisp-function-call form temp))
724 ((macsyma-special-macro-p (caar form))
725 (attempt-translate-random-macro-op form))
726 ((macsyma-special-op-p (caar form))
727 ;; a special form not handled yet! foobar!
728 (attempt-translate-random-special-op form))
729 ((or (get (caar form) 'noun) (get (caar form) 'operators))
730 ;; puntastical case. the weird ones are presumably taken care
731 ;; of by TRANSLATE properties by now.
732 (tr-infamous-noun-form form))
734 ;; "What does a macsyma function call mean?".
735 ;; By the way, (A:'B,B:'C,C:'D)$ A(3) => D(3)
736 ;; is not supported.
738 (tr-macsyma-user-function-call (caar form) (cdr form) form))))
742 (defmvar $tr_bound_function_applyp t)
744 (defun tr-macsyma-user-function-call (function args form)
745 ;; this needs some work, output load-time code to
746 ;; check for MMACRO properties, etc, to be really
747 ;; foolproof.
748 (cond ((eq $tr_function_call_default '$apply)
749 (translate `(($apply) ,(caar form) ((mlist) ,@(cdr form)))))
750 ((eq $tr_function_call_default '$expr)
751 (tr-lisp-function-call form 'subr))
752 ((eq $tr_function_call_default '$general)
753 (cond
754 ;;; G(F,X):=F(X+1); case.
755 ((and $tr_bound_function_applyp (tboundp function))
756 (let ((new-form `(($apply) ,function ((mlist) ,@args))))
757 (tr-format (intl:gettext "warning: ~:M is a bound variable in ~:M, but it is used as a function.~%") function form)
758 (tr-format (intl:gettext "note: instead I'll translate it as: ~:M~%") new-form)
759 (translate new-form)))
760 ;; MFUNCTION-CALL cleverely punts this question to a FSUBR in the
761 ;; interpreter, and a macro in the compiler. This is good style,
762 ;; if a user is compiling then assume he is less lossage prone.
764 (pushnew (caar form) *untranslated-functions-called*)
765 (call-and-simp
766 (function-mode (caar form))
767 'mfunction-call `(,(caar form) ,@(tr-args args))))))
769 ;; This case used to be the most common, a real loser.
770 (warn-meval form)
771 (punt-to-meval form (function-mode (caar form))))))
774 (defun attempt-translate-random-macro-op (form)
775 (warn-fexpr form)
776 `($any . ,(cons (caar form) (cdr form))))
778 (defun attempt-translate-random-special-op (form)
779 (warn-fexpr form)
780 (punt-to-meval form (function-mode (caar form))))
783 (defun tr-lisp-function-call (form type)
784 (let ((op (caar form)) (mode) (args))
785 (setq args (cond ((member type '(subr lsubr expr) :test #'eq)
786 (mapcar #'(lambda (llis) (dconvx (translate llis)))
787 (cdr form)))
789 (mapcar 'dtranslate (cdr form))))
790 mode (function-mode op))
791 (call-and-simp mode op args)))
793 ;;the once-translated is so that inside translate file where a function
794 ;;has been translated, subsequent calls won't use mfunction call
795 (defun get-lisp-fun-type (fun &aux temp)
796 ;; N.B. this is Functional types. NOT special-forms,
797 ;; lisp special forms are meaningless to macsyma.
798 (cond ((get fun '*lexpr) 'lsubr)
799 ((get fun '*expr) 'subr)
800 ;; *LEXPR & *EXPR gotten from DEFMFUN declarations
801 ;; which is loaded by TrData.
802 ((mget fun '$fixed_num_args_function)
803 'subr)
804 ((mget fun '$variable_num_args_function)
805 'lsubr)
806 ((setq temp (getl fun '(expr subr lsubr)))
807 (car temp))
808 ((get fun 'once-translated))
809 ((get fun 'translated))
810 (t nil)))
812 (defun tr-infamous-noun-form (form)
813 ;; 'F(X,Y) means noun-form. The arguments are evaluated.
814 ;; but the function is cons on, not applied.
815 ;; N.B. for special forms and macros this is totally wrong.
816 ;; But, those cases are filtered out already, presumably.
818 (let ((op (cond ((member 'array (car form) :test #'eq)
819 `(,(caar form) array))
820 (t `(,(caar form)))))
821 (args (tr-args (cdr form))))
822 `($any . (simplify (list ',op ,@args)))))
824 ;;; Some atoms, solely by usage, are self evaluating.
825 (defun implied-quotep (x)
826 (safe-get x 'implied-quotep))
828 (defun self-evaluating-lisp-object-p (x)
829 (not (or (symbolp x) (consp x))))
831 ;;; the Translation Properties. the heart of TRANSL.
833 ;;; This conses up the call to the function, adding in the
834 ;;; SIMPLIFY i the mode is $ANY. This should be called everywhere.
835 ;;; instead of duplicating the COND everywhere, as is done now in TRANSL.
837 (defun tr-nosimpp (op)
838 (cond ((atom op)
839 (get op 'tr-nosimp))
840 (t nil)))
842 (defun call-and-simp (mode fun args)
843 (cond ((or (not (eq mode '$any))
844 (tr-nosimpp fun))
845 `(,mode ,fun . ,args))
847 `(,mode simplify (,fun . ,args)))))
849 (defmspec $declare_translated (fns)
850 (setq fns (cdr fns))
851 (loop for v in fns
852 when (or (symbolp v) (and (stringp v) (setq v ($verbify v))))
853 do (setf (get v 'once-translated) t)
854 (pushnew v *declared-translated-functions*)
855 else do (merror (intl:gettext "declare_translated: arguments must be symbols or strings; found: ~:M") v)))
857 (def%tr $eval_when (form)
858 (tr-format (intl:gettext "error: found 'eval_when' in a function or expression: ~:M~%") form)
859 (tr-format (intl:gettext "note: 'eval_when' can appear only at the top level in a file.~%"))
860 (tr-abort)
861 '($any . nil))
863 (def%tr mdefmacro (form)
864 (tr-format (intl:gettext "warning: globally defining macro ~:M now to ensure correct macro expansions.~%") (caaadr form))
865 ; Define the macro now to ensure that it's defined when it's time
866 ; to expand it. It's a bug that this definition occurs during
867 ; translation without being cleaned it up afterward, but simply
868 ; removing this breaks things.
869 (meval form)
870 (punt-to-meval form))
872 (def%tr $local (form)
873 (when local
874 (tr-format (intl:gettext "error: there is already a 'local' in this block.~%"))
875 (tr-abort)
876 (return-from $local nil))
877 (setq local t)
878 ; We can't just translate to a call to MLOCAL here (which is
879 ; what used to happen). That would push onto LOCLIST and bind
880 ; MLOCP at the "wrong time". The push onto LOCLIST and the
881 ; binding of MLOCP are handled in TR-LAMBDA.
882 (punt-to-meval form))
885 (def%tr mquote (form)
886 (list (tr-class (cadr form)) 'quote (cadr form)))
889 (defun tr-lambda (form &optional (tr-body #'tr-seq) &rest tr-body-argl
890 &aux
891 (arglist (mparams (cadr form)))
892 (easy-assigns nil)
893 (local nil))
894 ;; This function is defined to take a simple macsyma lambda expression and
895 ;; return a simple lisp lambda expression. The optional TR-BODY hook
896 ;; can be used for translating other special forms that do lambda binding.
898 ;; Local SPECIAL declarations are not used because
899 ;; the multics lisp compiler does not support them. They are of course
900 ;; a purely syntactic construct that doesn't buy much. I have been
901 ;; advocating the use of DEFINE_VARIABLE in macsyma user programs so
902 ;; that the use of DECLARE(FOO,SPECIAL) will be phased out at that level.
904 (mapc #'tbind arglist)
905 (destructuring-let* (((mode . nbody) (apply tr-body (cddr form) tr-body-argl))
906 (local-declares (make-declares arglist t))
907 (body (if local
908 `((let ((mlocp t))
909 (push nil loclist)
910 (unwind-protect
911 (progn ,@nbody)
912 (munlocal))))
913 nbody)))
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 `(,mode . (lambda ,(tunbinds arglist)
928 ,local-declares
929 ,@easy-assigns
930 ,@body))
931 ;; Case with arbitrary ASSIGN's.
932 (let ((temps (mapcar #'(lambda (ign) ign (tr-gensym)) arglist)))
933 `(,mode . (lambda ,temps
934 (unwind-protect
935 (progn
936 ;; [1] Check before binding.
937 ,@(mapcan #'(lambda (var val)
938 (let ((assign (get var 'assign)))
939 (if assign
940 (let ((assign-fn (if (symbolp assign) `(quote ,assign) (coerce assign 'function))))
941 (list `(funcall ,assign-fn ',var ,val))))))
942 arglist temps)
943 ;; [2] do the binding.
944 ((lambda ,(tunbinds arglist)
945 ,local-declares
946 ,@body)
947 ,@temps))
948 ;; [2] check when unbinding too.
949 ,@(mapcan #'(lambda (var)
950 (let ((assign (get var 'assign)))
951 (if assign
952 (let ((assign-fn (if (symbolp assign) `(quote ,assign) (coerce assign 'function))))
953 (list `(funcall ,assign-fn ',var
954 ;; use DTRANSLATE to
955 ;; catch global
956 ;; scoping if any.
957 ,(dtranslate var)))))))
958 arglist))))))))
961 (defun make-declares (varlist localp &aux (dl) (fx) (fl) specs)
962 (do ((l varlist (cdr l))
963 (mode) (var))
964 ((null l))
966 ;; When a variable is declared special, be sure to declare it
967 ;; special here.
968 (when (and localp (tr-get-special (car l)))
969 (push (car l) specs))
971 (when (or (not localp)
972 (not (tr-get-special (car l))))
973 ;; don't output local declarations on special variables.
974 (setq var (teval (car l)) mode (value-mode var))
975 (setq specs (cons var specs))
977 (cond ((eq '$fixnum mode) (pushnew var fx :test #'eq))
978 ((eq '$float mode) (pushnew var fl :test #'eq)))))
979 (if fx (pushnew `(fixnum . ,fx) dl :test #'eq))
980 (if fl (pushnew `(type flonum . ,fl) dl :test #'eq))
981 (if specs (pushnew `(special . ,specs) dl :test #'eq))
982 (if dl `(declare . ,dl)))
984 (defun tr-seq (l)
985 (do ((mode nil)
986 (body nil))
987 ((null l)
988 (cons mode (nreverse body)))
989 (let ((exp (translate (pop l))))
990 (setq mode (car exp))
991 (push (cdr exp) body))))
993 (def%tr mprogn (form)
994 (setq form (tr-seq (cdr form)))
995 (cons (car form) `(progn ,@(cdr form))))
997 (defun go-tag-p (e)
998 (or (symbolp e) (integerp e)))
1000 (def%tr mprog (form)
1001 (let (arglist body val-list)
1002 ;; [1] normalize the MPROG syntax.
1003 (cond (($listp (cadr form))
1004 (setq arglist (cdadr form)
1005 body (cddr form)))
1007 (setq arglist nil
1008 body (cdr form))))
1009 (cond ((null body)
1010 (setq body '(((mquote) $done)))))
1011 (setq val-list (mapcar #'(lambda (u)
1012 (if (atom u) u
1013 (translate (caddr u))))
1014 arglist)
1015 arglist (mapcar #'(lambda (u)
1016 ;; X or ((MSETQ) X Y)
1017 (if (atom u) u (cadr u)))
1018 arglist))
1019 (let ((dup (find-duplicate arglist :test #'eq)))
1020 (when dup
1021 (tr-format (intl:gettext "error: ~M occurs more than once in block variable list") dup)
1022 (tr-abort)
1023 (return-from mprog nil)))
1024 (setq form
1025 (tr-lambda
1026 ;; [2] call the lambda translator.
1027 `((lambda) ((mlist) ,@arglist) ,@body)
1028 ;; [3] supply our own body translator.
1029 #'tr-mprog-body
1030 val-list
1031 arglist))
1032 (cons (car form) `(,(cdr form) ,@val-list))))
1034 (defun tr-mprog-body (body val-list arglist
1035 &aux
1036 (inside-mprog t)
1037 (return-mode nil)
1038 (need-prog? nil)
1039 (returns nil) ;; not used but must be bound.
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 ((go-tag-p 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 (go-tag-p (cadr form)))
1102 (tr-format (intl:gettext "warning: 'go' tag must be a symbol or an integer: ~: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 (let ((args (tr-args args))
1118 (fn (translate fn)))
1119 (cons (car fn) `(mfuncall ,(cdr fn) ,@args))))
1120 ((not aryp)
1121 `($any simplify (mapply ,(dconvx (translate fn))
1122 (list ,@(tr-args args))
1123 ',fn)))
1125 (warn-meval form)
1126 (punt-to-meval form)))))
1128 (defun mcond-eval-symbols-tr (form)
1129 (mcond-eval-symbols #'maybe-msymeval form))
1131 (def%tr mcond (form)
1132 (let ((g (tr-gensym))
1133 (nl nil)
1134 (mode nil))
1135 (do ((l (cdr form) (cddr l))) ((null l))
1136 ; Optimize the else-if case: if we're at the else case at the end
1137 ; and the body is just another conditional, then we just continue
1138 ; directly with the clauses of the inner conditional instead of
1139 ; nesting.
1140 (when (and (null (cddr l))
1141 (eq (car l) t)
1142 (consp (cadr l))
1143 (eq (caaadr l) 'mcond))
1144 (setq l (cdadr l)))
1145 (let ((wrap-a-pred 'mcond))
1146 (declare (special wrap-a-pred))
1147 (destructuring-let (((pred-mode . pred-tr) (translate-predicate (car l)))
1148 ((body-mode . body-tr) (translate (cadr l))))
1149 (setq mode (*union-mode mode body-mode))
1150 (if (eq pred-mode '$boolean)
1151 (setq nl (list* body-tr pred-tr nl))
1152 (setq nl (list* `(list* '(mcond) ,g (mapcar #'mcond-eval-symbols-tr ',(cdr l)))
1153 `(not (null ,g))
1154 body-tr
1155 `(eq t (setq ,g ,pred-tr))
1156 nl))))))
1157 ; We leave off the final clause if the condition is true
1158 ; and the consequent is false.
1159 (when (and (eq t (cadr nl)) (null (car nl)))
1160 (setq nl (cddr nl)))
1161 (setq form nil)
1162 (do ((l nl (cddr l))) ((null l))
1163 (setq form
1164 (cons (cons (cadr l)
1165 (cond ((and (not (atom (car l)))
1166 (cdr l)
1167 (eq (caar l) 'progn))
1168 (cdar l))
1169 ((and (equal (car l) (cadr l))
1170 (atom (car l))) nil)
1171 (t (list (car l)))))
1172 form)))
1173 (if (among g form)
1174 (cons '$any `(let (,g) (cond ,@form)))
1175 (cons mode `(cond ,@form)))))
1177 ;; The MDO and MDOIN translators should be changed to use the TR-LAMBDA.
1178 ;; Perhaps a mere expansion into an MPROG would be best.
1180 (def%tr mdo (form)
1181 (let (returns assigns return-mode (inside-mprog t) need-prog?)
1182 (let (mode var init next test-form action varmode)
1183 (setq var (cond ((cadr form)) (t 'mdo)))
1184 (tbind var)
1185 (setq init (if (caddr form) (translate (caddr form)) '($fixnum . 1)))
1186 (cond ((not (setq varmode (tr-get-mode var)))
1187 (declvalue var (car init) t)))
1188 (setq next (translate (cond ((cadddr form) (list '(mplus) (cadddr form) var))
1189 ((car (cddddr form)))
1190 (t (list '(mplus) 1 var)))))
1191 (setq form (copy-list form))
1192 ;;to make the end test for thru be numberp if the index is numberp
1193 ;;and to eliminate reevaluation
1194 (cond ((not varmode)
1195 (declvalue var (*union-mode (car init) (car next)) t))
1197 (warn-mode var varmode (*union-mode (car init) (car next)))))
1198 (destructuring-bind (test-mode . test-pred)
1199 (translate-predicate
1200 (list '(mor)
1201 (cond ((null (cadr (cddddr form))) nil)
1202 ((and (cadddr form)
1203 (mnegp ($numfactor (simplify (cadddr form)))))
1204 (list '(mlessp) var (cadr (cddddr form))))
1205 (t (list '(mgreaterp) var (cadr (cddddr form)))))
1206 (caddr (cddddr form))))
1207 (if (eq test-mode '$boolean)
1208 (setq test-form test-pred)
1209 (setq test-form `(let (($prederror t)) ,test-pred))))
1210 (setq action (translate (cadddr (cddddr form)))
1211 mode (cond ((null returns) '$any)
1212 (t return-mode)))
1213 (setq var (tunbind (cond ((cadr form)) (t 'mdo))))
1214 `(,mode do ((,var ,(cdr init) ,(cdr next)))
1215 (,test-form '$done) . ((declare (special ,var)) .
1216 ,(cond ((atom (cdr action)) nil)
1217 ((eq 'progn (cadr action)) (cddr action))
1218 (t (list (cdr action)))))))))
1220 (def%tr mdoin (form)
1221 (let (returns assigns return-mode (inside-mprog t) need-prog?)
1222 (prog (mode var init action)
1223 (setq var (tbind (cadr form))) (tbind 'mdo)
1224 (setq init (dtranslate (caddr form)))
1225 (cond ((or (cadr (cddddr form)) (caddr (cddddr form)))
1226 (tunbind 'mdo) (tunbind (cadr form))
1227 (return (punt-to-meval `((mdoin) . ,(cdr form))))))
1228 (setq action (translate (cadddr (cddddr form)))
1229 mode (cond ((null returns) '$any)
1230 (t return-mode)))
1231 (tunbind 'mdo) (tunbind (cadr form))
1232 (return
1233 `(,mode do ((,var) (mdo (cdr ,init) (cdr mdo)))
1234 ((null mdo) '$done) .
1235 ((declare (special ,var)) (setq ,var (car mdo)) .
1236 ,(cond ((atom (cdr action)) nil)
1237 ((eq 'progn (cadr action)) (cddr action))
1238 (t (list (cdr action))))))))))
1241 (defun lambda-wrap1 (tn val form)
1242 (if (or (atom val)
1243 (eq (car val) 'quote))
1244 (subst val tn form)
1245 `((lambda (,tn) ,form) ,val)))
1247 (def%tr msetq (form)
1248 (let ((var (cadr form))
1249 (val (caddr form))
1250 assign
1251 mode)
1252 (cond ((atom var)
1253 (setq mode (value-mode var) val (translate val))
1254 (warn-mode var mode (car val))
1255 (if (eq '$any mode)
1256 (setq mode (car val) val (cdr val))
1257 (setq val (dconv val mode)))
1258 (cons mode
1259 (if (setq assign (get var 'assign))
1260 (let ((tn (tr-gensym))
1261 (assign-fn (if (symbolp assign) `(quote ,assign) (coerce assign 'function))))
1262 (lambda-wrap1 tn val `(let nil
1263 (declare (special ,var ,(teval var)))
1264 (funcall ,assign-fn ',var ,tn)
1265 (setq ,(teval var) ,tn))))
1266 `(let nil (declare (special ,(teval var)))
1267 (if (not (boundp ',(teval var)))
1268 (add2lnc ',(teval var) $values))
1269 (,(if *macexpr-top-level-form-p*
1270 'defparameter
1271 'setq)
1272 ,(teval var) ,val)))))
1273 ((member 'array (car var) :test #'eq)
1274 (tr-arraysetq var val))
1276 (unless (safe-get (caar var) 'mset_extension_operator)
1277 (tr-format (intl:gettext "warning: no assignment operator known for ~:M~%") var)
1278 (tr-format (intl:gettext "note: just keep going and hope for the best.~%")))
1279 (setq val (translate val))
1280 `(,(car val) mset ',var ,(cdr val))))))
1282 (def%tr $max (x) (translate-$max-$min x))
1283 (def%tr $min (x) (translate-$max-$min x))
1284 (def%tr %max (x) (translate-$max-$min x))
1285 (def%tr %min (x) (translate-$max-$min x))
1287 (defun translate-$max-$min (form)
1288 (let ((mode) (arglist) (op (stripdollar (caar form))))
1289 (setq arglist
1290 (mapcar (lambda (l)
1291 (setq l (translate l))
1292 (cond ((null mode)
1293 (setq mode (car l)))
1294 ((eq mode (car l)))
1296 (setq mode '$any)))
1298 (cdr form)))
1299 ; To match the interpreted case, and to make sure we use the
1300 ; correct mode for the return value, we do not apply float
1301 ; contagion to the arguments and we use a special translation
1302 ; to call MAX or MIN only when every argument has the same
1303 ; mode (either all fixnum or all float). CLHS says that
1304 ; implementations have choices they can make about what MAX
1305 ; and MIN return when the arguments are a mix of float and
1306 ; rational types.
1307 ; Example: if an implementation decides to apply float contagion
1308 ; to the arguments of MAX (MIN), then it can return either an
1309 ; integer or a float if the greatest (least) argument was an
1310 ; integer.
1311 (if (member mode '($fixnum $float) :test #'eq)
1312 `(,mode ,(if (eq 'min op) 'min 'max) . ,(mapcar 'cdr arglist))
1313 `($any ,(if (eq 'min op) '$lmin '$lmax)
1314 (list '(mlist) . ,(mapcar 'dconvx arglist))))))
1317 ;;; mode accessing, binding, handling. Super over-simplified.
1319 (defun tr-class (x)
1320 (cond ((integerp x) '$fixnum)
1321 ((floatp x) '$float)
1322 ((member x '(t nil) :test #'eq) '$boolean)
1323 ((atom x) '$any)
1324 ((eq 'rat (caar x)) '$rational)
1325 (t '$any)))
1327 (defun *union-mode (mode1 mode2)
1328 (cond ((eq mode1 mode2) mode1)
1329 ((null mode1) mode2)
1330 ((null mode2) mode1)
1331 ((eq '$boolean mode1) '$any)
1332 ((eq '$boolean mode2) '$any)
1333 ((member mode2 *$any-modes* :test #'eq) '$any)
1334 ((member mode1 *$any-modes* :test #'eq) '$any)
1335 ((eq '$fixnum mode1) mode2)
1336 ((eq '$float mode1)
1337 (if (eq '$number mode2) '$number '$float))
1338 ((eq '$rational mode1)
1339 (if (eq '$float mode2) '$float '$any))
1340 ((eq '$number mode1)
1341 (if (eq '$rational mode2) '$any '$number))
1342 (t '$any)))
1344 (defun value-mode (var)
1345 (cond ((tr-get-mode var))
1347 (warn-undeclared var)
1348 '$any)))
1350 (defun decmode-arrayfun (f m)
1351 (putprop f m 'arrayfun-mode))
1353 (defun array-mode (ar)
1354 (cond ((get ar 'array-mode)) (t '$any)))
1356 (defun arrayfun-mode (ar)
1357 (cond ((get ar 'arrayfun-mode)) (t '$any)))
1359 (defun function-mode (f)
1360 (cond ((get f 'function-mode)) (t '$any)))
1362 (defun function-mode-@ (f)
1363 (ass-eq-ref (tr-get-val-modes f) 'function-mode '$any))
1365 (defun array-mode-@ (f)
1366 (ass-eq-ref (tr-get-val-modes f) 'array-mode '$any))
1369 (defvar $tr_bind_mode_hook nil
1370 "A hack to allow users to key the modes of variables
1371 off of variable spelling, and other things like that.")
1373 ;; TBIND, below, copies the MODE, VAL-MODES, and SPECIAL properties
1374 ;; into the a table named TSTACK, and then removes those properties.
1375 ;; So if TBIND has been called, we will need to look for those
1376 ;; properties in TSTACK instead of the symbol property list.
1378 (defstruct (tstack-slot (:conc-name tstack-slot-))
1379 mode
1380 tbind
1381 val-modes
1382 ;; an alist telling second order info
1383 ;; about APPLY(VAR,[X]), ARRAYAPPLY(F,[X]) etc.
1384 special)
1386 (defun tr-get-mode (a)
1387 (if (get a 'tbind)
1388 (let ((my-slot (cdr (assoc a tstack))))
1389 (tstack-slot-mode my-slot))
1390 (get a 'mode)))
1392 #-gcl (defun (setf tr-get-mode) (b a)
1393 (if (get a 'tbind)
1394 (let ((my-slot (cdr (assoc a tstack))))
1395 (setf (tstack-slot-mode my-slot) b))
1396 (setf (get a 'mode) b)))
1398 #+gcl (defsetf tr-get-mode (a) (b)
1399 `(if (get ,a 'tbind)
1400 (let ((my-slot (cdr (assoc ,a tstack))))
1401 (setf (tstack-slot-mode my-slot) ,b))
1402 (setf (get ,a 'mode) ,b)))
1404 (defun tr-get-val-modes (a)
1405 (if (get a 'tbind)
1406 (let ((my-slot (cdr (assoc a tstack))))
1407 (tstack-slot-val-modes my-slot))
1408 (get a 'val-modes)))
1410 #-gcl (defun (setf tr-get-val-modes) (b a)
1411 (if (get a 'tbind)
1412 (let ((my-slot (cdr (assoc a tstack))))
1413 (setf (tstack-slot-val-modes my-slot) b))
1414 (setf (get a 'val-modes) b)))
1416 #+gcl (defsetf tr-get-val-modes (a) (b)
1417 `(if (get ,a 'tbind)
1418 (let ((my-slot (cdr (assoc ,a tstack))))
1419 (setf (tstack-slot-val-modes my-slot) ,b))
1420 (setf (get ,a 'val-modes) ,b)))
1422 (defun tr-get-special (a)
1423 (if (get a 'tbind)
1424 (let ((my-slot (cdr (assoc a tstack))))
1425 (tstack-slot-special my-slot))
1426 (get a 'special)))
1428 #-gcl (defun (setf tr-get-special) (b a)
1429 (if (get a 'tbind)
1430 (let ((my-slot (cdr (assoc a tstack))))
1431 (setf (tstack-slot-special my-slot) b))
1432 (setf (get a 'special) b)))
1434 #+gcl (defsetf tr-get-special (a) (b)
1435 `(if (get ,a 'tbind)
1436 (let ((my-slot (cdr (assoc ,a tstack))))
1437 (setf (tstack-slot-special my-slot) ,b))
1438 (setf (get ,a 'special) ,b)))
1440 ;;; should be a macro (TBINDV <var-list> ... forms)
1441 ;;; so that TUNBIND is assured, and also so that the stupid ASSQ doesn't
1442 ;;; have to be done on the darn TSTACK. This will have to wait till
1443 ;;; the basic special form translation properties are rewritten.
1445 (defun variable-p (var)
1446 (and var (symbolp var) (not (eq var t))))
1448 (defun bad-var-warn (var)
1449 (tr-format (intl:gettext "warning: ~:M cannot be used as a variable.~%") var))
1451 (defun tbind (var &aux old)
1452 (cond ((variable-p var)
1453 (setq old (make-tstack-slot :mode (get var 'mode)
1454 :tbind (get var 'tbind)
1455 :val-modes (get var 'val-modes)
1456 :special (get var 'special)))
1457 (push (cons var old) tstack)
1458 (cond ((not (specialp var))
1459 ;; It is the lisp convention in use to inherit
1460 ;; specialness from higher context.
1461 ;; Spurious MODEDECLARATIONS get put in the environment
1462 ;; when code is MEVAL'd since there is no way to stack
1463 ;; the mode properties. Certainly nobody is willing
1464 ;; to hack MEVAL in JPG;MLISP
1465 (remprop var 'val-modes)
1466 (remprop var 'mode)
1467 (remprop var 'special)))
1468 (putprop var var 'tbind)
1469 (if $tr_bind_mode_hook
1470 (let ((mode? (mapply $tr_bind_mode_hook
1471 (list var)
1472 '$tr_bind_mode_hook)))
1473 (if mode? (tr-declare-varmode var mode?))))
1474 var)
1476 (bad-var-warn var))))
1478 (defun tunbind (var &aux (old (assoc var tstack :test #'eq)))
1479 (when (variable-p var)
1480 (prog1
1481 (teval var)
1482 (cond (old
1483 (setq tstack (delete old tstack :test #'eq)) ; POP should be all we need.
1484 (setq old (cdr old))
1485 (putprop1 var (tstack-slot-mode old) 'mode)
1486 (putprop1 var (tstack-slot-tbind old) 'tbind)
1487 (putprop1 var (tstack-slot-val-modes old) 'val-modes)
1488 (putprop1 var (tstack-slot-special old) 'special))))))
1490 (defun putprop1 (name value key)
1491 ;; leaves property list clean after unwinding, this
1492 ;; is an efficiency/storage issue only.
1493 (if value
1494 (putprop name value key)
1495 (progn
1496 (remprop name key)
1497 nil)))
1499 (defun tunbinds (l)
1500 (do ((nl))
1501 ((null l) nl)
1502 (setq nl (cons (tunbind (caar tstack)) nl)
1503 l (cdr l))))
1505 (defun tboundp (var)
1506 ;; really LEXICAL-VARP.
1507 (and (symbolp var) (get var 'tbind) (not (tr-get-special var))))
1509 (defun teval (var)
1510 (or (and (symbolp var) (get var 'tbind)) var))
1512 ;; Local Modes:
1513 ;; Mode: LISP
1514 ;; Comment Col: 40
1515 ;; END: