source: trunk/j/src/org/armedbear/lisp/top-level.lisp @ 4213

Last change on this file since 4213 was 4213, checked in by piso, 20 years ago

MACROEXPAND-COMMAND

File size: 7.8 KB
Line 
1;;; top-level.lisp
2;;;
3;;; Copyright (C) 2003 Peter Graves
4;;; $Id: top-level.lisp,v 1.7 2003-10-06 01:03:57 piso Exp $
5;;;
6;;; This program is free software; you can redistribute it and/or
7;;; modify it under the terms of the GNU General Public License
8;;; as published by the Free Software Foundation; either version 2
9;;; of the License, or (at your option) any later version.
10;;;
11;;; This program is distributed in the hope that it will be useful,
12;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14;;; GNU General Public License for more details.
15;;;
16;;; You should have received a copy of the GNU General Public License
17;;; along with this program; if not, write to the Free Software
18;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19
20;;; Adapted from SB-ACLREPL (originally written by Kevin Rosenberg).
21
22;;; A few things we're gonna be needing...
23(mapc #'sys::resolve '(sort position break write))
24
25(in-package "EXTENSIONS")
26
27(export '(*saved-backtrace*))
28
29(defvar *saved-backtrace* nil)
30
31(in-package "TOP-LEVEL")
32
33(defvar *break-level* 0)
34
35(defparameter *command-char* #\:)
36
37(defvar *cmd-number* 1
38  "Number of the next command")
39
40(defun prompt-package-name ()
41  (car (sort (append
42              (package-nicknames cl:*package*)
43              (list (package-name cl:*package*)))
44             (lambda (a b) (< (length a) (length b))))))
45
46(defun repl-prompt-fun (stream)
47  (fresh-line stream)
48  (when (> *break-level* 0)
49    (format stream "[~D] " *break-level*))
50  (format stream "~A(~D): " (prompt-package-name) *cmd-number*))
51
52(defparameter *repl-prompt-fun* #'repl-prompt-fun)
53
54(defun peek-char-non-whitespace (stream)
55  (loop
56    (let ((c (read-char stream nil)))
57      (unless (eql c #\Space)
58        (unread-char c stream)
59        (return c)))))
60
61(defun read-cmd (stream)
62  (let ((c (peek-char-non-whitespace stream)))
63    (if (eql c *command-char*)
64        (read-line stream)
65        (read stream nil))))
66
67(defun apropos-command (args)
68  (when args (apropos args)))
69
70(defun backtrace-command (args)
71  (let ((count (or (and args (ignore-errors (parse-integer args)))
72                   most-positive-fixnum))
73        (n 0))
74    (dolist (frame *saved-backtrace*)
75      (format t "  ~D: ~A~%" n frame)
76      (incf n)
77      (when (>= n count)
78        (return))))
79  (values))
80
81(defun continue-command (ignored)
82  (if (> *break-level* 0)
83      (throw 'continue-catcher nil)))
84
85(defun describe-command (args)
86  (let ((obj (eval (read-from-string args))))
87    (describe obj)))
88
89(defun macroexpand-command (args)
90  (format t "~S~%" (macroexpand (read-from-string args)))
91  (values))
92
93(defun package-command (args)
94  (cond ((null args)
95         (format *standard-output* "The ~A package is current.~%"
96                 (package-name *package*)))
97        (t
98         (when (and (plusp (length args)) (eql (char args 0) #\:))
99           (setf args (subseq args 1)))
100         (setf args (nstring-upcase args))
101         (let ((pkg (find-package args)))
102           (if pkg
103               (setf *package* pkg)
104               (format *standard-output* "Unknown package ~A.~%" args))))))
105
106(defun reset-command (ignored)
107  (throw 'top-level-catcher nil))
108
109(defun exit-command (ignored)
110  (exit))
111
112(defun cd-command (args)
113  (cond ((or (null args) (string= args "~"))
114         (setf args (namestring (user-homedir-pathname))))
115        ((and (> (length args) 1) (string= (subseq args 0 2) "~/")
116              (setf args (concatenate 'string
117                                      (namestring (user-homedir-pathname))
118                                      (subseq args 2))))))
119  (let ((dir (probe-directory args)))
120    (if dir
121        (progn
122          (setf *default-pathname-defaults* dir)
123          (format t "~A" (namestring *default-pathname-defaults*)))
124        (format t "Error: no such directory (~S).~%" args))))
125
126(defvar *last-files-loaded* nil)
127
128(defun tokenize (string)
129  (do* ((res nil)
130        (string (string-left-trim " " string)
131                (string-left-trim " " (subseq string end)))
132        (end (position #\Space string) (position #\Space string)))
133       ((zerop (length string)) (nreverse res))
134    (unless end
135      (setf end (length string)))
136    (push (subseq string 0 end) res)))
137
138(defun ld-command (args)
139  (let ((files (if args (tokenize args) *last-files-loaded*)))
140    (setf *last-files-loaded* files)
141    (dolist (file files)
142      (load file))))
143
144(defun pwd-command (ignored)
145  (format t "~A~%" *default-pathname-defaults*))
146
147(defconstant spaces (make-string 32 :initial-element #\space))
148
149(defun pad (string width)
150  (if (< (length string) width)
151      (concatenate 'string string (subseq spaces 0 (- width (length string))))
152      string))
153
154(defun help-command (ignored)
155  (format t "COMMAND     ABBR DESCRIPTION~%")
156  (dolist (entry *command-table*)
157    (format t "~A~A~A~%"
158            (pad (entry-name entry) 12)
159            (pad (entry-abbr entry) 5)
160            (entry-help entry)
161            )))
162
163(defparameter *command-table*
164  '(("apropos" 2 apropos-command "show apropos")
165    ("bt" 2 backtrace-command "backtrace n stack frames (default all)")
166    ("cd" 2 cd-command "change default directory")
167    ("continue" 4 continue-command "return from break")
168    ("describe" 2 describe-command "describe an object")
169    ("exit" 2 exit-command "exit lisp")
170    ("help" 2 help-command "print this help")
171    ("ld" 2 ld-command "load a file")
172    ("macroexpand" 2 macroexpand-command "macroexpand an expression")
173    ("package" 2 package-command "change current package")
174    ("pwd" 3 pwd-command "print current directory")
175    ("reset" 3 reset-command "return to top level")
176    ))
177
178(defun entry-name (entry)
179  (first entry))
180
181(defun entry-min-len (entry)
182  (second entry))
183
184(defun entry-abbr (entry)
185  (if (< (entry-min-len entry) (length (entry-name entry)))
186      (subseq (entry-name entry) 0 (entry-min-len entry))
187      ""))
188
189(defun entry-command (entry)
190  (third entry))
191
192(defun entry-help (entry)
193  (fourth entry))
194
195(defun find-command (string)
196  (let ((len (length string)))
197    (dolist (entry *command-table*)
198      (let ((min-len (entry-min-len entry)))
199        (when (and (>= len min-len)
200                   (string-equal (entry-name entry) string :end1 len))
201          (return (entry-command entry)))))))
202
203(defun process-cmd (form)
204  (when (and (stringp form) (> (length form) 1) (eql (char form 0) *command-char*))
205    (let* ((pos (position #\Space form))
206           (cmd (subseq form 1 pos))
207           (args (if pos (subseq form (1+ pos)) nil)))
208      (when args
209        (setf args (string-trim " " args))
210        (when (zerop (length args))
211          (setf args nil)))
212      (let ((fun (find-command cmd)))
213        (if fun
214            (funcall fun args)
215            (format t "Unknown top-level command ~S.~%" cmd)))
216      (return-from process-cmd t)))
217  nil)
218
219(defun repl-read-form-fun (in out)
220  (loop
221    (let ((form (read-cmd in)))
222      (setf (charpos out) 0)
223      (incf *cmd-number*)
224      (cond ((process-cmd form)
225             (funcall *repl-prompt-fun* *standard-output*)
226             (finish-output *standard-output*))
227            (t
228             (return form))))))
229
230(defparameter *repl-read-form-fun* #'repl-read-form-fun)
231
232(defun repl ()
233  (loop
234    (funcall *repl-prompt-fun* *standard-output*)
235    (finish-output *standard-output*)
236    (let* ((form (funcall *repl-read-form-fun*
237                          *standard-input*
238                          *standard-output*))
239           (results (multiple-value-list (sys::interactive-eval form))))
240      (dolist (result results)
241        (fresh-line)
242        (prin1 result)))))
243
244(defun top-level-loop ()
245  (fresh-line)
246  (format t "Type :HELP for a list of top-level commands.~%")
247  (loop
248    (catch 'top-level-catcher
249      (handler-case
250          (repl)
251        #+j (stream-error (c) (return-from top-level-loop)) ; FIXME
252        (error (c) (format t "Error: ~S.~%" c) (break) (throw 'top-level-catcher nil))))))
Note: See TracBrowser for help on using the repository browser.