summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-03-28 21:37:52 -0400
committerDavid Thompson <dthompson2@worcester.edu>2023-03-28 21:38:42 -0400
commite702206d6b90325cd83ea044e129951972eaaa24 (patch)
tree33e3c94cd9b9369599d4b04404dd45f4560dc304
parent9fd716bcdf9a0020830437fda759df2d2a860180 (diff)
repl: Allow multiple expressions per line.
-rw-r--r--catbird/repl.scm41
1 files 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 <repl>) line)
- (call-with-input-string line
- (lambda (port)
- ((language-reader (language repl)) port (module repl)))))
+(define-method (repl-read-expressions (repl <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 <repl>) thunk)
(let* ((vals #f)
@@ -255,15 +259,24 @@
(with-exception-handler exception-handler throw-handler #:unwind? #t)))
(define-method (repl-compile (repl <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 <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)))