repl.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. ;;; Read-Eval-Print Loop
  2. ;; Copyright (C) 2001, 2009, 2010, 2011, 2013,
  3. ;; 2014 Free Software Foundation, Inc.
  4. ;; This library is free software; you can redistribute it and/or
  5. ;; modify it under the terms of the GNU Lesser General Public
  6. ;; License as published by the Free Software Foundation; either
  7. ;; version 3 of the License, or (at your option) any later version.
  8. ;;
  9. ;; This library is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;; Lesser General Public License for more details.
  13. ;;
  14. ;; You should have received a copy of the GNU Lesser General Public
  15. ;; License along with this library; if not, write to the Free Software
  16. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. ;; 02110-1301 USA
  18. ;;; Code:
  19. (define-module (system repl repl)
  20. #:use-module (system base language)
  21. #:use-module (system repl error-handling)
  22. #:use-module (system repl common)
  23. #:use-module (system repl command)
  24. #:use-module (ice-9 control)
  25. #:export (start-repl run-repl %inhibit-welcome-message))
  26. ;;;
  27. ;;; Comments
  28. ;;;
  29. ;;; (You don't want a comment to force a continuation line.)
  30. ;;;
  31. (define (read-scheme-line-comment port)
  32. (let lp ()
  33. (let ((ch (read-char port)))
  34. (or (eof-object? ch)
  35. (eqv? ch #\newline)
  36. (lp)))))
  37. (define (read-scheme-datum-comment port)
  38. (read port))
  39. ;; ch is a peeked char
  40. (define (read-comment lang port ch)
  41. (and (eq? (language-name lang) 'scheme)
  42. (case ch
  43. ((#\;)
  44. (read-char port)
  45. (read-scheme-line-comment port)
  46. #t)
  47. ((#\#)
  48. (read-char port)
  49. (case (peek-char port)
  50. ((#\;)
  51. (read-char port)
  52. (read-scheme-datum-comment port)
  53. #t)
  54. ;; Not doing R6RS block comments because of the possibility
  55. ;; of read-hash extensions. Lame excuse. Not doing scsh
  56. ;; block comments either, because I don't feel like handling
  57. ;; #!r6rs.
  58. (else
  59. (unread-char #\# port)
  60. #f)))
  61. (else
  62. #f))))
  63. ;;;
  64. ;;; Meta commands
  65. ;;;
  66. (define meta-command-token (cons 'meta 'command))
  67. (define (meta-reader lang env)
  68. (lambda* (#:optional (port (current-input-port)))
  69. (with-input-from-port port
  70. (lambda ()
  71. (let ((ch (flush-leading-whitespace)))
  72. (cond ((eof-object? ch)
  73. (read-char)) ; consume the EOF and return it
  74. ((eqv? ch #\,)
  75. (read-char)
  76. meta-command-token)
  77. ((read-comment lang port ch)
  78. *unspecified*)
  79. (else ((language-reader lang) port env))))))))
  80. ;; repl-reader is a function defined in boot-9.scm, and is replaced by
  81. ;; something else if readline has been activated. much of this hoopla is
  82. ;; to be able to re-use the existing readline machinery.
  83. ;;
  84. ;; Catches read errors, returning *unspecified* in that case.
  85. ;;
  86. ;; Note: although not exported, this is used by (system repl coop-server)
  87. (define (prompting-meta-read repl)
  88. (catch #t
  89. (lambda ()
  90. (repl-reader (lambda () (repl-prompt repl))
  91. (meta-reader (repl-language repl) (current-module))))
  92. (lambda (key . args)
  93. (case key
  94. ((quit)
  95. (apply throw key args))
  96. (else
  97. (format (current-output-port) "While reading expression:\n")
  98. (print-exception (current-output-port) #f key args)
  99. (flush-all-input)
  100. *unspecified*)))))
  101. ;;;
  102. ;;; The repl
  103. ;;;
  104. ;; Provide a hook for users to inhibit the welcome message.
  105. ;; For example, .guile might include
  106. ;; ((@ (system repl repl) %inhibit-welcome-message) #f)
  107. (define %inhibit-welcome-message (make-parameter #f))
  108. (define* (start-repl #:optional (lang (current-language)) #:key debug)
  109. (start-repl* lang debug prompting-meta-read))
  110. ;; Note: although not exported, this is used by (system repl coop-server)
  111. (define (start-repl* lang debug prompting-meta-read)
  112. ;; ,language at the REPL will update the current-language. Make
  113. ;; sure that it does so in a new dynamic scope.
  114. (parameterize ((current-language lang))
  115. (run-repl* (make-repl lang debug) prompting-meta-read)))
  116. ;; (put 'abort-on-error 'scheme-indent-function 1)
  117. (define-syntax-rule (abort-on-error string exp)
  118. (catch #t
  119. (lambda () exp)
  120. (lambda (key . args)
  121. (format #t "While ~A:~%" string)
  122. (print-exception (current-output-port) #f key args)
  123. (abort))))
  124. (define (run-repl repl)
  125. (run-repl* repl prompting-meta-read))
  126. (define (run-repl* repl prompting-meta-read)
  127. (define (with-stack-and-prompt thunk)
  128. (call-with-prompt (default-prompt-tag)
  129. (lambda () (start-stack #t (thunk)))
  130. (lambda (k proc)
  131. (with-stack-and-prompt (lambda () (proc k))))))
  132. (% (with-fluids ((*repl-stack*
  133. (cons repl (or (fluid-ref *repl-stack*) '()))))
  134. (if (and (null? (cdr (fluid-ref *repl-stack*)))
  135. (not (%inhibit-welcome-message)))
  136. (repl-welcome repl))
  137. (let prompt-loop ()
  138. (let ((exp (prompting-meta-read repl)))
  139. (cond
  140. ((eqv? exp *unspecified*)) ; read error or comment, pass
  141. ((eq? exp meta-command-token)
  142. (catch #t
  143. (lambda ()
  144. (meta-command repl))
  145. (lambda (k . args)
  146. (if (eq? k 'quit)
  147. (abort args)
  148. (begin
  149. (format #t "While executing meta-command:~%")
  150. (print-exception (current-output-port) #f k args))))))
  151. ((eof-object? exp)
  152. (newline)
  153. (abort '()))
  154. (else
  155. ;; since the input port is line-buffered, consume up to the
  156. ;; newline
  157. (flush-to-newline)
  158. (call-with-error-handling
  159. (lambda ()
  160. (catch 'quit
  161. (lambda ()
  162. (call-with-values
  163. (lambda ()
  164. (% (let ((thunk
  165. (abort-on-error "compiling expression"
  166. (repl-prepare-eval-thunk
  167. repl
  168. (abort-on-error "parsing expression"
  169. (repl-parse repl exp))))))
  170. (run-hook before-eval-hook exp)
  171. (call-with-error-handling
  172. (lambda ()
  173. (with-stack-and-prompt thunk))
  174. #:on-error (repl-option-ref repl 'on-error)))
  175. (lambda (k) (values))))
  176. (lambda l
  177. (for-each (lambda (v)
  178. (repl-print repl v))
  179. l))))
  180. (lambda (k . args)
  181. (abort args))))
  182. #:on-error (repl-option-ref repl 'on-error)
  183. #:trap-handler 'disabled)))
  184. (flush-to-newline) ;; consume trailing whitespace
  185. (prompt-loop))))
  186. (lambda (k status)
  187. status)))
  188. ;; Returns first non-whitespace char.
  189. (define (flush-leading-whitespace)
  190. (let ((ch (peek-char)))
  191. (cond ((eof-object? ch) ch)
  192. ((char-whitespace? ch) (read-char) (flush-leading-whitespace))
  193. (else ch))))
  194. (define (flush-to-newline)
  195. (if (char-ready?)
  196. (let ((ch (peek-char)))
  197. (if (and (not (eof-object? ch)) (char-whitespace? ch))
  198. (begin
  199. (read-char)
  200. (if (not (char=? ch #\newline))
  201. (flush-to-newline)))))))