|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define-module (ice-9 scm-style-repl) |
|
#:use-module (ice-9 save-stack) |
|
|
|
#:export (scm-repl-silent |
|
scm-repl-print-unspecified |
|
scm-repl-verbose |
|
scm-repl-prompt |
|
assert-repl-silence |
|
assert-repl-print-unspecified |
|
assert-repl-verbosity |
|
|
|
default-pre-unwind-handler |
|
bad-throw |
|
error-catching-loop |
|
error-catching-repl |
|
scm-style-repl |
|
handle-system-error)) |
|
|
|
(define scm-repl-silent #f) |
|
(define (assert-repl-silence v) (set! scm-repl-silent v)) |
|
|
|
(define scm-repl-print-unspecified #f) |
|
(define (assert-repl-print-unspecified v) (set! scm-repl-print-unspecified v)) |
|
|
|
(define scm-repl-verbose #f) |
|
(define (assert-repl-verbosity v) (set! scm-repl-verbose v)) |
|
|
|
(define scm-repl-prompt "guile> ") |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(define (bad-throw key . args) |
|
(let ((default (symbol-property key 'throw-handler-default))) |
|
(or (and default (apply default key args)) |
|
(apply error "unhandled-exception:" key args)))) |
|
|
|
|
|
|
|
(define (default-pre-unwind-handler key . args) |
|
|
|
(save-stack 2) |
|
(apply throw key args)) |
|
|
|
|
|
|
|
(define has-shown-debugger-hint? #f) |
|
|
|
(define (error-catching-loop thunk) |
|
(let ((status #f) |
|
(interactive #t)) |
|
(define (loop first) |
|
(let ((next |
|
(catch #t |
|
|
|
(lambda () |
|
(call-with-unblocked-asyncs |
|
(lambda () |
|
(first) |
|
|
|
|
|
|
|
|
|
|
|
(set! first #f) |
|
(let loop ((v (thunk))) |
|
(loop (thunk))) |
|
#f))) |
|
|
|
(lambda (key . args) |
|
(case key |
|
((quit) |
|
(set! status args) |
|
#f) |
|
|
|
((switch-repl) |
|
(apply throw 'switch-repl args)) |
|
|
|
((abort) |
|
|
|
|
|
|
|
(lambda () |
|
(run-hook abort-hook) |
|
(force-output (current-output-port)) |
|
(display "ABORT: " (current-error-port)) |
|
(write args (current-error-port)) |
|
(newline (current-error-port)) |
|
(if interactive |
|
(begin |
|
(if (and |
|
(not has-shown-debugger-hint?) |
|
(not (memq 'backtrace |
|
(debug-options-interface))) |
|
(stack? (fluid-ref the-last-stack))) |
|
(begin |
|
(newline (current-error-port)) |
|
(display |
|
"Type \"(backtrace)\" to get more information or \"(debug)\" to enter the debugger.\n" |
|
(current-error-port)) |
|
(set! has-shown-debugger-hint? #t))) |
|
(force-output (current-error-port))) |
|
(begin |
|
(primitive-exit 1))) |
|
(set! stack-saved? #f))) |
|
|
|
(else |
|
|
|
(lambda () |
|
(cond ((= (length args) 4) |
|
(apply handle-system-error key args)) |
|
(else |
|
(apply bad-throw key args))))))) |
|
|
|
default-pre-unwind-handler))) |
|
|
|
(if next (loop next) status))) |
|
(set! ensure-batch-mode! (lambda () |
|
(set! interactive #f) |
|
(restore-signals))) |
|
(set! batch-mode? (lambda () (not interactive))) |
|
(call-with-blocked-asyncs |
|
(lambda () (loop (lambda () #t)))))) |
|
|
|
(define (error-catching-repl r e p) |
|
(error-catching-loop |
|
(lambda () |
|
(call-with-values (lambda () (e (r))) |
|
(lambda the-values (for-each p the-values)))))) |
|
|
|
(define (scm-style-repl) |
|
(letrec ( |
|
(start-gc-rt #f) |
|
(start-rt #f) |
|
(repl-report-start-timing (lambda () |
|
(set! start-gc-rt (gc-run-time)) |
|
(set! start-rt (get-internal-run-time)))) |
|
(repl-report (lambda () |
|
(display ";;; ") |
|
(display (inexact->exact |
|
(* 1000 (/ (- (get-internal-run-time) start-rt) |
|
internal-time-units-per-second)))) |
|
(display " msec (") |
|
(display (inexact->exact |
|
(* 1000 (/ (- (gc-run-time) start-gc-rt) |
|
internal-time-units-per-second)))) |
|
(display " msec in gc)\n"))) |
|
|
|
(consume-trailing-whitespace |
|
(lambda () |
|
(let ((ch (peek-char))) |
|
(cond |
|
((eof-object? ch)) |
|
((or (char=? ch #\space) (char=? ch #\tab)) |
|
(read-char) |
|
(consume-trailing-whitespace)) |
|
((char=? ch #\newline) |
|
(read-char)))))) |
|
(-read (lambda () |
|
(let ((val |
|
(let ((prompt (cond ((string? scm-repl-prompt) |
|
scm-repl-prompt) |
|
((thunk? scm-repl-prompt) |
|
(scm-repl-prompt)) |
|
(scm-repl-prompt "> ") |
|
(else "")))) |
|
(repl-reader prompt)))) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(if (not (eof-object? val)) |
|
(consume-trailing-whitespace)) |
|
(run-hook after-read-hook) |
|
(if (eof-object? val) |
|
(begin |
|
(repl-report-start-timing) |
|
(if scm-repl-verbose |
|
(begin |
|
(newline) |
|
(display ";;; EOF -- quitting") |
|
(newline))) |
|
(quit 0))) |
|
val))) |
|
|
|
(-eval (lambda (sourc) |
|
(repl-report-start-timing) |
|
(run-hook before-eval-hook sourc) |
|
(let ((val (start-stack 'repl-stack |
|
|
|
|
|
|
|
|
|
|
|
(primitive-eval sourc)))) |
|
(run-hook after-eval-hook sourc) |
|
val))) |
|
|
|
|
|
(-print (let ((maybe-print (lambda (result) |
|
(if (or scm-repl-print-unspecified |
|
(not (unspecified? result))) |
|
(begin |
|
(write result) |
|
(newline)))))) |
|
(lambda (result) |
|
(if (not scm-repl-silent) |
|
(begin |
|
(run-hook before-print-hook result) |
|
(maybe-print result) |
|
(run-hook after-print-hook result) |
|
(if scm-repl-verbose |
|
(repl-report)) |
|
(force-output)))))) |
|
|
|
(-quit (lambda (args) |
|
(if scm-repl-verbose |
|
(begin |
|
(display ";;; QUIT executed, repl exitting") |
|
(newline) |
|
(repl-report))) |
|
args))) |
|
|
|
(let ((status (error-catching-repl -read |
|
-eval |
|
-print))) |
|
(-quit status)))) |
|
|
|
(define (handle-system-error key . args) |
|
(let ((cep (current-error-port))) |
|
(cond ((not (stack? (fluid-ref the-last-stack)))) |
|
((memq 'backtrace (debug-options-interface)) |
|
(let ((highlights (if (or (eq? key 'wrong-type-arg) |
|
(eq? key 'out-of-range)) |
|
(list-ref args 3) |
|
'()))) |
|
(run-hook before-backtrace-hook) |
|
(newline cep) |
|
(display "Backtrace:\n") |
|
(display-backtrace (fluid-ref the-last-stack) cep |
|
#f #f highlights) |
|
(newline cep) |
|
(run-hook after-backtrace-hook)))) |
|
(run-hook before-error-hook) |
|
(apply display-error (fluid-ref the-last-stack) cep args) |
|
(run-hook after-error-hook) |
|
(force-output cep) |
|
(throw 'abort key))) |
|
|