cenv.scm 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. ;;; Ported from Scheme 48 1.9. See file COPYING for notices and license.
  2. ;;;
  3. ;;; Port Author: Andrew Whatson
  4. ;;;
  5. ;;; Original Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
  6. ;;;
  7. ;;; scheme48-1.9.2/scheme/bcomp/cenv.scm
  8. (define-module (prescheme bcomp cenv)
  9. #:use-module (srfi srfi-9)
  10. #:use-module (prescheme record-discloser)
  11. #:export (make-compiler-env ;; re-exported by syntactic
  12. compiler-env?
  13. lookup
  14. bind1
  15. bind-source-file-name ;; re-exported by syntactic
  16. source-file-name
  17. comp-env-macro-eval
  18. comp-env-define!
  19. extract-package-from-comp-env)
  20. #:re-export (bind))
  21. ;; Compile-time environments
  22. ;; These are functions
  23. ;; name -> node ; lexical variable
  24. ;; binding ; package variable, any syntax
  25. ;; #f ; free
  26. ;;
  27. ;; Special names are used to retrieve various values from compiler environments.
  28. (define-record-type :compiler-specials
  29. (make-compiler-specials lookup define! macro-eval package source-file-name)
  30. compiler-specials?
  31. (lookup compiler-specials-lookup)
  32. (define! compiler-specials-define!)
  33. (macro-eval compiler-specials-macro-eval)
  34. (package compiler-specials-package)
  35. (source-file-name compiler-specials-source-file-name))
  36. (define-record-type :compiler-env
  37. (really-make-compiler-env specials alist)
  38. compiler-env?
  39. (specials compiler-env-specials)
  40. (alist compiler-env-alist))
  41. (define (lookup cenv name)
  42. (cond
  43. ((assq name (compiler-env-alist cenv)) => cdr)
  44. (else
  45. ((compiler-specials-lookup (compiler-env-specials cenv)) name))))
  46. (define (bind1 name binding cenv)
  47. (really-make-compiler-env (compiler-env-specials cenv)
  48. (cons (cons name binding) (compiler-env-alist cenv))))
  49. (define (bind names bindings cenv)
  50. (really-make-compiler-env (compiler-env-specials cenv)
  51. (append (map cons names bindings)
  52. (compiler-env-alist cenv))))
  53. ;; Making the initial compiler environment.
  54. ;;
  55. ;; lookup : name -> binding or (binding . path) or #f
  56. ;; define! : name type [static] -> void
  57. ;; macro-eval : reflective tower, i.e. promise that returns
  58. ;; (<eval> . <env>) for evaluating macro expanders
  59. (define (make-compiler-env lookup define! macro-eval package)
  60. (really-make-compiler-env (make-compiler-specials lookup define! macro-eval package #f)
  61. '()))
  62. ;; EVAL function for evaluating macro expanders.
  63. (define (comp-env-macro-eval cenv)
  64. (compiler-specials-macro-eval (compiler-env-specials cenv)))
  65. ;; Function for adding definitions to the outer package.
  66. (define (comp-env-define! cenv name type . maybe-value)
  67. (apply (compiler-specials-define! (compiler-env-specials cenv))
  68. name type maybe-value))
  69. ;; The package on which the compiler environment is based. This is a
  70. ;; temporary hack to keep the package-editing code working.
  71. (define (extract-package-from-comp-env cenv)
  72. (compiler-specials-package (compiler-env-specials cenv)))
  73. ;; The name of the source file.
  74. ;; This is used by the %FILE-NAME% special form,
  75. ;; which is in turn used by the (MODULE ...) form to save the current file in
  76. ;; each package,
  77. ;; which is (finally) used to look up filenames in the correct directory.
  78. (define (bind-source-file-name filename env)
  79. (if filename
  80. (let ((specials (compiler-env-specials env)))
  81. (really-make-compiler-env (make-compiler-specials
  82. (compiler-specials-lookup specials)
  83. (compiler-specials-define! specials)
  84. (compiler-specials-macro-eval specials)
  85. (compiler-specials-package specials)
  86. filename)
  87. (compiler-env-alist env)))
  88. env))
  89. (define (source-file-name cenv)
  90. (compiler-specials-source-file-name (compiler-env-specials cenv)))