1 ;;; Read-Eval-Print Loop
3 ;; Copyright (C) 2001, 2009 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 repl common)
28 #:use-module (system repl command)
29 #:use-module (system vm vm)
30 #:use-module (system vm debug)
31 #:export (start-repl call-with-backtrace))
33 (define meta-command-token (cons 'meta 'command))
35 (define (meta-reader read)
38 (if (pair? read-args) (car read-args) (current-input-port))
40 (let ((ch (next-char #t)))
41 (cond ((eof-object? ch)
42 ;; apparently sometimes even if this is eof, read will
43 ;; wait on somethingorother. strange.
50 ;; repl-reader is a function defined in boot-9.scm, and is replaced by
51 ;; something else if readline has been activated. much of this hoopla is
52 ;; to be able to re-use the existing readline machinery.
53 (define (prompting-meta-read repl)
54 (let ((prompt (lambda () (repl-prompt repl)))
55 (lread (language-reader (repl-language repl))))
56 (with-fluid* current-reader (meta-reader lread)
57 (lambda () (repl-reader (lambda () (repl-prompt repl)))))))
59 (define (default-catch-handler . args)
63 ((,key ,subr ,msg ,args . ,rest)
64 (let ((cep (current-error-port)))
65 (cond ((not (stack? (fluid-ref the-last-stack))))
66 ((memq 'backtrace (debug-options-interface))
67 (let ((highlights (if (or (eq? key 'wrong-type-arg)
68 (eq? key 'out-of-range))
71 (run-hook before-backtrace-hook)
73 (display "Backtrace:\n")
74 (display-backtrace (fluid-ref the-last-stack) cep
77 (run-hook after-backtrace-hook))))
78 (run-hook before-error-hook)
79 (display-error (fluid-ref the-last-stack) cep subr msg args rest)
80 (run-hook after-error-hook)
81 (set! stack-saved? #f)
84 (format (current-error-port) "\nERROR: uncaught throw to `~a', args: ~a\n"
85 (car args) (cdr args)))))
87 (define (call-with-backtrace thunk)
89 (lambda () (%start-stack #t thunk))
91 default-pre-unwind-handler))
93 (define-macro (with-backtrace form)
94 `(call-with-backtrace (lambda () ,form)))
96 (define (start-repl lang)
97 (let ((repl (make-repl lang))
101 (let ((exp (with-backtrace (prompting-meta-read repl))))
103 ((eqv? exp (if #f #f))) ; read error, pass
104 ((eq? exp meta-command-token)
105 (with-backtrace (meta-command repl)))
110 ;; since the input port is line-buffered, consume up to the
118 (run-hook before-eval-hook exp)
120 (repl-eval repl (repl-parse repl exp))))
122 (for-each (lambda (v)
123 (run-hook before-print-hook v)
127 (set! status args))))))
130 (next-char #f) ;; consume trailing whitespace
133 (define (next-char wait)
134 (if (or wait (char-ready?))
135 (let ((ch (peek-char)))
136 (cond ((eof-object? ch) ch)
137 ((char-whitespace? ch) (read-char) (next-char wait))
141 (define (flush-to-newline)
143 (let ((ch (peek-char)))
144 (if (and (not (eof-object? ch)) (char-whitespace? ch))
147 (if (not (char=? ch #\newline))
148 (flush-to-newline)))))))