summaryrefslogtreecommitdiff
path: root/scheme/guile/geiser/evaluation.scm
blob: 3e44834444b5df2a85be3bd68087a0191e4c8259 (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
;;; evaluation.scm -- evaluation, compilation and macro-expansion

;; Copyright (C) 2009 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: Mon Mar 02, 2009 02:46

(define-module (geiser evaluation)
  #:export (ge:eval
            ge:compile
            ge:macroexpand
            ge:compile-file
            ge:load-file)
  #:use-module (srfi srfi-1)
  #:use-module (system base compile)
  #:use-module (system base pmatch)
  #:use-module (system vm program)
  #:use-module (ice-9 pretty-print))

(define (handle-error stack . args)
  (pmatch args
    ((,key ,subr ,msg ,args . ,rest)
     (display "Backtrace:\n")
     (if (stack? stack)
         (display-backtrace stack (current-output-port)))
     (newline)
     (display-error stack (current-output-port) subr msg args rest))
    (else (display (format "ERROR: ~a, args: ~a" (car args) (cdr args)))))
  `(error (key . ,(car args))))

(define (ge:compile form module-name)
  (let* ((module (or (and (list? module-name)
                          (resolve-module module-name))
                     (current-module)))
         (result #f)
         (captured-stack #f)
         (err #f)
         (ev (lambda ()
               (set! result (call-with-values
                                (lambda () (compile form #:env module))
                              (lambda vs (map object->string vs)))))))
    (let ((output
           (with-output-to-string
             (lambda ()
               (catch #t
                 (lambda () (start-stack 'geiser-eval (ev)))
                 (lambda args
                   (set! err (apply handle-error captured-stack args)))
                 (lambda args
                   (set! captured-stack (make-stack #t 2 7))))))))
      (write `(,(or err (cons 'result result))
               (output . ,output)))
      (newline))))

(define ge:eval ge:compile)

(define (ge:compile-file path)
  "Compile a file, given its full @var{path}."
  (ge:compile `(compile-and-load ,path) '(geiser evaluation)))

(define (ge:load-file path)
  "Load file, given its full @var{path}."
  (ge:compile `(load-compiled ,(compiled-file-name path)) '(geiser evaluation)))

(define (ge:macroexpand form . all)
  (let ((all (and (not (null? all)) (car all))))
    (with-output-to-string
      (lambda ()
        (pretty-print ((if all macroexpand macroexpand-1) form))))))

;;; evaluation.scm ends here