eval.scm 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Ivan Shmakov, Mike Sperber
  3. ; This file contains things that tie together the compiler and the
  4. ; run-time system.
  5. ; EVAL
  6. (define (eval form package)
  7. (compile-and-run (list form) package #f #f))
  8. ; LOAD-INTO - load file into package.
  9. (define (load-into filename package)
  10. (really-load-into filename package #f #f))
  11. (define (load-script-into filename package)
  12. (really-load-into filename package #f #t))
  13. ; Evaluate forms as if they came from the given file.
  14. (define (eval-from-file forms package filename)
  15. (if filename
  16. ((fluid-cell-ref $note-file-package)
  17. filename package))
  18. (compile-and-run forms package filename #t))
  19. ; LOAD
  20. (define (load filename . package-option)
  21. (let ((package (if (null? package-option)
  22. (interaction-environment)
  23. (car package-option))))
  24. (really-load-into filename package #t #f)))
  25. ;----------------
  26. (define (really-load-into filename package note-undefined? script?)
  27. (force-output (current-output-port)) ; just to make the output nice
  28. (let ((forms (read-forms filename package script?)))
  29. (newline (current-noise-port)) ; READ-FORMS prints the filename
  30. (compile-and-run forms
  31. package
  32. filename
  33. note-undefined?)))
  34. (define (compile-and-run forms package maybe-filename note-undefined?)
  35. (let* ((env (if maybe-filename
  36. (bind-source-file-name maybe-filename
  37. (package->environment package))
  38. (package->environment package)))
  39. (template (compile-forms (map (lambda (form)
  40. (delay (expand-scanned-form form env)))
  41. (scan-forms forms env))
  42. maybe-filename
  43. (package-uid package))))
  44. (link! template package note-undefined?)
  45. (with-load-filename maybe-filename
  46. (lambda ()
  47. (invoke-closure
  48. (make-closure template
  49. (package-uid package)))))))
  50. (define $load-filename (make-fluid (make-cell #f)))
  51. (define (with-load-filename filename thunk)
  52. (let-fluid $load-filename (make-cell filename)
  53. thunk))
  54. (define (current-load-filename)
  55. (fluid-cell-ref $load-filename))