1 ;;; eval.rkt -- evaluation
3 ;; Copyright (C) 2009, 2010, 2011, 2012 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
21 (require geiser/enter geiser/modules geiser/images)
22 (require errortrace/errortrace-lib)
24 (define last-result (void))
26 (define last-namespace (make-parameter (current-namespace)))
29 (vector-ref (struct->vector e) 0))
31 (define (set-last-error e)
32 (set! last-result `((error (key . ,(exn-key e)))))
33 (display (exn-message e))
35 (parameterize ([error-context-display-depth 10])
36 (print-error-trace (current-output-port) e)))
38 (define (write-value v)
39 (with-output-to-string
40 (lambda () (maybe-write-image v))))
42 (define (set-last-result . vs)
43 (set! last-result `((result ,@(map write-value vs)))))
45 (define (call-with-result thunk)
46 (set-last-result (void))
48 (with-output-to-string
50 (parameterize ([current-error-port (current-output-port)])
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 (eval form (module-spec->namespace spec lang)))))
61 (define (load-file file)
62 (load-module file (current-output-port) (last-namespace)))
64 (define (macroexpand form . all)
65 (let ([all (and (not (null? all)) (car all))])
66 (with-output-to-string
68 (pretty-print (syntax->datum ((if all expand expand-once) form)))))))
70 (define (add-to-load-path p)
72 (let ([p (string->path p)]
73 [cps (current-library-collection-paths)])
74 (unless (member p cps)
75 (current-library-collection-paths
79 (define (make-repl-reader reader)
81 (last-namespace (current-namespace))