Make do with a single connection
[geiser.git] / scheme / racket / geiser / eval.rkt
blobc406aae295e111b9b3b1f6d1ef1e6108df851a1c
1 ;;; eval.rkt -- evaluation
3 ;; Copyright (C) 2009, 2010 Jose Antonio Ortega Ruiz
5 ;; This program is free software; you can redistribute it and/or
6 ;; modify it under the terms of the Modified BSD License. You should
7 ;; have received a copy of the license along with this program. If
8 ;; not, see <http://www.xfree86.org/3.3.6/COPYRIGHT2.html#5>.
10 ;; Start date: Sun Apr 26, 2009 00:44
12 #lang racket
14 (provide eval-in
15          compile-in
16          load-file
17          compile-file
18          macroexpand
19          make-repl-reader)
22 (require geiser/enter geiser/modules geiser/autodoc)
23 (require errortrace/errortrace-lib)
25 (define last-result (void))
27 (define last-namespace (make-parameter (current-namespace)))
29 (define (exn-key e)
30   (vector-ref (struct->vector e) 0))
32 (define (set-last-error e)
33   (set! last-result `((error (key . ,(exn-key e)))))
34   (display (exn-message e))
35   (newline) (newline)
36   (parameterize ([error-context-display-depth 10])
37     (print-error-trace (current-output-port) e)))
39 (define (write-value v)
40   (with-output-to-string
41     (lambda () (write v))))
43 (define (set-last-result . vs)
44   (set! last-result `((result  ,@(map write-value vs)))))
46 (define (call-with-result thunk)
47   (set-last-result (void))
48   (let ([output
49          (with-output-to-string
50            (lambda ()
51              (with-handlers ([exn? set-last-error])
52                (call-with-values thunk set-last-result))))])
53     (append last-result `((output . ,output)))))
55 (define (eval-in form spec lang)
56   (write (call-with-result
57           (lambda ()
58             (update-signature-cache spec form)
59             (eval form (module-spec->namespace spec lang)))))
60   (newline))
62 (define compile-in eval-in)
64 (define (load-file file)
65   (load-module file (current-output-port) (last-namespace))
66   (update-signature-cache file))
68 (define compile-file load-file)
70 (define (macroexpand form . all)
71   (let ([all (and (not (null? all)) (car all))])
72     (with-output-to-string
73       (lambda ()
74         (pretty-print (syntax->datum ((if all expand expand-once) form)))))))
76 (define (make-repl-reader reader)
77   (lambda ()
78     (last-namespace (current-namespace))
79     (reader)))
81 ;;; eval.rkt ends here