shadow.scm 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. ; Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees. See file COPYING.
  2. ; Deal with shadowed variables.
  3. ; When a variable is shadowed by a variable, split the existing shared
  4. ; location into two replacement locations.
  5. ; name (structure-ref p name) (define name ...) within a single template
  6. ; will lose big.
  7. ;(define *replaced-locations* '()) ;alist of (old rep ((uid ...) . new))
  8. (define (shadow-location! old p-uids new replacement)
  9. (if (location-defined? old)
  10. (set-contents! replacement (contents old)))
  11. (set-location-id! old
  12. (vector replacement p-uids new))
  13. (set-location-defined?! old #f)) ;so that exceptions will be raised
  14. (define maybe-replace-location
  15. (let ((memv memv))
  16. (lambda (loc p-uid) ;Package's unique id
  17. (let ((foo (location-id loc)))
  18. (if (vector? foo)
  19. (maybe-replace-location
  20. (if (memv p-uid (vector-ref foo 1))
  21. (vector-ref foo 2)
  22. (vector-ref foo 0))
  23. p-uid)
  24. loc)))))
  25. ; Exception handler:
  26. (define (deal-with-replaced-variables succeed)
  27. (lambda (opcode reason loc template index . rest)
  28. (if (= reason (enum exception undefined-global))
  29. (deal-with-replaced-variable opcode reason loc template index rest
  30. succeed)
  31. (apply signal-vm-exception opcode reason loc rest))))
  32. (define (deal-with-replaced-variable opcode reason loc template index rest
  33. succeed)
  34. (primitive-catch
  35. (lambda (cont)
  36. (if (eq? (template-ref template index) loc)
  37. (let* ((p-uid (template-package-id template))
  38. (new (maybe-replace-location loc p-uid)))
  39. (if (eq? new loc)
  40. (apply signal-vm-exception opcode reason loc rest)
  41. (begin (template-set! template index new)
  42. ;(signal 'note "Replaced location" loc new p-uid)
  43. (if (location-defined? new)
  44. (succeed new rest)
  45. (apply signal-vm-exception opcode reason new rest)))))
  46. (error "lossage in deal-with-replaced-variables"
  47. loc index)))))
  48. (define-vm-exception-handler (enum op global)
  49. (deal-with-replaced-variables
  50. (lambda (loc more-args)
  51. (contents loc))))
  52. (define-vm-exception-handler (enum op set-global!)
  53. (deal-with-replaced-variables
  54. (lambda (loc more-args)
  55. (set-contents! loc (car more-args)))))