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
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)))
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))
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))
49 (with-output-to-string
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
58 (update-signature-cache spec form)
59 (eval form (module-spec->namespace spec lang)))))
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
74 (pretty-print (syntax->datum ((if all expand expand-once) form)))))))
76 (define (make-repl-reader reader)
78 (last-namespace (current-namespace))
81 ;;; eval.rkt ends here