summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Thompson <dthompson2@worcester.edu>2023-03-27 20:22:23 -0400
committerDavid Thompson <dthompson2@worcester.edu>2023-03-27 20:23:02 -0400
commit183f3858c4d20d363329340af0f1f4b055fb1f39 (patch)
treefd89af37ccf3b4ed24d083084ac43ad5f9677383
parent72c76fb7b6f1b99c654ae9b8deee0dacfda6a7d5 (diff)
repl: Add multiple return value support.
-rw-r--r--catbird/repl.scm34
1 files changed, 20 insertions, 14 deletions
diff --git a/catbird/repl.scm b/catbird/repl.scm
index d3768b5..3c56f35 100644
--- a/catbird/repl.scm
+++ b/catbird/repl.scm
@@ -42,12 +42,13 @@
#:use-module (srfi srfi-9)
#:use-module (system base compile)
#:use-module (system base language)
+ #:use-module (system vm loader)
#:export (<repl>
<repl-mode>
resize-repl
repl-print))
-;; TODO: Multiple values
+;; TODO: $number values like regular Guile REPL
;; TODO: Multiple expressions
;; TODO: Debugger
;; TODO: Switching languages
@@ -174,17 +175,15 @@
((language-reader (language repl)) port (module repl)))))
(define-method (with-output-to-log (repl <repl>) thunk)
- (let* ((val *unspecified*)
+ (let* ((vals #f)
(str (call-with-output-string
(lambda (port)
(parameterize ((current-output-port port)
(current-error-port port))
- (set! val (thunk)))))))
+ (set! vals (call-with-values thunk list)))))))
(unless (string-null? str)
- (for-each (lambda (line)
- (log-append repl line))
- (string-split str #\newline)))
- val))
+ (log-append repl str))
+ (apply values vals)))
(define-method (with-error-handling (repl <repl>) thunk)
(let ((stack #f))
@@ -227,17 +226,22 @@
(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-line)
- (with-output-to-log repl
- (lambda ()
- (compile (repl-read-expression repl line)
- #:from (language repl)
- #:env (module repl)))))
+ (with-output-to-log repl thunk))
(with-error-handling repl compile-line))
(define-method (write-value-to-log (repl <repl>) x)
(unless (unspecified? x)
- (log-append repl (repl-print x))))
+ (log-append repl (make <horizontal-container>
+ #:children
+ (list (make <label> #:text "=> ")
+ (repl-print x))))))
(define (skip-whitespace str i)
(let loop ((i i))
@@ -296,7 +300,9 @@
(log-append repl (string-append (prompt editor) line))
(if (meta-command-string? line)
(meta-command repl line)
- (write-value-to-log repl (repl-compile repl line)))
+ (for-each (lambda (val)
+ (write-value-to-log repl val))
+ (call-with-values (lambda () (repl-compile repl line)) list)))
(clear-line editor)
(refresh-log repl)
(refresh-prompt repl)))