common.scm 9.2 KB

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