summaryrefslogtreecommitdiff
path: root/scheme/racket/geiser/user.rkt
blob: 37763b92554985f87c91c49413a89964e84057ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
;;; user.rkt -- global bindings visible to geiser users

;; Copyright (C) 2010 Jose Antonio Ortega Ruiz

;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the Modified BSD License. You should
;; have received a copy of the license along with this program. If
;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.

;; Start date: Wed Mar 31, 2010 22:24

#lang racket/base

(provide init-geiser-repl run-geiser-repl enter!)

(require (for-syntax racket/base)
	 geiser/main geiser/enter geiser/eval geiser/modules)

(define top-namespace (current-namespace))

(define (enter! mod stx)
  (cond ((not mod) (current-namespace top-namespace))
        ((module-path? mod)
         (enter-module mod)
         (current-namespace (module->namespace mod)))
        (else (raise-syntax-error
               #f
               "not a valid module path, and not #f"
               stx
               mod))))

(define orig-loader (current-load/use-compiled))
(define geiser-loader (module-loader orig-loader))

(define geiser-send-null (make-parameter #f))

(define (geiser-eval)
  (define geiser-main (module->namespace 'geiser/main))
  (geiser-send-null #t)
  (let* ([mod (read)]
         [lang (read)]
         [form (read)])
    (datum->syntax #f
                   (list 'quote
                         (cond [(equal? form '(unquote apply))
                                (let* ([proc (eval (read) geiser-main)]
                                       [args (read)])
                                  ((geiser:eval lang) `(,proc ,@args) mod))]
                               [else ((geiser:eval lang) form mod)])))))

(define (geiser-read)
  (if (geiser-send-null)
      (begin (geiser-send-null #f)
	     (write-char #\nul))
      (printf "racket@~a> " (namespace->module-name (current-namespace))))
  (flush-output)
  (let* ([in (current-input-port)]
	 [form ((current-read-interaction) (object-name in) in)])
    (syntax-case form ()
      [(uq cmd) (eq? 'unquote (syntax-e #'uq))
       (case (syntax-e #'cmd)
         ((enter) (enter! (read) #'cmd))
         ((geiser-eval) (geiser-eval))
         ((geiser-no-values) (datum->syntax #f (void)))
         (else form))]
      [_ form])))

(define geiser-prompt-read (make-repl-reader geiser-read))

(define (init-geiser-repl)
  (compile-enforce-module-constants #f)
  (current-load/use-compiled geiser-loader)
  (current-prompt-read geiser-prompt-read))

(define (run-geiser-repl in out enforce-module-constants)
  (parameterize [(compile-enforce-module-constants enforce-module-constants)
                 (current-input-port in)
                 (current-output-port out)
                 (current-error-port out)
                 (current-load/use-compiled geiser-loader)
                 (current-prompt-read geiser-prompt-read)]
    (read-eval-print-loop)))