1 ;;; Repl common routines
3 ;; Copyright (C) 2001, 2008, 2009, 2010 Free Software Foundation, Inc.
5 ;;; This library is free software; you can redistribute it and/or
6 ;;; modify it under the terms of the GNU Lesser General Public
7 ;;; License as published by the Free Software Foundation; either
8 ;;; version 3 of the License, or (at your option) any later version.
10 ;;; This library is distributed in the hope that it will be useful,
11 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;;; Lesser General Public License for more details.
15 ;;; You should have received a copy of the GNU Lesser General Public
16 ;;; License along with this library; if not, write to the Free Software
17 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 (define-module (system repl common)
22 #:use-module (system base syntax)
23 #:use-module (system base compile)
24 #:use-module (system base language)
25 #:use-module (system vm program)
26 #:use-module (ice-9 control)
27 #:export (<repl> make-repl repl-language repl-options
28 repl-tm-stats repl-gc-stats repl-inport repl-outport repl-debug
29 repl-welcome repl-prompt
30 repl-read repl-compile repl-prepare-eval-thunk repl-eval
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*))
37 (format #f "GNU Guile ~A
38 Copyright (C) 1995-2010 Free Software Foundation, Inc.
40 Guile comes with ABSOLUTELY NO WARRANTY; for details type `,show w'.
41 This program is free software, and you are welcome to redistribute it
42 under certain conditions; type `,show c' for details." (version)))
45 "Guile is free software: you can redistribute it and/or modify
46 it under the terms of the GNU Lesser General Public License as
47 published by the Free Software Foundation, either version 3 of
48 the License, or (at your option) any later version.
50 Guile is distributed in the hope that it will be useful, but
51 WITHOUT ANY WARRANTY; without even the implied warranty of
52 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
53 Lesser General Public License for more details.
55 You should have received a copy of the GNU Lesser General Public
56 License along with this program. If not, see
57 <http://www.gnu.org/licenses/lgpl.html>.")
60 "Guile is distributed WITHOUT ANY WARRANTY. The following
61 sections from the GNU General Public License, version 3, should
64 15. Disclaimer of Warranty.
66 THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
67 APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
68 HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM \"AS IS\" WITHOUT WARRANTY
69 OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
70 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
71 PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
72 IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
73 ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
75 16. Limitation of Liability.
77 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
78 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
79 THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
80 GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
81 USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
82 DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
83 PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
84 EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
87 17. Interpretation of Sections 15 and 16.
89 If the disclaimer of warranty and limitation of liability provided
90 above cannot be given local legal effect according to their terms,
91 reviewing courts shall apply local law that most closely approximates
92 an absolute waiver of all civil liability in connection with the
93 Program, unless a warranty or assumption of liability accompanies a
94 copy of the Program in return for a fee.
96 See <http://www.gnu.org/licenses/lgpl.html>, for more details.")
103 (define-record/keywords <repl>
104 language options tm-stats gc-stats inport outport debug)
106 (define repl-default-options
107 '((compile-options . (#:warnings (unbound-variable arity-mismatch)))
111 (define %make-repl make-repl)
112 (define* (make-repl lang #:optional debug)
113 (%make-repl #:language (lookup-language lang)
114 #:options repl-default-options
116 #:gc-stats (gc-stats)
117 #:inport (current-input-port)
118 #:outport (current-output-port)
121 (define (repl-welcome repl)
125 (display "Enter `,help' for help.\n"))
127 (define (repl-prompt repl)
129 ((repl-option-ref repl 'prompt)
130 => (lambda (prompt) (prompt repl)))
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)
137 (if (zero? level) "" (format #f " [~a]" level)))))))
139 (define (repl-read repl)
140 ((language-reader (repl-language repl)) (repl-inport repl)
143 (define (repl-compile-options repl)
144 (repl-option-ref repl 'compile-options))
146 (define (repl-compile repl form)
147 (let ((from (repl-language repl))
148 (opts (repl-compile-options repl)))
149 (compile form #:from from #:to 'objcode #:opts opts
150 #:env (current-module))))
152 (define (repl-parse repl form)
153 (let ((parser (language-parser (repl-language repl))))
154 (if parser (parser form) form)))
156 (define (repl-prepare-eval-thunk repl form)
157 (let* ((eval (language-evaluator (repl-language repl))))
159 (or (null? (language-compilers (repl-language repl)))
160 (assq-ref (repl-options repl) 'interp)))
161 (lambda () (eval form (current-module)))
162 (make-program (repl-compile repl form)))))
164 (define (repl-eval repl form)
165 (let ((thunk (repl-prepare-eval-thunk repl form)))
168 (define (repl-print repl val)
169 (if (not (eq? val *unspecified*))
171 (run-hook before-print-hook val)
172 ;; The result of an evaluation is representable in scheme, and
173 ;; should be printed with the generic printer, `write'. The
174 ;; language-printer is something else: it prints expressions of
175 ;; a given language, not the result of evaluation.
176 (write val (repl-outport repl))
177 (newline (repl-outport repl)))))
179 (define (repl-option-ref repl key)
180 (assq-ref (repl-options repl) key))
182 (define (repl-option-set! repl key val)
183 (set! (repl-options repl) (assq-set! (repl-options repl) key val)))
185 (define (repl-default-option-set! key val)
186 (set! repl-default-options (assq-set! repl-default-options key val)))
188 (define (repl-default-prompt-set! prompt)
189 (repl-default-option-set!
192 ((string? prompt) (lambda (repl) prompt))
193 ((thunk? prompt) (lambda (repl) (prompt)))
194 ((procedure? prompt) prompt)
195 (else (error "Invalid prompt" prompt)))))
202 (define (puts x) (display x) (newline))
205 (object->string x display))
207 (define (user-error msg . args)
208 (throw 'user-error #f msg args #f))