1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019 Ludovic Courtès <ludo@gnu.org>
4 ;;; This file is part of GNU Guix.
6 ;;; GNU Guix is free software; you can redistribute it and/or modify it
7 ;;; under the terms of the GNU General Public License as published by
8 ;;; the Free Software Foundation; either version 3 of the License, or (at
9 ;;; your option) any later version.
11 ;;; GNU Guix is distributed in the hope that it will be useful, but
12 ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ;;; GNU General Public License for more details.
16 ;;; You should have received a copy of the GNU General Public License
17 ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
19 (define-module (guix repl)
20 #:use-module (rnrs bytevectors)
21 #:use-module (ice-9 match)
22 #:export (send-repl-response
27 ;;; This module implements the "machine-readable" REPL provided by
28 ;;; 'guix repl -t machine'. It's a lightweight module meant to be
29 ;;; embedded in any Guile process providing REPL functionality.
33 (define (self-quoting? x)
34 "Return #t if X is self-quoting."
35 (letrec-syntax ((one-of (syntax-rules ()
39 (one-of rest ...))))))
40 (one-of symbol? string? pair? null? vector?
41 bytevector? number? boolean?)))
44 (define (send-repl-response exp output)
45 "Write the response corresponding to the evaluation of EXP to PORT, an
47 (define (value->sexp value)
48 (if (self-quoting? value)
50 `(non-self-quoting ,(object-address value)
51 ,(object->string value))))
55 (let ((results (call-with-values
59 (write `(values ,@(map value->sexp results))
62 (force-output output)))
64 (write `(exception ,key ,@(map value->sexp args)))
66 (force-output output))))
68 (define* (machine-repl #:optional
69 (input (current-input-port))
70 (output (current-output-port)))
71 "Run a machine-usable REPL over ports INPUT and OUTPUT.
73 The protocol of this REPL is meant to be machine-readable and provides proper
74 support to represent multiple-value returns, exceptions, objects that lack a
75 read syntax, and so on. As such it is more convenient and robust than parsing
77 (write `(repl-version 0 0) output)
85 (send-repl-response exp output)