home *** CD-ROM | disk | FTP | other *** search
- ; -*- Mode: Lisp; Syntax: Common-Lisp; Package: SCHI; -*-
- ; File core.lisp / Copyright (c) 1991 Jonathan Rees / See file COPYING
-
- ;;;; Pseudoscheme runtime system
-
- (lisp:in-package "SCHEME-INTERNAL")
-
- (import '(scheme-hacks:make-photon
- scheme-hacks:qualified-symbol-p
- scheme-hacks:intern-renaming-perhaps
- scheme-hacks:find-symbol-renaming-perhaps
- scheme-hacks:lisp-package))
-
- (export '(lisp-package))
-
- ; The Scheme booleans
- ; - must be self-evaluating
- ; - can't be structures, because they will appear as constants in
- ; compiled files
- ; - must be uniquely created
- ; - can't be symbols without slowing down Scheme's SYMBOL? predicate
- ; - simlarly for numbers, pairs, etc.
- ; What values are self-evaluating Common Lisp objects with a read/print
- ; syntax that aren't used for anything in Scheme? ...
- ; There aren't any.
-
- (defvar false 'false) ;You can set this to 'nil if you want
- (defvar true 't)
-
- (proclaim '(inline truep true? scheme-symbol-p))
-
- ; Convert Scheme boolean to Lisp boolean.
- ; E.g. (lisp:if (truep foo) ...)
-
- (defun truep (scheme-test)
- (not (eq scheme-test false)))
-
- ; Convert Lisp boolean to Scheme boolean.
- ; E.g. (cons (true? (lisp:numberp x)) ...)
- ; This assumes that the argument is never the empty list.
-
- (defun true? (cl-test) (or cl-test false))
-
- (defun scheme-symbol-p (x)
- (declare (optimize (safety 0))) ;compilers are stupid
- (and (symbolp x) (not (eq (car (symbol-plist x)) 'not-a-symbol))))
-
- (setf (get true 'not-a-symbol) t)
- (setf (get false 'not-a-symbol) t)
- (setf (get nil 'not-a-symbol) t) ;used for Scheme's empty list
-
- ;
-
- (defvar scheme-package scheme-hacks:scheme-package)
- (defvar scheme-readtable scheme-hacks:scheme-readtable)
-
- ; Miscellaneous objects
-
- (defvar unspecified (make-photon "#{Unspecified}"))
- (defvar unassigned (make-photon "#{Unassigned}"))
-
- (defvar eof-object
- (if (find-package "PSEUDOSCHEME")
- ;; Temporary hack for coexistence with old versions of Pseudoscheme!
- (intern "EOF-OBJECT" (find-package "PSEUDOSCHEME"))
- (make-photon "#{end-of-file}")))
-
- ; PROCEDURE?
-
- (defparameter closures-might-be-conses-p
- #+Lucid nil ;suppress message about compiler optimizations
- #-Lucid
- (or (consp (eval '#'(lambda (x) x))) ;VAX LISP 2.1
- (consp (let ((g (gensym)))
- (eval `(progn (defun ,g () 0) #',g)))) ;Symbolics
- (consp (compile nil '(lambda (x) x))) ;just for kicks
- (consp (funcall (compile nil '(lambda (x) ;VAX LISP 2.2
- #'(lambda () (prog1 x (incf x)))))
- 0))))
-
- (defun procedurep (obj)
- (and (functionp obj)
- (not (symbolp obj))
- (or (not (consp obj))
- closures-might-be-conses-p)))
-
- ; Mumble
-
- (proclaim '(inline booleanp char-whitespace-p output-port-p))
-
- (defun booleanp (obj)
- (or (eq obj schi:true)
- (eq obj schi:false)))
-
- (defun char-whitespace-p (char)
- (or (char= char #\space)
- (not (graphic-char-p char))))
-
- (defun input-port-p (obj)
- (and (streamp obj)
- (input-stream-p obj)))
-
- (defun output-port-p (obj)
- (and (streamp obj)
- (input-stream-p obj)))
-
- ;This function is new in CLtL II / ANSI.
- (defun realp (obj)
- (and (numberp obj)
- (not (complexp obj))))
-
- ; Auxiliary for SET!
-
- (defun set!-aux (name value CL-sym)
- (case (get CL-sym 'defined)
- ((:assignable))
- ((:not-assignable)
- (cerror "Assign it anyhow"
- "Variable ~S isn't supposed to be SET!"
- (or name CL-sym)))
- ((nil)
- (unless (qualified-symbol-p name) ;(set! foo:bar ...)
- (warn "SET! of undefined variable ~S" (or name CL-sym)))))
- (setf (symbol-value CL-sym) value)
- (if (procedurep value)
- (setf (symbol-function CL-sym) value)
- (fmakunbound CL-sym))
- unspecified)
-
- ; Auxiliary for lambda-expression-containing top-level forms on Symbolics
-
- (defmacro at-top-level (&rest forms)
- (if (member :lispm *features*)
- (let ((g (gentemp "[TOP]")));;!?!?
- `(progn (defun ,g () ,@forms)
- (prog1 (,g)
- (fmakunbound ',g))))
- `(progn ,@forms)))
-
- (defvar *translated-file-type* (preferred-case "PSO"))
-
- ; Prelude on all translated files
-
- (defvar *target-package* nil) ;bound by scheme-load
-
- (defparameter cl-readtable (copy-readtable nil))
-
- (Defmacro begin-translated-file ()
- `(progn (in-package (package-name *target-package*))
- (eval-when (eval compile load)
- (setq *readtable* cl-readtable))))
-
- ; Auxiliaries for top-level DEFINE
-
- (defun set-value-from-function (CL-sym &optional name) ;Follows a DEFUN
- (setf (symbol-value CL-sym) (symbol-function CL-sym))
- (after-define CL-sym name))
-
- (defun really-set-function (CL-sym value)
- (cond ((procedurep value)
- #+Lucid
- (lcl:define-function CL-sym value)
- #-Lucid
- (setf (symbol-function CL-sym) value))
- (t
- (fmakunbound CL-sym))))
-
- (defun set-function-from-value (CL-sym &optional name) ;Follows a SETQ
- (let ((value (symbol-value CL-sym)))
- (really-set-function CL-sym value)
- #+Symbolics
- (scl:record-source-file-name CL-sym (if (procedurep value) 'defun 'defvar))
- (after-define CL-sym name)))
-
- ; Follows (SETQ *FOO* ...)
-
- (defun set-forwarding-function (CL-sym &optional name)
- (setf (symbol-function CL-sym)
- #'(lambda (&rest args)
- (apply (symbol-value CL-sym) args)))
- (after-define CL-sym name))
-
- (defun after-define (CL-sym name)
- (setf (get CL-sym 'defined) t)
- (when name
- (make-photon #'(lambda (port)
- (let ((*package* scheme-package))
- (format port "~S defined." name))))))
-
- ; EQUAL?
-
- ; Differs from Common Lisp EQUAL in that it descends into vectors.
- ; This is here instead of in rts.lisp because it's an auxiliary for
- ; open-coding MEMBER and ASSOC, and the rule is that all auxiliaries
- ; are in the SCHI package (not REVISED^4-SCHEME).
-
- (defun scheme-equal-p (obj1 obj2)
- (cond ((eql obj1 obj2) t)
- ((consp obj1) ;pair?
- (and (consp obj2)
- (scheme-equal-p (car obj1) (car obj2))
- (scheme-equal-p (cdr obj1) (cdr obj2))))
- ((simple-string-p obj1) ;string?
- (and (simple-string-p obj2)
- (string= (the simple-string obj1)
- (the simple-string obj2))))
- ((simple-vector-p obj1)
- (and (simple-vector-p obj2)
- (let ((z (length (the simple-vector obj1))))
- (declare (fixnum z))
- (and (= z (length (the simple-vector obj2)))
- (do ((i 0 (+ i 1)))
- ((= i z) t)
- (declare (fixnum i))
- (when (not (scheme-equal-p
- (aref (the simple-vector obj1) i)
- (aref (the simple-vector obj2) i)))
- (return nil)))))))
- (t nil)))
-