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 ;; Note: 'make-stack' returns #f if there's no 'handle-exception'
82 ;; stack frame, which is the case when this file is being
83 ;; interpreted as with 'primitive-load'.
84 `(exception (arguments ,key ,@(map value->sexp args))
85 (stack ,@(map frame->sexp
91 `(exception ,key ,@(map value->sexp args)))))
95 (force-output output))
99 (let ((results (call-with-values
101 (primitive-eval exp))
103 (write `(values ,@(map value->sexp results))
106 (force-output output)))
110 (define* (machine-repl #:optional
111 (input (current-input-port))
112 (output (current-output-port)))
113 "Run a machine-usable REPL over ports INPUT and OUTPUT.
115 The protocol of this REPL is meant to be machine-readable and provides proper
116 support to represent multiple-value returns, exceptions, objects that lack a
117 read syntax, and so on. As such it is more convenient and robust than parsing
118 Guile's REPL prompt."
120 (make-prompt-tag "repl-prompt"))
122 (define (loop exp version)
126 (send-repl-response exp output
128 (loop (read input) version))))
130 (write `(repl-version 0 1 1) output)
132 (force-output output)
134 ;; In protocol version (0 0), clients would not send their supported
135 ;; protocol version. Thus, the code below checks for two case: (1) a (0 0)
136 ;; client that directly sends an expression to evaluate, and (2) a more
137 ;; recent client that sends (() repl-version ...). This form is chosen to
138 ;; be unambiguously distinguishable from a regular Scheme expression.
140 (call-with-prompt tag
142 (parameterize ((repl-prompt tag))
144 ((() 'repl-version version ...)
145 (loop (read input) version))
147 (loop exp '(0 0))))))