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