Changeset 686


Ignore:
Timestamp:
Mar 22, 2004, 9:28:13 AM (21 years ago)
Author:
Gary Byers
Message:

Selection-stream stuff, apparently still buggy.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/ccl/level-1/l1-streams.lisp

    r585 r686  
    23062306      'fd-character-output-stream)))
    23072307
    2308 
    2309    
     2308(defstruct (input-selection (:include dll-node))
     2309  (package nil :type (or null string package))
     2310  (source-file nil :type (or null string pathname))
     2311  (string-stream nil :type (or null string-input-stream)))
     2312
     2313(defstruct (input-selection-queue (:include locked-dll-header)))
     2314
     2315(defclass selection-input-stream (fd-character-input-stream)
     2316    ((selections :initform (init-dll-header (make-input-selection-queue))
     2317                 :reader selection-input-stream-selections)
     2318     (current-selection :initform nil
     2319                        :accessor selection-input-stream-current-selection)
     2320     (peer-fd  :reader selection-input-stream-peer-fd)))
     2321
     2322(defmethod select-stream-class ((class (eql 'selection-input-stream))
     2323                                in-p out-p char-p)
     2324  (if (and in-p char-p (not out-p))
     2325    'selection-input-stream
     2326    (error "Can't create that type of stream.")))
     2327
     2328(defun make-selection-input-stream (fd &key peer-fd (elements-per-buffer *elements-per-buffer*))
     2329  (let* ((s (make-fd-stream fd
     2330                            :elements-per-buffer elements-per-buffer
     2331                            :class 'selection-input-stream)))
     2332    (setf (slot-value s 'peer-fd) peer-fd)
     2333    s))
     2334
     2335(defmethod stream-clear-input ((s selection-input-stream))
     2336  (call-next-method)
     2337  (let* ((q (selection-input-stream-selections s)))
     2338    (with-locked-dll-header (q)
     2339      (do* ((first (dll-header-first q) (dll-header-first q)))
     2340           ((eq first q))
     2341        (remove-dll-node first))))
     2342  (setf (selection-input-stream-current-selection s) nil))
     2343
     2344(defmethod enqueue-input-selection ((stream selection-input-stream)
     2345                                    (selection input-selection))
     2346  (let* ((q (selection-input-stream-selections stream)))
     2347    (with-locked-dll-header (q)
     2348      (append-dll-node selection q)
     2349      (%stack-block ((buf 1))
     2350        (setf (%get-unsigned-byte buf)
     2351              (logand (char-code #\d) #x1f))
     2352        (fd-write (slot-value stream 'peer-fd)
     2353                  buf
     2354                  1)))))
     2355             
     2356
     2357
    23102358(defresource *string-output-stream-pool*
    23112359  :constructor (make-string-output-stream)
     
    24272475          *standard-output* *trace-output*))
    24282476
     2477;;; Interaction with the REPL.  READ-TOPLEVEL-FORM should return 3
     2478;;; values: a form, a (possibly null) pathname, and a boolean that
     2479;;; indicates whether or not the result(s) of evaluating the form
     2480;;; should be printed.  (The last value has to do with how selections
     2481;;; that contain multiple forms are handled; see *VERBOSE-EVAL-SELECTION*
     2482;;; and the SELECTION-INPUT-STREAM method below.)
     2483(defmethod read-toplevel-form ((stream input-stream)
     2484                               eof-value)
     2485  (loop
     2486    (let* ((*in-read-loop* nil)
     2487           (form (read stream nil eof-value)))
     2488      (if (eq form eof-value)
     2489        (return (values form nil t))
     2490        (progn
     2491           (let ((ch))                 ;Trim whitespace
     2492            (while (and (listen stream)
     2493                        (setq ch (read-char stream nil nil))
     2494                        (whitespacep cH))
     2495              (setq ch nil))
     2496            (when ch (unread-char ch stream)))
     2497          (when *listener-indent*
     2498            (write-char #\space stream)
     2499            (write-char #\space stream))
     2500          (return (values (process-single-selection form) nil t)))))))
     2501
     2502(defparameter *verbose-eval-selection* nil
     2503  "When true, the results of evaluating all forms in an input selection
     2504are printed.  When false, only the results of evaluating the last form
     2505are printed.")
     2506
     2507(defmethod read-toplevel-form ((stream selection-input-stream)
     2508                               eof-value)
     2509  ;; If we don't have a selection, try to get one.  Read from the
     2510  ;; underlying input stream; if that yields an EOF, that -usually-
     2511  ;; means that a selection's been posted.
     2512  (do* ((selection (selection-input-stream-current-selection stream)))
     2513       ()
     2514    (when (null selection)
     2515      (let* ((form (call-next-method)))
     2516        (if (eq form eof-value)
     2517          (setq selection
     2518                (setf (selection-input-stream-current-selection stream)
     2519                      (locked-dll-header-dequeue
     2520                       (selection-input-stream-selections stream))))
     2521          (return (values form nil t)))))
     2522    (if (null selection)
     2523      (return (values eof-value nil t))
     2524      (let* ((*package* *package*)
     2525             (string-stream (input-selection-string-stream selection))
     2526             (selection-package (input-selection-package selection))
     2527             (pkg (if selection-package (pkg-arg selection-package))))
     2528        (when pkg (setq *package* pkg))
     2529        (let* ((form (read-toplevel-form string-stream eof-value))
     2530               (last-form-in-selection (eofp string-stream)))
     2531          (when last-form-in-selection
     2532            (setf (selection-input-stream-current-selection stream) nil))
     2533          (return (values form
     2534                          (input-selection-source-file selection)
     2535                          (or last-form-in-selection *verbose-eval-selection*))))))))
     2536
     2537                             
     2538       
     2539
     2540
    24292541; end of L1-streams.lisp
Note: See TracChangeset for help on using the changeset viewer.