From e702206d6b90325cd83ea044e129951972eaaa24 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 28 Mar 2023 21:37:52 -0400 Subject: repl: Allow multiple expressions per line. --- catbird/repl.scm | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-) diff --git a/catbird/repl.scm b/catbird/repl.scm index c281411..98798e6 100644 --- a/catbird/repl.scm +++ b/catbird/repl.scm @@ -49,7 +49,6 @@ repl-print)) ;; TODO: $number values like regular Guile REPL -;; TODO: Multiple expressions ;; TODO: Debugger ;; TODO: Switching languages @@ -206,10 +205,15 @@ (resize bg) (refresh-log repl))) -(define-method (repl-read-expression (repl ) line) - (call-with-input-string line - (lambda (port) - ((language-reader (language repl)) port (module repl))))) +(define-method (repl-read-expressions (repl ) line) + (let ((read* (language-reader (language repl)))) + (call-with-input-string line + (lambda (port) + (let loop () + (let ((exp (read* port (module repl)))) + (if (eof-object? exp) + '() + (cons exp (loop))))))))) (define-method (with-output-to-log (repl ) thunk) (let* ((vals #f) @@ -255,15 +259,24 @@ (with-exception-handler exception-handler throw-handler #:unwind? #t))) (define-method (repl-compile (repl ) line) - (define thunk - (load-thunk-from-memory - (compile (repl-read-expression repl line) - #:from (language repl) - #:to 'bytecode - #:env (module repl)))) + (define (compile-and-eval exp) + (define thunk + (load-thunk-from-memory + (compile exp + #:from (language repl) + #:to 'bytecode + #:env (module repl)))) + (save-module-excursion + (lambda () + (set-current-module (module repl)) + (call-with-values thunk list)))) (define (compile-line) - (with-output-to-log repl thunk)) - (with-error-handling repl compile-line)) + (append-map (lambda (exp) + (compile-and-eval exp)) + (repl-read-expressions repl line))) + (with-error-handling repl + (lambda () + (with-output-to-log repl compile-line)))) (define-method (write-value-to-log (repl ) x) (unless (unspecified? x) @@ -331,7 +344,7 @@ (meta-command repl line) (for-each (lambda (val) (write-value-to-log repl val)) - (call-with-values (lambda () (repl-compile repl line)) list))) + (repl-compile repl line))) (clear-line editor) (refresh-log repl) (refresh-prompt repl))) -- cgit v1.2.3