Translated if now honors prederror
[maxima.git] / src / transl.lisp
blob850b1aa61853238dab87ffa31e7205c425f69750
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1980 Massachusetts Institute of Technology ;;;
9 ;;; GJC 9:29am Saturday, 5 April 1980 ;;;
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 (in-package :maxima)
14 (macsyma-module transl)
16 ;;; File directory.
18 ;;; TRANSL Driver. Basic translation properties.
19 ;;; TRANSS User-interaction, FILE-I/O etc.
20 ;;; TRANS1 Translation of JPG;MLISP and other FSUBRS.
21 ;;; which take call-by-name parameters.
22 ;;; TRANS2 LISTS, ARRAYs, other random operators.
23 ;;; TRANS3 LAMBDA. CLOSURES. also used by fsubr call-by-name
24 ;;; compatibility package.
25 ;;; TRANS4 operators, ".", "^^" some functions such as GAMMA.
26 ;;; TRANS5 FSUBRS from COMM, and others, these are mere MACRO
27 ;;; FSUBRS.
28 ;;; TRANSF floating point intensive properties. BIGFLOAT stuff.
29 ;;; TROPER Basic OPERATORS.
30 ;;; TRUTIL transl utilities.
31 ;;; TRMODE definition of MODEDECLARE. run time error checking code.
32 ;;; TRDATA this is the MODE data for the "built-in" functions.
33 ;;; TRANSM This defines the macro DEF%TR. When compiled on MC
34 ;;; DEF%TR produces autoload definitions for TRANS1 thru L.
35 ;;; PROCS macro's needed.
36 ;;; TRANSQ these are macros for translated code. Loaded by TPRELU
37 ;;; this is compile-time only.
38 ;;; MDEFUN contains the macro which defines macsyma functions.
39 ;;; runtime and compile-time.
40 ;;; ACALL is some run time support for translated code, array calls.
41 ;;; FCALL run-time translated function call support for uncompiled
42 ;;; code. Many FSUBRS which are macros in TRANSQ.
43 ;;; EVALW EVAL-WHEN definition for interpreter.
44 ;;; MLOAD This has a hack hook into BATCH, which is needed to do
45 ;;; TRANSLATE_FILE I/O. when using old-i/o SUPRV.
48 ;;; Functions and literals have various MODE properties;;; >
49 ;;; (at user level set up by $MODEDECLARE), such as "$FLOAT" and "$ANY".
50 ;;; The main problem solved by this translator (and the reason that
51 ;;; it works on forms from the "inside out" as an evaluator would do
52 ;;; (expect for macro forms)), is the problem of type (MODE) dependent
53 ;;; function calling and mode conversion. The function TRANSLATE
54 ;;; returns a list where the CAR of the list is the MODE of the
55 ;;; expression and the CDR is the expression to be evaluated by
56 ;;; the lisp evaluator to give the equivalent result of evaluating
57 ;;; the given macsyma expression with the macsyma evaluator.
58 ;;; One doesn't know the MODE of an expression until seeing the modes
59 ;;; of all its parts. See "*UNION-MODE"
61 ;;; weak points in the code
62 ;;; [1] duplication of functionality in the translators for
63 ;;; MPLUS MTIMES etc.
64 ;;; [3] primitive mode scheme. lack of even the most primitive general
65 ;;; type coercion code. Most FORTRAN compilers are better than this.
66 ;;; [4] for a compiler, this code SUCKS when it comes to error checking
67 ;;; of the code it is munging. It doesn't even do a WNA check of system
68 ;;; functions!
69 ;;; [5]
70 ;;; The duplication of the code which handles lambda binding, in MDO, MDOIN
71 ;;; TR-LAMBDA, and MPROG, is very stupid. For macsyma this is one of
72 ;;; the hairier things. Declarations must be handled, ASSIGN properties...
73 ;;; -> Binding of ASSIGN properties should be handled with he "new"
74 ;;; UNWIND-PROTECT instead of at each RETURN, and at "hope" points such as
75 ;;; the ERRLIST. {Why wasn't this obvious need for UNWIND-PROTECT made
76 ;;; known to the lisp implementers by the macsyma implementers? Why did it
77 ;;; have to wait for the lisp machine group? Isn't this just a generalization
78 ;;; of special binding?}
79 ;;; [6] the DCONVX idea here is obscurely coded, incomplete, and totally
80 ;;; undocumented. It was probably an attempt to hack efficient
81 ;;; internal representations (internal to a given function), for some macsyma
82 ;;; data constructs, and yet still be sure that fully general legal data
83 ;;; frobs are seen outside of the functions. Note: this can be done
84 ;;; simply by type coercion and operator folding.
86 ;;; General comments on the structure of the code.
87 ;;; A function named TR-<something> means that it translates
88 ;;; something having to do with that something.
89 ;;; N.B. It does not mean that that is the translate property for <something>.
92 (defvar *untranslated-functions-called* nil)
94 (defmvar *declared-translated-functions* nil
95 "List of functions which are believed to be translated.")
97 (defmvar tstack nil " stack of local variable modes ")
99 (defmvar local nil "T if a $local statement is in the body.")
100 (defmvar tr-progret t)
101 (defmvar inside-mprog nil)
102 (defmvar returns nil "list of `translate'd return forms in the block.")
103 (defmvar return-mode nil "the highest(?) mode of all the returns.")
104 (defmvar need-prog? nil)
105 (defmvar assigns nil "These are very-special variables which have a Maxima
106 assign property which must be called to bind and unbind the variable
107 whenever it is `lambda' bound.")
109 (defmvar translate-time-evalables
110 '($modedeclare $alias $declare $infix $nofix $declare_translated
111 $matchfix $prefix $postfix $compfile))
113 (defmvar *transl-backtrace* nil
114 " What do you think? ")
116 (defmvar *transl-debug* nil "if T it pushes `backtrace' and `trace' ")
118 (defmvar tr-abort nil "set to T if abortion is requested by any of the
119 sub-parts of the translation. A *THROW would be better, although it
120 wouldn't cause the rest of the translation to continue, which may
121 be useful in translation for MAXIMA-ERROR checking.")
123 (defmvar tr-unique (gensym)
124 "this is just a unque object used for random purposes,
125 such as the second (file end) argument of READ.")
128 (defmvar $tr_warn_undeclared '$compile
129 "When to send warnings about undeclared variables to the TTY")
131 (defmvar $tr_warn_meval '$compfile
132 "If `meval' is called that indicates problems in the translation")
134 (defmvar $tr_warn_fexpr
135 '$compfile
136 "FEXPRS should not normally be output in translated code, all legitimate
137 special program forms are translated.")
139 (defmvar $tr_warn_mode '$all
140 "Warn when variables are assigned values out of their mode.")
142 (defmvar $tr_warn_undefined_variable '$all
143 "Warn when undefined global variables are seen.")
146 (defmvar *warned-un-declared-vars* nil "Warning State variable")
147 (defmvar *warned-fexprs* nil "Warning State variable")
148 (defmvar *warned-mode-vars* nil "Warning State variable")
150 (defmvar $tr_function_call_default '$general
152 FALSE means punt to MEVAL, EXPR means assume lisp fixed arg function.
153 GENERAL, the default gives code good for mexprs and mlexprs but not macros.
154 GENERAL assures variable bindings are correct in compiled code.
155 In GENERAL mode, when translating F(X), if F is a bound variable, then
156 it assumes that APPLY(F,[X]) is meant, and translates a such, with
157 appropriate warning. There is no need to turn this off.
158 APPLY means like APPLY.")
160 (defmvar $tr_array_as_ref t
161 "If true runtime code uses value of the variable as the array.")
163 (defmvar $tr_numer nil
164 "If `true' numer properties are used for atoms which have them, e.g. %pi")
166 (defvar *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 insetad
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 #+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 ((implied-quotep form)
689 `($any . ',form))
690 ((self-evaluating-lisp-object-p form)
691 `($any . ,form))
692 ((tboundp form)
693 (setq form (teval form))
694 `(,(value-mode form) . ,form))
696 (cond ((not (specialp form))
697 (warn-undefined-variable form)))
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 ((macsyma-special-macro-p (caar form))
727 (attempt-translate-random-macro-op form))
728 ((macsyma-special-op-p (caar form))
729 ;; a special form not handled yet! foobar!
730 (attempt-translate-random-special-op form))
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 (punt-to-meval form (function-mode (caar form))))))
776 (defun attempt-translate-random-macro-op (form)
777 (warn-fexpr form)
778 `($any . ,(cons (caar form) (cdr form))))
780 (defun attempt-translate-random-special-op (form)
781 (warn-fexpr form)
782 (punt-to-meval form (function-mode (caar form))))
785 (defun tr-lisp-function-call (form type)
786 (let ((op (caar form)) (mode) (args))
787 (setq args (cond ((member type '(subr lsubr expr) :test #'eq)
788 (mapcar #'(lambda (llis) (dconvx (translate llis)))
789 (cdr form)))
791 (mapcar 'dtranslate (cdr form))))
792 mode (function-mode op))
793 (call-and-simp mode op args)))
795 ;;the once-translated is so that inside translate file where a function
796 ;;has been translated, subsequent calls won't use mfunction call
797 (defun get-lisp-fun-type (fun &aux temp)
798 ;; N.B. this is Functional types. NOT special-forms,
799 ;; lisp special forms are meaningless to macsyma.
800 (cond ((get fun '*lexpr) 'lsubr)
801 ((get fun '*expr) 'subr)
802 ;; *LEXPR & *EXPR gotten from DEFMFUN declarations
803 ;; which is loaded by TrData.
804 ((mget fun '$fixed_num_args_function)
805 'subr)
806 ((mget fun '$variable_num_args_function)
807 'lsubr)
808 ((setq temp (getl fun '(expr subr lsubr)))
809 (car temp))
810 ((get fun 'once-translated))
811 ((get fun 'translated))
812 (t nil)))
814 (defun tr-infamous-noun-form (form)
815 ;; 'F(X,Y) means noun-form. The arguments are evaluated.
816 ;; but the function is cons on, not applied.
817 ;; N.B. for special forms and macros this is totally wrong.
818 ;; But, those cases are filtered out already, presumably.
820 (let ((op (cond ((member 'array (car form) :test #'eq)
821 `(,(caar form) array))
822 (t `(,(caar form)))))
823 (args (tr-args (cdr form))))
824 `($any . (simplify (list ',op ,@args)))))
826 ;;; Some atoms, solely by usage, are self evaluating.
827 (defun implied-quotep (x)
828 (safe-get x 'implied-quotep))
830 (defun self-evaluating-lisp-object-p (x)
831 (not (or (symbolp x) (consp x))))
833 ;;; the Translation Properties. the heart of TRANSL.
835 ;;; This conses up the call to the function, adding in the
836 ;;; SIMPLIFY i the mode is $ANY. This should be called everywhere.
837 ;;; instead of duplicating the COND everywhere, as is done now in TRANSL.
839 (defun tr-nosimpp (op)
840 (cond ((atom op)
841 (get op 'tr-nosimp))
842 (t nil)))
844 (defun call-and-simp (mode fun args)
845 (cond ((or (not (eq mode '$any))
846 (tr-nosimpp fun))
847 `(,mode ,fun . ,args))
849 `(,mode simplify (,fun . ,args)))))
851 (defmspec $declare_translated (fns)
852 (setq fns (cdr fns))
853 (loop for v in fns
854 when (or (symbolp v) (and (stringp v) (setq v ($verbify v))))
855 do (setf (get v 'once-translated) t)
856 (pushnew v *declared-translated-functions*)
857 else do (merror (intl:gettext "declare_translated: arguments must be symbols or strings; found: ~:M") v)))
859 (def%tr $eval_when (form)
860 (tr-format (intl:gettext "error: found 'eval_when' in a function or expression: ~:M~%") form)
861 (tr-format (intl:gettext "note: 'eval_when' can appear only at the top level in a file.~%"))
862 (tr-abort)
863 '($any . nil))
865 (def%tr mdefmacro (form)
866 (tr-format (intl:gettext "warning: globally defining macro ~:M now to ensure correct macro expansions.~%") (caaadr form))
867 ; Define the macro now to ensure that it's defined when it's time
868 ; to expand it. It's a bug that this definition occurs during
869 ; translation without being cleaned it up afterward, but simply
870 ; removing this breaks things.
871 (meval form)
872 (punt-to-meval form))
874 (def%tr $local (form)
875 (when local
876 (tr-format (intl:gettext "error: there is already a 'local' in this block.~%"))
877 (tr-abort)
878 (return-from $local nil))
879 (setq local t)
880 ; We can't just translate to a call to MLOCAL here (which is
881 ; what used to happen). That would push onto LOCLIST and bind
882 ; MLOCP at the "wrong time". The push onto LOCLIST and the
883 ; binding of MLOCP are handled in TR-LAMBDA.
884 (punt-to-meval form))
887 (def%tr mquote (form)
888 (list (tr-class (cadr form)) 'quote (cadr form)))
891 (defun tr-lambda (form &optional (tr-body #'tr-seq) &rest tr-body-argl
892 &aux
893 (arglist (mparams (cadr form)))
894 (easy-assigns nil)
895 (local nil))
896 ;; This function is defined to take a simple macsyma lambda expression and
897 ;; return a simple lisp lambda expression. The optional TR-BODY hook
898 ;; can be used for translating other special forms that do lambda binding.
900 ;; Local SPECIAL declarations are not used because
901 ;; the multics lisp compiler does not support them. They are of course
902 ;; a purely syntactic construct that doesn't buy much. I have been
903 ;; advocating the use of DEFINE_VARIABLE in macsyma user programs so
904 ;; that the use of DECLARE(FOO,SPECIAL) will be phased out at that level.
906 (mapc #'tbind arglist)
907 (destructuring-let* (((mode . nbody) (apply tr-body (cddr form) tr-body-argl))
908 (local-declares (make-declares arglist t))
909 (body (if local
910 `((let ((mlocp t))
911 (push nil loclist)
912 (unwind-protect
913 (progn ,@nbody)
914 (munlocal))))
915 nbody)))
916 ;; -> BINDING of variables with ASSIGN properties may be difficult to
917 ;; do correctly and efficiently if arbitrary code is to be run.
918 (if (or tr-lambda-punt-assigns
919 (do ((l arglist (cdr l)))
920 ((null l) t)
921 (let* ((var (car l))
922 (assign (get var 'assign)))
923 (if assign
924 (cond ((eq assign 'assign-mode-check)
925 (push `(,assign ',var ,(teval var)) easy-assigns))
927 (return nil)))))))
928 ;; Case with EASY or no ASSIGN's
929 `(,mode . (lambda ,(tunbinds arglist)
930 ,local-declares
931 ,@easy-assigns
932 ,@body))
933 ;; Case with arbitrary ASSIGN's.
934 (let ((temps (mapcar #'(lambda (ign) ign (tr-gensym)) arglist)))
935 `(,mode . (lambda ,temps
936 (unwind-protect
937 (progn
938 ;; [1] Check before binding.
939 ,@(mapcan #'(lambda (var val)
940 (let ((assign (get var 'assign)))
941 (if assign
942 (let ((assign-fn (if (symbolp assign) `(quote ,assign) (coerce assign 'function))))
943 (list `(funcall ,assign-fn ',var ,val))))))
944 arglist temps)
945 ;; [2] do the binding.
946 ((lambda ,(tunbinds arglist)
947 ,local-declares
948 ,@body)
949 ,@temps))
950 ;; [2] check when unbinding too.
951 ,@(mapcan #'(lambda (var)
952 (let ((assign (get var 'assign)))
953 (if assign
954 (let ((assign-fn (if (symbolp assign) `(quote ,assign) (coerce assign 'function))))
955 (list `(funcall ,assign-fn ',var
956 ;; use DTRANSLATE to
957 ;; catch global
958 ;; scoping if any.
959 ,(dtranslate var)))))))
960 arglist))))))))
963 (defun make-declares (varlist localp &aux (dl) (fx) (fl) specs)
964 (do ((l varlist (cdr l))
965 (mode) (var))
966 ((null l))
968 ;; When a variable is declared special, be sure to declare it
969 ;; special here.
970 (when (and localp (tr-get-special (car l)))
971 (push (car l) specs))
973 (when (or (not localp)
974 (not (tr-get-special (car l))))
975 ;; don't output local declarations on special variables.
976 (setq var (teval (car l)) mode (value-mode var))
977 (setq specs (cons var specs))
979 (cond ((eq '$fixnum mode) (pushnew var fx :test #'eq))
980 ((eq '$float mode) (pushnew var fl :test #'eq)))))
981 (if fx (pushnew `(fixnum . ,fx) dl :test #'eq))
982 (if fl (pushnew `(type flonum . ,fl) dl :test #'eq))
983 (if specs (pushnew `(special . ,specs) dl :test #'eq))
984 (if dl `(declare . ,dl)))
986 (defun tr-seq (l)
987 (do ((mode nil)
988 (body nil))
989 ((null l)
990 (cons mode (nreverse body)))
991 (let ((exp (translate (pop l))))
992 (setq mode (car exp))
993 (push (cdr exp) body))))
995 (def%tr mprogn (form)
996 (setq form (tr-seq (cdr form)))
997 (cons (car form) `(progn ,@(cdr form))))
999 (defun go-tag-p (e)
1000 (or (symbolp e) (integerp e)))
1002 (def%tr mprog (form)
1003 (let (arglist body val-list)
1004 ;; [1] normalize the MPROG syntax.
1005 (cond (($listp (cadr form))
1006 (setq arglist (cdadr form)
1007 body (cddr form)))
1009 (setq arglist nil
1010 body (cdr form))))
1011 (cond ((null body)
1012 (setq body '(((mquote) $done)))))
1013 (setq val-list (mapcar #'(lambda (u)
1014 (if (atom u) u
1015 (translate (caddr u))))
1016 arglist)
1017 arglist (mapcar #'(lambda (u)
1018 ;; X or ((MSETQ) X Y)
1019 (if (atom u) u (cadr u)))
1020 arglist))
1021 (let ((dup (find-duplicate arglist :test #'eq)))
1022 (when dup
1023 (tr-format (intl:gettext "error: ~M occurs more than once in block variable list") dup)
1024 (tr-abort)
1025 (return-from mprog nil)))
1026 (setq form
1027 (tr-lambda
1028 ;; [2] call the lambda translator.
1029 `((lambda) ((mlist) ,@arglist) ,@body)
1030 ;; [3] supply our own body translator.
1031 #'tr-mprog-body
1032 val-list
1033 arglist))
1034 (cons (car form) `(,(cdr form) ,@val-list))))
1036 (defun tr-mprog-body (body val-list arglist
1037 &aux
1038 (inside-mprog t)
1039 (return-mode nil)
1040 (need-prog? nil)
1041 (returns nil) ;; not used but must be bound.
1043 (do ((l nil))
1044 ((null body)
1045 ;; [5] hack the val-list for the mode context.
1046 ;; Perhaps the only use of the function MAP in all of macsyma.
1047 (mapl #'(lambda (val-list arglist)
1048 (cond ((atom (car val-list))
1049 (rplaca val-list
1050 (or (cdr (assoc (value-mode (car arglist))
1051 mode-init-value-table :test #'eq))
1052 `',(car arglist))))
1054 (warn-mode (car arglist)
1055 (value-mode (car arglist))
1056 (car (car val-list))
1057 "in a `block' statement")
1058 (rplaca val-list (cdr (car val-list))))))
1059 val-list arglist)
1060 (setq l (nreverse l))
1061 (cons return-mode
1062 (if need-prog?
1063 `((prog () ,@(delete nil l :test #'equal)))
1064 l)))
1065 ;; [4] translate a form in the body
1066 (let ((form (pop body)))
1067 (cond ((null body)
1068 ;; this is a really bad case.
1069 ;; we don't really know if the return mode
1070 ;; of the expression is for the value of the block.
1071 ;; Some people write RETURN at the end of a block
1072 ;; and some don't. In any case, the people not
1073 ;; use the PROG programming style won't be screwed
1074 ;; by this.
1075 (setq form (translate form))
1076 (setq return-mode (*union-mode (car form) return-mode))
1077 (setq form (cdr form))
1078 (if (and need-prog?
1079 (or (atom form)
1080 (not (eq (car form) 'return))))
1081 ;; put a RETURN on just in case.
1082 (setq form `(return ,form))))
1083 ((go-tag-p form))
1085 (setq form (dtranslate form))))
1086 (push form l))))
1088 (def%tr mreturn (form)
1089 (if (null inside-mprog)
1090 (tr-format (intl:gettext "warning: 'return' not within 'block' or 'do': ~:M~%") form))
1091 (setq need-prog? t)
1092 (setq form (translate (cadr form)))
1093 (setq return-mode (if return-mode (*union-mode (car form) return-mode)
1094 (car form)))
1095 (setq form `(return ,(cdr form)))
1096 (push form returns) ;; USED by lusing MDO etc not yet re-written.
1097 ;; MODE here should be $PHANTOM or something.
1098 `($any . ,form))
1100 (def%tr mgo (form)
1101 (if (null inside-mprog)
1102 (tr-format (intl:gettext "warning: 'go' not within 'block' or 'do': ~:M~%") form))
1103 (if (not (go-tag-p (cadr form)))
1104 (tr-format (intl:gettext "warning: 'go' tag must be a symbol or an integer: ~:M~%") form))
1105 (setq need-prog? t)
1106 `($any . (go ,(cadr form))))
1108 (def%tr mqapply (form)
1109 (let ((fn (cadr form)) (args (cddr form))
1110 (aryp (member 'array (cdar form) :test #'eq)))
1111 (cond ((atom fn)
1112 ;; I'm guessing (ATOM FN) is a parser error or other Lisp error,
1113 ;; so don't bother to translate the following error message.
1114 (tr-format "translator: MQAPPLY operator must be a cons; found: ~:M" form)
1115 nil)
1116 ((eq (caar fn) 'mquote)
1117 `($any list ',(cons (cadr fn) aryp) ,@(tr-args args)))
1118 ((eq (caar fn) 'lambda)
1119 (let ((args (tr-args args))
1120 (fn (translate fn)))
1121 (cons (car fn) `(mfuncall ,(cdr fn) ,@args))))
1122 ((not aryp)
1123 `($any simplify (mapply ,(dconvx (translate fn))
1124 (list ,@(tr-args args))
1125 ',fn)))
1127 (warn-meval form)
1128 (punt-to-meval form)))))
1130 (defun mcond-eval-symbols-tr (form)
1131 (mcond-eval-symbols #'maybe-msymeval form))
1133 (def%tr mcond (form)
1134 (let ((g (tr-gensym))
1135 (nl nil)
1136 (mode nil))
1137 (do ((l (cdr form) (cddr l))) ((null l))
1138 ; Optimize the else-if case: if we're at the else case at the end
1139 ; and the body is just another conditional, then we just continue
1140 ; directly with the clauses of the inner conditional instead of
1141 ; nesting.
1142 (when (and (null (cddr l))
1143 (eq (car l) t)
1144 (consp (cadr l))
1145 (eq (caaadr l) 'mcond))
1146 (setq l (cdadr l)))
1147 (let ((wrap-a-pred 'mcond))
1148 (declare (special wrap-a-pred))
1149 (destructuring-let (((pred-mode . pred-tr) (translate-predicate (car l)))
1150 ((body-mode . body-tr) (translate (cadr l))))
1151 (setq mode (*union-mode mode body-mode))
1152 (if (eq pred-mode '$boolean)
1153 (setq nl (list* body-tr pred-tr nl))
1154 (setq nl (list* `(list* '(mcond) ,g (mapcar #'mcond-eval-symbols-tr ',(cdr l)))
1155 `(not (null ,g))
1156 body-tr
1157 `(eq t (setq ,g ,pred-tr))
1158 nl))))))
1159 ; We leave off the final clause if the condition is true
1160 ; and the consequent is false.
1161 (when (and (eq t (cadr nl)) (null (car nl)))
1162 (setq nl (cddr nl)))
1163 (setq form nil)
1164 (do ((l nl (cddr l))) ((null l))
1165 (setq form
1166 (cons (cons (cadr l)
1167 (cond ((and (not (atom (car l)))
1168 (cdr l)
1169 (eq (caar l) 'progn))
1170 (cdar l))
1171 ((and (equal (car l) (cadr l))
1172 (atom (car l))) nil)
1173 (t (list (car l)))))
1174 form)))
1175 (if (among g form)
1176 (cons '$any `(let (,g) (cond ,@form)))
1177 (cons mode `(cond ,@form)))))
1179 ;; The MDO and MDOIN translators should be changed to use the TR-LAMBDA.
1180 ;; Perhaps a mere expansion into an MPROG would be best.
1182 (def%tr mdo (form)
1183 (let (returns assigns return-mode (inside-mprog t) need-prog?)
1184 (let (mode var init next test action varmode)
1185 (setq var (cond ((cadr form)) (t 'mdo)))
1186 (tbind var)
1187 (setq init (if (caddr form) (translate (caddr form)) '($fixnum . 1)))
1188 (cond ((not (setq varmode (tr-get-mode var)))
1189 (declvalue var (car init) t)))
1190 (setq next (translate (cond ((cadddr form) (list '(mplus) (cadddr form) var))
1191 ((car (cddddr form)))
1192 (t (list '(mplus) 1 var)))))
1193 (setq form (copy-list form))
1194 ;;to make the end test for thru be numberp if the index is numberp
1195 ;;and to eliminate reevaluation
1196 (cond ((not varmode)
1197 (declvalue var (*union-mode (car init) (car next)) t))
1199 (warn-mode var varmode (*union-mode (car init) (car next)))))
1200 (setq test (cdr (translate-predicate
1201 (list '(mor)
1202 (cond ((null (cadr (cddddr form))) nil)
1203 ((and (cadddr form)
1204 (mnegp ($numfactor (simplify (cadddr form)))))
1205 (list '(mlessp) var (cadr (cddddr form))))
1206 (t (list '(mgreaterp) var (cadr (cddddr form)))))
1207 (caddr (cddddr form))))))
1208 (setq action (translate (cadddr (cddddr form)))
1209 mode (cond ((null returns) '$any)
1210 (t return-mode)))
1211 (setq var (tunbind (cond ((cadr form)) (t 'mdo))))
1212 `(,mode do ((,var ,(cdr init) ,(cdr next)))
1213 (,test '$done) . ((declare (special ,var)) .
1214 ,(cond ((atom (cdr action)) nil)
1215 ((eq 'progn (cadr action)) (cddr action))
1216 (t (list (cdr action)))))))))
1218 (def%tr mdoin (form)
1219 (let (returns assigns return-mode (inside-mprog t) need-prog?)
1220 (prog (mode var init action)
1221 (setq var (tbind (cadr form))) (tbind 'mdo)
1222 (setq init (dtranslate (caddr form)))
1223 (cond ((or (cadr (cddddr form)) (caddr (cddddr form)))
1224 (tunbind 'mdo) (tunbind (cadr form))
1225 (return (punt-to-meval `((mdoin) . ,(cdr form))))))
1226 (setq action (translate (cadddr (cddddr form)))
1227 mode (cond ((null returns) '$any)
1228 (t return-mode)))
1229 (tunbind 'mdo) (tunbind (cadr form))
1230 (return
1231 `(,mode do ((,var) (mdo (cdr ,init) (cdr mdo)))
1232 ((null mdo) '$done) .
1233 ((declare (special ,var)) (setq ,var (car mdo)) .
1234 ,(cond ((atom (cdr action)) nil)
1235 ((eq 'progn (cadr action)) (cddr action))
1236 (t (list (cdr action))))))))))
1239 (defun lambda-wrap1 (tn val form)
1240 (if (or (atom val)
1241 (eq (car val) 'quote))
1242 (subst val tn form)
1243 `((lambda (,tn) ,form) ,val)))
1245 (def%tr msetq (form)
1246 (let ((var (cadr form))
1247 (val (caddr form))
1248 assign
1249 mode)
1250 (cond ((atom var)
1251 (setq mode (value-mode var) val (translate val))
1252 (warn-mode var mode (car val))
1253 (if (eq '$any mode)
1254 (setq mode (car val) val (cdr val))
1255 (setq val (dconv val mode)))
1256 (cons mode
1257 (if (setq assign (get var 'assign))
1258 (let ((tn (tr-gensym))
1259 (assign-fn (if (symbolp assign) `(quote ,assign) (coerce assign 'function))))
1260 (lambda-wrap1 tn val `(let nil
1261 (declare (special ,var ,(teval var)))
1262 (funcall ,assign-fn ',var ,tn)
1263 (setq ,(teval var) ,tn))))
1264 `(let nil (declare (special ,(teval var)))
1265 (if (not (boundp ',(teval var)))
1266 (add2lnc ',(teval var) $values))
1267 (,(if *macexpr-top-level-form-p*
1268 'defparameter
1269 'setq)
1270 ,(teval var) ,val)))))
1271 ((member 'array (car var) :test #'eq)
1272 (tr-arraysetq var val))
1274 (unless (safe-get (caar var) 'mset_extension_operator)
1275 (tr-format (intl:gettext "warning: no assignment operator known for ~:M~%") var)
1276 (tr-format (intl:gettext "note: just keep going and hope for the best.~%")))
1277 (setq val (translate val))
1278 `(,(car val) mset ',var ,(cdr val))))))
1280 (def%tr $max (x) (translate-$max-$min x))
1281 (def%tr $min (x) (translate-$max-$min x))
1282 (def%tr %max (x) (translate-$max-$min x))
1283 (def%tr %min (x) (translate-$max-$min x))
1285 (defun translate-$max-$min (form)
1286 (let ((mode) (arglist) (op (stripdollar (caar form))))
1287 (setq arglist
1288 (mapcar (lambda (l)
1289 (setq l (translate l))
1290 (cond ((null mode)
1291 (setq mode (car l)))
1292 ((eq mode (car l)))
1294 (setq mode '$any)))
1296 (cdr form)))
1297 ; To match the interpreted case, and to make sure we use the
1298 ; correct mode for the return value, we do not apply float
1299 ; contagion to the arguments and we use a special translation
1300 ; to call MAX or MIN only when every argument has the same
1301 ; mode (either all fixnum or all float). CLHS says that
1302 ; implementations have choices they can make about what MAX
1303 ; and MIN return when the arguments are a mix of float and
1304 ; rational types.
1305 ; Example: if an implementation decides to apply float contagion
1306 ; to the arguments of MAX (MIN), then it can return either an
1307 ; integer or a float if the greatest (least) argument was an
1308 ; integer.
1309 (if (member mode '($fixnum $float) :test #'eq)
1310 `(,mode ,(if (eq 'min op) 'min 'max) . ,(mapcar 'cdr arglist))
1311 `($any ,(if (eq 'min op) '$lmin '$lmax)
1312 (list '(mlist) . ,(mapcar 'dconvx arglist))))))
1315 ;;; mode acessing, binding, handling. Super over-simplified.
1317 (defun tr-class (x)
1318 (cond ((integerp x) '$fixnum)
1319 ((floatp x) '$float)
1320 ((member x '(t nil) :test #'eq) '$boolean)
1321 ((atom x) '$any)
1322 ((eq 'rat (caar x)) '$rational)
1323 (t '$any)))
1325 (defun *union-mode (mode1 mode2)
1326 (cond ((eq mode1 mode2) mode1)
1327 ((null mode1) mode2)
1328 ((null mode2) mode1)
1329 ((eq '$boolean mode1) '$any)
1330 ((eq '$boolean mode2) '$any)
1331 ((member mode2 *$any-modes* :test #'eq) '$any)
1332 ((member mode1 *$any-modes* :test #'eq) '$any)
1333 ((eq '$fixnum mode1) mode2)
1334 ((eq '$float mode1)
1335 (if (eq '$number mode2) '$number '$float))
1336 ((eq '$rational mode1)
1337 (if (eq '$float mode2) '$float '$any))
1338 ((eq '$number mode1)
1339 (if (eq '$rational mode2) '$any '$number))
1340 (t '$any)))
1342 (defun value-mode (var)
1343 (cond ((tr-get-mode var))
1345 (warn-undeclared var)
1346 '$any)))
1348 (defun decmode-arrayfun (f m)
1349 (putprop f m 'arrayfun-mode))
1351 (defun array-mode (ar)
1352 (cond ((get ar 'array-mode)) (t '$any)))
1354 (defun arrayfun-mode (ar)
1355 (cond ((get ar 'arrayfun-mode)) (t '$any)))
1357 (defun function-mode (f)
1358 (cond ((get f 'function-mode)) (t '$any)))
1360 (defun function-mode-@ (f)
1361 (ass-eq-ref (tr-get-val-modes f) 'function-mode '$any))
1363 (defun array-mode-@ (f)
1364 (ass-eq-ref (tr-get-val-modes f) 'array-mode '$any))
1367 (defvar $tr_bind_mode_hook nil
1368 "A hack to allow users to key the modes of variables
1369 off of variable spelling, and other things like that.")
1371 ;; TBIND, below, copies the MODE, VAL-MODES, and SPECIAL properties
1372 ;; into the a table named TSTACK, and then removes those properties.
1373 ;; So if TBIND has been called, we will need to look for those
1374 ;; properties in TSTACK instead of the symbol property list.
1376 (defstruct (tstack-slot (:conc-name tstack-slot-))
1377 mode
1378 tbind
1379 val-modes
1380 ;; an alist telling second order info
1381 ;; about APPLY(VAR,[X]), ARRAYAPPLY(F,[X]) etc.
1382 special)
1384 (defun tr-get-mode (a)
1385 (if (get a 'tbind)
1386 (let ((my-slot (cdr (assoc a tstack))))
1387 (tstack-slot-mode my-slot))
1388 (get a 'mode)))
1390 #-gcl (defun (setf tr-get-mode) (b a)
1391 (if (get a 'tbind)
1392 (let ((my-slot (cdr (assoc a tstack))))
1393 (setf (tstack-slot-mode my-slot) b))
1394 (setf (get a 'mode) b)))
1396 #+gcl (defsetf tr-get-mode (a) (b)
1397 `(if (get ,a 'tbind)
1398 (let ((my-slot (cdr (assoc ,a tstack))))
1399 (setf (tstack-slot-mode my-slot) ,b))
1400 (setf (get ,a 'mode) ,b)))
1402 (defun tr-get-val-modes (a)
1403 (if (get a 'tbind)
1404 (let ((my-slot (cdr (assoc a tstack))))
1405 (tstack-slot-val-modes my-slot))
1406 (get a 'val-modes)))
1408 #-gcl (defun (setf tr-get-val-modes) (b a)
1409 (if (get a 'tbind)
1410 (let ((my-slot (cdr (assoc a tstack))))
1411 (setf (tstack-slot-val-modes my-slot) b))
1412 (setf (get a 'val-modes) b)))
1414 #+gcl (defsetf tr-get-val-modes (a) (b)
1415 `(if (get ,a 'tbind)
1416 (let ((my-slot (cdr (assoc ,a tstack))))
1417 (setf (tstack-slot-val-modes my-slot) ,b))
1418 (setf (get ,a 'val-modes) ,b)))
1420 (defun tr-get-special (a)
1421 (if (get a 'tbind)
1422 (let ((my-slot (cdr (assoc a tstack))))
1423 (tstack-slot-special my-slot))
1424 (get a 'special)))
1426 #-gcl (defun (setf tr-get-special) (b a)
1427 (if (get a 'tbind)
1428 (let ((my-slot (cdr (assoc a tstack))))
1429 (setf (tstack-slot-special my-slot) b))
1430 (setf (get a 'special) b)))
1432 #+gcl (defsetf tr-get-special (a) (b)
1433 `(if (get ,a 'tbind)
1434 (let ((my-slot (cdr (assoc ,a tstack))))
1435 (setf (tstack-slot-special my-slot) ,b))
1436 (setf (get ,a 'special) ,b)))
1438 ;;; should be a macro (TBINDV <var-list> ... forms)
1439 ;;; so that TUNBIND is assured, and also so that the stupid ASSQ doesn't
1440 ;;; have to be done on the darn TSTACK. This will have to wait till
1441 ;;; the basic special form translation properties are rewritten.
1443 (defun variable-p (var)
1444 (and var (symbolp var) (not (eq var t))))
1446 (defun bad-var-warn (var)
1447 (tr-format (intl:gettext "warning: ~:M cannot be used as a variable.~%") var))
1449 (defun tbind (var &aux old)
1450 (cond ((variable-p var)
1451 (setq old (make-tstack-slot :mode (get var 'mode)
1452 :tbind (get var 'tbind)
1453 :val-modes (get var 'val-modes)
1454 :special (get var 'special)))
1455 (push (cons var old) tstack)
1456 (cond ((not (specialp var))
1457 ;; It is the lisp convention in use to inherit
1458 ;; specialness from higher context.
1459 ;; Spurious MODEDECLARATIONS get put in the environment
1460 ;; when code is MEVAL'd since there is no way to stack
1461 ;; the mode properties. Certainly nobody is willing
1462 ;; to hack MEVAL in JPG;MLISP
1463 (remprop var 'val-modes)
1464 (remprop var 'mode)
1465 (remprop var 'special)))
1466 (putprop var var 'tbind)
1467 (if $tr_bind_mode_hook
1468 (let ((mode? (mapply $tr_bind_mode_hook
1469 (list var)
1470 '$tr_bind_mode_hook)))
1471 (if mode? (tr-declare-varmode var mode?))))
1472 var)
1474 (bad-var-warn var))))
1476 (defun tunbind (var &aux (old (assoc var tstack :test #'eq)))
1477 (when (variable-p var)
1478 (prog1
1479 (teval var)
1480 (cond (old
1481 (setq tstack (delete old tstack :test #'eq)) ; POP should be all we need.
1482 (setq old (cdr old))
1483 (putprop1 var (tstack-slot-mode old) 'mode)
1484 (putprop1 var (tstack-slot-tbind old) 'tbind)
1485 (putprop1 var (tstack-slot-val-modes old) 'val-modes)
1486 (putprop1 var (tstack-slot-special old) 'special))))))
1488 (defun putprop1 (name value key)
1489 ;; leaves property list clean after unwinding, this
1490 ;; is an efficiency/storage issue only.
1491 (if value
1492 (putprop name value key)
1493 (progn
1494 (remprop name key)
1495 nil)))
1497 (defun tunbinds (l)
1498 (do ((nl))
1499 ((null l) nl)
1500 (setq nl (cons (tunbind (caar tstack)) nl)
1501 l (cdr l))))
1503 (defun tboundp (var)
1504 ;; really LEXICAL-VARP.
1505 (and (symbolp var) (get var 'tbind) (not (tr-get-special var))))
1507 (defun teval (var)
1508 (or (and (symbolp var) (get var 'tbind)) var))
1510 ;; Local Modes:
1511 ;; Mode: LISP
1512 ;; Comment Col: 40
1513 ;; END: