name.scm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Richard Kelsey, Jonathan Rees, Mike Sperber
  3. ; Names (symbols) and generated names.
  4. (define (name? thing)
  5. (or (symbol? thing)
  6. (generated? thing)))
  7. ; Generated names
  8. ; Generated names make lexically-scoped macros work. They're the same
  9. ; as what Alan Bawden and Chris Hanson call "aliases". The parent
  10. ; field is always another name (perhaps generated). The parent chain
  11. ; provides an access path to the name's binding, should one ever be
  12. ; needed. That is: If name M is bound to a transform T that generates
  13. ; name G as an alias for name N, then M is (generated-parent-name G),
  14. ; so we can get the binding of G by accessing the binding of N in T's
  15. ; environment of closure, and we get T by looking up M in the
  16. ; environment in which M is *used*.
  17. (define-record-type generated :generated
  18. (make-generated name token env parent-name)
  19. generated?
  20. (name generated-name)
  21. (token generated-token)
  22. (env generated-env)
  23. (parent-name generated-parent-name))
  24. (define-record-discloser :generated
  25. (lambda (name)
  26. (list 'generated (generated-name name) (generated-uid name))))
  27. (define (generate-name name env parent-name) ;for opt/inline.scm
  28. (make-generated name (cons #f #f) env parent-name))
  29. (define (generated-uid generated-name)
  30. (let ((token (generated-token generated-name)))
  31. (or (car token)
  32. (let ((uid *generated-uid*))
  33. (set! *generated-uid* (+ *generated-uid* 1))
  34. (set-car! token uid)
  35. uid))))
  36. (define *generated-uid* 0)
  37. (define (name->symbol name)
  38. (if (symbol? name)
  39. name
  40. (string->symbol (string-append (symbol->string
  41. (name->symbol (generated-name name)))
  42. "##"
  43. (number->string (generated-uid name))))))
  44. (define (name-hash name)
  45. (cond ((symbol? name)
  46. (string-hash (symbol->string name)))
  47. ((generated? name)
  48. (name-hash (generated-name name)))
  49. (else
  50. (assertion-violation 'name-hash "invalid name" name))))
  51. (define make-name-table
  52. (make-table-maker eq? name-hash))
  53. ; Used by QUOTE to turn generated names back into symbols
  54. (define (desyntaxify thing0)
  55. (let desyntaxify ((thing thing0))
  56. (cond ((or (boolean? thing) (null? thing) (number? thing)
  57. (symbol? thing) (char? thing))
  58. thing)
  59. ((string? thing)
  60. (make-immutable! thing))
  61. ((generated? thing)
  62. (desyntaxify (generated-name thing)))
  63. ((pair? thing)
  64. (make-immutable!
  65. (cons (desyntaxify (car thing))
  66. (desyntaxify (cdr thing)))))
  67. ((vector? thing)
  68. (make-immutable!
  69. (let ((new (make-vector (vector-length thing) #f)))
  70. (let loop ((i 0))
  71. (if (>= i (vector-length thing))
  72. new
  73. (begin
  74. (vector-set! new i (desyntaxify (vector-ref thing i)))
  75. (loop (+ i 1))))))))
  76. (else
  77. (syntax-violation 'quote "invalid datum in quotation" thing)))))
  78. ;----------------
  79. ; Qualified names
  80. ;
  81. ; A qualified name is a generated name that has been translated into a path.
  82. ; For example, if syntax A introduces a reference to procedure B, then the
  83. ; reference to B, as a qualified name, will be #(>> A B). If B refers to
  84. ; C and is substituted in-line, then the reference to C is #(>> #(>> A B) C).
  85. ; The binding for C can be located by going to the structure which supplies A,
  86. ; finding where it gets B from, and then looking up C there.
  87. ; These can't be records because they are included in linked images.
  88. (define (make-qualified transform-name sym uid)
  89. (vector '>> transform-name sym uid))
  90. (define (qualified? thing)
  91. (and (vector? thing)
  92. (= (vector-length thing) 4)
  93. (eq? (vector-ref thing 0) '>>)))
  94. (define (qualified-parent-name q) (vector-ref q 1))
  95. (define (qualified-symbol q) (vector-ref q 2))
  96. (define (qualified-uid q) (vector-ref q 3))