Defining and using a repl-print generic to allow customizing REPL output.

This commit is contained in:
Alex Shinn 2024-10-28 09:16:30 +09:00
parent 4f3a98b2b3
commit f4e3c0fd0b
2 changed files with 14 additions and 9 deletions

View file

@ -402,6 +402,11 @@
((= (length value) 1) (push-history-value! (car value))) ((= (length value) 1) (push-history-value! (car value)))
(else (push-history-value! value)))) (else (push-history-value! value))))
(define-generic repl-print)
(define-method (repl-print obj (out output-port?))
(write/ss obj out))
(define (repl/eval rp expr-list) (define (repl/eval rp expr-list)
(let ((thread (current-thread)) (let ((thread (current-thread))
(out (repl-out rp))) (out (repl-out rp)))
@ -422,17 +427,17 @@
(null? expr)) (null? expr))
(eval expr (repl-env rp)) (eval expr (repl-env rp))
expr)) expr))
(lambda res-list (lambda res-values
(cond (cond
((not (or (null? res-list) ((not (or (null? res-values)
(equal? res-list (list (if #f #f))))) (equal? res-values (list undefined-value))))
(push-history-value-maybe! res-list) (push-history-value-maybe! res-values)
(write/ss (car res-list) out) (repl-print (car res-values) out)
(for-each (for-each
(lambda (res) (lambda (res)
(write-char #\space out) (write-char #\space out)
(write/ss res out)) (repl-print res out))
(cdr res-list)) (cdr res-values))
(newline out)))))) (newline out))))))
expr-list)))))) expr-list))))))

View file

@ -1,8 +1,8 @@
(define-library (chibi repl) (define-library (chibi repl)
(export repl $0 $1 $2 $3 $4 $5 $6 $7 $8 $9) (export repl repl-print $0 $1 $2 $3 $4 $5 $6 $7 $8 $9)
(import (chibi) (only (meta) load-module module-name->file) (import (chibi) (only (meta) load-module module-name->file)
(chibi ast) (chibi modules) (chibi doc) (chibi ast) (chibi modules) (chibi doc) (chibi generic)
(chibi string) (chibi io) (chibi optional) (chibi string) (chibi io) (chibi optional)
(chibi process) (chibi term edit-line) (chibi process) (chibi term edit-line)
(srfi 1) (srfi 1)