repl.scm 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 syntax)
  21. #:use-module (system base pmatch)
  22. #:use-module (system base compile)
  23. #:use-module (system base language)
  24. #:use-module (system vm vm)
  25. #:use-module (system repl error-handling)
  26. #:use-module (system repl common)
  27. #:use-module (system repl command)
  28. #:use-module (ice-9 control)
  29. #:export (start-repl run-repl))
  30. ;;;
  31. ;;; Comments
  32. ;;;
  33. ;;; (You don't want a comment to force a continuation line.)
  34. ;;;
  35. (define (read-scheme-line-comment port)
  36. (let lp ()
  37. (let ((ch (read-char port)))
  38. (or (eof-object? ch)
  39. (eqv? ch #\newline)
  40. (lp)))))
  41. (define (read-scheme-datum-comment port)
  42. (read port))
  43. ;; ch is a peeked char
  44. (define (read-comment lang port ch)
  45. (and (eq? (language-name lang) 'scheme)
  46. (case ch
  47. ((#\;)
  48. (read-char port)
  49. (read-scheme-line-comment port)
  50. #t)
  51. ((#\#)
  52. (read-char port)
  53. (case (peek-char port)
  54. ((#\;)
  55. (read-char port)
  56. (read-scheme-datum-comment port)
  57. #t)
  58. ;; Not doing R6RS block comments because of the possibility
  59. ;; of read-hash extensions. Lame excuse. Not doing scsh
  60. ;; block comments either, because I don't feel like handling
  61. ;; #!r6rs.
  62. (else
  63. (unread-char #\# port)
  64. #f)))
  65. (else
  66. #f))))
  67. ;;;
  68. ;;; Meta commands
  69. ;;;
  70. (define meta-command-token (cons 'meta 'command))
  71. (define (meta-reader lang env)
  72. (lambda* (#:optional (port (current-input-port)))
  73. (with-input-from-port port
  74. (lambda ()
  75. (let ((ch (flush-leading-whitespace)))
  76. (cond ((eof-object? ch)
  77. (read-char)) ; consume the EOF and return it
  78. ((eqv? ch #\,)
  79. (read-char)
  80. meta-command-token)
  81. ((read-comment lang port ch)
  82. *unspecified*)
  83. (else ((language-reader lang) port env))))))))
  84. (define (flush-all-input)
  85. (if (and (char-ready?)
  86. (not (eof-object? (peek-char))))
  87. (begin
  88. (read-char)
  89. (flush-all-input))))
  90. ;; repl-reader is a function defined in boot-9.scm, and is replaced by
  91. ;; something else if readline has been activated. much of this hoopla is
  92. ;; to be able to re-use the existing readline machinery.
  93. ;;
  94. ;; Catches read errors, returning *unspecified* in that case.
  95. ;;
  96. ;; Note: although not exported, this is used by (system repl coop-server)
  97. (define (prompting-meta-read repl)
  98. (catch #t
  99. (lambda ()
  100. (repl-reader (lambda () (repl-prompt repl))
  101. (meta-reader (repl-language repl) (current-module))))
  102. (lambda (key . args)
  103. (case key
  104. ((quit)
  105. (apply throw key args))
  106. (else
  107. (format (current-output-port) "While reading expression:\n")
  108. (print-exception (current-output-port) #f key args)
  109. (flush-all-input)
  110. *unspecified*)))))
  111. ;;;
  112. ;;; The repl
  113. ;;;
  114. (define* (start-repl #:optional (lang (current-language)) #:key debug)
  115. (start-repl* lang debug prompting-meta-read))
  116. ;; Note: although not exported, this is used by (system repl coop-server)
  117. (define (start-repl* lang debug prompting-meta-read)
  118. ;; ,language at the REPL will update the current-language. Make
  119. ;; sure that it does so in a new dynamic scope.
  120. (parameterize ((current-language lang))
  121. (run-repl* (make-repl lang debug) prompting-meta-read)))
  122. ;; (put 'abort-on-error 'scheme-indent-function 1)
  123. (define-syntax-rule (abort-on-error string exp)
  124. (catch #t
  125. (lambda () exp)
  126. (lambda (key . args)
  127. (format #t "While ~A:~%" string)
  128. (print-exception (current-output-port) #f key args)
  129. (abort))))
  130. (define (run-repl repl)
  131. (run-repl* repl prompting-meta-read))
  132. (define (run-repl* repl prompting-meta-read)
  133. (define (with-stack-and-prompt thunk)
  134. (call-with-prompt (default-prompt-tag)
  135. (lambda () (start-stack #t (thunk)))
  136. (lambda (k proc)
  137. (with-stack-and-prompt (lambda () (proc k))))))
  138. (% (with-fluids ((*repl-stack*
  139. (cons repl (or (fluid-ref *repl-stack*) '()))))
  140. (if (null? (cdr (fluid-ref *repl-stack*)))
  141. (repl-welcome repl))
  142. (let prompt-loop ()
  143. (let ((exp (prompting-meta-read repl)))
  144. (cond
  145. ((eqv? exp *unspecified*)) ; read error or comment, pass
  146. ((eq? exp meta-command-token)
  147. (catch #t
  148. (lambda ()
  149. (meta-command repl))
  150. (lambda (k . args)
  151. (if (eq? k 'quit)
  152. (abort args)
  153. (begin
  154. (format #t "While executing meta-command:~%")
  155. (print-exception (current-output-port) #f k args))))))
  156. ((eof-object? exp)
  157. (newline)
  158. (abort '()))
  159. (else
  160. ;; since the input port is line-buffered, consume up to the
  161. ;; newline
  162. (flush-to-newline)
  163. (call-with-error-handling
  164. (lambda ()
  165. (catch 'quit
  166. (lambda ()
  167. (call-with-values
  168. (lambda ()
  169. (% (let ((thunk
  170. (abort-on-error "compiling expression"
  171. (repl-prepare-eval-thunk
  172. repl
  173. (abort-on-error "parsing expression"
  174. (repl-parse repl exp))))))
  175. (run-hook before-eval-hook exp)
  176. (call-with-error-handling
  177. (lambda ()
  178. (with-stack-and-prompt thunk))
  179. #:on-error (repl-option-ref repl 'on-error)))
  180. (lambda (k) (values))))
  181. (lambda l
  182. (for-each (lambda (v)
  183. (repl-print repl v))
  184. l))))
  185. (lambda (k . args)
  186. (abort args))))
  187. #:on-error (repl-option-ref repl 'on-error)
  188. #:trap-handler 'disabled)))
  189. (flush-to-newline) ;; consume trailing whitespace
  190. (prompt-loop))))
  191. (lambda (k status)
  192. status)))
  193. ;; Returns first non-whitespace char.
  194. (define (flush-leading-whitespace)
  195. (let ((ch (peek-char)))
  196. (cond ((eof-object? ch) ch)
  197. ((char-whitespace? ch) (read-char) (flush-leading-whitespace))
  198. (else ch))))
  199. (define (flush-to-newline)
  200. (if (char-ready?)
  201. (let ((ch (peek-char)))
  202. (if (and (not (eof-object? ch)) (char-whitespace? ch))
  203. (begin
  204. (read-char)
  205. (if (not (char=? ch #\newline))
  206. (flush-to-newline)))))))