name.scm 3.6 KB

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