repl.scm 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. (run-repl (make-repl lang debug)))
  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. (define (with-stack-and-prompt thunk)
  126. (call-with-prompt (default-prompt-tag)
  127. (lambda () (start-stack #t (thunk)))
  128. (lambda (k proc)
  129. (with-stack-and-prompt (lambda () (proc k))))))
  130. (% (with-fluids ((*repl-stack*
  131. (cons repl (or (fluid-ref *repl-stack*) '()))))
  132. (if (null? (cdr (fluid-ref *repl-stack*)))
  133. (repl-welcome repl))
  134. (let prompt-loop ()
  135. (let ((exp (prompting-meta-read repl)))
  136. (cond
  137. ((eqv? exp *unspecified*)) ; read error or comment, pass
  138. ((eq? exp meta-command-token)
  139. (catch #t
  140. (lambda ()
  141. (meta-command repl))
  142. (lambda (k . args)
  143. (if (eq? k 'quit)
  144. (abort args)
  145. (begin
  146. (format #t "While executing meta-command:~%")
  147. (print-exception (current-output-port) #f k args))))))
  148. ((eof-object? exp)
  149. (newline)
  150. (abort '()))
  151. (else
  152. ;; since the input port is line-buffered, consume up to the
  153. ;; newline
  154. (flush-to-newline)
  155. (call-with-error-handling
  156. (lambda ()
  157. (catch 'quit
  158. (lambda ()
  159. (call-with-values
  160. (lambda ()
  161. (% (let ((thunk
  162. (abort-on-error "compiling expression"
  163. (repl-prepare-eval-thunk
  164. repl
  165. (abort-on-error "parsing expression"
  166. (repl-parse repl exp))))))
  167. (run-hook before-eval-hook exp)
  168. (call-with-error-handling
  169. (lambda ()
  170. (with-stack-and-prompt thunk))
  171. #:on-error (repl-option-ref repl 'on-error)))
  172. (lambda (k) (values))))
  173. (lambda l
  174. (for-each (lambda (v)
  175. (repl-print repl v))
  176. l))))
  177. (lambda (k . args)
  178. (abort args))))
  179. #:on-error (repl-option-ref repl 'on-error)
  180. #:trap-handler 'disabled)))
  181. (flush-to-newline) ;; consume trailing whitespace
  182. (prompt-loop))))
  183. (lambda (k status)
  184. status)))
  185. ;; Returns first non-whitespace char.
  186. (define (flush-leading-whitespace)
  187. (let ((ch (peek-char)))
  188. (cond ((eof-object? ch) ch)
  189. ((char-whitespace? ch) (read-char) (flush-leading-whitespace))
  190. (else ch))))
  191. (define (flush-to-newline)
  192. (if (char-ready?)
  193. (let ((ch (peek-char)))
  194. (if (and (not (eof-object? ch)) (char-whitespace? ch))
  195. (begin
  196. (read-char)
  197. (if (not (char=? ch #\newline))
  198. (flush-to-newline)))))))