common.scm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. ;;; Repl common routines
  2. ;; Copyright (C) 2001, 2008-2016, 2019-2021 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 02110-1301 USA
  16. ;;; Code:
  17. (define-module (system repl common)
  18. #:use-module (system base syntax)
  19. #:use-module (system base compile)
  20. #:use-module (system base language)
  21. #:use-module (system base message)
  22. #:use-module (system vm program)
  23. #:use-module (system vm loader)
  24. #:use-module (ice-9 control)
  25. #:use-module (ice-9 copy-tree)
  26. #:use-module (ice-9 history)
  27. #:export (<repl> make-repl repl-language repl-options
  28. repl-tm-stats repl-gc-stats repl-debug
  29. repl-welcome repl-prompt
  30. repl-read repl-compile repl-prepare-eval-thunk repl-eval
  31. repl-expand repl-optimize
  32. repl-parse repl-print repl-option-ref repl-option-set!
  33. repl-default-option-set! repl-default-prompt-set!
  34. puts ->string user-error
  35. *warranty* *copying* *version*))
  36. (define *version*
  37. (format #f "GNU Guile ~A
  38. Copyright (C) 1995-2021 Free Software Foundation, Inc.
  39. Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
  40. This program is free software, and you are welcome to redistribute it
  41. under certain conditions; type `,show c' for details." (version)))
  42. (define *copying*
  43. "Guile is free software: you can redistribute it and/or modify
  44. it under the terms of the GNU Lesser General Public License as
  45. published by the Free Software Foundation, either version 3 of
  46. the License, or (at your option) any later version.
  47. Guile is distributed in the hope that it will be useful, but
  48. WITHOUT ANY WARRANTY; without even the implied warranty of
  49. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  50. Lesser General Public License for more details.
  51. You should have received a copy of the GNU Lesser General Public
  52. License along with this program. If not, see
  53. <http://www.gnu.org/licenses/lgpl.html>.")
  54. (define *warranty*
  55. "Guile is distributed WITHOUT ANY WARRANTY. The following
  56. sections from the GNU General Public License, version 3, should
  57. make that clear.
  58. 15. Disclaimer of Warranty.
  59. THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
  60. APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
  61. HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY
  62. OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
  63. THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  64. PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
  65. IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
  66. ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
  67. 16. Limitation of Liability.
  68. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
  69. WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
  70. THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
  71. GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
  72. USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
  73. DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
  74. PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
  75. EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
  76. SUCH DAMAGES.
  77. 17. Interpretation of Sections 15 and 16.
  78. If the disclaimer of warranty and limitation of liability provided
  79. above cannot be given local legal effect according to their terms,
  80. reviewing courts shall apply local law that most closely approximates
  81. an absolute waiver of all civil liability in connection with the
  82. Program, unless a warranty or assumption of liability accompanies a
  83. copy of the Program in return for a fee.
  84. See <http://www.gnu.org/licenses/lgpl.html>, for more details.")
  85. ;;;
  86. ;;; Repl type
  87. ;;;
  88. (define-record/keywords <repl>
  89. language options tm-stats gc-stats debug)
  90. (define repl-default-options
  91. (copy-tree
  92. `((compile-options ,%auto-compilation-options #f)
  93. (optimization-level #f (lambda (x)
  94. (unless (and (exact-integer? x) (<= 0 x 9))
  95. (error "Invalid optimization level" x))
  96. x))
  97. (warning-level #f (lambda (x)
  98. (unless (and (exact-integer? x) (<= 0 x 9))
  99. (error "Invalid warning level" x))
  100. x))
  101. (trace #f #f)
  102. (interp #f #f)
  103. (prompt #f ,(lambda (prompt)
  104. (cond
  105. ((not prompt) #f)
  106. ((string? prompt) (lambda (repl) prompt))
  107. ((thunk? prompt) (lambda (repl) (prompt)))
  108. ((procedure? prompt) prompt)
  109. (else (error "Invalid prompt" prompt)))))
  110. (print #f ,(lambda (print)
  111. (cond
  112. ((not print) #f)
  113. ((procedure? print) print)
  114. (else (error "Invalid print procedure" print)))))
  115. (value-history
  116. ,(value-history-enabled?)
  117. ,(lambda (x)
  118. (if x (enable-value-history!) (disable-value-history!))
  119. (->bool x)))
  120. (on-error
  121. debug
  122. ,(let ((vals '(debug backtrace report pass)))
  123. (lambda (x)
  124. (if (memq x vals)
  125. x
  126. (error "Bad on-error value ~a; expected one of ~a" x vals))))))))
  127. (define %make-repl make-repl)
  128. (define* (make-repl lang #:optional debug)
  129. (%make-repl #:language (if (language? lang)
  130. lang
  131. (lookup-language lang))
  132. #:options (copy-tree repl-default-options)
  133. #:tm-stats (times)
  134. #:gc-stats (gc-stats)
  135. #:debug debug))
  136. (define (repl-welcome repl)
  137. (display *version*)
  138. (newline)
  139. (newline)
  140. (display "Enter `,help' for help.\n"))
  141. (define (repl-prompt repl)
  142. (cond
  143. ((repl-option-ref repl 'prompt)
  144. => (lambda (prompt) (prompt repl)))
  145. (else
  146. (format #f "~A@~A~A> " (language-name (repl-language repl))
  147. (module-name (current-module))
  148. (let ((level (length (cond
  149. ((fluid-ref *repl-stack*) => cdr)
  150. (else '())))))
  151. (if (zero? level) "" (format #f " [~a]" level)))))))
  152. (define (repl-read repl)
  153. (let ((reader (language-reader (repl-language repl))))
  154. (reader (current-input-port) (current-module))))
  155. (define (repl-compile-options repl)
  156. (repl-option-ref repl 'compile-options))
  157. (define (repl-optimization-level repl)
  158. (or (repl-option-ref repl 'optimization-level)
  159. (default-optimization-level)))
  160. (define (repl-warning-level repl)
  161. (or (repl-option-ref repl 'warning-level)
  162. (default-warning-level)))
  163. (define (repl-compile repl form)
  164. (let ((from (repl-language repl))
  165. (opts (repl-compile-options repl)))
  166. (compile form #:from from #:to 'bytecode #:opts opts
  167. #:optimization-level (repl-optimization-level repl)
  168. #:warning-level (repl-warning-level repl)
  169. #:env (current-module))))
  170. (define* (repl-expand repl form #:key (lang 'tree-il))
  171. (let ((from (repl-language repl))
  172. (opts (repl-compile-options repl)))
  173. (decompile (compile form #:from from #:to lang #:opts opts
  174. #:env (current-module))
  175. #:from lang #:to from)))
  176. (define* (repl-optimize repl form #:key (lang 'tree-il))
  177. (let ((from (repl-language repl))
  178. (make-lower (language-lowerer (lookup-language lang)))
  179. (optimization-level (repl-optimization-level repl))
  180. (warning-level (repl-warning-level repl))
  181. (opts (repl-compile-options repl)))
  182. (unless make-lower
  183. (error "language has no optimizer" lang))
  184. (decompile ((make-lower optimization-level opts)
  185. (compile form #:from from #:to lang #:opts opts
  186. #:optimization-level optimization-level
  187. #:warning-level warning-level
  188. #:env (current-module))
  189. (current-module))
  190. #:from lang #:to from)))
  191. (define (repl-parse repl form)
  192. (let ((parser (language-parser (repl-language repl))))
  193. (if parser (parser form) form)))
  194. (define (repl-prepare-eval-thunk repl form)
  195. (let* ((eval (language-evaluator (repl-language repl))))
  196. (if (and eval
  197. (or (null? (language-compilers (repl-language repl)))
  198. (repl-option-ref repl 'interp)))
  199. (lambda () (eval form (current-module)))
  200. (load-thunk-from-memory (repl-compile repl form)))))
  201. (define (repl-eval repl form)
  202. (let ((thunk (repl-prepare-eval-thunk repl form)))
  203. (% (thunk))))
  204. (define (repl-print repl val)
  205. (if (not (eq? val *unspecified*))
  206. (begin
  207. (run-hook before-print-hook val)
  208. (cond
  209. ((repl-option-ref repl 'print)
  210. => (lambda (print) (print repl val)))
  211. (else
  212. ;; The result of an evaluation is representable in scheme, and
  213. ;; should be printed with the generic printer, `write'. The
  214. ;; language-printer is something else: it prints expressions of
  215. ;; a given language, not the result of evaluation.
  216. (write val)
  217. (newline))))))
  218. (define (repl-option-ref repl key)
  219. (cadr (or (assq key (repl-options repl))
  220. (error "unknown repl option" key))))
  221. (define (repl-option-set! repl key val)
  222. (let ((spec (or (assq key (repl-options repl))
  223. (error "unknown repl option" key))))
  224. (set-car! (cdr spec)
  225. (if (procedure? (caddr spec))
  226. ((caddr spec) val)
  227. val))))
  228. (define (repl-default-option-set! key val)
  229. (let ((spec (or (assq key repl-default-options)
  230. (error "unknown repl option" key))))
  231. (set-car! (cdr spec)
  232. (if (procedure? (caddr spec))
  233. ((caddr spec) val)
  234. val))))
  235. (define (repl-default-prompt-set! prompt)
  236. (repl-default-option-set! 'prompt prompt))
  237. ;;;
  238. ;;; Utilities
  239. ;;;
  240. (define (puts x) (display x) (newline))
  241. (define (->string x)
  242. (object->string x display))
  243. (define (user-error msg . args)
  244. (throw 'user-error #f msg args #f))