1 ;;; GNU Guix --- Functional package management for GNU
2 ;;; Copyright © 2018, 2019, 2020 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 (srfi srfi-1)
21 #:use-module (srfi srfi-26)
22 #:use-module (ice-9 match)
23 #:export (send-repl-response
28 ;;; This module implements the "machine-readable" REPL provided by
29 ;;; 'guix repl -t machine'. It's a lightweight module meant to be
30 ;;; embedded in any Guile process providing REPL functionality.
34 (define (self-quoting? x)
35 "Return #t if X is self-quoting."
36 (letrec-syntax ((one-of (syntax-rules ()
40 (one-of rest ...))))))
41 (one-of symbol? string? keyword? pair? null? array?
42 number? boolean? char?)))
45 ;; Current REPL prompt or #f.
48 (define (stack->frames stack)
49 "Return STACK's frames as a list."
50 (unfold (cute >= <> (stack-length stack))
51 (cut stack-ref stack <>)
55 (define* (send-repl-response exp output
56 #:key (version '(0 0)))
57 "Write the response corresponding to the evaluation of EXP to PORT, an
58 output port. VERSION is the client's protocol version we are targeting."
59 (define (value->sexp value)
60 (if (self-quoting? value)
62 `(non-self-quoting ,(object-address value)
63 ,(object->string value))))
65 (define (frame->sexp frame)
66 `(,(frame-procedure-name frame)
67 ,(match (frame-source frame)
68 ((_ (? string? file) (? integer? line) . (? integer? column))
69 (list file line column))
73 (define (handle-exception key . args)
76 ((0 1 (? positive?) _ ...)
77 ;; Protocol (0 1 1) and later.
78 (let ((stack (if (repl-prompt)
79 (make-stack #t handle-exception (repl-prompt))
81 `(exception (arguments ,key ,@(map value->sexp args))
82 (stack ,@(map frame->sexp (stack->frames stack))))))
85 `(exception ,key ,@(map value->sexp args)))))
89 (force-output output))
93 (let ((results (call-with-values
97 (write `(values ,@(map value->sexp results))
100 (force-output output)))
104 (define* (machine-repl #:optional
105 (input (current-input-port))
106 (output (current-output-port)))
107 "Run a machine-usable REPL over ports INPUT and OUTPUT.
109 The protocol of this REPL is meant to be machine-readable and provides proper
110 support to represent multiple-value returns, exceptions, objects that lack a
111 read syntax, and so on. As such it is more convenient and robust than parsing
112 Guile's REPL prompt."
114 (make-prompt-tag "repl-prompt"))
116 (define (loop exp version)
120 (send-repl-response exp output
122 (loop (read input) version))))
124 (write `(repl-version 0 1 1) output)
126 (force-output output)
128 ;; In protocol version (0 0), clients would not send their supported
129 ;; protocol version. Thus, the code below checks for two case: (1) a (0 0)
130 ;; client that directly sends an expression to evaluate, and (2) a more
131 ;; recent client that sends (() repl-version ...). This form is chosen to
132 ;; be unambiguously distinguishable from a regular Scheme expression.
134 (call-with-prompt tag
136 (parameterize ((repl-prompt tag))
138 ((() 'repl-version version ...)
139 (loop (read input) version))
141 (loop exp '(0 0))))))