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"
130 :setting-list
($all $compile $compfile $translate
))
132 (defmvar $tr_warn_meval
'$compfile
133 "If `meval' is called that indicates problems in the translation"
134 :setting-list
($all $compile $compfile $translate
))
136 (defmvar $tr_warn_fexpr
138 "FEXPRS should not normally be output in translated code, all legitimate
139 special program forms are translated."
140 :setting-list
($all $compile $compfile $translate
))
142 (defmvar $tr_warn_mode
'$all
143 "Warn when variables are assigned values out of their mode."
144 :setting-list
($all $compile $compfile $translate
))
146 (defmvar $tr_warn_undefined_variable
'$all
147 "Warn when undefined global variables are seen."
148 :setting-list
($all $compile $compfile $translate
))
151 (defmvar *warned-un-declared-vars
* nil
"Warning State variable")
152 (defmvar *warned-fexprs
* nil
"Warning State variable")
153 (defmvar *warned-mode-vars
* nil
"Warning State variable")
155 (defmvar $tr_function_call_default
'$general
157 FALSE means punt to MEVAL, EXPR means assume lisp fixed arg function.
158 GENERAL, the default gives code good for mexprs and mlexprs but not macros.
159 GENERAL assures variable bindings are correct in compiled code.
160 In GENERAL mode, when translating F(X), if F is a bound variable, then
161 it assumes that APPLY(F,[X]) is meant, and translates a such, with
162 appropriate warning. There is no need to turn this off.
163 APPLY means like APPLY.")
165 (defmvar $tr_array_as_ref t
166 "If true runtime code uses value of the variable as the array.")
168 (defmvar $tr_numer nil
169 "If `true' numer properties are used for atoms which have them, e.g. %pi")
171 (defvar *tr-free-vars-to-capture
* '())
173 (defvar boolean-object-table
174 '(($true .
($boolean . t
))
175 ($false .
($boolean . nil
))
177 (nil .
($boolean . nil
))))
179 (defvar mode-init-value-table
186 (defvar tr-lambda-punt-assigns nil
187 "Kludge argument to `tr-lambda' due to lack of keyword argument passing")
189 (defvar defined_variables
())
191 (defvar $define_variable
())
193 ;; FIXME: AFAICT (rtoy), *in-compile* is only used in this file and no
194 ;; one ever changes the value of *in-compile* to anything other than
195 ;; NIL. Perhaps remove this and the only other use of it below.
196 (defvar *in-compile
* nil
)
198 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
205 (tr-format (intl:gettext
"Internal translator error: ~M~%") msg
)
206 (cond (*transl-debug
*
207 (break "transl barfo"))
212 (defun specialp (var)
214 (tr-get-special var
)))
217 ;;; The error message system. Crude as it is.
218 ;;; I tell you how this aught to work:
219 ;;; (1) All state should be in one structure, one state variable.
220 ;;; (2) Should print out short message on-the-fly so that it
221 ;;; gives something to watch, and also so that it says something
223 ;;; (3) Summaries on a cross-referenced per-function and per-item
224 ;;; should be printed at the end, as a table.
226 ;;; Undefined Functions used in
227 ;;; FOO BAR, BAZ,BOMB
229 ;;; Undefined Variables ... same thing
230 ;;; Incomprehensible special forms
232 ;;; Predicate Mode Targeting failures.
235 ;;; The way it works now is to print too little or too much.
236 ;;; Many items are only warned about the first time seen.
237 ;;; However, this isn't too much of a problem when using Emacs
238 ;;; to edit code, because searching for warned-about tokens
239 ;;; is quick and easy.
241 (defmvar *tr-warn-break
* t
242 " if in debug mode `warning's signaled go to lisp break loops ")
244 (defmacro tr-warnbreak
()
245 `(and *transl-debug
* *tr-warn-break
* (break "transl")))
247 (defun tr-warnp (val)
250 (member val
'($all $compile $compfile $translate
) :test
#'eq
))
251 ((or *in-compfile
* *in-translate-file
*)
252 (member val
'($all $compfile $translate
) :test
#'eq
))
254 (member val
'($all $translate
) :test
#'eq
)))))
256 (defvar warned-undefined-variables nil
)
258 (defun warn-undefined-variable (form)
259 (and (tr-warnp $tr_warn_undefined_variable
)
260 (cond ((member form warned-undefined-variables
:test
#'eq
))
262 (push form warned-undefined-variables
)
263 (tr-format (intl:gettext
"warning: encountered undefined variable ~:M in translation.~%") form
)
266 (defun warn-undeclared (form &optional comment
)
267 (and (tr-warnp $tr_warn_undeclared
)
268 (cond ((member form
*warned-un-declared-vars
* :test
#'equal
) t
)
270 (push form
*warned-un-declared-vars
*)
271 (tr-format (intl:gettext
"warning: no type declaration for ~:M; assume type is 'any'.~%") form
)
272 (tr-format (intl:gettext
"note: 'modedeclare' declares types for translation.~%"))
274 (dolist (v *translation-msgs-files
*)
280 (defun warn-meval (form &optional comment
)
281 (cond ((tr-warnp $tr_warn_meval
)
282 (tr-format (intl:gettext
"warning: emit call to MEVAL for expression: ~:M~%") form
)
283 (cond (comment (dolist (v *translation-msgs-files
*)
290 (defun warn-mode (var mode newmode
&optional comment
)
291 (cond ((eq mode newmode
))
293 (cond ((and (tr-warnp $tr_warn_mode
)
294 (not (covers mode newmode
))
295 (not (member (list var mode newmode
) *warned-mode-vars
* :test
#'equal
)))
296 (push (list var mode newmode
) *warned-mode-vars
*)
297 (tr-format (intl:gettext
"warning: variable ~:M (declared type ~:M) assigned type ~:M.~%") var mode newmode
)
299 (dolist (v *translation-msgs-files
*)
304 (defun warn-fexpr (form &optional comment
)
305 (cond ((and (tr-warnp $tr_warn_fexpr
)
306 (not (member form
*warned-fexprs
* :test
#'equal
)))
307 (push form
*warned-fexprs
*)
308 (tr-format (intl:gettext
"warning: ~:M is a special function without a full Lisp translation.~%") form
)
309 (tr-format (intl:gettext
"warning: calling ~:M in compiled code might not have the desired effect.~%") form
)
311 (dolist (v *translation-msgs-files
*)
317 (defun macsyma-special-macro-p (fcn)
318 (getl-lm-fcn-prop fcn
'(macro)))
320 (defun macsyma-special-op-p (f)
321 (getl f
'(fsubr fexpr mfexpr
* mfexpr
*s
*fexpr
)))
323 (defun possible-predicate-op-p (f)
324 (member f
'(mnotequal mequal $equal mgreaterp mgeqp mlessp mleqp
) :test
#'eq
))
326 ;;;***************************************************************;;;
328 ;;; This function is the way to call the TRANSLATOR on an expression with
329 ;;; locally bound internal mode declarations. Result of TR-LAMBDA will be
330 ;;; (MODE . (LAMBDA (...) (DECLARE ...) TRANSLATED-EXP))
332 (defun tr-local-exp (exp &rest vars-modes
)
333 (let ((loc (let ((tr-lambda-punt-assigns t
))
334 (tr-lambda `((lambda) ((mlist) ,@(do ((l vars-modes
(cddr l
))
335 (ll nil
(cons (car l
) ll
)))
337 (or (variable-p (car l
))
338 (bad-var-warn (car l
)))))
339 (($modedeclare
) ,@vars-modes
)
341 (let ((mode (car loc
))
342 (exp (car (last loc
))))
345 (defun tr-args (form)
346 (mapcar #'(lambda (x) (dconvx (translate x
))) form
))
348 (defun dtranslate (form) (cdr (translate form
)))
350 (defun dconv (x mode
)
351 (cond ((eq '$float mode
) (dconv-$float x
))
352 ((eq '$cre mode
) (dconv-$cre x
))
356 (if (member (car x
) '(ratexpr pexpr
) :test
#'eq
)
360 (defun dconv-$float
(x)
361 (cond ((member (car x
) '($fixnum $number
) :test
#'eq
)
362 (if (integerp (cdr x
)) (float (cdr x
)) (list 'float
(cdr x
))))
363 ((eq '$rational
(car x
))
364 (cond ((or (atom (cdr x
))
365 (not (eq 'quote
(cadr x
))))
368 (/ (float (cadadr (cdr x
))) (float (caddr (caddr x
)))))))
371 (defun dconv-$cre
(x)
372 (if (eq '$cre
(car x
))
376 (defmvar *$any-modes
* '($any $list
))
378 (defun covers (mode1 mode2
)
379 (cond ((eq mode1 mode2
) t
)
380 ((eq '$float mode1
) (member mode2
'($float $fixnum $rational
) :test
#'eq
))
381 ((eq '$number mode1
) (member mode2
'($fixnum $float
) :test
#'eq
))
382 ((member mode1
*$any-modes
* :test
#'eq
) t
)))
384 ;;; takes a function name as input.
386 (defun tr-mfun (name &aux
(*transl-backtrace
* nil
))
387 (let ((def-form (consfundef name nil nil
)))
388 (cond ((null def-form
)
391 (tr-mdefine-toplevel def-form
)))))
394 ;;; All the hair here to deal with macsyma fexprs has been flushed.
395 ;;; Right now this handles MDEFMACRO and MDEFINE. The decisions
396 ;;; of where to put the actual properties and what kind of
397 ;;; defuns to make (LEXPR EXPR for maclisp) are punted to the
400 (defun tr-mdefine-toplevel (form &aux
(and-restp nil
))
401 (destructuring-let (((((name . flags
) . args
) body
) (cdr form
))
402 (a-args) kind out-forms
)
404 (do ((args args
(cdr args
))
405 ;; array functions cannot be LEXPR-like. gee.
406 ;; there is no good reason for that, except for efficiency,
407 ;; and I know that efficiency was not a consideration.
408 (full-restricted-flag (or (eq name
'mqapply
)
409 (member 'array flags
:test
#'eq
))))
410 ((null args
) (setq a-args
(nreverse a-args
)))
411 (let ((u (car args
)))
414 ((and (not full-restricted-flag
)
417 (cdr u
) (atom (cadr u
)))
418 (push (cadr u
) a-args
)
421 (push tr-unique a-args
)))))
424 (cond ((eq name
'mqapply
)
425 ;; don't you love syntax!
426 ;; do a switch-a-roo here. Calling ourselves recursively
427 ;; like this allows all legal forms and also catches
428 ;; errors. However, certain generalizations are also
429 ;; allowed. They won't get passed the interpreter, but
430 ;; interesting things may happen here. Thats what you
431 ;; get from too much syntax, so don't sweat it.
433 ;; the allowed generalizations aren't necessarily subscripted
434 ;; functions, but we'll act like they are when determining
435 ;; the free vars to capture.
436 ;; don't sweat this either.
437 (let ((*tr-free-vars-to-capture
* (union (cdar args
) *tr-free-vars-to-capture
*)))
439 `(,(car form
) ,(car args
)
440 ((lambda) ((mlist) ,@(cdr args
)) ,body
)))))
441 ((member tr-unique a-args
:test
#'eq
)
442 ;; WHAT IS "BAD" ABOUT THE ARGUMENT LIST HERE ??
443 (tr-format (intl:gettext
"error: unhandled argument list in function definition: ~:M~%") `((mlist),@args
))
446 ((member (caar form
) '(mdefine mdefmacro
) :test
#'eq
)
447 (setq kind
(cond ((eq (caar form
) 'mdefmacro
) 'macro
)
448 ((member 'array flags
:test
#'eq
) 'array
)
451 (tr-lambda `((lambda) ((mlist) ,@a-args
) ,body
)))
453 `(,name
,(car t-form
) ,(caar form
)
454 ,and-restp
,(eq kind
'array
))))
455 (cond ((eq kind
'func
)
456 (push-pre-transl-form
457 `(defmtrfun-external ,desc-header
))
458 (and (not (member (car t-form
) '($any nil
) :test
#'eq
))
459 (putprop name
(car t-form
) 'function-mode
)))
461 (and (not (member (car t-form
) '($any nil
) :test
#'eq
))
462 (decmode-arrayfun name
(car t-form
)))))
464 (cond ((or *in-translate
* (not $packagefile
))
465 ; These are all properties which tell the
466 ; user that functions are in the environment,
467 ; and that also allow him to SAVE the functions.
468 (push `(defprop ,name t translated
) out-forms
)
469 (push `(add2lnc ',name $props
) out-forms
)
470 (cond ((eq '$all $savedef
)
473 '((,name
,@flags
) ,@args
)
477 (macro '$macros
))) out-forms
)))))
478 (cond ((eq '$all $savedef
)
479 ;; For some reason one may want to save the
480 ;; interpreted definition even if in a PACKAGEFILE.
481 ;; not a good idea to use SAVEDEF anyway though.
482 (push `(mdefprop ,name
483 ((lambda) ((mlist) ,@args
) ,body
)
489 ;;once a function has been translated we want to make sure mfunction-call is eliminated.
491 (remprop (car desc-header
) 'undefined-warnp
)
492 (setf (get (car desc-header
) 'once-translated
) "I was once translated"))
494 ,@(nreverse out-forms
)
495 (defmtrfun ,desc-header
,@(cdr (cdr t-form
))))))
499 (defun translate-function (name)
501 (setq *in-translate
* t
)
502 (let ((lisp-def-form (tr-mfun name
))
503 (delete-subr?
(and (get name
'translated
)
504 (not (get name
'expr
)))))
508 (if delete-subr?
(remprop name
'subr
))
509 (if (mget name
'trace
) (macsyma-untrace name
))
510 (if (not $savedef
) (meval `(($remfunction
) ,name
)))
511 (handler-case (eval lisp-def-form
)
515 (return-from translate-function nil
)))
518 (defun punt-to-meval (form &optional
(mode '$any
))
519 (cons mode
`(meval ',form
)))
521 (defun trfail (x &optional msg
)
522 (tr-format (intl:gettext
"Error: failed to translate ~:@M~%") x
)
524 (tr-format (intl:gettext
"Message: ~A~%") msg
))
527 (defun translate-and-eval-macsyma-expression (form)
528 ;; this is the hyper-random entry to the transl package!
529 ;; it is used by MLISP for TRANSLATE:TRUE "::=".
531 (setq *in-translate
* t
)
532 ;; Use TRANSLATOR-EVAL so we don't have to lose badly by tracing EVAL
533 (translator-eval (translate-macexpr-toplevel form
))))
535 (defun translator-eval (x)
538 ;; This basically tells the msetq def%tr to use defparameter instead
539 ;; of setq because we're doing a setq at top-level, which isn't
540 ;; specified by ANSI CL.
541 (defvar *macexpr-top-level-form-p
* nil
)
543 (defun translate-macexpr-toplevel (form &aux
(*transl-backtrace
* nil
) tr-abort
)
544 ;; there are very few top-level special cases, I don't
545 ;; think it would help the code any to generalize TRANSLATE
548 ;; Except msetq at top-level is special for ANSI CL. See below.
549 (setq form
(toplevel-optimize form
))
550 (cond ((atom form
) nil
)
551 ((eq (caar form
) '$eval_when
)
552 (let ((whens (cadr form
))
553 (body (cddr form
)) tr-whens
)
554 (setq whens
(cond (($listp whens
) (cdr whens
))
555 ((atom whens
) (list whens
))
557 (tr-format (intl:gettext
"error: 'eval_when' argument must be a list or atom; found: ~:M~%") (cadr form
))
559 (setq tr-whens
(mapcar 'stripdollar whens
))
560 (cond ((member '$translate whens
:test
#'eq
)
562 (cond ((member '$loadfile whens
:test
#'eq
)
564 ,@(mapcar 'translate-macexpr-toplevel body
)))
565 ((setq tr-whens
(intersect tr-whens
'(:compile-toplevel
:load-toplevel
:execute
)))
568 ,@(mapcar 'translate-macexpr-toplevel body
)))
569 ((member '$compile whens
:test
#'eq
)
570 ;; strictly for the knowledgeable user.
573 ,@(mapcar 'translate-macexpr-toplevel body
))))))
574 ((member (caar form
) translate-time-evalables
:test
#'eq
)
577 (:compile-toplevel
:load-toplevel
:execute
)
579 ((member (caar form
) '(mdefine mdefmacro
) :test
#'eq
)
580 (let ((name (caaadr form
))
582 (tr-format (intl:gettext
"note: translating ~:@M~%") name
)
583 (setq trl
(tr-mdefine-toplevel form
))
585 (tr-format (intl:gettext
"error: failed to translate ~:@M~%") name
)
586 (tr-format (intl:gettext
"note: keep going and hope for the best.~%"))
589 ((eq 'mprogn
(caar form
))
590 ;; note that this ignores the $%% crock.
591 `(progn ,@(mapcar #'translate-macexpr-toplevel
(cdr form
))))
592 ((eq 'msetq
(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
))
597 ((eq '$define_variable
(caar form
))
598 ;; Toplevel msetq's should really be defparameter instead of
599 ;; setq for Common Lisp.
600 (let ((*macexpr-top-level-form-p
* t
))
603 (let ((t-form (dtranslate form
)))
611 (defmvar $tr_optimize_max_loop
100.
612 "The maximum number of times the macro-expansion and optimization
613 pass of the translator will loop in considering a form.
614 This is to catch macro expansion errors, and non-terminating
615 optimization properties.")
617 (defun toplevel-optimize (form)
618 ;; it is vital that optimizations be done within the
619 ;; context of variable meta bindings, declarations, etc.
620 ;; Also: think about calling the simplifier here.
622 (cond ((symbolp form
)
623 ;; If this symbol has the constant property, then
624 ;; use its assigned constant value in place of the
626 (let ((v (getl (mget form
'$props
) '($constant
))))
627 (if v
(cadr v
) form
)))
631 (kount 0 (1+ kount
)))
632 ;; tailrecursion should always arrange for a counter
633 ;; to check for mobylossage.
634 ((> kount $tr_optimize_max_loop
)
635 (tr-format (intl:gettext
"warning: I've looped ~A times in macro expansion; just give up and return ~:@M~%")
636 $tr_optimize_max_loop
(caar form
))
638 (setq new-form
(toplevel-optimize-1 form
))
639 (cond ((atom new-form
)
640 (return (toplevel-optimize new-form
)))
644 (setq form new-form
)))))))
646 (defun toplevel-optimize-1 (form &aux
(op (car form
)) prop
)
648 (member 'array op
:test
#'eq
)) form
)
649 ((progn (setq op
(car op
))
651 (if $transrun
; crock a minute.
652 (or (get op
'translated-mmacro
)
654 (or (mget op
'mmacro
)
655 (get op
'translated-mmacro
)))))
656 (mmacro-apply prop form
))
657 ((setq prop
($get op
'$optimize
))
658 ;; interesting, the MAPPLY here causes the simplification
659 ;; of the form and the result.
660 ;; The optimize property can be used to implement
661 ;; such niceties as the $%% crock.
662 (mapply1 prop
(list form
) "an optimizer property" nil
))
663 ((and ($get op
'$transload
)
665 ;; check for all reasonable definitions,
666 ;; $OPTIMIZE and MACRO already checked.
667 (not (or (get-lisp-fun-type op
)
668 (getl op
'(translate mfexpr
* mfexpr
*s
673 (mgetl op
'(mexpr)))))
676 (cons (car form
) (cdr form
)))
679 (defun translate (form)
680 (and *transl-debug
* (push form
*transl-backtrace
*))
681 (setq form
(toplevel-optimize form
))
682 (and *transl-debug
* (pop *transl-backtrace
*))
684 (and *transl-debug
* (push form
*transl-backtrace
*))
686 (translate-atom form
)
687 (translate-form form
))
688 ;; hey boy, reclaim that cons, just don't pop it!
689 (and *transl-debug
* (pop *transl-backtrace
*))))
691 (defun translate-atom (form &aux temp
)
692 (cond ((numberp form
) (cons (tr-class form
) form
))
693 ((setq temp
(assoc form boolean-object-table
:test
#'eq
))
695 ((and (setq temp
(mget form
'$numer
)) $tr_numer
)
697 ((implied-quotep form
)
699 ((self-evaluating-lisp-object-p form
)
702 (setq form
(teval form
))
703 `(,(value-mode form
) .
,form
))
705 (cond ((not (specialp form
))
706 (warn-undefined-variable form
)))
707 ;; note that the lisp analysis code must know that
708 ;; the TRD-MSYMEVAL form is a semantic variable.
709 (let* ((mode (value-mode form
))
710 (init-val (assoc mode mode-init-value-table
:test
#'eq
)))
711 (setq init-val
(cond (init-val (cdr init-val
))
713 ;; in the compiler TRD-MSYMEVAL doesn't do a darn
714 ;; thing, but it provides dynamic initialization of
715 ;; variables in interpreted code which is translated
716 ;; in-core. In FILE loaded code the DEFVAR will take
718 (push-defvar form init-val
)
719 `(,mode .
(trd-msymeval ,form
,init-val
))))))
721 (defun translate-form (form &aux temp
)
722 (cond ((eq (car form
) 'meval
) (cons '$any form
)) ;;for those lispy macsyma forms
723 ((not (atom (caar form
)))
724 ;; this is a check like that in the simplifier. form could
725 ;; result from substitution macros.
726 (translate `((mqapply) ,(caar form
) .
,(cdr form
))))
727 ((member 'array
(cdar form
) :test
#'eq
)
728 ;; dispatch this bad-boy to another module quick.
730 ;; TRANSLATE properties have priority.
731 ((setq temp
(get (caar form
) 'translate
))
733 ((setq temp
(get-lisp-fun-type (caar form
)))
734 (tr-lisp-function-call form temp
))
735 ((macsyma-special-macro-p (caar form
))
736 (attempt-translate-random-macro-op form
))
737 ((macsyma-special-op-p (caar form
))
738 ;; a special form not handled yet! foobar!
739 (attempt-translate-random-special-op form
))
740 ((or (get (caar form
) 'noun
) (get (caar form
) 'operators
))
741 ;; puntastical case. the weird ones are presumably taken care
742 ;; of by TRANSLATE properties by now.
743 (tr-infamous-noun-form form
))
745 ;; "What does a macsyma function call mean?".
746 ;; By the way, (A:'B,B:'C,C:'D)$ A(3) => D(3)
749 (tr-macsyma-user-function-call (caar form
) (cdr form
) form
))))
753 (defmvar $tr_bound_function_applyp t
)
755 (defun tr-macsyma-user-function-call (function args form
)
756 ;; this needs some work, output load-time code to
757 ;; check for MMACRO properties, etc, to be really
759 (cond ((eq $tr_function_call_default
'$apply
)
760 (translate `(($apply
) ,(caar form
) ((mlist) ,@(cdr form
)))))
761 ((eq $tr_function_call_default
'$expr
)
762 (tr-lisp-function-call form
'subr
))
763 ((eq $tr_function_call_default
'$general
)
765 ;;; G(F,X):=F(X+1); case.
766 ((and $tr_bound_function_applyp
(tboundp function
))
767 (let ((new-form `(($apply
) ,function
((mlist) ,@args
))))
768 (tr-format (intl:gettext
"warning: ~:M is a bound variable in ~:M, but it is used as a function.~%") function form
)
769 (tr-format (intl:gettext
"note: instead I'll translate it as: ~:M~%") new-form
)
770 (translate new-form
)))
771 ;; MFUNCTION-CALL cleverely punts this question to a FSUBR in the
772 ;; interpreter, and a macro in the compiler. This is good style,
773 ;; if a user is compiling then assume he is less lossage prone.
775 (pushnew (caar form
) *untranslated-functions-called
*)
777 (function-mode (caar form
))
778 'mfunction-call
`(,(caar form
) ,@(tr-args args
))))))
780 ;; This case used to be the most common, a real loser.
782 (punt-to-meval form
(function-mode (caar form
))))))
785 (defun attempt-translate-random-macro-op (form)
787 `($any .
,(cons (caar form
) (cdr form
))))
789 (defun attempt-translate-random-special-op (form)
791 (punt-to-meval form
(function-mode (caar form
))))
794 (defun tr-lisp-function-call (form type
)
795 (let ((op (caar form
)) (mode) (args))
796 (setq args
(cond ((member type
'(subr lsubr expr
) :test
#'eq
)
797 (mapcar #'(lambda (llis) (dconvx (translate llis
)))
800 (mapcar 'dtranslate
(cdr form
))))
801 mode
(function-mode op
))
802 (call-and-simp mode op args
)))
804 ;;the once-translated is so that inside translate file where a function
805 ;;has been translated, subsequent calls won't use mfunction call
806 (defun get-lisp-fun-type (fun &aux temp
)
807 ;; N.B. this is Functional types. NOT special-forms,
808 ;; lisp special forms are meaningless to macsyma.
809 (cond ((get fun
'*lexpr
) 'lsubr
)
810 ((get fun
'*expr
) 'subr
)
811 ;; *LEXPR & *EXPR gotten from DEFMFUN declarations
812 ;; which is loaded by TrData.
813 ((mget fun
'$fixed_num_args_function
)
815 ((mget fun
'$variable_num_args_function
)
817 ((setq temp
(getl fun
'(expr subr lsubr
)))
819 ((get fun
'once-translated
))
820 ((get fun
'translated
))
823 (defun tr-infamous-noun-form (form)
824 ;; 'F(X,Y) means noun-form. The arguments are evaluated.
825 ;; but the function is cons on, not applied.
826 ;; N.B. for special forms and macros this is totally wrong.
827 ;; But, those cases are filtered out already, presumably.
829 (let ((op (cond ((member 'array
(car form
) :test
#'eq
)
830 `(,(caar form
) array
))
831 (t `(,(caar form
)))))
832 (args (tr-args (cdr form
))))
833 `($any .
(simplify (list ',op
,@args
)))))
835 ;;; Some atoms, solely by usage, are self evaluating.
836 (defun implied-quotep (x)
837 (safe-get x
'implied-quotep
))
839 (defun self-evaluating-lisp-object-p (x)
840 (not (or (symbolp x
) (consp x
))))
842 ;;; the Translation Properties. the heart of TRANSL.
844 ;;; This conses up the call to the function, adding in the
845 ;;; SIMPLIFY i the mode is $ANY. This should be called everywhere.
846 ;;; instead of duplicating the COND everywhere, as is done now in TRANSL.
848 (defun tr-nosimpp (op)
853 (defun call-and-simp (mode fun args
)
854 (cond ((or (not (eq mode
'$any
))
856 `(,mode
,fun .
,args
))
858 `(,mode simplify
(,fun .
,args
)))))
860 (defmspec $declare_translated
(fns)
863 when
(or (symbolp v
) (and (stringp v
) (setq v
($verbify v
))))
864 do
(setf (get v
'once-translated
) t
)
865 (pushnew v
*declared-translated-functions
*)
866 else do
(merror (intl:gettext
"declare_translated: arguments must be symbols or strings; found: ~:M") v
)))
868 (def%tr $eval_when
(form)
869 (tr-format (intl:gettext
"error: found 'eval_when' in a function or expression: ~:M~%") form
)
870 (tr-format (intl:gettext
"note: 'eval_when' can appear only at the top level in a file.~%"))
874 (def%tr mdefmacro
(form)
875 (tr-format (intl:gettext
"warning: globally defining macro ~:M now to ensure correct macro expansions.~%") (caaadr form
))
876 ; Define the macro now to ensure that it's defined when it's time
877 ; to expand it. It's a bug that this definition occurs during
878 ; translation without being cleaned it up afterward, but simply
879 ; removing this breaks things.
881 (punt-to-meval form
))
883 (def%tr $local
(form)
885 (tr-format (intl:gettext
"error: there is already a 'local' in this block.~%"))
887 (return-from $local nil
))
889 ; We can't just translate to a call to MLOCAL here (which is
890 ; what used to happen). That would push onto LOCLIST and bind
891 ; MLOCP at the "wrong time". The push onto LOCLIST and the
892 ; binding of MLOCP are handled in TR-LAMBDA.
893 (punt-to-meval form
))
896 (def%tr mquote
(form)
897 (list (tr-class (cadr form
)) 'quote
(cadr form
)))
900 (defun tr-lambda (form &optional
(tr-body #'tr-seq
) &rest tr-body-argl
902 (arglist (mparams (cadr form
)))
905 ;; This function is defined to take a simple macsyma lambda expression and
906 ;; return a simple lisp lambda expression. The optional TR-BODY hook
907 ;; can be used for translating other special forms that do lambda binding.
909 ;; Local SPECIAL declarations are not used because
910 ;; the multics lisp compiler does not support them. They are of course
911 ;; a purely syntactic construct that doesn't buy much. I have been
912 ;; advocating the use of DEFINE_VARIABLE in macsyma user programs so
913 ;; that the use of DECLARE(FOO,SPECIAL) will be phased out at that level.
915 (mapc #'tbind arglist
)
916 (destructuring-let* (((mode . nbody
) (apply tr-body
(cddr form
) tr-body-argl
))
917 (local-declares (make-declares arglist t
))
925 ;; -> BINDING of variables with ASSIGN properties may be difficult to
926 ;; do correctly and efficiently if arbitrary code is to be run.
927 (if (or tr-lambda-punt-assigns
928 (do ((l arglist
(cdr l
)))
931 (assign (get var
'assign
)))
933 (cond ((eq assign
'assign-mode-check
)
934 (push `(,assign
',var
,(teval var
)) easy-assigns
))
937 ;; Case with EASY or no ASSIGN's
938 `(,mode .
(lambda ,(tunbinds arglist
)
942 ;; Case with arbitrary ASSIGN's.
943 (let ((temps (mapcar #'(lambda (ign) ign
(tr-gensym)) arglist
)))
944 `(,mode .
(lambda ,temps
947 ;; [1] Check before binding.
948 ,@(mapcan #'(lambda (var val
)
949 (let ((assign (get var
'assign
)))
951 (let ((assign-fn (if (symbolp assign
) `(quote ,assign
) (coerce assign
'function
))))
952 (list `(funcall ,assign-fn
',var
,val
))))))
954 ;; [2] do the binding.
955 ((lambda ,(tunbinds arglist
)
959 ;; [2] check when unbinding too.
960 ,@(mapcan #'(lambda (var)
961 (let ((assign (get var
'assign
)))
963 (let ((assign-fn (if (symbolp assign
) `(quote ,assign
) (coerce assign
'function
))))
964 (list `(funcall ,assign-fn
',var
968 ,(dtranslate var
)))))))
972 (defun make-declares (varlist localp
&aux
(dl) (fx) (fl) specs
)
973 (do ((l varlist
(cdr l
))
977 ;; When a variable is declared special, be sure to declare it
979 (when (and localp
(tr-get-special (car l
)))
980 (push (car l
) specs
))
982 (when (or (not localp
)
983 (not (tr-get-special (car l
))))
984 ;; don't output local declarations on special variables.
985 (setq var
(teval (car l
)) mode
(value-mode var
))
986 (setq specs
(cons var specs
))
988 (cond ((eq '$fixnum mode
) (pushnew var fx
:test
#'eq
))
989 ((eq '$float mode
) (pushnew var fl
:test
#'eq
)))))
990 (if fx
(pushnew `(fixnum .
,fx
) dl
:test
#'eq
))
991 (if fl
(pushnew `(type flonum .
,fl
) dl
:test
#'eq
))
992 (if specs
(pushnew `(special .
,specs
) dl
:test
#'eq
))
993 (if dl
`(declare .
,dl
)))
999 (cons mode
(nreverse body
)))
1000 (let ((exp (translate (pop l
))))
1001 (setq mode
(car exp
))
1002 (push (cdr exp
) body
))))
1004 (def%tr mprogn
(form)
1005 (setq form
(tr-seq (cdr form
)))
1006 (cons (car form
) `(progn ,@(cdr form
))))
1009 (or (symbolp e
) (integerp e
)))
1011 (def%tr mprog
(form)
1012 (let (arglist body val-list
)
1013 ;; [1] normalize the MPROG syntax.
1014 (cond (($listp
(cadr form
))
1015 (setq arglist
(cdadr form
)
1021 (setq body
'(((mquote) $done
)))))
1022 (setq val-list
(mapcar #'(lambda (u)
1024 (translate (caddr u
))))
1026 arglist
(mapcar #'(lambda (u)
1027 ;; X or ((MSETQ) X Y)
1028 (if (atom u
) u
(cadr u
)))
1030 (let ((dup (find-duplicate arglist
:test
#'eq
)))
1032 (tr-format (intl:gettext
"error: ~M occurs more than once in block variable list") dup
)
1034 (return-from mprog nil
)))
1037 ;; [2] call the lambda translator.
1038 `((lambda) ((mlist) ,@arglist
) ,@body
)
1039 ;; [3] supply our own body translator.
1043 (cons (car form
) `(,(cdr form
) ,@val-list
))))
1045 (defun tr-mprog-body (body val-list arglist
1050 (*returns
* nil
) ;; not used but must be bound.
1054 ;; [5] hack the val-list for the mode context.
1055 ;; Perhaps the only use of the function MAP in all of macsyma.
1056 (mapl #'(lambda (val-list arglist
)
1057 (cond ((atom (car val-list
))
1059 (or (cdr (assoc (value-mode (car arglist
))
1060 mode-init-value-table
:test
#'eq
))
1063 (warn-mode (car arglist
)
1064 (value-mode (car arglist
))
1065 (car (car val-list
))
1066 "in a `block' statement")
1067 (rplaca val-list
(cdr (car val-list
))))))
1069 (setq l
(nreverse l
))
1072 `((prog () ,@(delete nil l
:test
#'equal
)))
1074 ;; [4] translate a form in the body
1075 (let ((form (pop body
)))
1077 ;; this is a really bad case.
1078 ;; we don't really know if the return mode
1079 ;; of the expression is for the value of the block.
1080 ;; Some people write RETURN at the end of a block
1081 ;; and some don't. In any case, the people not
1082 ;; use the PROG programming style won't be screwed
1084 (setq form
(translate form
))
1085 (setq return-mode
(*union-mode
(car form
) return-mode
))
1086 (setq form
(cdr form
))
1089 (not (eq (car form
) 'return
))))
1090 ;; put a RETURN on just in case.
1091 (setq form
`(return ,form
))))
1094 (setq form
(dtranslate form
))))
1097 (def%tr mreturn
(form)
1098 (if (null inside-mprog
)
1099 (tr-format (intl:gettext
"warning: 'return' not within 'block' or 'do': ~:M~%") form
))
1101 (setq form
(translate (cadr form
)))
1102 (setq return-mode
(if return-mode
(*union-mode
(car form
) return-mode
)
1104 (setq form
`(return ,(cdr form
)))
1105 (push form
*returns
*) ;; USED by lusing MDO etc not yet re-written.
1106 ;; MODE here should be $PHANTOM or something.
1110 (if (null inside-mprog
)
1111 (tr-format (intl:gettext
"warning: 'go' not within 'block' or 'do': ~:M~%") form
))
1112 (if (not (go-tag-p (cadr form
)))
1113 (tr-format (intl:gettext
"warning: 'go' tag must be a symbol or an integer: ~:M~%") form
))
1115 `($any .
(go ,(cadr form
))))
1117 (def%tr mqapply
(form)
1118 (let ((fn (cadr form
)) (args (cddr form
))
1119 (aryp (member 'array
(cdar form
) :test
#'eq
)))
1121 ;; I'm guessing (ATOM FN) is a parser error or other Lisp error,
1122 ;; so don't bother to translate the following error message.
1123 (tr-format "translator: MQAPPLY operator must be a cons; found: ~:M" form
)
1125 ((eq (caar fn
) 'mquote
)
1126 `($any list
',(cons (cadr fn
) aryp
) ,@(tr-args args
)))
1127 ((eq (caar fn
) 'lambda
)
1128 (let ((args (tr-args args
))
1129 (fn (translate fn
)))
1130 (cons (car fn
) `(mfuncall ,(cdr fn
) ,@args
))))
1132 `($any simplify
(mapply ,(dconvx (translate fn
))
1133 (list ,@(tr-args args
))
1137 (punt-to-meval form
)))))
1139 (defun mcond-eval-symbols-tr (form)
1140 (mcond-eval-symbols #'maybe-msymeval form
))
1142 (def%tr mcond
(form)
1143 (let ((g (tr-gensym))
1146 (do ((l (cdr form
) (cddr l
))) ((null l
))
1147 ; Optimize the else-if case: if we're at the else case at the end
1148 ; and the body is just another conditional, then we just continue
1149 ; directly with the clauses of the inner conditional instead of
1151 (when (and (null (cddr l
))
1154 (eq (caaadr l
) 'mcond
))
1156 (let ((wrap-a-pred 'mcond
))
1157 (declare (special wrap-a-pred
))
1158 (destructuring-let (((pred-mode . pred-tr
) (translate-predicate (car l
)))
1159 ((body-mode . body-tr
) (translate (cadr l
))))
1160 (setq mode
(*union-mode mode body-mode
))
1161 (if (eq pred-mode
'$boolean
)
1162 (setq nl
(list* body-tr pred-tr nl
))
1163 (setq nl
(list* `(list* '(mcond) ,g
(mapcar #'mcond-eval-symbols-tr
',(cdr l
)))
1166 `(eq t
(setq ,g
,pred-tr
))
1168 ; We leave off the final clause if the condition is true
1169 ; and the consequent is false.
1170 (when (and (eq t
(cadr nl
)) (null (car nl
)))
1171 (setq nl
(cddr nl
)))
1173 (do ((l nl
(cddr l
))) ((null l
))
1175 (cons (cons (cadr l
)
1176 (cond ((and (not (atom (car l
)))
1178 (eq (caar l
) 'progn
))
1180 ((and (equal (car l
) (cadr l
))
1181 (atom (car l
))) nil
)
1182 (t (list (car l
)))))
1185 (cons '$any
`(let (,g
) (cond ,@form
)))
1186 (cons mode
`(cond ,@form
)))))
1188 ;; The MDO and MDOIN translators should be changed to use the TR-LAMBDA.
1189 ;; Perhaps a mere expansion into an MPROG would be best.
1192 (let (*returns
* assigns return-mode
(inside-mprog t
) need-prog?
)
1193 (let (mode var init next test-form action varmode
)
1194 (setq var
(cond ((cadr form
)) (t 'mdo
)))
1196 (setq init
(if (caddr form
) (translate (caddr form
)) '($fixnum .
1)))
1197 (cond ((not (setq varmode
(tr-get-mode var
)))
1198 (declvalue var
(car init
) t
)))
1199 (setq next
(translate (cond ((cadddr form
) (list '(mplus) (cadddr form
) var
))
1200 ((car (cddddr form
)))
1201 (t (list '(mplus) 1 var
)))))
1202 (setq form
(copy-list form
))
1203 ;;to make the end test for thru be numberp if the index is numberp
1204 ;;and to eliminate reevaluation
1205 (cond ((not varmode
)
1206 (declvalue var
(*union-mode
(car init
) (car next
)) t
))
1208 (warn-mode var varmode
(*union-mode
(car init
) (car next
)))))
1209 (destructuring-bind (test-mode . test-pred
)
1210 (translate-predicate
1212 (cond ((null (cadr (cddddr form
))) nil
)
1214 (mnegp ($numfactor
(simplify (cadddr form
)))))
1215 (list '(mlessp) var
(cadr (cddddr form
))))
1216 (t (list '(mgreaterp) var
(cadr (cddddr form
)))))
1217 (caddr (cddddr form
))))
1218 (if (eq test-mode
'$boolean
)
1219 (setq test-form test-pred
)
1220 (setq test-form
`(let (($prederror t
)) ,test-pred
))))
1221 (setq action
(translate (cadddr (cddddr form
)))
1222 mode
(cond ((null *returns
*) '$any
)
1224 (setq var
(tunbind (cond ((cadr form
)) (t 'mdo
))))
1225 `(,mode do
((,var
,(cdr init
) ,(cdr next
)))
1226 (,test-form
'$done
) .
((declare (special ,var
)) .
1227 ,(cond ((atom (cdr action
)) nil
)
1228 ((eq 'progn
(cadr action
)) (cddr action
))
1229 (t (list (cdr action
)))))))))
1231 (def%tr mdoin
(form)
1232 (let (*returns
* assigns return-mode
(inside-mprog t
) need-prog?
)
1233 (prog (mode var init action
)
1234 (setq var
(tbind (cadr form
))) (tbind 'mdo
)
1235 (setq init
(dtranslate (caddr form
)))
1236 (cond ((or (cadr (cddddr form
)) (caddr (cddddr form
)))
1237 (tunbind 'mdo
) (tunbind (cadr form
))
1238 (return (punt-to-meval `((mdoin) .
,(cdr form
))))))
1239 (setq action
(translate (cadddr (cddddr form
)))
1240 mode
(cond ((null *returns
*) '$any
)
1242 (tunbind 'mdo
) (tunbind (cadr form
))
1244 `(,mode do
((,var
) (mdo (cdr ,init
) (cdr mdo
)))
1245 ((null mdo
) '$done
) .
1246 ((declare (special ,var
)) (setq ,var
(car mdo
)) .
1247 ,(cond ((atom (cdr action
)) nil
)
1248 ((eq 'progn
(cadr action
)) (cddr action
))
1249 (t (list (cdr action
))))))))))
1252 (defun lambda-wrap1 (tn val form
)
1254 (eq (car val
) 'quote
))
1256 `((lambda (,tn
) ,form
) ,val
)))
1258 (def%tr msetq
(form)
1259 (let ((var (cadr form
))
1264 (setq mode
(value-mode var
) val
(translate val
))
1265 (warn-mode var mode
(car val
))
1267 (setq mode
(car val
) val
(cdr val
))
1268 (setq val
(dconv val mode
)))
1270 (if (setq assign
(get var
'assign
))
1271 (let ((tn (tr-gensym))
1272 (assign-fn (if (symbolp assign
) `(quote ,assign
) (coerce assign
'function
))))
1273 (lambda-wrap1 tn val
`(let nil
1274 (declare (special ,var
,(teval var
)))
1275 (funcall ,assign-fn
',var
,tn
)
1276 (setq ,(teval var
) ,tn
))))
1277 `(let nil
(declare (special ,(teval var
)))
1278 (if (not (boundp ',(teval var
)))
1279 (add2lnc ',(teval var
) $values
))
1280 (,(if *macexpr-top-level-form-p
*
1283 ,(teval var
) ,val
)))))
1284 ((member 'array
(car var
) :test
#'eq
)
1285 (tr-arraysetq var val
))
1287 (unless (safe-get (caar var
) 'mset_extension_operator
)
1288 (tr-format (intl:gettext
"warning: no assignment operator known for ~:M~%") var
)
1289 (tr-format (intl:gettext
"note: just keep going and hope for the best.~%")))
1290 (setq val
(translate val
))
1291 `(,(car val
) mset
',var
,(cdr val
))))))
1293 (def%tr $max
(x) (translate-$max-$min x
))
1294 (def%tr $min
(x) (translate-$max-$min x
))
1295 (def%tr %max
(x) (translate-$max-$min x
))
1296 (def%tr %min
(x) (translate-$max-$min x
))
1298 (defun translate-$max-$min
(form)
1299 (let ((mode) (arglist) (op (stripdollar (caar form
))))
1302 (setq l
(translate l
))
1304 (setq mode
(car l
)))
1310 ; To match the interpreted case, and to make sure we use the
1311 ; correct mode for the return value, we do not apply float
1312 ; contagion to the arguments and we use a special translation
1313 ; to call MAX or MIN only when every argument has the same
1314 ; mode (either all fixnum or all float). CLHS says that
1315 ; implementations have choices they can make about what MAX
1316 ; and MIN return when the arguments are a mix of float and
1318 ; Example: if an implementation decides to apply float contagion
1319 ; to the arguments of MAX (MIN), then it can return either an
1320 ; integer or a float if the greatest (least) argument was an
1322 (if (member mode
'($fixnum $float
) :test
#'eq
)
1323 `(,mode
,(if (eq 'min op
) 'min
'max
) .
,(mapcar 'cdr arglist
))
1324 `($any
,(if (eq 'min op
) '$lmin
'$lmax
)
1325 (list '(mlist) .
,(mapcar 'dconvx arglist
))))))
1328 ;;; mode accessing, binding, handling. Super over-simplified.
1331 (cond ((integerp x
) '$fixnum
)
1332 ((floatp x
) '$float
)
1333 ((member x
'(t nil
) :test
#'eq
) '$boolean
)
1335 ((eq 'rat
(caar x
)) '$rational
)
1338 (defun *union-mode
(mode1 mode2
)
1339 (cond ((eq mode1 mode2
) mode1
)
1340 ((null mode1
) mode2
)
1341 ((null mode2
) mode1
)
1342 ((eq '$boolean mode1
) '$any
)
1343 ((eq '$boolean mode2
) '$any
)
1344 ((member mode2
*$any-modes
* :test
#'eq
) '$any
)
1345 ((member mode1
*$any-modes
* :test
#'eq
) '$any
)
1346 ((eq '$fixnum mode1
) mode2
)
1348 (if (eq '$number mode2
) '$number
'$float
))
1349 ((eq '$rational mode1
)
1350 (if (eq '$float mode2
) '$float
'$any
))
1351 ((eq '$number mode1
)
1352 (if (eq '$rational mode2
) '$any
'$number
))
1355 (defun value-mode (var)
1356 (cond ((tr-get-mode var
))
1358 (warn-undeclared var
)
1361 (defun decmode-arrayfun (f m
)
1362 (putprop f m
'arrayfun-mode
))
1364 (defun array-mode (ar)
1365 (cond ((get ar
'array-mode
)) (t '$any
)))
1367 (defun arrayfun-mode (ar)
1368 (cond ((get ar
'arrayfun-mode
)) (t '$any
)))
1370 (defun function-mode (f)
1371 (cond ((get f
'function-mode
)) (t '$any
)))
1373 (defun function-mode-@ (f)
1374 (ass-eq-ref (tr-get-val-modes f
) 'function-mode
'$any
))
1376 (defun array-mode-@ (f)
1377 (ass-eq-ref (tr-get-val-modes f
) 'array-mode
'$any
))
1380 (defvar $tr_bind_mode_hook nil
1381 "A hack to allow users to key the modes of variables
1382 off of variable spelling, and other things like that.")
1384 ;; TBIND, below, copies the MODE, VAL-MODES, and SPECIAL properties
1385 ;; into the a table named TSTACK, and then removes those properties.
1386 ;; So if TBIND has been called, we will need to look for those
1387 ;; properties in TSTACK instead of the symbol property list.
1389 (defstruct (tstack-slot (:conc-name tstack-slot-
))
1393 ;; an alist telling second order info
1394 ;; about APPLY(VAR,[X]), ARRAYAPPLY(F,[X]) etc.
1397 (defun tr-get-mode (a)
1399 (let ((my-slot (cdr (assoc a tstack
))))
1400 (tstack-slot-mode my-slot
))
1403 #-gcl
(defun (setf tr-get-mode
) (b a
)
1405 (let ((my-slot (cdr (assoc a tstack
))))
1406 (setf (tstack-slot-mode my-slot
) b
))
1407 (setf (get a
'mode
) b
)))
1409 #+gcl
(defsetf tr-get-mode
(a) (b)
1410 `(if (get ,a
'tbind
)
1411 (let ((my-slot (cdr (assoc ,a tstack
))))
1412 (setf (tstack-slot-mode my-slot
) ,b
))
1413 (setf (get ,a
'mode
) ,b
)))
1415 (defun tr-get-val-modes (a)
1417 (let ((my-slot (cdr (assoc a tstack
))))
1418 (tstack-slot-val-modes my-slot
))
1419 (get a
'val-modes
)))
1421 #-gcl
(defun (setf tr-get-val-modes
) (b a
)
1423 (let ((my-slot (cdr (assoc a tstack
))))
1424 (setf (tstack-slot-val-modes my-slot
) b
))
1425 (setf (get a
'val-modes
) b
)))
1427 #+gcl
(defsetf tr-get-val-modes
(a) (b)
1428 `(if (get ,a
'tbind
)
1429 (let ((my-slot (cdr (assoc ,a tstack
))))
1430 (setf (tstack-slot-val-modes my-slot
) ,b
))
1431 (setf (get ,a
'val-modes
) ,b
)))
1433 (defun tr-get-special (a)
1435 (let ((my-slot (cdr (assoc a tstack
))))
1436 (tstack-slot-special my-slot
))
1439 #-gcl
(defun (setf tr-get-special
) (b a
)
1441 (let ((my-slot (cdr (assoc a tstack
))))
1442 (setf (tstack-slot-special my-slot
) b
))
1443 (setf (get a
'special
) b
)))
1445 #+gcl
(defsetf tr-get-special
(a) (b)
1446 `(if (get ,a
'tbind
)
1447 (let ((my-slot (cdr (assoc ,a tstack
))))
1448 (setf (tstack-slot-special my-slot
) ,b
))
1449 (setf (get ,a
'special
) ,b
)))
1451 ;;; should be a macro (TBINDV <var-list> ... forms)
1452 ;;; so that TUNBIND is assured, and also so that the stupid ASSQ doesn't
1453 ;;; have to be done on the darn TSTACK. This will have to wait till
1454 ;;; the basic special form translation properties are rewritten.
1456 (defun variable-p (var)
1457 (and var
(symbolp var
) (not (eq var t
))))
1459 (defun bad-var-warn (var)
1460 (tr-format (intl:gettext
"warning: ~:M cannot be used as a variable.~%") var
))
1462 (defun tbind (var &aux old
)
1463 (cond ((variable-p var
)
1464 (setq old
(make-tstack-slot :mode
(get var
'mode
)
1465 :tbind
(get var
'tbind
)
1466 :val-modes
(get var
'val-modes
)
1467 :special
(get var
'special
)))
1468 (push (cons var old
) tstack
)
1469 (cond ((not (specialp var
))
1470 ;; It is the lisp convention in use to inherit
1471 ;; specialness from higher context.
1472 ;; Spurious MODEDECLARATIONS get put in the environment
1473 ;; when code is MEVAL'd since there is no way to stack
1474 ;; the mode properties. Certainly nobody is willing
1475 ;; to hack MEVAL in JPG;MLISP
1476 (remprop var
'val-modes
)
1478 (remprop var
'special
)))
1479 (putprop var var
'tbind
)
1480 (if $tr_bind_mode_hook
1481 (let ((mode?
(mapply $tr_bind_mode_hook
1483 '$tr_bind_mode_hook
)))
1484 (if mode?
(tr-declare-varmode var mode?
))))
1487 (bad-var-warn var
))))
1489 (defun tunbind (var &aux
(old (assoc var tstack
:test
#'eq
)))
1490 (when (variable-p var
)
1494 (setq tstack
(delete old tstack
:test
#'eq
)) ; POP should be all we need.
1495 (setq old
(cdr old
))
1496 (putprop1 var
(tstack-slot-mode old
) 'mode
)
1497 (putprop1 var
(tstack-slot-tbind old
) 'tbind
)
1498 (putprop1 var
(tstack-slot-val-modes old
) 'val-modes
)
1499 (putprop1 var
(tstack-slot-special old
) 'special
))))))
1501 (defun putprop1 (name value key
)
1502 ;; leaves property list clean after unwinding, this
1503 ;; is an efficiency/storage issue only.
1505 (putprop name value key
)
1513 (setq nl
(cons (tunbind (caar tstack
)) nl
)
1516 (defun tboundp (var)
1517 ;; really LEXICAL-VARP.
1518 (and (symbolp var
) (get var
'tbind
) (not (tr-get-special var
))))
1521 (or (and (symbolp var
) (get var
'tbind
)) var
))