1 ;;; Read-Eval-Print Loop
3 ;; Copyright (C) 2001, 2009, 2010 Free Software Foundation, Inc.
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 (define-module (system repl repl)
23 #:use-module (system base syntax)
24 #:use-module (system base pmatch)
25 #:use-module (system base compile)
26 #:use-module (system base language)
27 #:use-module (system vm vm)
28 #:use-module (system repl error-handling)
29 #:use-module (system repl common)
30 #:use-module (system repl command)
31 #:use-module (ice-9 control)
32 #:export (start-repl run-repl))
40 (define meta-command-token (cons 'meta 'command))
42 (define (meta-reader read env)
44 (let ((port (if (pair? read-args) (car read-args) (current-input-port))))
45 (with-input-from-port port
47 (let ((ch (next-char #t)))
48 (cond ((eof-object? ch)
49 ;; apparently sometimes even if this is eof, read will
50 ;; wait on somethingorother. strange.
55 (else (read port env)))))))))
57 ;; repl-reader is a function defined in boot-9.scm, and is replaced by
58 ;; something else if readline has been activated. much of this hoopla is
59 ;; to be able to re-use the existing readline machinery.
61 ;; Catches read errors, returning *unspecified* in that case.
62 (define (prompting-meta-read repl)
65 (repl-reader (lambda () (repl-prompt repl))
66 (meta-reader (language-reader (repl-language repl))
71 (apply throw key args))
74 ((,subr ,msg ,args . ,rest)
75 (format #t "Throw to key `~a' while reading expression:\n" key)
76 (display-error #f (current-output-port) subr msg args rest))
78 (format #t "Throw to key `~a' with args `~s' while reading expression.\n"
89 (define* (start-repl #:optional (lang (current-language)) #:key debug)
90 (run-repl (make-repl lang debug)))
92 ;; (put 'abort-on-error 'scheme-indent-function 1)
93 (define-syntax abort-on-error
99 (format #t "While ~A:~%" string)
101 ((,subr ,msg ,args . ,rest)
102 (display-error #f (current-output-port) subr msg args rest))
104 (format #t "ERROR: Throw to key `~a' with args `~s'.\n" key args)))
108 (define (run-repl repl)
109 (define (with-stack-and-prompt thunk)
110 (call-with-prompt (default-prompt-tag)
111 (lambda () (start-stack #t (thunk)))
113 (with-stack-and-prompt (lambda () (proc k))))))
115 (% (with-fluids ((*repl-stack*
116 (cons repl (or (fluid-ref *repl-stack*) '()))))
117 (if (null? (cdr (fluid-ref *repl-stack*)))
120 (let ((exp (prompting-meta-read repl)))
122 ((eqv? exp *unspecified*)) ; read error, pass
123 ((eq? exp meta-command-token)
131 (format #t "While executing meta-command:~%" string)
133 ((,subr ,msg ,args . ,rest)
134 (display-error #f (current-output-port) subr msg args rest))
136 (format #t "ERROR: Throw to key `~a' with args `~s'.\n" k args)))
142 ;; since the input port is line-buffered, consume up to the
145 (call-with-error-handling
152 (abort-on-error "compiling expression"
153 (repl-prepare-eval-thunk
155 (abort-on-error "parsing expression"
156 (repl-parse repl exp))))))
157 (run-hook before-eval-hook exp)
159 (with-stack-and-prompt thunk)))
160 (lambda (k) (values))))
162 (for-each (lambda (v)
167 #:trap-handler 'disabled)))
168 (next-char #f) ;; consume trailing whitespace
173 (define (next-char wait)
174 (if (or wait (char-ready?))
175 (let ((ch (peek-char)))
176 (cond ((eof-object? ch) ch)
177 ((char-whitespace? ch) (read-char) (next-char wait))
181 (define (flush-to-newline)
183 (let ((ch (peek-char)))
184 (if (and (not (eof-object? ch)) (char-whitespace? ch))
187 (if (not (char=? ch #\newline))
188 (flush-to-newline)))))))