environment.sls 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!r6rs
  2. ;;; Copyright © 2016 Federico Beffa <beffa@fbengineering.ch>
  3. ;;;
  4. ;;; This program is free software; you can redistribute it and/or modify it
  5. ;;; under the terms of the GNU General Public License as published by
  6. ;;; the Free Software Foundation; either version 3 of the License, or (at
  7. ;;; your option) any later version.
  8. ;;;
  9. ;;; This program is distributed in the hope that it will be useful, but
  10. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;;; GNU General Public License for more details.
  13. ;;;
  14. ;;; You should have received a copy of the GNU General Public License
  15. ;;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Code
  17. (library (mit environment)
  18. (export ge access
  19. user-initial-environment
  20. system-global-environment
  21. environment-define
  22. extend-top-level-environment
  23. environment-bindings
  24. nearest-repl/environment
  25. environment-bound?
  26. environment-assign!
  27. environment-lookup
  28. environment-link-name
  29. environment-update-from-child)
  30. (import (except (rnrs) error assert)
  31. (only (chezscheme) interaction-environment set-top-level-value!
  32. scheme-environment copy-environment environment-symbols
  33. define-top-level-value
  34. top-level-value top-level-bound?
  35. top-level-syntax? define-top-level-syntax top-level-syntax))
  36. (define user-initial-environment
  37. (copy-environment (scheme-environment))
  38. ;;(interaction-environment)
  39. )
  40. (define system-global-environment
  41. (copy-environment (scheme-environment)))
  42. (define (ge env)
  43. (interaction-environment env))
  44. ;; XXX: should add the set! use. (identifier-syntax?)
  45. (define-syntax access
  46. (syntax-rules ()
  47. ((_ name env) (top-level-value 'name env))))
  48. (define (environment-define env var obj)
  49. (define-top-level-value var obj env))
  50. (define (extend-top-level-environment env)
  51. (copy-environment env))
  52. (define (environment-bindings env)
  53. (let ((ss (if env
  54. (filter (lambda (s) (top-level-bound? s env))
  55. (environment-symbols env))
  56. '())))
  57. (map (lambda (s) (list s (top-level-value s env))) ss)))
  58. (define nearest-repl/environment
  59. (lambda () (interaction-environment)))
  60. (define (environment-bound? env symbol)
  61. (top-level-bound? symbol env))
  62. (define (environment-assign! env symbol object)
  63. (set-top-level-value! symbol object env))
  64. (define (environment-lookup env symbol)
  65. (top-level-value symbol env))
  66. (define (environment-link-name dest-env src-env name)
  67. (define-top-level-value name (top-level-value name src-env) dest-env))
  68. (define environment-update-from-child
  69. (case-lambda
  70. ((env child-env)
  71. (environment-update-from-child env child-env (environment-symbols child-env)))
  72. ((env child-env child-symbols)
  73. (for-each (lambda (s)
  74. (unless (top-level-bound? s env)
  75. (cond
  76. ((top-level-syntax? s child-env)
  77. (define-top-level-syntax s (top-level-syntax s child-env) env))
  78. (else
  79. (define-top-level-value s (top-level-value s child-env) env)))))
  80. child-symbols))))
  81. )