re-enable computed goto; fix ,help in the repl; subr dispatch optimizations
[bpt/guile.git] / module / system / repl / common.scm
1 ;;; Repl common routines
2
3 ;; Copyright (C) 2001 Free Software Foundation, Inc.
4
5 ;; This program is free software; you can redistribute it and/or modify
6 ;; it under the terms of the GNU General Public License as published by
7 ;; the Free Software Foundation; either version 2, or (at your option)
8 ;; any later version.
9 ;;
10 ;; This program 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
13 ;; GNU General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU General Public License
16 ;; along with this program; see the file COPYING. If not, write to
17 ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 ;; Boston, MA 02111-1307, USA.
19
20 ;;; Code:
21
22 (define-module (system repl common)
23 :use-syntax (system base syntax)
24 :use-module (system base compile)
25 :use-module (system base language)
26 :use-module (system vm core)
27 :export (<repl> make-repl repl-vm repl-language repl-options
28 repl-tm-stats repl-gc-stats repl-vm-stats
29 repl-welcome repl-prompt repl-read repl-compile repl-eval
30 repl-print repl-option-ref repl-option-set!
31 puts ->string user-error))
32
33 \f
34 ;;;
35 ;;; Repl type
36 ;;;
37
38 (define-record (<repl> vm language options tm-stats gc-stats vm-stats))
39
40 (define repl-default-options
41 '((trace . #f)))
42
43 (define %make-repl make-repl)
44 (define (make-repl lang)
45 (%make-repl :vm (the-vm)
46 :language (lookup-language lang)
47 :options repl-default-options
48 :tm-stats (times)
49 :gc-stats (gc-stats)
50 :vm-stats (vm-stats (the-vm))))
51
52 (define (repl-welcome repl)
53 (let ((language (repl-language repl)))
54 (format #t "~A interpreter ~A on Guile ~A\n"
55 (language-title language) (language-version language) (version)))
56 (display "Copyright (C) 2001-2008 Free Software Foundation, Inc.\n\n")
57 (display "Enter `,help' for help.\n"))
58
59 (define (repl-prompt repl)
60 (format #f "~A@~A> " (language-name (repl-language repl))
61 (module-name (current-module))))
62
63 (define (repl-read repl)
64 ((language-reader (repl-language repl))))
65
66 (define (repl-compile repl form . opts)
67 (apply compile-in form (current-module) (repl-language repl) opts))
68
69 (define (repl-eval repl form)
70 (let ((eval (language-evaluator (repl-language repl))))
71 (if eval
72 (eval form (current-module))
73 (vm-load (repl-vm repl) (repl-compile repl form)))))
74
75 (define (repl-print repl val)
76 (if (not (eq? val *unspecified*))
77 (begin
78 ((language-printer (repl-language repl)) val)
79 (newline))))
80
81 (define (repl-option-ref repl key)
82 (assq-ref (repl-options repl) key))
83
84 (define (repl-option-set! repl key val)
85 (set! (repl-options repl) (assq-set! (repl-options repl) key val)))
86
87 \f
88 ;;;
89 ;;; Utilities
90 ;;;
91
92 (define (puts x) (display x) (newline))
93
94 (define (->string x)
95 (object->string x display))
96
97 (define (user-error msg . args)
98 (throw 'user-error #f msg args #f))