Do not allow duplicate elements in parameter/variable lists
[maxima.git] / src / transl.lisp
blob500e6c35fd4355c2852137a499b8c0d221981af7
1 ;;; -*- Mode: Lisp; Package: Maxima; Syntax: Common-Lisp; Base: 10 -*- ;;;;
2 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3 ;;; The data in this file contains enhancments. ;;;;;
4 ;;; ;;;;;
5 ;;; Copyright (c) 1984,1987 by William Schelter,University of Texas ;;;;;
6 ;;; All rights reserved ;;;;;
7 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
8 ;;; (c) Copyright 1980 Massachusetts Institute of Technology ;;;
9 ;;; GJC 9:29am Saturday, 5 April 1980 ;;;
10 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
12 (in-package :maxima)
14 (macsyma-module transl)
16 ;;; File directory.
18 ;;; TRANSL Driver. Basic translation properties.
19 ;;; TRANSS User-interaction, FILE-I/O etc.
20 ;;; TRANS1 Translation of JPG;MLISP and other FSUBRS.
21 ;;; which take call-by-name parameters.
22 ;;; TRANS2 LISTS, ARRAYs, other random operators.
23 ;;; TRANS3 LAMBDA. CLOSURES. also used by fsubr call-by-name
24 ;;; compatibility package.
25 ;;; TRANS4 operators, ".", "^^" some functions such as GAMMA.
26 ;;; TRANS5 FSUBRS from COMM, and others, these are mere MACRO
27 ;;; FSUBRS.
28 ;;; TRANSF floating point intensive properties. BIGFLOAT stuff.
29 ;;; TROPER Basic OPERATORS.
30 ;;; TRUTIL transl utilities.
31 ;;; TRMODE definition of MODEDECLARE. run time error checking code.
32 ;;; TRDATA this is the MODE data for the "built-in" functions.
33 ;;; TRANSM This defines the macro DEF%TR. When compiled on MC
34 ;;; DEF%TR produces autoload definitions for TRANS1 thru L.
35 ;;; PROCS macro's needed.
36 ;;; TRANSQ these are macros for translated code. Loaded by TPRELU
37 ;;; this is compile-time only.
38 ;;; MDEFUN contains the macro which defines macsyma functions.
39 ;;; runtime and compile-time.
40 ;;; ACALL is some run time support for translated code, array calls.
41 ;;; FCALL run-time translated function call support for uncompiled
42 ;;; code. Many FSUBRS which are macros in TRANSQ.
43 ;;; EVALW EVAL-WHEN definition for interpreter.
44 ;;; MLOAD This has a hack hook into BATCH, which is needed to do
45 ;;; TRANSLATE_FILE I/O. when using old-i/o SUPRV.
48 ;;; Functions and literals have various MODE properties;;; >
49 ;;; (at user level set up by $MODEDECLARE), such as "$FLOAT" and "$ANY".
50 ;;; The main problem solved by this translator (and the reason that
51 ;;; it works on forms from the "inside out" as an evaluator would do
52 ;;; (expect for macro forms)), is the problem of type (MODE) dependent
53 ;;; function calling and mode conversion. The function TRANSLATE
54 ;;; returns a list where the CAR of the list is the MODE of the
55 ;;; expression and the CDR is the expression to be evaluated by
56 ;;; the lisp evaluator to give the equivalent result of evaluating
57 ;;; the given macsyma expression with the macsyma evaluator.
58 ;;; One doesn't know the MODE of an expression until seeing the modes
59 ;;; of all its parts. See "*UNION-MODE"
61 ;;; weak points in the code
62 ;;; [1] duplication of functionality in the translators for
63 ;;; MPLUS MTIMES etc.
64 ;;; [3] primitive mode scheme. lack of even the most primitive general
65 ;;; type coercion code. Most FORTRAN compilers are better than this.
66 ;;; [4] for a compiler, this code SUCKS when it comes to error checking
67 ;;; of the code it is munging. It doesn't even do a WNA check of system
68 ;;; functions!
69 ;;; [5]
70 ;;; The duplication of the code which handles lambda binding, in MDO, MDOIN
71 ;;; TR-LAMBDA, and MPROG, is very stupid. For macsyma this is one of
72 ;;; the hairier things. Declarations must be handled, ASSIGN properties...
73 ;;; -> Binding of ASSIGN properties should be handled with he "new"
74 ;;; UNWIND-PROTECT instead of at each RETURN, and at "hope" points such as
75 ;;; the ERRLIST. {Why wasn't this obvious need for UNWIND-PROTECT made
76 ;;; known to the lisp implementers by the macsyma implementers? Why did it
77 ;;; have to wait for the lisp machine group? Isn't this just a generalization
78 ;;; of special binding?}
79 ;;; [6] the DCONVX idea here is obscurely coded, incomplete, and totally
80 ;;; undocumented. It was probably an attempt to hack efficient
81 ;;; internal representations (internal to a given function), for some macsyma
82 ;;; data constructs, and yet still be sure that fully general legal data
83 ;;; frobs are seen outside of the functions. Note: this can be done
84 ;;; simply by type coercion and operator folding.
86 ;;; General comments on the structure of the code.
87 ;;; A function named TR-<something> means that it translates
88 ;;; something having to do with that something.
89 ;;; N.B. It does not mean that that is the translate property for <something>.
92 (defvar *untranslated-functions-called* nil)
94 (defmvar *declared-translated-functions* nil
95 "List of functions which are believed to be translated.")
97 (defmvar $tr_semicompile nil
98 "If TRUE TRANSLATE_FILE and COMPFILE output forms which will~
99 be macroexpanded but not compiled into machine code by the~
100 lisp compiler.")
102 (defmvar $transcompile t
103 "If TRUE TRANSLATE_FILE outputs declarations for the COMPLR.
104 The only use of the switch is to save the space declarations take
105 up in interpreted code.")
107 (defmvar tstack nil " stack of local variable modes ")
109 (defmvar local nil "T if a $local statement is in the body.")
110 (defmvar arrays nil "arrays to declare to `complr'")
111 (defmvar lexprs nil "Lexprs to declare.")
112 (defmvar exprs nil "what else?")
113 (defmvar fexprs nil "Fexprs to declare.")
114 (defmvar tr-progret t)
115 (defmvar inside-mprog nil)
116 (defmvar returns nil "list of `translate'd return forms in the block.")
117 (defmvar return-mode nil "the highest(?) mode of all the returns.")
118 (defmvar need-prog? nil)
119 (defmvar assigns nil "These are very-special variables which have a Maxima
120 assign property which must be called to bind and unbind the variable
121 whenever it is `lambda' bound.")
123 (defmvar specials nil "variables to declare special to the complr.")
125 (defmvar translate-time-evalables
126 '($modedeclare $alias $declare $infix $nofix $declare_translated
127 $matchfix $prefix $postfix $compfile))
129 (defmvar *transl-backtrace* nil
130 " What do you think? ")
132 (defmvar *transl-debug* nil "if T it pushes `backtrace' and `trace' ")
134 (defmvar tr-abort nil "set to T if abortion is requested by any of the
135 sub-parts of the translation. A *THROW would be better, although it
136 wouldn't cause the rest of the translation to continue, which may
137 be useful in translation for MAXIMA-ERROR checking.")
139 (defmvar tr-unique (gensym)
140 "this is just a unque object used for random purposes,
141 such as the second (file end) argument of READ.")
144 (defmvar $tr_warn_undeclared '$compile
145 "When to send warnings about undeclared variables to the TTY")
147 (defmvar $tr_warn_meval '$compfile
148 "If `meval' is called that indicates problems in the translation")
150 (defmvar $tr_warn_fexpr
151 '$compfile
152 "FEXPRS should not normally be output in translated code, all legitimate
153 special program forms are translated.")
155 (defmvar $tr_warn_mode '$all
156 "Warn when variables are assigned values out of their mode.")
158 (defmvar $tr_warn_undefined_variable '$all
159 "Warn when undefined global variables are seen.")
162 (defmvar *warned-un-declared-vars* nil "Warning State variable")
163 (defmvar *warned-fexprs* nil "Warning State variable")
164 (defmvar *warned-mode-vars* nil "Warning State variable")
166 (defmvar $tr_function_call_default '$general
168 FALSE means punt to MEVAL, EXPR means assume lisp fixed arg function.
169 GENERAL, the default gives code good for mexprs and mlexprs but not macros.
170 GENERAL assures variable bindings are correct in compiled code.
171 In GENERAL mode, when translating F(X), if F is a bound variable, then
172 it assumes that APPLY(F,[X]) is meant, and translates a such, with
173 appropriate warning. There is no need to turn this off.
174 APPLY means like APPLY.")
176 (defmvar $tr_array_as_ref t
177 "If true runtime code uses value of the variable as the array.")
179 (defmvar $tr_numer nil
180 "If `true' numer properties are used for atoms which have them, e.g. %pi")
182 (defmvar $tr_predicate_brain_damage nil
183 "If TRUE, output possible multiple evaluations in an attempt
184 to interface to the COMPARE package.")
186 (defvar boolean-object-table
187 '(($true . ($boolean . t))
188 ($false . ($boolean . nil))
189 (t . ($boolean . t))
190 (nil . ($boolean . nil))))
192 (defvar mode-init-value-table
193 '(($float . 0.0)
194 ($fixnum . 0)
195 ($number . 0)
196 ($list . '((mlist)))
197 ($boolean . nil)))
199 (defvar tr-lambda-punt-assigns nil
200 "Kludge argument to `tr-lambda' due to lack of keyword argument passing")
202 (or (boundp '*in-compile*)
203 (setq *in-compile* nil))
205 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
208 (defun barfo (&rest l)
209 (apply #'tr-format
210 (nconc (list (intl:gettext "translator: internal error. Message: ~:M~%")) l))
211 (cond (*transl-debug*
212 (break "transl barfo ~S" t))
214 (setq tr-abort t)
215 nil)))
217 (defun specialp (var)
218 (cond ((or (optionp var)
219 (get var 'special))
220 (if $transcompile (pushnew var specials :test #'eq))
221 t)))
224 ;;; The error message system. Crude as it is.
225 ;;; I tell you how this aught to work:
226 ;;; (1) All state should be in one structure, one state variable.
227 ;;; (2) Should print out short message on-the-fly so that it
228 ;;; gives something to watch, and also so that it says something
229 ;;; if things crash.
230 ;;; (3) Summaries on a cross-referenced per-function and per-item
231 ;;; should be printed at the end, as a table.
232 ;;; e.g.
233 ;;; Undefined Functions used in
234 ;;; FOO BAR, BAZ,BOMB
235 ;;; FOOA P,Q
236 ;;; Undefined Variables ... same thing
237 ;;; Incomprehensible special forms
238 ;;; EV ....
239 ;;; Predicate Mode Targeting failures.
240 ;;; ..... -gjc
242 ;;; The way it works now is to print too little or too much.
243 ;;; Many items are only warned about the first time seen.
244 ;;; However, this isn't too much of a problem when using Emacs
245 ;;; to edit code, because searching for warned-about tokens
246 ;;; is quick and easy.
248 (defmvar *tr-warn-break* t
249 " if in debug mode `warning's signaled go to lisp break loops ")
251 (defmacro tr-warnbreak ()
252 `(and *transl-debug* *tr-warn-break* (break "transl ~S" t )))
254 (defun tr-warnp (val)
255 (and val
256 (cond (*in-compile*
257 (member val '($all $compile $compfile $translate) :test #'eq))
258 ((or *in-compfile* *in-translate-file*)
259 (member val '($all $compfile $translate) :test #'eq))
260 (*in-translate*
261 (member val '($all $translate) :test #'eq)))))
263 (defvar warned-undefined-variables nil)
265 (defun warn-undefined-variable (form)
266 (and (tr-warnp $tr_warn_undefined_variable)
267 (cond ((member form warned-undefined-variables :test #'eq))
269 (push form warned-undefined-variables)
270 (tr-format (intl:gettext "warning: encountered undefined variable ~:M in translation.~%") form)
271 (tr-warnbreak)))))
273 (defun warn-undeclared (form &optional comment)
274 (and (tr-warnp $tr_warn_undeclared)
275 (cond ((member form *warned-un-declared-vars* :test #'equal) t)
277 (push form *warned-un-declared-vars*)
278 (tr-format (intl:gettext "warning: no type declaration for ~:M; assume type is 'any'.~%") form)
279 (tr-format (intl:gettext "note: 'modedeclare' declares types for translation.~%"))
280 (cond (comment
281 (dolist (v *translation-msgs-files*)
282 (terpri v)
283 (princ comment v))))
284 (tr-warnbreak)
285 nil))))
287 (defun warn-meval (form &optional comment)
288 (cond ((tr-warnp $tr_warn_meval)
289 (tr-format (intl:gettext "warning: emit call to MEVAL for expression: ~:M~%") form)
290 (cond (comment (dolist (v *translation-msgs-files*)
291 (terpri v)
292 (princ comment v))))
293 (tr-warnbreak)
294 'warned)))
297 (defun warn-mode (var mode newmode &optional comment)
298 (cond ((eq mode newmode))
300 (cond ((and (tr-warnp $tr_warn_mode)
301 (not (covers mode newmode))
302 (not (member (list var mode newmode) *warned-mode-vars* :test #'equal)))
303 (push (list var mode newmode) *warned-mode-vars*)
304 (tr-format (intl:gettext "warning: variable ~:M (declared type ~:M) assigned type ~:M.~%") var mode newmode)
305 (cond (comment
306 (dolist (v *translation-msgs-files*)
307 (terpri v)
308 (princ comment v))))
309 (tr-warnbreak))))))
311 (defun warn-fexpr (form &optional comment)
312 (cond ((and (tr-warnp $tr_warn_fexpr)
313 (not (member form *warned-fexprs* :test #'equal)))
314 (push form *warned-fexprs*)
315 (tr-format (intl:gettext "warning: ~:M is a special function without a full Lisp translation.~%") form)
316 (tr-format (intl:gettext "warning: calling ~:M in compiled code might not have the desired effect.~%") form)
317 (cond (comment
318 (dolist (v *translation-msgs-files*)
319 (terpri v)
320 (princ comment v))))
321 (tr-warnbreak))))
324 (defun macsyma-special-macro-p (fcn)
325 (getl-lm-fcn-prop fcn '(macro)))
327 (defun macsyma-special-op-p (f)
328 (getl f '(fsubr fexpr mfexpr* mfexpr*s *fexpr)))
330 (defun possible-predicate-op-p (f)
331 (member f '(mnotequal mequal $equal mgreaterp mgeqp mlessp mleqp) :test #'eq))
333 ;;;***************************************************************;;;
335 ;;; This function is the way to call the TRANSLATOR on an expression with
336 ;;; locally bound internal mode declarations. Result of TR-LAMBDA will be
337 ;;; (MODE . (LAMBDA (...) (DECLARE ...) TRANSLATED-EXP))
339 (defun tr-local-exp (exp &rest vars-modes)
340 (let ((loc (let ((tr-lambda-punt-assigns t))
341 (tr-lambda `((lambda) ((mlist) ,@(do ((l vars-modes (cddr l))
342 (ll nil (cons (car l) ll)))
343 ((null l) ll)
344 (or (variable-p (car l))
345 (bad-var-warn (car l)))))
346 (($modedeclare) ,@vars-modes)
347 ,exp)))))
348 (let ((mode (car loc))
349 (exp (car (last loc)))) ;;; length varies with TRANSCOMPILE.
350 (cons mode exp))))
352 (defun tr-args (form)
353 (mapcar #'(lambda (x) (dconvx (translate x))) form))
355 (defun dtranslate (form) (cdr (translate form)))
357 (defun dconv (x mode)
358 (cond ((eq '$float mode) (dconv-$float x))
359 ((eq '$cre mode) (dconv-$cre x))
360 (t (cdr x))))
362 (defun dconvx (x)
363 (if (member (car x) '(ratexpr pexpr) :test #'eq)
364 (dconv-$cre x)
365 (cdr x)))
367 (defun dconv-$float (x)
368 (cond ((member (car x) '($fixnum $number) :test #'eq)
369 (if (integerp (cdr x)) (float (cdr x)) (list 'float (cdr x))))
370 ((eq '$rational (car x))
371 (cond ((not (eq 'quote (cadr x)))
372 `($float ,(cdr x)))
374 (/ (float (cadadr (cdr x))) (float (caddr (caddr x)))))))
375 (t (cdr x))))
377 (defun dconv-$cre (x)
378 (if (eq '$cre (car x))
379 (cdr x)
380 `(ratf ,(cdr x))))
382 (defmvar *$any-modes* '($any $list))
384 (defun covers (mode1 mode2)
385 (cond ((eq mode1 mode2) t)
386 ((eq '$float mode1) (member mode2 '($float $fixnum $rational) :test #'eq))
387 ((eq '$number mode1) (member mode2 '($fixnum $float) :test #'eq))
388 ((member mode1 *$any-modes* :test #'eq) t)))
390 ;;; takes a function name as input.
392 (defun tr-mfun (name &aux (*transl-backtrace* nil))
393 (let ((def-form (consfundef name nil nil)))
394 (cond ((null def-form)
395 (setq tr-abort t))
397 (tr-mdefine-toplevel def-form)))))
399 ;;; DEFUN
400 ;;; All the hair here to deal with macsyma fexprs has been flushed.
401 ;;; Right now this handles MDEFMACRO and MDEFINE. The decisions
402 ;;; of where to put the actual properties and what kind of
403 ;;; defuns to make (LEXPR EXPR for maclisp) are punted to the
404 ;;; macro package.
406 (defun tr-mdefine-toplevel (form &aux (and-restp nil))
407 (destructuring-let (((((name . flags) . args) body) (cdr form))
408 (a-args) kind out-forms)
410 (do ((args args (cdr args))
411 ;; array functions cannot be LEXPR-like. gee.
412 ;; there is no good reason for that, except for efficiency,
413 ;; and I know that efficiency was not a consideration.
414 (full-restricted-flag (or (eq name 'mqapply)
415 (member 'array flags :test #'eq))))
416 ((null args) (setq a-args (nreverse a-args)))
417 (let ((u (car args)))
418 (cond ((atom u)
419 (push u a-args))
420 ((and (not full-restricted-flag)
421 (not and-restp)
422 (eq (caar u) 'mlist)
423 (cdr u) (atom (cadr u)))
424 (push (cadr u) a-args)
425 (setq and-restp t))
427 (push tr-unique a-args)))))
430 (cond ((eq name 'mqapply)
431 ;; don't you love syntax!
432 ;; do a switch-a-roo here. Calling ourselves recursively
433 ;; like this allows all legal forms and also catches
434 ;; errors. However, certain generalizations are also
435 ;; allowed. They won't get passed the interpreter, but
436 ;; interesting things may happen here. Thats what you
437 ;; get from too much syntax, so don't sweat it.
438 (tr-mdefine-toplevel
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))
444 (setq tr-abort t)
445 nil)
446 ((member (caar form) '(mdefine mdefmacro) :test #'eq)
447 (setq kind (cond ((eq (caar form) 'mdefmacro) 'macro)
448 ((member 'array flags :test #'eq) 'array)
449 (t 'func)))
450 (let* ((t-form
451 (tr-lambda `((lambda) ((mlist) ,@a-args) ,body)))
452 (desc-header
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)))
460 ((eq kind 'array)
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)
471 (push
472 `(add2lnc
473 '((,name ,@flags) ,@args)
474 ,(case kind
475 (array '$arrays)
476 (func '$functions)
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)
484 ,(case kind
485 (array 'aexpr)
486 (macro 'mmacro)
487 (func 'mexpr)))
488 out-forms)))
489 ;;once a function has been translated we want to make sure mfunction-call is eliminated.
490 (progn
491 (remprop (car desc-header) 'undefined-warnp)
492 (setf (get (car desc-header) 'once-translated) "I was once translated"))
493 `(progn
494 ,@(nreverse out-forms)
495 (defmtrfun ,desc-header ,@(cdr (cdr t-form))))))
497 (barfo '?)))))
500 (defun lisp-fcn-typep (fcn type)
501 (get fcn type))
503 (defun translate-function (name)
504 (bind-transl-state
505 (setq *in-translate* t)
506 (let ((lisp-def-form (tr-mfun name))
507 (delete-subr? (and (get name 'translated)
508 (not (lisp-fcn-typep name 'expr)))))
509 (cond (tr-abort
510 (trfail name))
512 (if delete-subr? (remprop name 'subr))
513 (if (mget name 'trace) (macsyma-untrace name))
514 (if (not $savedef) (meval `(($remfunction) ,name)))
515 (let ((lisp-action
516 ; apply EVAL so it is easy to TRACE.
517 ; ERRSET is crude, but...
518 (errset (apply 'eval (list lisp-def-form)))))
519 (cond ((not lisp-action)
520 (trfail name))
521 (t (values name lisp-def-form)))))))))
524 (defun trfail (x)
525 (tr-format (intl:gettext "error: failed to translate ~:@M~%") x)
526 nil)
528 (defmfun translate-and-eval-macsyma-expression (form)
529 ;; this is the hyper-random entry to the transl package!
530 ;; it is used by MLISP for TRANSLATE:TRUE ":=".
531 (bind-transl-state
532 (setq *in-translate* t)
533 ;; Use FUNCALL so that we can be sure we can TRACE this even when
534 ;; JPG sets PURE to NIL. Also, use a function named TRANSLATOR-EVAL
535 ;; so we don't have to lose badly by tracing EVAL!
536 (funcall (progn 'translator-eval)
537 (funcall (progn 'translate-macexpr-toplevel) form))))
539 (defun translator-eval (x)
540 (eval x))
542 ;; This basically tells the msetq def%tr to use defparameter insetad
543 ;; of setq because we're doing a setq at top-level, which isn't
544 ;; specified by ANSI CL.
545 (defvar *macexpr-top-level-form-p* nil)
547 (defmfun translate-macexpr-toplevel (form &aux (*transl-backtrace* nil) tr-abort)
548 ;; there are very few top-level special cases, I don't
549 ;; think it would help the code any to generalize TRANSLATE
550 ;; to target levels.
552 ;; Except msetq at top-level is special for ANSI CL. See below.
553 (setq form (toplevel-optimize form))
554 (cond ((atom form) nil)
555 ((eq (caar form) '$eval_when)
556 (let ((whens (cadr form))
557 (body (cddr form)) tr-whens)
558 (setq whens (cond (($listp whens) (cdr whens))
559 ((atom whens) (list whens))
561 (tr-format (intl:gettext "error: 'eval-when' argument must be a list or atom; found: ~:M~%") (cadr form))
562 nil)))
563 (setq tr-whens (mapcar 'stripdollar whens))
564 (cond ((member '$translate whens :test #'eq)
565 (mapc 'meval body)))
566 (cond ((member '$loadfile whens :test #'eq)
567 `(progn
568 ,@(mapcar 'translate-macexpr-toplevel body)))
569 ((setq tr-whens (intersect tr-whens '(:compile-toplevel :load-toplevel :execute)))
570 `(eval-when
571 ,tr-whens
572 ,@(mapcar 'translate-macexpr-toplevel body)))
573 ((member '$compile whens :test #'eq)
574 ;; strictly for the knowledgeable user.
575 ;; I.E. so I can use EVAL_WHEN(COMPILE,?SPECIALS:TRUE)
576 `(eval-when
577 #+gcl (compile)
578 #-gcl (:compile-toplevel)
579 ,@(mapcar 'translate-macexpr-toplevel body))))))
580 ((member (caar form) translate-time-evalables :test #'eq)
581 (meval1 form)
582 `(eval-when
583 #+gcl (compile load eval)
584 #-gcl (:compile-toplevel :load-toplevel :execute)
585 (meval* ',form)))
586 ((member (caar form) '(mdefine mdefmacro) :test #'eq)
587 (let ((name (caaadr form))
588 (trl))
589 (tr-format (intl:gettext "note: translating ~:@M~%") name)
590 (setq trl (tr-mdefine-toplevel form))
591 (cond (tr-abort
592 (tr-format (intl:gettext "error: failed to translate ~:@M~%") name)
593 (tr-format (intl:gettext "note: keep going and hope for the best.~%"))
594 `(meval* ',form))
595 (t trl))))
596 ((eq 'mprogn (caar form))
597 ;; flatten out all PROGN's of course COMPLR needs PROGN 'COMPILE to
598 ;; tell it to flatten. I don't really see the use of that since one
599 ;; almost allways wants to. flatten.
600 ;; note that this ignores the $%% crock.
601 `(progn 'compile
602 ,@(mapcar #'translate-macexpr-toplevel (cdr form))))
603 ((eq 'msetq (caar form))
604 ;; Toplevel msetq's should really be defparameter instead of
605 ;; setq for Common Lisp.
606 (let ((*macexpr-top-level-form-p* t))
607 (dtranslate form)))
608 ((eq '$define_variable (caar form))
609 ;; Toplevel msetq's should really be defparameter instead of
610 ;; setq for Common Lisp.
611 (let ((*macexpr-top-level-form-p* t))
612 (dtranslate form)))
614 (let ((t-form (dtranslate form)))
615 (cond (tr-abort
616 `(meval* ',form))
618 t-form))))))
622 (defmvar $tr_optimize_max_loop 100.
623 "The maximum number of times the macro-expansion and optimization
624 pass of the translator will loop in considering a form.
625 This is to catch macro expansion errors, and non-terminating
626 optimization properties.")
628 (defun toplevel-optimize (form)
629 ;; it is vital that optimizations be done within the
630 ;; context of variable meta bindings, declarations, etc.
631 ;; Also: think about calling the simplifier here.
632 (cond ((atom form)
633 (cond ((symbolp form)
634 ;; FOLLOWING CODE APPEARS TO BE BROKEN; NOT SURE WHAT WAS THE INTENT.
635 ;; FOLLOWING CODE ALWAYS RETURNS SYMBOL ITSELF EVEN WHEN '$CONSTANTP IS A PROPERTY
636 ;; IS IT SUPPOSED TO FETCH A DECLARED CONSTANT VALUE ??
637 ;; JUST LEAVE IT BE FOR NOW; DO NOT TRY TO REVISE WITH KINDP
638 (let ((v (getl (mget form '$props) '($constant))))
639 (if v (cadr v) form)))
640 (t form)))
642 (do ((new-form)
643 (kount 0 (1+ kount)))
644 ;; tailrecursion should always arrange for a counter
645 ;; to check for mobylossage.
646 ((> kount $tr_optimize_max_loop)
647 (tr-format (intl:gettext "warning: I've looped ~A times in macro expansion; just give up and return ~:@M~%")
648 $tr_optimize_max_loop (caar form))
649 form)
650 (setq new-form (toplevel-optimize-1 form))
651 (cond ((atom new-form)
652 (return (toplevel-optimize new-form)))
653 ((eq new-form form)
654 (return form))
656 (setq form new-form)))))))
658 (defun toplevel-optimize-1 (form &aux (op (car form)) prop)
659 (cond ((or (atom op)
660 (member 'array op :test #'eq)) form)
661 ((progn (setq op (car op))
662 (setq prop
663 (if $transrun ; crock a minute.
664 (or (get op 'translated-mmacro)
665 (mget op 'mmacro))
666 (or (mget op 'mmacro)
667 (get op 'translated-mmacro)))))
668 (mmacro-apply prop form))
669 ((setq prop ($get op '$optimize))
670 ;; interesting, the MAPPLY here causes the simplification
671 ;; of the form and the result.
672 ;; The optimize property can be used to implement
673 ;; such niceties as the $%% crock.
674 (mapply1 prop (list form) "an optimizer property" nil))
675 ((and ($get op '$transload)
676 (get op 'autoload)
677 ;; check for all reasonable definitions,
678 ;; $OPTIMIZE and MACRO already checked.
679 (not (or (get-lisp-fun-type op)
680 (getl op '(translate mfexpr* mfexpr*s
681 fsubr fexpr *fexpr
682 macro
683 ;; foobar?
685 (mgetl op '(mexpr)))))
686 (load-function op t)
687 ;; to loop.
688 (cons (car form) (cdr form)))
689 (t form)))
691 (defun translate (form)
692 (and *transl-debug* (push form *transl-backtrace*))
693 (setq form (toplevel-optimize form))
694 (and *transl-debug* (pop *transl-backtrace*))
695 (prog2
696 (and *transl-debug* (push form *transl-backtrace*))
697 (cond ((atom form)
698 (translate-atom form))
699 ((consp form)
700 (translate-form form))
702 (barfo "help")))
703 ;; hey boy, reclaim that cons, just don't pop it!
704 (and *transl-debug* (pop *transl-backtrace*))))
706 (defun translate-atom (form &aux temp)
707 (cond ((numberp form) (cons (tr-class form) form))
708 ((setq temp (assoc form boolean-object-table :test #'eq))
709 (cdr temp))
710 ((and (setq temp (mget form '$numer)) $tr_numer)
711 `($float . ,temp))
712 ((setq temp (implied-quotep form))
713 `($any . ',temp))
714 ((tboundp form)
715 (specialp form) ;; notes its usage if special.
716 (setq form (teval form))
717 `(,(value-mode form) . ,form))
719 (cond ((not (specialp form))
720 (warn-undefined-variable form)
721 (if $transcompile (pushnew form specials :test #'eq))))
722 ;; note that the lisp analysis code must know that
723 ;; the TRD-MSYMEVAL form is a semantic variable.
724 (let* ((mode (value-mode form))
725 (init-val (assoc mode mode-init-value-table :test #'eq)))
726 (setq init-val (cond (init-val (cdr init-val))
727 (t `',form)))
728 ;; in the compiler TRD-MSYMEVAL doesn't do a darn
729 ;; thing, but it provides dynamic initialization of
730 ;; variables in interpreted code which is translated
731 ;; in-core. In FILE loaded code the DEFVAR will take
732 ;; care of this.
733 (push-defvar form init-val)
734 `(,mode . (trd-msymeval ,form ,init-val))))))
736 (defun translate-form (form &aux temp)
737 (cond ((eq (car form) 'meval) (cons '$any form)) ;;for those lispy macsyma forms
738 ((not (atom (caar form)))
739 ;; this is a check like that in the simplifier. form could
740 ;; result from substitution macros.
741 (translate `((mqapply) ,(caar form) . ,(cdr form))))
742 ((member 'array (cdar form) :test #'eq)
743 ;; dispatch this bad-boy to another module quick.
744 (tr-arraycall form))
745 ;; TRANSLATE properties have priority.
746 ((setq temp (get (caar form) 'translate))
747 (funcall temp form))
748 ((setq temp (get-lisp-fun-type (caar form)))
749 (tr-lisp-function-call form temp))
750 ((setq temp (macsyma-special-macro-p (caar form)))
751 (attempt-translate-random-macro-op form temp))
752 ((setq temp (macsyma-special-op-p (caar form)))
753 ;; a special form not handled yet! foobar!
754 (attempt-translate-random-special-op form temp))
755 ((getl (caar form) '(noun operators))
756 ;; puntastical case. the weird ones are presumably taken care
757 ;; of by TRANSLATE properties by now.
758 (tr-infamous-noun-form form))
760 ;; "What does a macsyma function call mean?".
761 ;; By the way, (A:'B,B:'C,C:'D)$ A(3) => D(3)
762 ;; is not supported.
764 (tr-macsyma-user-function-call (caar form) (cdr form) form))))
768 (defmvar $tr_bound_function_applyp t)
770 (defun tr-macsyma-user-function-call (function args form)
771 ;; this needs some work, output load-time code to
772 ;; check for MMACRO properties, etc, to be really
773 ;; foolproof.
774 (cond ((eq $tr_function_call_default '$apply)
775 (translate `(($apply) ,(caar form) ((mlist) ,@(cdr form)))))
776 ((eq $tr_function_call_default '$expr)
777 (tr-lisp-function-call form 'subr))
778 ((eq $tr_function_call_default '$general)
779 (cond
780 ;;; G(F,X):=F(X+1); case.
781 ((and $tr_bound_function_applyp (tboundp function))
782 (let ((new-form `(($apply) ,function ((mlist) ,@args))))
783 (tr-format (intl:gettext "warning: ~:M is a bound variable in ~:M, but it is used as a function.~%") function form)
784 (tr-format (intl:gettext "note: instead I'll translate it as: ~:M~%") new-form)
785 (translate new-form)))
786 ;; MFUNCTION-CALL cleverely punts this question to a FSUBR in the
787 ;; interpreter, and a macro in the compiler. This is good style,
788 ;; if a user is compiling then assume he is less lossage prone.
790 (pushnew (caar form) *untranslated-functions-called*)
791 (call-and-simp
792 (function-mode (caar form))
793 'mfunction-call `(,(caar form) ,@(tr-args args))))))
795 ;; This case used to be the most common, a real loser.
796 (warn-meval form)
797 `(,(function-mode (caar form)) . (meval ',form)))))
800 (defun attempt-translate-random-macro-op (form typel &aux tem)
801 (warn-fexpr form)
802 (setq tem (translate-atoms form))
803 (setf (car tem) (caar tem))
804 `($any . ,tem))
806 (defun attempt-translate-random-special-op (form typel)
807 (warn-fexpr form)
808 `(,(function-mode (caar form)) . (meval ',(translate-atoms form))))
811 (defun tr-lisp-function-call (form type)
812 (let ((op (caar form)) (mode) (args))
813 (setq args (cond ((member type '(subr lsubr expr) :test #'eq)
814 (if $transcompile
815 (case type
816 ((subr) (pushnew op exprs :test #'eq))
817 ((lsubr) (pushnew op lexprs :test #'eq))
818 (t nil)))
819 (mapcar #'(lambda (llis) (dconvx (translate llis)))
820 (cdr form)))
822 (if $transcompile (pushnew op fexprs :test #'eq))
823 (mapcar 'dtranslate (cdr form))))
824 mode (function-mode op))
825 (call-and-simp mode op args)))
827 ;;the once-translated is so that inside translate file where a function
828 ;;has been translated, subsequent calls won't use mfunction call
829 (defun get-lisp-fun-type (fun &aux temp)
830 ;; N.B. this is Functional types. NOT special-forms,
831 ;; lisp special forms are meaningless to macsyma.
832 (cond ((get fun '*lexpr) 'lsubr)
833 ((get fun '*expr) 'subr)
834 ;; *LEXPR & *EXPR gotten from DEFMFUN declarations
835 ;; which is loaded by TrData.
836 ((mget fun '$fixed_num_args_function)
837 'subr)
838 ((mget fun '$variable_num_args_function)
839 'lsubr)
840 ((setq temp (getl fun '(expr subr lsubr)))
841 (car temp))
842 ((get fun 'once-translated))
843 ((get fun 'translated))
844 (t nil)))
846 (defun tr-infamous-noun-form (form)
847 ;; 'F(X,Y) means noun-form. The arguments are evaluated.
848 ;; but the function is cons on, not applied.
849 ;; N.B. for special forms and macros this is totally wrong.
850 ;; But, those cases are filtered out already, presumably.
852 (let ((op (cond ((member 'array (car form) :test #'eq)
853 `(,(caar form) array))
854 (t `(,(caar form)))))
855 (args (tr-args (cdr form))))
856 `($any . (simplify (list ',op ,@args)))))
860 ;;; Some atoms, solely by usage, are self evaluating.
862 (defun implied-quotep (atom)
863 (cond
864 ((stringp atom)
865 (cond
866 ;; I WONDER IF THIS NEXT CONDITION CAN BE CUT OUT ?? !!
867 ((equal atom "**") ;;; foolishness. The PARSER should do this.
868 ;; Losing Fortran hackers.
869 (tr-format "~% `**' is obsolete, use `^' !!!")
870 "^")
871 (t atom)))
872 ((get atom 'implied-quotep) atom)
873 (t nil)))
875 (defun translate-atoms (form)
876 ;; This is an oldy moldy which tries to declare everything
877 ;; special so that calling fexpr's will work in compiled
878 ;; code. What a joke.
879 (cond ((atom form)
880 (cond ((or (numberp form) (member form '(t nil) :test #'eq)) form)
881 ((tboundp form)
882 (if $transcompile
883 (or (specialp form)
884 (pushnew form specials :test #'eq)))
885 form)
887 (if $transcompile (pushnew form specials :test #'eq))
888 form)))
889 ((eq 'mquote (caar form)) form)
890 (t (cons (car form) (mapcar #'translate-atoms (cdr form))))))
893 ;;; the Translation Properties. the heart of TRANSL.
895 ;;; This conses up the call to the function, adding in the
896 ;;; SIMPLIFY i the mode is $ANY. This should be called everywhere.
897 ;;; instead of duplicating the COND everywhere, as is done now in TRANSL.
899 (defun tr-nosimpp (op)
900 (cond ((atom op)
901 (get op 'tr-nosimp))
902 (t nil)))
904 (defun call-and-simp (mode fun args)
905 (cond ((or (not (eq mode '$any))
906 (tr-nosimpp fun))
907 `(,mode ,fun . ,args))
909 `(,mode simplify (,fun . ,args)))))
911 (defmspec $declare_translated (fns)
912 (setq fns (cdr fns))
913 (loop for v in fns
914 when (or (symbolp v) (and (stringp v) (setq v ($verbify v))))
915 do (setf (get v 'once-translated) t)
916 (pushnew v *declared-translated-functions*)
917 else do (merror (intl:gettext "declare_translated: arguments must be symbols or strings; found: ~:M") v)))
919 (def%tr $declare (form)
920 (do ((l (cdr form) (cddr l)) (nl))
921 ((null l) (if nl `($any $declare . ,(nreverse nl))))
922 (setq nl (cons (cadr l) (cons (car l) nl)))))
924 (def%tr $eval_when (form)
925 (tr-format (intl:gettext "error: found 'eval_when' in a function or expression: ~:M~%") form)
926 (tr-format (intl:gettext "note: 'eval_when' can appear only at the top level in a file.~%"))
927 (setq tr-abort t)
928 '($any . nil))
930 (def%tr mdefmacro (form)
931 (meval form) ;; HMM, THIS HAS A SIDE EFFECT AT THE TIME OF TRANSLATION !!
932 `($any . (meval ',form)))
934 (def%tr $local (form)
935 (cond (local
936 (tr-format (intl:gettext "error: there is already a 'local' in this block.~%"))
937 (setq tr-abort t))
939 (setq local t)))
940 (cons nil `(mapply 'mlocal ',(cdr form) '$local)))
943 (def%tr mquote (form)
944 (list (tr-class (cadr form)) 'quote (cadr form)))
947 (defun tr-lambda (form &optional (tr-body #'tr-seq) &rest tr-body-argl
948 &aux
949 (arglist (mparams (cadr form)))
950 (easy-assigns nil))
951 ;; This function is defined to take a simple macsyma lambda expression and
952 ;; return a simple lisp lambda expression. The optional TR-BODY hook
953 ;; can be used for translating other special forms that do lambda binding.
955 ;; Local SPECIAL declarations are not used because
956 ;; the multics lisp compiler does not support them. They are of course
957 ;; a purely syntactic construct that doesn't buy much. I have been
958 ;; advocating the use of DEFINE_VARIABLE in macsyma user programs so
959 ;; that the use of DECLARE(FOO,SPECIAL) will be phased out at that level.
961 (mapc #'tbind arglist)
962 (destructuring-let (((mode . nbody) (apply tr-body (cddr form) tr-body-argl))
963 (local-declares (make-declares arglist t)))
964 ;; -> BINDING of variables with ASSIGN properties may be difficult to
965 ;; do correctly and efficiently if arbitrary code is to be run.
966 (if (or tr-lambda-punt-assigns
967 (do ((l arglist (cdr l)))
968 ((null l) t)
969 (let* ((var (car l))
970 (assign (get var 'assign)))
971 (if assign
972 (cond ((eq assign 'assign-mode-check)
973 (push `(,assign ',var ,(teval var)) easy-assigns))
975 (return nil)))))))
976 ;; Case with EASY or no ASSIGN's
977 (progn ;;-have to undo any local assignments. --wfs
978 `(,mode . (lambda ,(tunbinds arglist)
979 ,local-declares
980 ,@easy-assigns
981 ,@nbody)))
982 ;; Case with arbitrary ASSIGN's.
983 (let ((temps (mapcar #'(lambda (ign) ign (tr-gensym)) arglist)))
984 `(,mode . (lambda ,temps
985 (unwind-protect
986 (progn
987 ;; [1] Check before binding.
988 ,@(mapcan #'(lambda (var val)
989 (let ((assign (get var 'assign)))
990 (if assign
991 (list `(,assign ',var ,val)))))
992 arglist temps)
993 ;; [2] do the binding.
994 ((lambda ,(tunbinds arglist)
995 ,local-declares
996 ,@nbody)
997 ,@temps))
998 ;; [2] check when unbinding too.
999 ,@(mapcan #'(lambda (var)
1000 (let ((assign (get var 'assign)))
1001 (if assign
1002 (list `(,assign ',var
1003 ;; use DTRANSLATE to
1004 ;; catch global
1005 ;; scoping if any.
1006 ,(dtranslate var))))))
1007 arglist))))))))
1010 (defun make-declares (varlist localp &aux (dl) (fx) (fl) specs)
1011 (when $transcompile
1012 (do ((l varlist (cdr l))
1013 (mode) (var))
1014 ((null l))
1016 ;; When a variable is declared special, be sure to declare it
1017 ;; special here.
1018 (when (and localp (get (car l) 'special))
1019 (push (car l) specs))
1021 (when (or (not localp)
1022 (not (get (car l) 'special)))
1023 ;; don't output local declarations on special variables.
1024 (setq var (teval (car l)) mode (value-mode var))
1025 (setq specs (cons var specs))
1027 (cond ((eq '$fixnum mode) (pushnew var fx :test #'eq))
1028 ((eq '$float mode) (pushnew var fl :test #'eq)))))
1029 (if fx (pushnew `(fixnum . ,fx) dl :test #'eq))
1030 (if fl (pushnew `(type flonum . ,fl) dl :test #'eq))
1031 (if specs (pushnew `(special . ,specs) dl :test #'eq))
1032 (if dl `(declare . ,dl))))
1034 (def%tr dolist (form)
1035 (translate `((mprogn) . ,(cdr form))))
1037 (defun tr-seq (l)
1038 (do ((mode nil)
1039 (body nil))
1040 ((null l)
1041 (cons mode (nreverse body)))
1042 (let ((exp (translate (pop l))))
1043 (setq mode (car exp))
1044 (push (cdr exp) body))))
1046 (def%tr mprogn (form)
1047 (setq form (tr-seq (cdr form)))
1048 (cons (car form) `(progn ,@(cdr form))))
1051 (def%tr mprog (form)
1052 (let (arglist body val-list)
1053 ;; [1] normalize the MPROG syntax.
1054 (cond (($listp (cadr form))
1055 (setq arglist (cdadr form)
1056 body (cddr form)))
1058 (setq arglist nil
1059 body (cdr form))))
1060 (cond ((null body)
1061 (setq body '(((mquote) $done)))))
1062 (setq val-list (mapcar #'(lambda (u)
1063 (if (atom u) u
1064 (translate (caddr u))))
1065 arglist)
1066 arglist (mapcar #'(lambda (u)
1067 ;; X or ((MSETQ) X Y)
1068 (if (atom u) u (cadr u)))
1069 arglist))
1070 (let ((dup (find-duplicate arglist :test #'eq)))
1071 (when dup
1072 (tr-format (intl:gettext "error: ~M occurs more than once in block variable list") dup)
1073 (setq tr-abort t)))
1074 (unless tr-abort
1075 (setq form
1076 (tr-lambda
1077 ;; [2] call the lambda translator.
1078 `((lambda) ((mlist) ,@arglist) ,@body)
1079 ;; [3] supply our own body translator.
1080 #'tr-mprog-body
1081 val-list
1082 arglist))
1083 (cons (car form) `(,(cdr form) ,@val-list)))))
1085 (defun tr-mprog-body (body val-list arglist
1086 &aux
1087 (inside-mprog t)
1088 (return-mode nil)
1089 (need-prog? nil)
1090 (returns nil) ;; not used but must be bound.
1091 (local nil)
1093 (do ((l nil))
1094 ((null body)
1095 ;; [5] hack the val-list for the mode context.
1096 ;; Perhaps the only use of the function MAP in all of macsyma.
1097 (mapl #'(lambda (val-list arglist)
1098 (cond ((atom (car val-list))
1099 (rplaca val-list
1100 (or (cdr (assoc (value-mode (car arglist))
1101 mode-init-value-table :test #'eq))
1102 `',(car arglist))))
1104 (warn-mode (car arglist)
1105 (value-mode (car arglist))
1106 (car (car val-list))
1107 "in a `block' statement")
1108 (rplaca val-list (cdr (car val-list))))))
1109 val-list arglist)
1110 (setq l (nreverse l))
1111 (cons return-mode
1112 (if need-prog?
1113 `((prog () ,@(delete nil l :test #'equal)))
1114 l)))
1115 ;; [4] translate a form in the body
1116 (let ((form (pop body)))
1117 (cond ((null body)
1118 ;; this is a really bad case.
1119 ;; we don't really know if the return mode
1120 ;; of the expression is for the value of the block.
1121 ;; Some people write RETURN at the end of a block
1122 ;; and some don't. In any case, the people not
1123 ;; use the PROG programming style won't be screwed
1124 ;; by this.
1125 (setq form (translate form))
1126 (setq return-mode (*union-mode (car form) return-mode))
1127 (setq form (cdr form))
1128 (if (and need-prog?
1129 (or (atom form)
1130 (not (eq (car form) 'return))))
1131 ;; put a RETURN on just in case.
1132 (setq form `(return ,form))))
1133 ((symbolp form))
1135 (setq form (dtranslate form))))
1136 (push form l))))
1138 (def%tr mreturn (form)
1139 (if (null inside-mprog)
1140 (tr-format (intl:gettext "warning: 'return' not within 'block' or 'do': ~:M~%") form))
1141 (setq need-prog? t)
1142 (setq form (translate (cadr form)))
1143 (setq return-mode (if return-mode (*union-mode (car form) return-mode)
1144 (car form)))
1145 (setq form `(return ,(cdr form)))
1146 (push form returns) ;; USED by lusing MDO etc not yet re-written.
1147 ;; MODE here should be $PHANTOM or something.
1148 `($any . ,form))
1150 (def%tr mgo (form)
1151 (if (null inside-mprog)
1152 (tr-format (intl:gettext "warning: 'go' not within 'block' or 'do': ~:M~%") form))
1153 (if (not (symbolp (cadr form)))
1154 (tr-format (intl:gettext "warning: 'go' tag must be a symbol: ~:M~%") form))
1155 (setq need-prog? t)
1156 `($any . (go ,(cadr form))))
1158 (def%tr mqapply (form)
1159 (let ((fn (cadr form)) (args (cddr form))
1160 (aryp (member 'array (cdar form) :test #'eq)))
1161 (cond ((atom fn)
1162 ;; I'm guessing (ATOM FN) is a parser error or other Lisp error,
1163 ;; so don't bother to translate the following error message.
1164 (mformat *translation-msgs-files* "translator: MQAPPLY operator must be a cons; found: ~:M" form)
1165 nil)
1166 ((eq (caar fn) 'mquote)
1167 `($any list ',(cons (cadr fn) aryp) ,@(tr-args args)))
1168 ((eq (caar fn) 'lambda)
1169 ;; LAMBDA([X,'Y,[L]],...)(A,B,C) is a bogus form. Don't bother with it.
1170 ;; ((LAMBDA) ((MLIST) ....) ....)
1171 (cond ((member 'bogus (mapcar #'(lambda (arg)
1172 (cond ((or (mquotep arg)
1173 ($listp arg))
1174 'bogus)))
1175 (cdr (cadr fn))) :test #'eq)
1176 (tr-format (intl:gettext "error: quote or list arguments are not allowed in MQAPPLY; found: ~:M~%") form)
1177 (setq tr-abort t)
1178 nil)
1180 (setq fn (tr-lambda fn)
1181 args (tr-args args))
1182 `(,(car fn) ,(cdr fn) ,@args))))
1183 ((not aryp)
1184 `($any simplify (mapply ,(dconvx (translate fn))
1185 (list ,@(tr-args args))
1186 ',fn)))
1188 (warn-meval form)
1189 `($any meval ',form)))))
1193 (def%tr mcond (form)
1194 (prog (dummy mode nl)
1195 (setq dummy (translate (caddr form))
1196 mode (car dummy)
1197 nl (list dummy (translate-predicate (cadr form))))
1198 (do ((l (cdddr form) (cddr l))) ((null l))
1199 (cond ((and (not (atom (cadr l))) (eq 'mcond (caaadr l)))
1200 (setq l (cdadr l))))
1201 (setq dummy (translate (cadr l))
1202 mode (*union-mode mode (car dummy))
1203 nl (cons dummy
1204 (cons (translate-predicate (car l))
1205 nl))))
1206 (setq form nil)
1207 (do ((l nl (cddr l))) ((null l))
1208 (cond ((and (eq t (cadr l)) (null (cdar l))))
1209 (t (setq form
1210 (cons (cons (cadr l)
1211 (cond ((and (not (atom (cdar l)))
1212 (cddar l)
1213 (eq (cadar l) 'progn))
1214 (nreverse
1215 (cons (dconv (cons (caar l)
1216 (car (reverse (cddar l))))
1217 mode)
1218 (cdr (reverse (cddar l))))))
1219 ((and (equal (cdar l) (cadr l))
1220 (atom (cdar l))) nil)
1221 (t (list (cdr (car l))))))
1222 form)))))
1223 (return (cons mode (cons 'cond form)))))
1227 ;; The MDO and MDOIN translators should be changed to use the TR-LAMBDA.
1228 ;; Perhaps a mere expansion into an MPROG would be best.
1230 (def%tr mdo (form)
1231 (let (returns assigns return-mode local (inside-mprog t) need-prog?)
1232 (let (mode var init next test action varmode)
1233 (setq var (cond ((cadr form)) (t 'mdo)))
1234 (specialp var)
1235 (tbind var)
1236 (setq init (if (caddr form) (translate (caddr form)) '($fixnum . 1)))
1237 (cond ((not (setq varmode (get var 'mode)))
1238 (declvalue var (car init) t)))
1239 (setq next (translate (cond ((cadddr form) (list '(mplus) (cadddr form) var))
1240 ((car (cddddr form)))
1241 (t (list '(mplus) 1 var)))))
1242 (setq form (copy-list form))
1243 ;;to make the end test for thru be numberp if the index is numberp
1244 ;;and to eliminate reevaluation
1245 (cond ((not varmode)
1246 (declvalue var (*union-mode (car init) (car next)) t))
1248 (warn-mode var varmode (*union-mode (car init) (car next)))))
1249 (setq test (translate-predicate
1250 (list '(mor)
1251 (cond ((null (cadr (cddddr form))) nil)
1252 ((and (cadddr form)
1253 (mnegp ($numfactor (simplify (cadddr form)))))
1254 (list '(mlessp) var (cadr (cddddr form))))
1255 (t (list '(mgreaterp) var (cadr (cddddr form)))))
1256 (caddr (cddddr form)))))
1257 (setq action (translate (cadddr (cddddr form)))
1258 mode (cond ((null returns) '$any)
1259 (t return-mode)))
1260 (setq var (tunbind (cond ((cadr form)) (t 'mdo))))
1261 `(,mode do ((,var ,(cdr init) ,(cdr next)))
1262 (,test '$done) . ((declare (special ,var)) .
1263 ,(cond ((atom (cdr action)) nil)
1264 ((eq 'progn (cadr action)) (cddr action))
1265 (t (list (cdr action)))))))))
1267 (def%tr mdoin (form)
1268 (let (returns assigns return-mode local (inside-mprog t) need-prog?)
1269 (prog (mode var init action)
1270 (setq var (tbind (cadr form))) (tbind 'mdo)
1271 (specialp var)
1272 (setq init (dtranslate (caddr form)))
1273 (cond ((or (cadr (cddddr form)) (caddr (cddddr form)))
1274 (tunbind 'mdo) (tunbind (cadr form))
1275 (return `($any meval '((mdoin) . ,(cdr form))))))
1276 (setq action (translate (cadddr (cddddr form)))
1277 mode (cond ((null returns) '$any)
1278 (t return-mode)))
1279 (tunbind 'mdo) (tunbind (cadr form))
1280 (return
1281 `(,mode do ((,var) (mdo (cdr ,init) (cdr mdo)))
1282 ((null mdo) '$done) .
1283 ((declare (special ,var)) (setq ,var (car mdo)) .
1284 ,(cond ((atom (cdr action)) nil)
1285 ((eq 'progn (cadr action)) (cddr action))
1286 (t (list (cdr action))))))))))
1289 (defun lambda-wrap1 (tn val form)
1290 (if (or (atom val)
1291 (eq (car val) 'quote))
1292 (subst val tn form)
1293 `((lambda (,tn) ,form) ,val)))
1295 (def%tr msetq (form)
1296 (let ((var (cadr form))
1297 (val (caddr form))
1298 assign
1299 mode)
1300 (cond ((atom var)
1301 (setq mode (value-mode var) val (translate val))
1302 (cond ((not (tboundp var))
1303 (pushnew var specials :test #'eq)))
1304 (warn-mode var mode (car val))
1305 (if (eq '$any mode)
1306 (setq mode (car val) val (cdr val))
1307 (setq val (dconv val mode)))
1308 (cons mode
1309 (if (setq assign (get var 'assign))
1310 (let ((tn (tr-gensym)))
1311 (lambda-wrap1 tn val `(progn (,assign ',var ,tn)
1312 (setq ,(teval var) ,tn))))
1313 `(progn
1314 (if (not (boundp ',(teval var)))
1315 (add2lnc ',(teval var) $values))
1316 (,(if *macexpr-top-level-form-p*
1317 'defparameter
1318 'setq)
1319 ,(teval var) ,val)))))
1320 ((member 'array (car var) :test #'eq)
1321 (tr-arraysetq var val))
1323 (unless (safe-get (caar var) 'mset_extension_operator)
1324 (tr-format (intl:gettext "warning: no assignment operator known for ~:M~%") var)
1325 (tr-format (intl:gettext "note: just keep going and hope for the best.~%")))
1326 (setq val (translate val))
1327 `(,(car val) mset ',(translate-atoms var) ,(cdr val))))))
1329 (def%tr $max (x) (translate-$max-$min x))
1330 (def%tr $min (x) (translate-$max-$min x))
1331 (def%tr %max (x) (translate-$max-$min x))
1332 (def%tr %min (x) (translate-$max-$min x))
1334 (defun translate-$max-$min (form)
1335 (let ((mode) (arglist) (op (stripdollar (caar form))))
1336 (setq arglist
1337 (mapcar #'(lambda (l) (setq l (translate l)
1338 mode (*union-mode (car l) mode))
1340 (cdr form)))
1341 (if (member mode '($fixnum $float $number) :test #'eq)
1342 `(,mode ,(if (eq 'min op) 'min 'max) . ,(mapcar 'cdr arglist))
1343 `($any ,(if (eq 'min op) '$lmin '$lmax)
1344 (list '(mlist) . ,(mapcar 'dconvx arglist))))))
1347 ;;; mode acessing, binding, handling. Super over-simplified.
1349 (defun tr-class (x)
1350 (cond ((integerp x) '$fixnum)
1351 ((floatp x) '$float)
1352 ((member x '(t nil) :test #'eq) '$boolean)
1353 ((atom x) '$any)
1354 ((eq 'rat (caar x)) '$rational)
1355 (t '$any)))
1357 (defun *union-mode (mode1 mode2)
1358 (cond ((eq mode1 mode2) mode1)
1359 ((null mode1) mode2)
1360 ((null mode2) mode1)
1361 ((member mode2 *$any-modes* :test #'eq) '$any)
1362 ((member mode1 *$any-modes* :test #'eq) '$any)
1363 ((eq '$fixnum mode1) mode2)
1364 ((eq '$float mode1)
1365 (if (eq '$number mode2) '$number '$float))
1366 ((eq '$rational mode1)
1367 (if (eq '$float mode2) '$float '$any))
1368 ((eq '$number mode1)
1369 (if (eq '$rational mode2) '$any '$number))
1370 (t '$any)))
1372 (defun value-mode (var)
1373 (cond ((get var 'mode))
1375 (warn-undeclared var)
1376 '$any)))
1378 (defun decmode-arrayfun (f m)
1379 (putprop f m 'arrayfun-mode))
1381 (defun array-mode (ar)
1382 (cond ((get ar 'array-mode)) (t '$any)))
1384 (defun arrayfun-mode (ar)
1385 (cond ((get ar 'arrayfun-mode)) (t '$any)))
1387 (defun function-mode (f)
1388 (cond ((get f 'function-mode)) (t '$any)))
1390 (defun function-mode-@ (f)
1391 (ass-eq-ref (get f 'val-modes) 'function-mode '$any))
1393 (defun array-mode-@ (f)
1394 (ass-eq-ref (get f 'val-modes) 'array-mode '$any))
1397 (defvar $tr_bind_mode_hook nil
1398 "A hack to allow users to key the modes of variables
1399 off of variable spelling, and other things like that.")
1401 (defstruct (tstack-slot (:conc-name tstack-slot-))
1402 mode
1403 tbind
1404 val-modes
1405 ;; an alist telling second order info
1406 ;; about APPLY(VAR,[X]), ARRAYAPPLY(F,[X]) etc.
1407 special)
1409 ;;; should be a macro (TBINDV <var-list> ... forms)
1410 ;;; so that TUNBIND is assured, and also so that the stupid ASSQ doesn't
1411 ;;; have to be done on the darn TSTACK. This will have to wait till
1412 ;;; the basic special form translation properties are rewritten.
1414 (defun variable-p (var)
1415 (and var (symbolp var) (not (eq var t))))
1417 (defun bad-var-warn (var)
1418 (tr-format (intl:gettext "warning: ~:M cannot be used as a variable.~%") var))
1420 (defun tbind (var &aux old)
1421 (cond ((variable-p var)
1422 (setq old (make-tstack-slot :mode (get var 'mode)
1423 :tbind (get var 'tbind)
1424 :val-modes (get var 'val-modes)
1425 :special (get var 'special)))
1426 (push (cons var old) tstack)
1427 (cond ((not (specialp var))
1428 ;; It is the lisp convention in use to inherit
1429 ;; specialness from higher context.
1430 ;; Spurious MODEDECLARATIONS get put in the environment
1431 ;; when code is MEVAL'd since there is no way to stack
1432 ;; the mode properties. Certainly nobody is willing
1433 ;; to hack MEVAL in JPG;MLISP
1434 (remprop var 'val-modes)
1435 (remprop var 'mode)
1436 (remprop var 'special)))
1437 (putprop var var 'tbind)
1438 (if $tr_bind_mode_hook
1439 (let ((mode? (mapply $tr_bind_mode_hook
1440 (list var)
1441 '$tr_bind_mode_hook)))
1442 (if mode? (tr-declare-varmode var mode?))))
1443 var)
1445 (bad-var-warn var))))
1447 (defun tunbind (var &aux (old (assoc var tstack :test #'eq)))
1448 (when (variable-p var)
1449 (prog1
1450 (teval var)
1451 (cond (old
1452 (setq tstack (delete old tstack :test #'eq)) ; POP should be all we need.
1453 (setq old (cdr old))
1454 (putprop1 var (tstack-slot-mode old) 'mode)
1455 (putprop1 var (tstack-slot-tbind old) 'tbind)
1456 (putprop1 var (tstack-slot-val-modes old) 'val-modes)
1457 (putprop1 var (tstack-slot-special old) 'special))))))
1459 (defun putprop1 (name value key)
1460 ;; leaves property list clean after unwinding, this
1461 ;; is an efficiency/storage issue only.
1462 (if value
1463 (putprop name value key)
1464 (progn
1465 (remprop name key)
1466 nil)))
1468 (defun tunbinds (l)
1469 (do ((nl))
1470 ((null l) nl)
1471 (setq nl (cons (tunbind (caar tstack)) nl)
1472 l (cdr l))))
1474 (defun tboundp (var)
1475 ;; really LEXICAL-VARP.
1476 (and (symbolp var) (get var 'tbind) (not (get var 'special))))
1478 (defun teval (var)
1479 (or (and (symbolp var) (get var 'tbind)) var))
1481 ;; Local Modes:
1482 ;; Mode: LISP
1483 ;; Comment Col: 40
1484 ;; END: