shadow.scm 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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-global-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-global-exception opcode reason loc rest)
  41. (begin (template-set! template index new)
  42. ;(note 'deal-with-replaced-variable "Replaced location" loc new p-uid)
  43. (if (location-defined? new)
  44. (succeed new rest)
  45. (apply signal-global-exception opcode reason loc new rest)))))
  46. (assertion-violation 'deal-with-replaced-variable
  47. "lossage in deal-with-replaced-variables"
  48. loc index)))))
  49. (define (signal-global-exception opcode reason loc . rest)
  50. (signal-condition
  51. (condition
  52. (construct-vm-exception opcode reason)
  53. (make-assertion-violation)
  54. (make-who-condition (enumerand->name opcode op))
  55. (make-message-condition
  56. (if (location-defined? loc)
  57. "unassigned variable"
  58. "undefined variable"))
  59. (make-irritants-condition
  60. (cons (or (location-name loc) loc)
  61. (let ((pack (location-package-name loc)))
  62. (if pack
  63. (cons pack rest)
  64. rest)))))))
  65. (define-vm-exception-handler (enum op global)
  66. (deal-with-replaced-variables
  67. (lambda (loc more-args)
  68. (contents loc))))
  69. (define-vm-exception-handler (enum op set-global!)
  70. (deal-with-replaced-variables
  71. (lambda (loc more-args)
  72. (set-contents! loc (car more-args)))))