avoid running the debugger during parsing or compilation at the repl
[bpt/guile.git] / module / system / repl / common.scm
1 ;;; Repl common routines
2
3 ;; Copyright (C) 2001, 2008, 2009, 2010 Free Software Foundation, Inc.
4
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.
9 ;;;
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.
14 ;;;
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
18
19 ;;; Code:
20
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*))
35
36 (define *version*
37 (format #f "GNU Guile ~A
38 Copyright (C) 1995-2010 Free Software Foundation, Inc.
39
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)))
43
44 (define *copying*
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.
49
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.
54
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>.")
58
59 (define *warranty*
60 "Guile is distributed WITHOUT ANY WARRANTY. The following
61 sections from the GNU General Public License, version 3, should
62 make that clear.
63
64 15. Disclaimer of Warranty.
65
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.
74
75 16. Limitation of Liability.
76
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
85 SUCH DAMAGES.
86
87 17. Interpretation of Sections 15 and 16.
88
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.
95
96 See <http://www.gnu.org/licenses/lgpl.html>, for more details.")
97
98 \f
99 ;;;
100 ;;; Repl type
101 ;;;
102
103 (define-record/keywords <repl>
104 language options tm-stats gc-stats inport outport debug)
105
106 (define repl-default-options
107 '((compile-options . (#:warnings (unbound-variable arity-mismatch)))
108 (trace . #f)
109 (interp . #f)))
110
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
115 #:tm-stats (times)
116 #:gc-stats (gc-stats)
117 #:inport (current-input-port)
118 #:outport (current-output-port)
119 #:debug debug))
120
121 (define (repl-welcome repl)
122 (display *version*)
123 (newline)
124 (newline)
125 (display "Enter `,help' for help.\n"))
126
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
139 (define (repl-read repl)
140 ((language-reader (repl-language repl)) (repl-inport repl)
141 (current-module)))
142
143 (define (repl-compile-options repl)
144 (repl-option-ref repl 'compile-options))
145
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))))
151
152 (define (repl-parse repl form)
153 (let ((parser (language-parser (repl-language repl))))
154 (if parser (parser form) form)))
155
156 (define (repl-prepare-eval-thunk repl form)
157 (let* ((eval (language-evaluator (repl-language repl))))
158 (if (and eval
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)))))
163
164 (define (repl-eval repl form)
165 (let ((thunk (repl-prepare-eval-thunk repl form)))
166 (% (thunk))))
167
168 (define (repl-print repl val)
169 (if (not (eq? val *unspecified*))
170 (begin
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)))))
178
179 (define (repl-option-ref repl key)
180 (assq-ref (repl-options repl) key))
181
182 (define (repl-option-set! repl key val)
183 (set! (repl-options repl) (assq-set! (repl-options repl) key val)))
184
185 (define (repl-default-option-set! key val)
186 (set! repl-default-options (assq-set! repl-default-options key val)))
187
188 (define (repl-default-prompt-set! prompt)
189 (repl-default-option-set!
190 'prompt
191 (cond
192 ((string? prompt) (lambda (repl) prompt))
193 ((thunk? prompt) (lambda (repl) (prompt)))
194 ((procedure? prompt) prompt)
195 (else (error "Invalid prompt" prompt)))))
196
197 \f
198 ;;;
199 ;;; Utilities
200 ;;;
201
202 (define (puts x) (display x) (newline))
203
204 (define (->string x)
205 (object->string x display))
206
207 (define (user-error msg . args)
208 (throw 'user-error #f msg args #f))