repl.scm 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. ;;; Read-Eval-Print Loop
  2. ;; Copyright (C) 2001, 2009, 2010, 2011 Free Software Foundation, Inc.
  3. ;; This library is free software; you can redistribute it and/or
  4. ;; modify it under the terms of the GNU Lesser General Public
  5. ;; License as published by the Free Software Foundation; either
  6. ;; version 3 of the License, or (at your option) any later version.
  7. ;;
  8. ;; This library is distributed in the hope that it will be useful,
  9. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. ;; Lesser General Public License for more details.
  12. ;;
  13. ;; You should have received a copy of the GNU Lesser General Public
  14. ;; License along with this library; if not, write to the Free Software
  15. ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. ;; 02110-1301 USA
  17. ;;; Code:
  18. (define-module (system repl repl)
  19. #:use-module (system base syntax)
  20. #:use-module (system base pmatch)
  21. #:use-module (system base compile)
  22. #:use-module (system base language)
  23. #:use-module (system vm vm)
  24. #:use-module (system repl error-handling)
  25. #:use-module (system repl common)
  26. #:use-module (system repl command)
  27. #:use-module (ice-9 control)
  28. #:export (start-repl run-repl))
  29. ;;;
  30. ;;; Comments
  31. ;;;
  32. ;;; (You don't want a comment to force a continuation line.)
  33. ;;;
  34. (define (read-scheme-line-comment port)
  35. (let lp ()
  36. (let ((ch (read-char port)))
  37. (or (eof-object? ch)
  38. (eqv? ch #\newline)
  39. (lp)))))
  40. (define (read-scheme-datum-comment port)
  41. (read port))
  42. ;; ch is a peeked char
  43. (define (read-comment lang port ch)
  44. (and (eq? (language-name lang) 'scheme)
  45. (case ch
  46. ((#\;)
  47. (read-char port)
  48. (read-scheme-line-comment port)
  49. #t)
  50. ((#\#)
  51. (read-char port)
  52. (case (peek-char port)
  53. ((#\;)
  54. (read-char port)
  55. (read-scheme-datum-comment port)
  56. #t)
  57. ;; Not doing R6RS block comments because of the possibility
  58. ;; of read-hash extensions. Lame excuse. Not doing scsh
  59. ;; block comments either, because I don't feel like handling
  60. ;; #!r6rs.
  61. (else
  62. (unread-char #\# port)
  63. #f)))
  64. (else
  65. #f))))
  66. ;;;
  67. ;;; Meta commands
  68. ;;;
  69. (define meta-command-token (cons 'meta 'command))
  70. (define (meta-reader lang env)
  71. (lambda* (#:optional (port (current-input-port)))
  72. (with-input-from-port port
  73. (lambda ()
  74. (let ((ch (flush-leading-whitespace)))
  75. (cond ((eof-object? ch)
  76. ;; EOF objects are not buffered. It's quite possible
  77. ;; to peek an EOF then read something else. It's
  78. ;; strange but it's how it works.
  79. ch)
  80. ((eqv? ch #\,)
  81. (read-char port)
  82. meta-command-token)
  83. ((read-comment lang port ch)
  84. *unspecified*)
  85. (else ((language-reader lang) port env))))))))
  86. (define (flush-all-input)
  87. (if (and (char-ready?)
  88. (not (eof-object? (peek-char))))
  89. (begin
  90. (read-char)
  91. (flush-all-input))))
  92. ;; repl-reader is a function defined in boot-9.scm, and is replaced by
  93. ;; something else if readline has been activated. much of this hoopla is
  94. ;; to be able to re-use the existing readline machinery.
  95. ;;
  96. ;; Catches read errors, returning *unspecified* in that case.
  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. ;; ,language at the REPL will fluid-set! the *current-language*. Make
  116. ;; sure that it does so in a new scope.
  117. (with-fluids ((*current-language* lang))
  118. (run-repl (make-repl lang debug))))
  119. ;; (put 'abort-on-error 'scheme-indent-function 1)
  120. (define-syntax-rule (abort-on-error string exp)
  121. (catch #t
  122. (lambda () exp)
  123. (lambda (key . args)
  124. (format #t "While ~A:~%" string)
  125. (print-exception (current-output-port) #f key args)
  126. (abort))))
  127. (define (run-repl repl)
  128. (define (with-stack-and-prompt thunk)
  129. (call-with-prompt (default-prompt-tag)
  130. (lambda () (start-stack #t (thunk)))
  131. (lambda (k proc)
  132. (with-stack-and-prompt (lambda () (proc k))))))
  133. (% (with-fluids ((*repl-stack*
  134. (cons repl (or (fluid-ref *repl-stack*) '()))))
  135. (if (null? (cdr (fluid-ref *repl-stack*)))
  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)))))))