From bfb698fd1bcf1d99e348379f2d220b7cbc874c24 Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Sun, 12 Sep 2010 06:53:41 +0000 Subject: [PATCH] keeping track of history in repl --- lib/chibi/repl.module | 3 ++- lib/chibi/repl.scm | 41 +++++++++++++++++++++++------------------ 2 files changed, 25 insertions(+), 19 deletions(-) diff --git a/lib/chibi/repl.module b/lib/chibi/repl.module index 742b9581..2bb224e5 100644 --- a/lib/chibi/repl.module +++ b/lib/chibi/repl.module @@ -5,5 +5,6 @@ (import (chibi ast) (chibi process) (chibi term edit-line) - (srfi 18)) + (srfi 18) + (srfi 38)) (include "repl.scm")) diff --git a/lib/chibi/repl.scm b/lib/chibi/repl.scm index b7ff79bc..d4ae19c9 100644 --- a/lib/chibi/repl.scm +++ b/lib/chibi/repl.scm @@ -18,24 +18,29 @@ thunk (lambda () (set-signal-action! sig old-handler))))) -(define (run-repl module env) - (let ((line (edit-line (if module (string-append (symbol->string module) "> ") "> ")))) - (cond - ((or (not line) (eof-object? line))) - ((equal? line "") (run-repl module env)) - (else - (handle-exceptions exn (print-exception exn (current-error-port)) - (let* ((expr (call-with-input-string line read)) - (thread (make-thread (lambda () - (let ((res (eval expr env))) - (if (not (eq? res (if #f #f))) - (write res))))))) - (with-signal-handler - signal/interrupt - (lambda (n) (thread-terminate! thread)) - (lambda () (thread-start! thread) (thread-join! thread))))) - (newline) - (run-repl module env))))) +(define (run-repl module env . o) + (let ((history (make-history))) + (let lp ((module module) (env env)) + (let ((line (edit-line (if module (string-append (symbol->string module) "> ") "> ") + 'history: history))) + (cond + ((or (not line) (eof-object? line))) + ((equal? line "") (lp module env)) + (else + (history-commit! history line) + (handle-exceptions + exn (print-exception exn (current-error-port)) + (let* ((expr (call-with-input-string line read/ss)) + (thread (make-thread (lambda () + (let ((res (eval expr env))) + (if (not (eq? res (if #f #f))) + (write/ss res)) + (newline)))))) + (with-signal-handler + signal/interrupt + (lambda (n) (thread-terminate! thread)) + (lambda () (thread-start! thread) (thread-join! thread))))) + (lp module env))))))) (define (repl) (run-repl #f (interaction-environment)))