expand.scm 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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
  6. ;;;
  7. ;;; scheme48-1.9.2/ps-compiler/prescheme/expand.scm
  8. ;;;
  9. ;;; Expanding using the Scheme 48 expander.
  10. (define-module (ps-compiler prescheme expand)
  11. #:use-module (prescheme scheme48)
  12. #:use-module (prescheme bcomp node)
  13. #:use-module (prescheme bcomp binding)
  14. #:use-module (prescheme bcomp package)
  15. #:use-module (prescheme bcomp scan-package)
  16. #:use-module (prescheme bcomp syntax)
  17. #:use-module (prescheme locations)
  18. #:use-module (ps-compiler node variable)
  19. #:use-module (ps-compiler util util)
  20. #:use-module (ps-compiler prescheme eval)
  21. #:use-module (ps-compiler prescheme primitive)
  22. #:export (scan-packages))
  23. (define (scan-packages packages)
  24. (let ((definitions
  25. (fold (lambda (package definitions)
  26. (let ((cenv (package->environment package)))
  27. (fold (lambda (form definitions)
  28. (let ((node (expand-form form cenv)))
  29. (cond ((define-node? node)
  30. (cons (eval-define (expand node cenv)
  31. cenv)
  32. definitions))
  33. (else
  34. (eval-node (expand node cenv)
  35. global-ref
  36. global-set!
  37. eval-primitive)
  38. definitions))))
  39. (call-with-values
  40. (lambda ()
  41. (package-source package))
  42. (lambda (files.forms usual-transforms primitives?)
  43. (scan-forms (apply append (map cdr files.forms))
  44. cenv)))
  45. definitions)))
  46. packages
  47. '())))
  48. (reverse (map (lambda (var)
  49. (let ((value (variable-flag var)))
  50. (set-variable-flag! var #f)
  51. (cons var value)))
  52. definitions))))
  53. (define define-node? (node-predicate 'define))
  54. (define (eval-define node cenv)
  55. (let* ((form (node-form node))
  56. (value (eval-node (caddr form)
  57. global-ref
  58. global-set!
  59. eval-primitive))
  60. (lhs (cadr form)))
  61. (global-set! lhs value)
  62. (name->variable-or-value lhs)))
  63. (define (global-ref name)
  64. (let ((thing (name->variable-or-value name)))
  65. (if (variable? thing)
  66. (variable-flag thing)
  67. thing)))
  68. (define (global-set! name value)
  69. (let ((thing (name->variable-or-value name)))
  70. (if (primitive? thing)
  71. (bug "trying to set the value of primitive ~S" thing)
  72. (set-variable-flag! thing value))))
  73. (define (name->variable-or-value name)
  74. (let ((binding (node-ref name 'binding)))
  75. (if (binding? binding)
  76. (let ((value (binding-place binding))
  77. (static (binding-static binding)))
  78. (cond ((primitive? static)
  79. static)
  80. ((variable? value)
  81. value)
  82. ((and (location? value)
  83. (constant? (contents value)))
  84. (contents value))
  85. (else
  86. (bug "global binding is not a variable, primitive or constant ~S" name))))
  87. (user-error "unbound variable ~S" (node-form name)))))