keeping track of history in repl

This commit is contained in:
Alex Shinn 2010-09-12 06:53:41 +00:00
parent 37f8c6c8b9
commit bfb698fd1b
2 changed files with 25 additions and 19 deletions

View file

@ -5,5 +5,6 @@
(import (chibi ast) (import (chibi ast)
(chibi process) (chibi process)
(chibi term edit-line) (chibi term edit-line)
(srfi 18)) (srfi 18)
(srfi 38))
(include "repl.scm")) (include "repl.scm"))

View file

@ -18,24 +18,29 @@
thunk thunk
(lambda () (set-signal-action! sig old-handler))))) (lambda () (set-signal-action! sig old-handler)))))
(define (run-repl module env) (define (run-repl module env . o)
(let ((line (edit-line (if module (string-append (symbol->string module) "> ") "> ")))) (let ((history (make-history)))
(let lp ((module module) (env env))
(let ((line (edit-line (if module (string-append (symbol->string module) "> ") "> ")
'history: history)))
(cond (cond
((or (not line) (eof-object? line))) ((or (not line) (eof-object? line)))
((equal? line "") (run-repl module env)) ((equal? line "") (lp module env))
(else (else
(handle-exceptions exn (print-exception exn (current-error-port)) (history-commit! history line)
(let* ((expr (call-with-input-string line read)) (handle-exceptions
exn (print-exception exn (current-error-port))
(let* ((expr (call-with-input-string line read/ss))
(thread (make-thread (lambda () (thread (make-thread (lambda ()
(let ((res (eval expr env))) (let ((res (eval expr env)))
(if (not (eq? res (if #f #f))) (if (not (eq? res (if #f #f)))
(write res))))))) (write/ss res))
(newline))))))
(with-signal-handler (with-signal-handler
signal/interrupt signal/interrupt
(lambda (n) (thread-terminate! thread)) (lambda (n) (thread-terminate! thread))
(lambda () (thread-start! thread) (thread-join! thread))))) (lambda () (thread-start! thread) (thread-join! thread)))))
(newline) (lp module env)))))))
(run-repl module env)))))
(define (repl) (define (repl)
(run-repl #f (interaction-environment))) (run-repl #f (interaction-environment)))