1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancements. ;;;;;
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 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14 (macsyma-module transl
)
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
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
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
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
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
))
172 (nil .
($boolean . nil
))))
174 (defvar mode-init-value-table
181 (defvar tr-lambda-punt-assigns nil
182 "Kludge argument to `tr-lambda' due to lack of keyword argument passing")
184 (defvar defined_variables
())
186 (defvar $define_variable
())
188 ;; FIXME: AFAICT (rtoy), *in-compile* is only used in this file and no
189 ;; one ever changes the value of *in-compile* to anything other than
190 ;; NIL. Perhaps remove this and the only other use of it below.
191 (defvar *in-compile
* nil
)
193 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
200 (tr-format (intl:gettext
"Internal translator error: ~M~%") msg
)
201 (cond (*transl-debug
*
202 (break "transl barfo"))
207 (defun specialp (var)
209 (tr-get-special var
)))
212 ;;; The error message system. Crude as it is.
213 ;;; I tell you how this aught to work:
214 ;;; (1) All state should be in one structure, one state variable.
215 ;;; (2) Should print out short message on-the-fly so that it
216 ;;; gives something to watch, and also so that it says something
218 ;;; (3) Summaries on a cross-referenced per-function and per-item
219 ;;; should be printed at the end, as a table.
221 ;;; Undefined Functions used in
222 ;;; FOO BAR, BAZ,BOMB
224 ;;; Undefined Variables ... same thing
225 ;;; Incomprehensible special forms
227 ;;; Predicate Mode Targeting failures.
230 ;;; The way it works now is to print too little or too much.
231 ;;; Many items are only warned about the first time seen.
232 ;;; However, this isn't too much of a problem when using Emacs
233 ;;; to edit code, because searching for warned-about tokens
234 ;;; is quick and easy.
236 (defmvar *tr-warn-break
* t
237 " if in debug mode `warning's signaled go to lisp break loops ")
239 (defmacro tr-warnbreak
()
240 `(and *transl-debug
* *tr-warn-break
* (break "transl")))
242 (defun tr-warnp (val)
245 (member val
'($all $compile $compfile $translate
) :test
#'eq
))
246 ((or *in-compfile
* *in-translate-file
*)
247 (member val
'($all $compfile $translate
) :test
#'eq
))
249 (member val
'($all $translate
) :test
#'eq
)))))
251 (defvar warned-undefined-variables nil
)
253 (defun warn-undefined-variable (form)
254 (and (tr-warnp $tr_warn_undefined_variable
)
255 (cond ((member form warned-undefined-variables
:test
#'eq
))
257 (push form warned-undefined-variables
)
258 (tr-format (intl:gettext
"warning: encountered undefined variable ~:M in translation.~%") form
)
261 (defun warn-undeclared (form &optional comment
)
262 (and (tr-warnp $tr_warn_undeclared
)
263 (cond ((member form
*warned-un-declared-vars
* :test
#'equal
) t
)
265 (push form
*warned-un-declared-vars
*)
266 (tr-format (intl:gettext
"warning: no type declaration for ~:M; assume type is 'any'.~%") form
)
267 (tr-format (intl:gettext
"note: 'modedeclare' declares types for translation.~%"))
269 (dolist (v *translation-msgs-files
*)
275 (defun warn-meval (form &optional comment
)
276 (cond ((tr-warnp $tr_warn_meval
)
277 (tr-format (intl:gettext
"warning: emit call to MEVAL for expression: ~:M~%") form
)
278 (cond (comment (dolist (v *translation-msgs-files
*)
285 (defun warn-mode (var mode newmode
&optional comment
)
286 (cond ((eq mode newmode
))
288 (cond ((and (tr-warnp $tr_warn_mode
)
289 (not (covers mode newmode
))
290 (not (member (list var mode newmode
) *warned-mode-vars
* :test
#'equal
)))
291 (push (list var mode newmode
) *warned-mode-vars
*)
292 (tr-format (intl:gettext
"warning: variable ~:M (declared type ~:M) assigned type ~:M.~%") var mode newmode
)
294 (dolist (v *translation-msgs-files
*)
299 (defun warn-fexpr (form &optional comment
)
300 (cond ((and (tr-warnp $tr_warn_fexpr
)
301 (not (member form
*warned-fexprs
* :test
#'equal
)))
302 (push form
*warned-fexprs
*)
303 (tr-format (intl:gettext
"warning: ~:M is a special function without a full Lisp translation.~%") form
)
304 (tr-format (intl:gettext
"warning: calling ~:M in compiled code might not have the desired effect.~%") form
)
306 (dolist (v *translation-msgs-files
*)
312 (defun macsyma-special-macro-p (fcn)
313 (getl-lm-fcn-prop fcn
'(macro)))
315 (defun macsyma-special-op-p (f)
316 (getl f
'(fsubr fexpr mfexpr
* mfexpr
*s
*fexpr
)))
318 (defun possible-predicate-op-p (f)
319 (member f
'(mnotequal mequal $equal mgreaterp mgeqp mlessp mleqp
) :test
#'eq
))
321 ;;;***************************************************************;;;
323 ;;; This function is the way to call the TRANSLATOR on an expression with
324 ;;; locally bound internal mode declarations. Result of TR-LAMBDA will be
325 ;;; (MODE . (LAMBDA (...) (DECLARE ...) TRANSLATED-EXP))
327 (defun tr-local-exp (exp &rest vars-modes
)
328 (let ((loc (let ((tr-lambda-punt-assigns t
))
329 (tr-lambda `((lambda) ((mlist) ,@(do ((l vars-modes
(cddr l
))
330 (ll nil
(cons (car l
) ll
)))
332 (or (variable-p (car l
))
333 (bad-var-warn (car l
)))))
334 (($modedeclare
) ,@vars-modes
)
336 (let ((mode (car loc
))
337 (exp (car (last loc
))))
340 (defun tr-args (form)
341 (mapcar #'(lambda (x) (dconvx (translate x
))) form
))
343 (defun dtranslate (form) (cdr (translate form
)))
345 (defun dconv (x mode
)
346 (cond ((eq '$float mode
) (dconv-$float x
))
347 ((eq '$cre mode
) (dconv-$cre x
))
351 (if (member (car x
) '(ratexpr pexpr
) :test
#'eq
)
355 (defun dconv-$float
(x)
356 (cond ((member (car x
) '($fixnum $number
) :test
#'eq
)
357 (if (integerp (cdr x
)) (float (cdr x
)) (list 'float
(cdr x
))))
358 ((eq '$rational
(car x
))
359 (cond ((or (atom (cdr x
))
360 (not (eq 'quote
(cadr x
))))
363 (/ (float (cadadr (cdr x
))) (float (caddr (caddr x
)))))))
366 (defun dconv-$cre
(x)
367 (if (eq '$cre
(car x
))
371 (defmvar *$any-modes
* '($any $list
))
373 (defun covers (mode1 mode2
)
374 (cond ((eq mode1 mode2
) t
)
375 ((eq '$float mode1
) (member mode2
'($float $fixnum $rational
) :test
#'eq
))
376 ((eq '$number mode1
) (member mode2
'($fixnum $float
) :test
#'eq
))
377 ((member mode1
*$any-modes
* :test
#'eq
) t
)))
379 ;;; takes a function name as input.
381 (defun tr-mfun (name &aux
(*transl-backtrace
* nil
))
382 (let ((def-form (consfundef name nil nil
)))
383 (cond ((null def-form
)
386 (tr-mdefine-toplevel def-form
)))))
389 ;;; All the hair here to deal with macsyma fexprs has been flushed.
390 ;;; Right now this handles MDEFMACRO and MDEFINE. The decisions
391 ;;; of where to put the actual properties and what kind of
392 ;;; defuns to make (LEXPR EXPR for maclisp) are punted to the
395 (defun tr-mdefine-toplevel (form &aux
(and-restp nil
))
396 (destructuring-let (((((name . flags
) . args
) body
) (cdr form
))
397 (a-args) kind out-forms
)
399 (do ((args args
(cdr args
))
400 ;; array functions cannot be LEXPR-like. gee.
401 ;; there is no good reason for that, except for efficiency,
402 ;; and I know that efficiency was not a consideration.
403 (full-restricted-flag (or (eq name
'mqapply
)
404 (member 'array flags
:test
#'eq
))))
405 ((null args
) (setq a-args
(nreverse a-args
)))
406 (let ((u (car args
)))
409 ((and (not full-restricted-flag
)
412 (cdr u
) (atom (cadr u
)))
413 (push (cadr u
) a-args
)
416 (push tr-unique a-args
)))))
419 (cond ((eq name
'mqapply
)
420 ;; don't you love syntax!
421 ;; do a switch-a-roo here. Calling ourselves recursively
422 ;; like this allows all legal forms and also catches
423 ;; errors. However, certain generalizations are also
424 ;; allowed. They won't get passed the interpreter, but
425 ;; interesting things may happen here. Thats what you
426 ;; get from too much syntax, so don't sweat it.
428 ;; the allowed generalizations aren't necessarily subscripted
429 ;; functions, but we'll act like they are when determining
430 ;; the free vars to capture.
431 ;; don't sweat this either.
432 (let ((*tr-free-vars-to-capture
* (union (cdar args
) *tr-free-vars-to-capture
*)))
434 `(,(car form
) ,(car args
)
435 ((lambda) ((mlist) ,@(cdr args
)) ,body
)))))
436 ((member tr-unique a-args
:test
#'eq
)
437 ;; WHAT IS "BAD" ABOUT THE ARGUMENT LIST HERE ??
438 (tr-format (intl:gettext
"error: unhandled argument list in function definition: ~:M~%") `((mlist),@args
))
441 ((member (caar form
) '(mdefine mdefmacro
) :test
#'eq
)
442 (setq kind
(cond ((eq (caar form
) 'mdefmacro
) 'macro
)
443 ((member 'array flags
:test
#'eq
) 'array
)
446 (tr-lambda `((lambda) ((mlist) ,@a-args
) ,body
)))
448 `(,name
,(car t-form
) ,(caar form
)
449 ,and-restp
,(eq kind
'array
))))
450 (cond ((eq kind
'func
)
451 (push-pre-transl-form
452 `(defmtrfun-external ,desc-header
))
453 (and (not (member (car t-form
) '($any nil
) :test
#'eq
))
454 (putprop name
(car t-form
) 'function-mode
)))
456 (and (not (member (car t-form
) '($any nil
) :test
#'eq
))
457 (decmode-arrayfun name
(car t-form
)))))
459 (cond ((or *in-translate
* (not $packagefile
))
460 ; These are all properties which tell the
461 ; user that functions are in the environment,
462 ; and that also allow him to SAVE the functions.
463 (push `(defprop ,name t translated
) out-forms
)
464 (push `(add2lnc ',name $props
) out-forms
)
465 (cond ((eq '$all $savedef
)
468 '((,name
,@flags
) ,@args
)
472 (macro '$macros
))) out-forms
)))))
473 (cond ((eq '$all $savedef
)
474 ;; For some reason one may want to save the
475 ;; interpreted definition even if in a PACKAGEFILE.
476 ;; not a good idea to use SAVEDEF anyway though.
477 (push `(mdefprop ,name
478 ((lambda) ((mlist) ,@args
) ,body
)
484 ;;once a function has been translated we want to make sure mfunction-call is eliminated.
486 (remprop (car desc-header
) 'undefined-warnp
)
487 (setf (get (car desc-header
) 'once-translated
) "I was once translated"))
489 ,@(nreverse out-forms
)
490 (defmtrfun ,desc-header
,@(cdr (cdr t-form
))))))
494 (defun translate-function (name)
496 (setq *in-translate
* t
)
497 (let ((lisp-def-form (tr-mfun name
))
498 (delete-subr?
(and (get name
'translated
)
499 (not (get name
'expr
)))))
503 (if delete-subr?
(remprop name
'subr
))
504 (if (mget name
'trace
) (macsyma-untrace name
))
505 (if (not $savedef
) (meval `(($remfunction
) ,name
)))
506 (handler-case (eval lisp-def-form
)
510 (return-from translate-function nil
)))
513 (defun punt-to-meval (form &optional
(mode '$any
))
514 (cons mode
`(meval ',form
)))
516 (defun trfail (x &optional msg
)
517 (tr-format (intl:gettext
"Error: failed to translate ~:@M~%") x
)
519 (tr-format (intl:gettext
"Message: ~A~%") msg
))
522 (defun translate-and-eval-macsyma-expression (form)
523 ;; this is the hyper-random entry to the transl package!
524 ;; it is used by MLISP for TRANSLATE:TRUE "::=".
526 (setq *in-translate
* t
)
527 ;; Use TRANSLATOR-EVAL so we don't have to lose badly by tracing EVAL
528 (translator-eval (translate-macexpr-toplevel form
))))
530 (defun translator-eval (x)
533 ;; This basically tells the msetq def%tr to use defparameter instead
534 ;; of setq because we're doing a setq at top-level, which isn't
535 ;; specified by ANSI CL.
536 (defvar *macexpr-top-level-form-p
* nil
)
538 (defun translate-macexpr-toplevel (form &aux
(*transl-backtrace
* nil
) tr-abort
)
539 ;; there are very few top-level special cases, I don't
540 ;; think it would help the code any to generalize TRANSLATE
543 ;; Except msetq at top-level is special for ANSI CL. See below.
544 (setq form
(toplevel-optimize form
))
545 (cond ((atom form
) nil
)
546 ((eq (caar form
) '$eval_when
)
547 (let ((whens (cadr form
))
548 (body (cddr form
)) tr-whens
)
549 (setq whens
(cond (($listp whens
) (cdr whens
))
550 ((atom whens
) (list whens
))
552 (tr-format (intl:gettext
"error: 'eval_when' argument must be a list or atom; found: ~:M~%") (cadr form
))
554 (setq tr-whens
(mapcar 'stripdollar whens
))
555 (cond ((member '$translate whens
:test
#'eq
)
557 (cond ((member '$loadfile whens
:test
#'eq
)
559 ,@(mapcar 'translate-macexpr-toplevel body
)))
560 ((setq tr-whens
(intersect tr-whens
'(:compile-toplevel
:load-toplevel
:execute
)))
563 ,@(mapcar 'translate-macexpr-toplevel body
)))
564 ((member '$compile whens
:test
#'eq
)
565 ;; strictly for the knowledgeable user.
568 ,@(mapcar 'translate-macexpr-toplevel body
))))))
569 ((member (caar form
) translate-time-evalables
:test
#'eq
)
572 (:compile-toplevel
:load-toplevel
:execute
)
574 ((member (caar form
) '(mdefine mdefmacro
) :test
#'eq
)
575 (let ((name (caaadr form
))
577 (tr-format (intl:gettext
"note: translating ~:@M~%") name
)
578 (setq trl
(tr-mdefine-toplevel form
))
580 (tr-format (intl:gettext
"error: failed to translate ~:@M~%") name
)
581 (tr-format (intl:gettext
"note: keep going and hope for the best.~%"))
584 ((eq 'mprogn
(caar form
))
585 ;; note that this ignores the $%% crock.
586 `(progn ,@(mapcar #'translate-macexpr-toplevel
(cdr form
))))
587 ((eq 'msetq
(caar form
))
588 ;; Toplevel msetq's should really be defparameter instead of
589 ;; setq for Common Lisp.
590 (let ((*macexpr-top-level-form-p
* t
))
592 ((eq '$define_variable
(caar form
))
593 ;; Toplevel msetq's should really be defparameter instead of
594 ;; setq for Common Lisp.
595 (let ((*macexpr-top-level-form-p
* t
))
598 (let ((t-form (dtranslate form
)))
606 (defmvar $tr_optimize_max_loop
100.
607 "The maximum number of times the macro-expansion and optimization
608 pass of the translator will loop in considering a form.
609 This is to catch macro expansion errors, and non-terminating
610 optimization properties.")
612 (defun toplevel-optimize (form)
613 ;; it is vital that optimizations be done within the
614 ;; context of variable meta bindings, declarations, etc.
615 ;; Also: think about calling the simplifier here.
617 (cond ((symbolp form
)
618 ;; If this symbol has the constant property, then
619 ;; use its assigned constant value in place of the
621 (let ((v (getl (mget form
'$props
) '($constant
))))
622 (if v
(cadr v
) form
)))
626 (kount 0 (1+ kount
)))
627 ;; tailrecursion should always arrange for a counter
628 ;; to check for mobylossage.
629 ((> kount $tr_optimize_max_loop
)
630 (tr-format (intl:gettext
"warning: I've looped ~A times in macro expansion; just give up and return ~:@M~%")
631 $tr_optimize_max_loop
(caar form
))
633 (setq new-form
(toplevel-optimize-1 form
))
634 (cond ((atom new-form
)
635 (return (toplevel-optimize new-form
)))
639 (setq form new-form
)))))))
641 (defun toplevel-optimize-1 (form &aux
(op (car form
)) prop
)
643 (member 'array op
:test
#'eq
)) form
)
644 ((progn (setq op
(car op
))
646 (if $transrun
; crock a minute.
647 (or (get op
'translated-mmacro
)
649 (or (mget op
'mmacro
)
650 (get op
'translated-mmacro
)))))
651 (mmacro-apply prop form
))
652 ((setq prop
($get op
'$optimize
))
653 ;; interesting, the MAPPLY here causes the simplification
654 ;; of the form and the result.
655 ;; The optimize property can be used to implement
656 ;; such niceties as the $%% crock.
657 (mapply1 prop
(list form
) "an optimizer property" nil
))
658 ((and ($get op
'$transload
)
660 ;; check for all reasonable definitions,
661 ;; $OPTIMIZE and MACRO already checked.
662 (not (or (get-lisp-fun-type op
)
663 (getl op
'(translate mfexpr
* mfexpr
*s
668 (mgetl op
'(mexpr)))))
671 (cons (car form
) (cdr form
)))
674 (defun translate (form)
675 (and *transl-debug
* (push form
*transl-backtrace
*))
676 (setq form
(toplevel-optimize form
))
677 (and *transl-debug
* (pop *transl-backtrace
*))
679 (and *transl-debug
* (push form
*transl-backtrace
*))
681 (translate-atom form
)
682 (translate-form form
))
683 ;; hey boy, reclaim that cons, just don't pop it!
684 (and *transl-debug
* (pop *transl-backtrace
*))))
686 (defun translate-atom (form &aux temp
)
687 (cond ((numberp form
) (cons (tr-class form
) form
))
688 ((setq temp
(assoc form boolean-object-table
:test
#'eq
))
690 ((and (setq temp
(mget form
'$numer
)) $tr_numer
)
692 ((implied-quotep form
)
694 ((self-evaluating-lisp-object-p form
)
697 (setq form
(teval form
))
698 `(,(value-mode form
) .
,form
))
700 (cond ((not (specialp form
))
701 (warn-undefined-variable form
)))
702 ;; note that the lisp analysis code must know that
703 ;; the TRD-MSYMEVAL form is a semantic variable.
704 (let* ((mode (value-mode form
))
705 (init-val (assoc mode mode-init-value-table
:test
#'eq
)))
706 (setq init-val
(cond (init-val (cdr init-val
))
708 ;; in the compiler TRD-MSYMEVAL doesn't do a darn
709 ;; thing, but it provides dynamic initialization of
710 ;; variables in interpreted code which is translated
711 ;; in-core. In FILE loaded code the DEFVAR will take
713 (push-defvar form init-val
)
714 `(,mode .
(trd-msymeval ,form
,init-val
))))))
716 (defun translate-form (form &aux temp
)
717 (cond ((eq (car form
) 'meval
) (cons '$any form
)) ;;for those lispy macsyma forms
718 ((not (atom (caar form
)))
719 ;; this is a check like that in the simplifier. form could
720 ;; result from substitution macros.
721 (translate `((mqapply) ,(caar form
) .
,(cdr form
))))
722 ((member 'array
(cdar form
) :test
#'eq
)
723 ;; dispatch this bad-boy to another module quick.
725 ;; TRANSLATE properties have priority.
726 ((setq temp
(get (caar form
) 'translate
))
728 ((setq temp
(get-lisp-fun-type (caar form
)))
729 (tr-lisp-function-call form temp
))
730 ((macsyma-special-macro-p (caar form
))
731 (attempt-translate-random-macro-op form
))
732 ((macsyma-special-op-p (caar form
))
733 ;; a special form not handled yet! foobar!
734 (attempt-translate-random-special-op form
))
735 ((or (get (caar form
) 'noun
) (get (caar form
) 'operators
))
736 ;; puntastical case. the weird ones are presumably taken care
737 ;; of by TRANSLATE properties by now.
738 (tr-infamous-noun-form form
))
740 ;; "What does a macsyma function call mean?".
741 ;; By the way, (A:'B,B:'C,C:'D)$ A(3) => D(3)
744 (tr-macsyma-user-function-call (caar form
) (cdr form
) form
))))
748 (defmvar $tr_bound_function_applyp t
)
750 (defun tr-macsyma-user-function-call (function args form
)
751 ;; this needs some work, output load-time code to
752 ;; check for MMACRO properties, etc, to be really
754 (cond ((eq $tr_function_call_default
'$apply
)
755 (translate `(($apply
) ,(caar form
) ((mlist) ,@(cdr form
)))))
756 ((eq $tr_function_call_default
'$expr
)
757 (tr-lisp-function-call form
'subr
))
758 ((eq $tr_function_call_default
'$general
)
760 ;;; G(F,X):=F(X+1); case.
761 ((and $tr_bound_function_applyp
(tboundp function
))
762 (let ((new-form `(($apply
) ,function
((mlist) ,@args
))))
763 (tr-format (intl:gettext
"warning: ~:M is a bound variable in ~:M, but it is used as a function.~%") function form
)
764 (tr-format (intl:gettext
"note: instead I'll translate it as: ~:M~%") new-form
)
765 (translate new-form
)))
766 ;; MFUNCTION-CALL cleverely punts this question to a FSUBR in the
767 ;; interpreter, and a macro in the compiler. This is good style,
768 ;; if a user is compiling then assume he is less lossage prone.
770 (pushnew (caar form
) *untranslated-functions-called
*)
772 (function-mode (caar form
))
773 'mfunction-call
`(,(caar form
) ,@(tr-args args
))))))
775 ;; This case used to be the most common, a real loser.
777 (punt-to-meval form
(function-mode (caar form
))))))
780 (defun attempt-translate-random-macro-op (form)
782 `($any .
,(cons (caar form
) (cdr form
))))
784 (defun attempt-translate-random-special-op (form)
786 (punt-to-meval form
(function-mode (caar form
))))
789 (defun tr-lisp-function-call (form type
)
790 (let ((op (caar form
)) (mode) (args))
791 (setq args
(cond ((member type
'(subr lsubr expr
) :test
#'eq
)
792 (mapcar #'(lambda (llis) (dconvx (translate llis
)))
795 (mapcar 'dtranslate
(cdr form
))))
796 mode
(function-mode op
))
797 (call-and-simp mode op args
)))
799 ;;the once-translated is so that inside translate file where a function
800 ;;has been translated, subsequent calls won't use mfunction call
801 (defun get-lisp-fun-type (fun &aux temp
)
802 ;; N.B. this is Functional types. NOT special-forms,
803 ;; lisp special forms are meaningless to macsyma.
804 (cond ((get fun
'*lexpr
) 'lsubr
)
805 ((get fun
'*expr
) 'subr
)
806 ;; *LEXPR & *EXPR gotten from DEFMFUN declarations
807 ;; which is loaded by TrData.
808 ((mget fun
'$fixed_num_args_function
)
810 ((mget fun
'$variable_num_args_function
)
812 ((setq temp
(getl fun
'(expr subr lsubr
)))
814 ((get fun
'once-translated
))
815 ((get fun
'translated
))
818 (defun tr-infamous-noun-form (form)
819 ;; 'F(X,Y) means noun-form. The arguments are evaluated.
820 ;; but the function is cons on, not applied.
821 ;; N.B. for special forms and macros this is totally wrong.
822 ;; But, those cases are filtered out already, presumably.
824 (let ((op (cond ((member 'array
(car form
) :test
#'eq
)
825 `(,(caar form
) array
))
826 (t `(,(caar form
)))))
827 (args (tr-args (cdr form
))))
828 `($any .
(simplify (list ',op
,@args
)))))
830 ;;; Some atoms, solely by usage, are self evaluating.
831 (defun implied-quotep (x)
832 (safe-get x
'implied-quotep
))
834 (defun self-evaluating-lisp-object-p (x)
835 (not (or (symbolp x
) (consp x
))))
837 ;;; the Translation Properties. the heart of TRANSL.
839 ;;; This conses up the call to the function, adding in the
840 ;;; SIMPLIFY i the mode is $ANY. This should be called everywhere.
841 ;;; instead of duplicating the COND everywhere, as is done now in TRANSL.
843 (defun tr-nosimpp (op)
848 (defun call-and-simp (mode fun args
)
849 (cond ((or (not (eq mode
'$any
))
851 `(,mode
,fun .
,args
))
853 `(,mode simplify
(,fun .
,args
)))))
855 (defmspec $declare_translated
(fns)
858 when
(or (symbolp v
) (and (stringp v
) (setq v
($verbify v
))))
859 do
(setf (get v
'once-translated
) t
)
860 (pushnew v
*declared-translated-functions
*)
861 else do
(merror (intl:gettext
"declare_translated: arguments must be symbols or strings; found: ~:M") v
)))
863 (def%tr $eval_when
(form)
864 (tr-format (intl:gettext
"error: found 'eval_when' in a function or expression: ~:M~%") form
)
865 (tr-format (intl:gettext
"note: 'eval_when' can appear only at the top level in a file.~%"))
869 (def%tr mdefmacro
(form)
870 (tr-format (intl:gettext
"warning: globally defining macro ~:M now to ensure correct macro expansions.~%") (caaadr form
))
871 ; Define the macro now to ensure that it's defined when it's time
872 ; to expand it. It's a bug that this definition occurs during
873 ; translation without being cleaned it up afterward, but simply
874 ; removing this breaks things.
876 (punt-to-meval form
))
878 (def%tr $local
(form)
880 (tr-format (intl:gettext
"error: there is already a 'local' in this block.~%"))
882 (return-from $local nil
))
884 ; We can't just translate to a call to MLOCAL here (which is
885 ; what used to happen). That would push onto LOCLIST and bind
886 ; MLOCP at the "wrong time". The push onto LOCLIST and the
887 ; binding of MLOCP are handled in TR-LAMBDA.
888 (punt-to-meval form
))
891 (def%tr mquote
(form)
892 (list (tr-class (cadr form
)) 'quote
(cadr form
)))
895 (defun tr-lambda (form &optional
(tr-body #'tr-seq
) &rest tr-body-argl
897 (arglist (mparams (cadr form
)))
900 ;; This function is defined to take a simple macsyma lambda expression and
901 ;; return a simple lisp lambda expression. The optional TR-BODY hook
902 ;; can be used for translating other special forms that do lambda binding.
904 ;; Local SPECIAL declarations are not used because
905 ;; the multics lisp compiler does not support them. They are of course
906 ;; a purely syntactic construct that doesn't buy much. I have been
907 ;; advocating the use of DEFINE_VARIABLE in macsyma user programs so
908 ;; that the use of DECLARE(FOO,SPECIAL) will be phased out at that level.
910 (mapc #'tbind arglist
)
911 (destructuring-let* (((mode . nbody
) (apply tr-body
(cddr form
) tr-body-argl
))
912 (local-declares (make-declares arglist t
))
920 ;; -> BINDING of variables with ASSIGN properties may be difficult to
921 ;; do correctly and efficiently if arbitrary code is to be run.
922 (if (or tr-lambda-punt-assigns
923 (do ((l arglist
(cdr l
)))
926 (assign (get var
'assign
)))
928 (cond ((eq assign
'assign-mode-check
)
929 (push `(,assign
',var
,(teval var
)) easy-assigns
))
932 ;; Case with EASY or no ASSIGN's
933 `(,mode .
(lambda ,(tunbinds arglist
)
937 ;; Case with arbitrary ASSIGN's.
938 (let ((temps (mapcar #'(lambda (ign) ign
(tr-gensym)) arglist
)))
939 `(,mode .
(lambda ,temps
942 ;; [1] Check before binding.
943 ,@(mapcan #'(lambda (var val
)
944 (let ((assign (get var
'assign
)))
946 (let ((assign-fn (if (symbolp assign
) `(quote ,assign
) (coerce assign
'function
))))
947 (list `(funcall ,assign-fn
',var
,val
))))))
949 ;; [2] do the binding.
950 ((lambda ,(tunbinds arglist
)
954 ;; [2] check when unbinding too.
955 ,@(mapcan #'(lambda (var)
956 (let ((assign (get var
'assign
)))
958 (let ((assign-fn (if (symbolp assign
) `(quote ,assign
) (coerce assign
'function
))))
959 (list `(funcall ,assign-fn
',var
963 ,(dtranslate var
)))))))
967 (defun make-declares (varlist localp
&aux
(dl) (fx) (fl) specs
)
968 (do ((l varlist
(cdr l
))
972 ;; When a variable is declared special, be sure to declare it
974 (when (and localp
(tr-get-special (car l
)))
975 (push (car l
) specs
))
977 (when (or (not localp
)
978 (not (tr-get-special (car l
))))
979 ;; don't output local declarations on special variables.
980 (setq var
(teval (car l
)) mode
(value-mode var
))
981 (setq specs
(cons var specs
))
983 (cond ((eq '$fixnum mode
) (pushnew var fx
:test
#'eq
))
984 ((eq '$float mode
) (pushnew var fl
:test
#'eq
)))))
985 (if fx
(pushnew `(fixnum .
,fx
) dl
:test
#'eq
))
986 (if fl
(pushnew `(type flonum .
,fl
) dl
:test
#'eq
))
987 (if specs
(pushnew `(special .
,specs
) dl
:test
#'eq
))
988 (if dl
`(declare .
,dl
)))
994 (cons mode
(nreverse body
)))
995 (let ((exp (translate (pop l
))))
996 (setq mode
(car exp
))
997 (push (cdr exp
) body
))))
999 (def%tr mprogn
(form)
1000 (setq form
(tr-seq (cdr form
)))
1001 (cons (car form
) `(progn ,@(cdr form
))))
1004 (or (symbolp e
) (integerp e
)))
1006 (def%tr mprog
(form)
1007 (let (arglist body val-list
)
1008 ;; [1] normalize the MPROG syntax.
1009 (cond (($listp
(cadr form
))
1010 (setq arglist
(cdadr form
)
1016 (setq body
'(((mquote) $done
)))))
1017 (setq val-list
(mapcar #'(lambda (u)
1019 (translate (caddr u
))))
1021 arglist
(mapcar #'(lambda (u)
1022 ;; X or ((MSETQ) X Y)
1023 (if (atom u
) u
(cadr u
)))
1025 (let ((dup (find-duplicate arglist
:test
#'eq
)))
1027 (tr-format (intl:gettext
"error: ~M occurs more than once in block variable list") dup
)
1029 (return-from mprog nil
)))
1032 ;; [2] call the lambda translator.
1033 `((lambda) ((mlist) ,@arglist
) ,@body
)
1034 ;; [3] supply our own body translator.
1038 (cons (car form
) `(,(cdr form
) ,@val-list
))))
1040 (defun tr-mprog-body (body val-list arglist
1045 (*returns
* nil
) ;; not used but must be bound.
1049 ;; [5] hack the val-list for the mode context.
1050 ;; Perhaps the only use of the function MAP in all of macsyma.
1051 (mapl #'(lambda (val-list arglist
)
1052 (cond ((atom (car val-list
))
1054 (or (cdr (assoc (value-mode (car arglist
))
1055 mode-init-value-table
:test
#'eq
))
1058 (warn-mode (car arglist
)
1059 (value-mode (car arglist
))
1060 (car (car val-list
))
1061 "in a `block' statement")
1062 (rplaca val-list
(cdr (car val-list
))))))
1064 (setq l
(nreverse l
))
1067 `((prog () ,@(delete nil l
:test
#'equal
)))
1069 ;; [4] translate a form in the body
1070 (let ((form (pop body
)))
1072 ;; this is a really bad case.
1073 ;; we don't really know if the return mode
1074 ;; of the expression is for the value of the block.
1075 ;; Some people write RETURN at the end of a block
1076 ;; and some don't. In any case, the people not
1077 ;; use the PROG programming style won't be screwed
1079 (setq form
(translate form
))
1080 (setq return-mode
(*union-mode
(car form
) return-mode
))
1081 (setq form
(cdr form
))
1084 (not (eq (car form
) 'return
))))
1085 ;; put a RETURN on just in case.
1086 (setq form
`(return ,form
))))
1089 (setq form
(dtranslate form
))))
1092 (def%tr mreturn
(form)
1093 (if (null inside-mprog
)
1094 (tr-format (intl:gettext
"warning: 'return' not within 'block' or 'do': ~:M~%") form
))
1096 (setq form
(translate (cadr form
)))
1097 (setq return-mode
(if return-mode
(*union-mode
(car form
) return-mode
)
1099 (setq form
`(return ,(cdr form
)))
1100 (push form
*returns
*) ;; USED by lusing MDO etc not yet re-written.
1101 ;; MODE here should be $PHANTOM or something.
1105 (if (null inside-mprog
)
1106 (tr-format (intl:gettext
"warning: 'go' not within 'block' or 'do': ~:M~%") form
))
1107 (if (not (go-tag-p (cadr form
)))
1108 (tr-format (intl:gettext
"warning: 'go' tag must be a symbol or an integer: ~:M~%") form
))
1110 `($any .
(go ,(cadr form
))))
1112 (def%tr mqapply
(form)
1113 (let ((fn (cadr form
)) (args (cddr form
))
1114 (aryp (member 'array
(cdar form
) :test
#'eq
)))
1116 ;; I'm guessing (ATOM FN) is a parser error or other Lisp error,
1117 ;; so don't bother to translate the following error message.
1118 (tr-format "translator: MQAPPLY operator must be a cons; found: ~:M" form
)
1120 ((eq (caar fn
) 'mquote
)
1121 `($any list
',(cons (cadr fn
) aryp
) ,@(tr-args args
)))
1122 ((eq (caar fn
) 'lambda
)
1123 (let ((args (tr-args args
))
1124 (fn (translate fn
)))
1125 (cons (car fn
) `(mfuncall ,(cdr fn
) ,@args
))))
1127 `($any simplify
(mapply ,(dconvx (translate fn
))
1128 (list ,@(tr-args args
))
1132 (punt-to-meval form
)))))
1134 (defun mcond-eval-symbols-tr (form)
1135 (mcond-eval-symbols #'maybe-msymeval form
))
1137 (def%tr mcond
(form)
1138 (let ((g (tr-gensym))
1141 (do ((l (cdr form
) (cddr l
))) ((null l
))
1142 ; Optimize the else-if case: if we're at the else case at the end
1143 ; and the body is just another conditional, then we just continue
1144 ; directly with the clauses of the inner conditional instead of
1146 (when (and (null (cddr l
))
1149 (eq (caaadr l
) 'mcond
))
1151 (let ((wrap-a-pred 'mcond
))
1152 (declare (special wrap-a-pred
))
1153 (destructuring-let (((pred-mode . pred-tr
) (translate-predicate (car l
)))
1154 ((body-mode . body-tr
) (translate (cadr l
))))
1155 (setq mode
(*union-mode mode body-mode
))
1156 (if (eq pred-mode
'$boolean
)
1157 (setq nl
(list* body-tr pred-tr nl
))
1158 (setq nl
(list* `(list* '(mcond) ,g
(mapcar #'mcond-eval-symbols-tr
',(cdr l
)))
1161 `(eq t
(setq ,g
,pred-tr
))
1163 ; We leave off the final clause if the condition is true
1164 ; and the consequent is false.
1165 (when (and (eq t
(cadr nl
)) (null (car nl
)))
1166 (setq nl
(cddr nl
)))
1168 (do ((l nl
(cddr l
))) ((null l
))
1170 (cons (cons (cadr l
)
1171 (cond ((and (not (atom (car l
)))
1173 (eq (caar l
) 'progn
))
1175 ((and (equal (car l
) (cadr l
))
1176 (atom (car l
))) nil
)
1177 (t (list (car l
)))))
1180 (cons '$any
`(let (,g
) (cond ,@form
)))
1181 (cons mode
`(cond ,@form
)))))
1183 ;; The MDO and MDOIN translators should be changed to use the TR-LAMBDA.
1184 ;; Perhaps a mere expansion into an MPROG would be best.
1187 (let (*returns
* assigns return-mode
(inside-mprog t
) need-prog?
)
1188 (let (mode var init next test-form action varmode
)
1189 (setq var
(cond ((cadr form
)) (t 'mdo
)))
1191 (setq init
(if (caddr form
) (translate (caddr form
)) '($fixnum .
1)))
1192 (cond ((not (setq varmode
(tr-get-mode var
)))
1193 (declvalue var
(car init
) t
)))
1194 (setq next
(translate (cond ((cadddr form
) (list '(mplus) (cadddr form
) var
))
1195 ((car (cddddr form
)))
1196 (t (list '(mplus) 1 var
)))))
1197 (setq form
(copy-list form
))
1198 ;;to make the end test for thru be numberp if the index is numberp
1199 ;;and to eliminate reevaluation
1200 (cond ((not varmode
)
1201 (declvalue var
(*union-mode
(car init
) (car next
)) t
))
1203 (warn-mode var varmode
(*union-mode
(car init
) (car next
)))))
1204 (destructuring-bind (test-mode . test-pred
)
1205 (translate-predicate
1207 (cond ((null (cadr (cddddr form
))) nil
)
1209 (mnegp ($numfactor
(simplify (cadddr form
)))))
1210 (list '(mlessp) var
(cadr (cddddr form
))))
1211 (t (list '(mgreaterp) var
(cadr (cddddr form
)))))
1212 (caddr (cddddr form
))))
1213 (if (eq test-mode
'$boolean
)
1214 (setq test-form test-pred
)
1215 (setq test-form
`(let (($prederror t
)) ,test-pred
))))
1216 (setq action
(translate (cadddr (cddddr form
)))
1217 mode
(cond ((null *returns
*) '$any
)
1219 (setq var
(tunbind (cond ((cadr form
)) (t 'mdo
))))
1220 `(,mode do
((,var
,(cdr init
) ,(cdr next
)))
1221 (,test-form
'$done
) .
((declare (special ,var
)) .
1222 ,(cond ((atom (cdr action
)) nil
)
1223 ((eq 'progn
(cadr action
)) (cddr action
))
1224 (t (list (cdr action
)))))))))
1226 (def%tr mdoin
(form)
1227 (let (*returns
* assigns return-mode
(inside-mprog t
) need-prog?
)
1228 (prog (mode var init action
)
1229 (setq var
(tbind (cadr form
))) (tbind 'mdo
)
1230 (setq init
(dtranslate (caddr form
)))
1231 (cond ((or (cadr (cddddr form
)) (caddr (cddddr form
)))
1232 (tunbind 'mdo
) (tunbind (cadr form
))
1233 (return (punt-to-meval `((mdoin) .
,(cdr form
))))))
1234 (setq action
(translate (cadddr (cddddr form
)))
1235 mode
(cond ((null *returns
*) '$any
)
1237 (tunbind 'mdo
) (tunbind (cadr form
))
1239 `(,mode do
((,var
) (mdo (cdr ,init
) (cdr mdo
)))
1240 ((null mdo
) '$done
) .
1241 ((declare (special ,var
)) (setq ,var
(car mdo
)) .
1242 ,(cond ((atom (cdr action
)) nil
)
1243 ((eq 'progn
(cadr action
)) (cddr action
))
1244 (t (list (cdr action
))))))))))
1247 (defun lambda-wrap1 (tn val form
)
1249 (eq (car val
) 'quote
))
1251 `((lambda (,tn
) ,form
) ,val
)))
1253 (def%tr msetq
(form)
1254 (let ((var (cadr form
))
1259 (setq mode
(value-mode var
) val
(translate val
))
1260 (warn-mode var mode
(car val
))
1262 (setq mode
(car val
) val
(cdr val
))
1263 (setq val
(dconv val mode
)))
1265 (if (setq assign
(get var
'assign
))
1266 (let ((tn (tr-gensym))
1267 (assign-fn (if (symbolp assign
) `(quote ,assign
) (coerce assign
'function
))))
1268 (lambda-wrap1 tn val
`(let nil
1269 (declare (special ,var
,(teval var
)))
1270 (funcall ,assign-fn
',var
,tn
)
1271 (setq ,(teval var
) ,tn
))))
1272 `(let nil
(declare (special ,(teval var
)))
1273 (if (not (boundp ',(teval var
)))
1274 (add2lnc ',(teval var
) $values
))
1275 (,(if *macexpr-top-level-form-p
*
1278 ,(teval var
) ,val
)))))
1279 ((member 'array
(car var
) :test
#'eq
)
1280 (tr-arraysetq var val
))
1282 (unless (safe-get (caar var
) 'mset_extension_operator
)
1283 (tr-format (intl:gettext
"warning: no assignment operator known for ~:M~%") var
)
1284 (tr-format (intl:gettext
"note: just keep going and hope for the best.~%")))
1285 (setq val
(translate val
))
1286 `(,(car val
) mset
',var
,(cdr val
))))))
1288 (def%tr $max
(x) (translate-$max-$min x
))
1289 (def%tr $min
(x) (translate-$max-$min x
))
1290 (def%tr %max
(x) (translate-$max-$min x
))
1291 (def%tr %min
(x) (translate-$max-$min x
))
1293 (defun translate-$max-$min
(form)
1294 (let ((mode) (arglist) (op (stripdollar (caar form
))))
1297 (setq l
(translate l
))
1299 (setq mode
(car l
)))
1305 ; To match the interpreted case, and to make sure we use the
1306 ; correct mode for the return value, we do not apply float
1307 ; contagion to the arguments and we use a special translation
1308 ; to call MAX or MIN only when every argument has the same
1309 ; mode (either all fixnum or all float). CLHS says that
1310 ; implementations have choices they can make about what MAX
1311 ; and MIN return when the arguments are a mix of float and
1313 ; Example: if an implementation decides to apply float contagion
1314 ; to the arguments of MAX (MIN), then it can return either an
1315 ; integer or a float if the greatest (least) argument was an
1317 (if (member mode
'($fixnum $float
) :test
#'eq
)
1318 `(,mode
,(if (eq 'min op
) 'min
'max
) .
,(mapcar 'cdr arglist
))
1319 `($any
,(if (eq 'min op
) '$lmin
'$lmax
)
1320 (list '(mlist) .
,(mapcar 'dconvx arglist
))))))
1323 ;;; mode accessing, binding, handling. Super over-simplified.
1326 (cond ((integerp x
) '$fixnum
)
1327 ((floatp x
) '$float
)
1328 ((member x
'(t nil
) :test
#'eq
) '$boolean
)
1330 ((eq 'rat
(caar x
)) '$rational
)
1333 (defun *union-mode
(mode1 mode2
)
1334 (cond ((eq mode1 mode2
) mode1
)
1335 ((null mode1
) mode2
)
1336 ((null mode2
) mode1
)
1337 ((eq '$boolean mode1
) '$any
)
1338 ((eq '$boolean mode2
) '$any
)
1339 ((member mode2
*$any-modes
* :test
#'eq
) '$any
)
1340 ((member mode1
*$any-modes
* :test
#'eq
) '$any
)
1341 ((eq '$fixnum mode1
) mode2
)
1343 (if (eq '$number mode2
) '$number
'$float
))
1344 ((eq '$rational mode1
)
1345 (if (eq '$float mode2
) '$float
'$any
))
1346 ((eq '$number mode1
)
1347 (if (eq '$rational mode2
) '$any
'$number
))
1350 (defun value-mode (var)
1351 (cond ((tr-get-mode var
))
1353 (warn-undeclared var
)
1356 (defun decmode-arrayfun (f m
)
1357 (putprop f m
'arrayfun-mode
))
1359 (defun array-mode (ar)
1360 (cond ((get ar
'array-mode
)) (t '$any
)))
1362 (defun arrayfun-mode (ar)
1363 (cond ((get ar
'arrayfun-mode
)) (t '$any
)))
1365 (defun function-mode (f)
1366 (cond ((get f
'function-mode
)) (t '$any
)))
1368 (defun function-mode-@ (f)
1369 (ass-eq-ref (tr-get-val-modes f
) 'function-mode
'$any
))
1371 (defun array-mode-@ (f)
1372 (ass-eq-ref (tr-get-val-modes f
) 'array-mode
'$any
))
1375 (defvar $tr_bind_mode_hook nil
1376 "A hack to allow users to key the modes of variables
1377 off of variable spelling, and other things like that.")
1379 ;; TBIND, below, copies the MODE, VAL-MODES, and SPECIAL properties
1380 ;; into the a table named TSTACK, and then removes those properties.
1381 ;; So if TBIND has been called, we will need to look for those
1382 ;; properties in TSTACK instead of the symbol property list.
1384 (defstruct (tstack-slot (:conc-name tstack-slot-
))
1388 ;; an alist telling second order info
1389 ;; about APPLY(VAR,[X]), ARRAYAPPLY(F,[X]) etc.
1392 (defun tr-get-mode (a)
1394 (let ((my-slot (cdr (assoc a tstack
))))
1395 (tstack-slot-mode my-slot
))
1398 #-gcl
(defun (setf tr-get-mode
) (b a
)
1400 (let ((my-slot (cdr (assoc a tstack
))))
1401 (setf (tstack-slot-mode my-slot
) b
))
1402 (setf (get a
'mode
) b
)))
1404 #+gcl
(defsetf tr-get-mode
(a) (b)
1405 `(if (get ,a
'tbind
)
1406 (let ((my-slot (cdr (assoc ,a tstack
))))
1407 (setf (tstack-slot-mode my-slot
) ,b
))
1408 (setf (get ,a
'mode
) ,b
)))
1410 (defun tr-get-val-modes (a)
1412 (let ((my-slot (cdr (assoc a tstack
))))
1413 (tstack-slot-val-modes my-slot
))
1414 (get a
'val-modes
)))
1416 #-gcl
(defun (setf tr-get-val-modes
) (b a
)
1418 (let ((my-slot (cdr (assoc a tstack
))))
1419 (setf (tstack-slot-val-modes my-slot
) b
))
1420 (setf (get a
'val-modes
) b
)))
1422 #+gcl
(defsetf tr-get-val-modes
(a) (b)
1423 `(if (get ,a
'tbind
)
1424 (let ((my-slot (cdr (assoc ,a tstack
))))
1425 (setf (tstack-slot-val-modes my-slot
) ,b
))
1426 (setf (get ,a
'val-modes
) ,b
)))
1428 (defun tr-get-special (a)
1430 (let ((my-slot (cdr (assoc a tstack
))))
1431 (tstack-slot-special my-slot
))
1434 #-gcl
(defun (setf tr-get-special
) (b a
)
1436 (let ((my-slot (cdr (assoc a tstack
))))
1437 (setf (tstack-slot-special my-slot
) b
))
1438 (setf (get a
'special
) b
)))
1440 #+gcl
(defsetf tr-get-special
(a) (b)
1441 `(if (get ,a
'tbind
)
1442 (let ((my-slot (cdr (assoc ,a tstack
))))
1443 (setf (tstack-slot-special my-slot
) ,b
))
1444 (setf (get ,a
'special
) ,b
)))
1446 ;;; should be a macro (TBINDV <var-list> ... forms)
1447 ;;; so that TUNBIND is assured, and also so that the stupid ASSQ doesn't
1448 ;;; have to be done on the darn TSTACK. This will have to wait till
1449 ;;; the basic special form translation properties are rewritten.
1451 (defun variable-p (var)
1452 (and var
(symbolp var
) (not (eq var t
))))
1454 (defun bad-var-warn (var)
1455 (tr-format (intl:gettext
"warning: ~:M cannot be used as a variable.~%") var
))
1457 (defun tbind (var &aux old
)
1458 (cond ((variable-p var
)
1459 (setq old
(make-tstack-slot :mode
(get var
'mode
)
1460 :tbind
(get var
'tbind
)
1461 :val-modes
(get var
'val-modes
)
1462 :special
(get var
'special
)))
1463 (push (cons var old
) tstack
)
1464 (cond ((not (specialp var
))
1465 ;; It is the lisp convention in use to inherit
1466 ;; specialness from higher context.
1467 ;; Spurious MODEDECLARATIONS get put in the environment
1468 ;; when code is MEVAL'd since there is no way to stack
1469 ;; the mode properties. Certainly nobody is willing
1470 ;; to hack MEVAL in JPG;MLISP
1471 (remprop var
'val-modes
)
1473 (remprop var
'special
)))
1474 (putprop var var
'tbind
)
1475 (if $tr_bind_mode_hook
1476 (let ((mode?
(mapply $tr_bind_mode_hook
1478 '$tr_bind_mode_hook
)))
1479 (if mode?
(tr-declare-varmode var mode?
))))
1482 (bad-var-warn var
))))
1484 (defun tunbind (var &aux
(old (assoc var tstack
:test
#'eq
)))
1485 (when (variable-p var
)
1489 (setq tstack
(delete old tstack
:test
#'eq
)) ; POP should be all we need.
1490 (setq old
(cdr old
))
1491 (putprop1 var
(tstack-slot-mode old
) 'mode
)
1492 (putprop1 var
(tstack-slot-tbind old
) 'tbind
)
1493 (putprop1 var
(tstack-slot-val-modes old
) 'val-modes
)
1494 (putprop1 var
(tstack-slot-special old
) 'special
))))))
1496 (defun putprop1 (name value key
)
1497 ;; leaves property list clean after unwinding, this
1498 ;; is an efficiency/storage issue only.
1500 (putprop name value key
)
1508 (setq nl
(cons (tunbind (caar tstack
)) nl
)
1511 (defun tboundp (var)
1512 ;; really LEXICAL-VARP.
1513 (and (symbolp var
) (get var
'tbind
) (not (tr-get-special var
))))
1516 (or (and (symbolp var
) (get var
'tbind
)) var
))