From 183f3858c4d20d363329340af0f1f4b055fb1f39 Mon Sep 17 00:00:00 2001 From: David Thompson Date: Mon, 27 Mar 2023 20:22:23 -0400 Subject: repl: Add multiple return value support. --- catbird/repl.scm | 34 ++++++++++++++++++++-------------- 1 file 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 ( 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 ) 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 ) thunk) (let ((stack #f)) @@ -227,17 +226,22 @@ (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-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 ) x) (unless (unspecified? x) - (log-append repl (repl-print x)))) + (log-append repl (make + #:children + (list (make