interface.scm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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, Jonathan Rees, Mike Sperber
  6. ;;;
  7. ;;; scheme48-1.9.2/scheme/bcomp/package.scm
  8. (define-module (prescheme bcomp interface)
  9. #:use-module (srfi srfi-9)
  10. #:use-module (prescheme scheme48)
  11. #:use-module (prescheme record-discloser)
  12. #:use-module (prescheme population)
  13. #:use-module (prescheme bcomp mtype)
  14. #:export (make-simple-interface
  15. make-compound-interface
  16. make-modified-interface-maker
  17. note-reference-to-interface!
  18. interface-ref
  19. interface-member?
  20. interface?
  21. interface-clients
  22. for-each-declaration
  23. note-interface-name!))
  24. ;; Interfaces
  25. ;;
  26. ;; An interface has four fields:
  27. ;; - A procedure for looking up names in the interface. The procedure
  28. ;; returns a base name and a type. If the name is not exported the
  29. ;; base name is #F.
  30. ;; - A procedure for walking over the declarations in the interfaces.
  31. ;; The name, base-name, and type of each exported name are passed
  32. ;; to the action procedure.
  33. ;; - A population containing the structures that export this interface and
  34. ;; any compound or modified interfaces that build on it. This population
  35. ;; is used for propogating changes when an interface or structure is
  36. ;; redefined.
  37. ;; - A name for debugging.
  38. (define-record-type :interface
  39. (really-make-interface ref walk clients name)
  40. interface?
  41. (ref ref-method)
  42. (walk walk-method)
  43. (clients interface-clients)
  44. (name interface-name set-interface-name!))
  45. (define-record-discloser :interface
  46. (lambda (int)
  47. (list 'interface (interface-name int))))
  48. (define (make-interface ref walk name)
  49. (really-make-interface ref
  50. walk
  51. (make-population)
  52. name))
  53. ;; The generic lookup function, and a simplified version for use when the
  54. ;; base name and type are not needed.
  55. (define (interface-ref int name)
  56. ((ref-method int) name))
  57. (define (interface-member? int name)
  58. (receive (base-name type)
  59. (interface-ref int name)
  60. base-name))
  61. ;; The generic walk function.
  62. (define (for-each-declaration proc int)
  63. ((walk-method int) proc))
  64. ;; Adding to the client population.
  65. (define (note-reference-to-interface! int thing)
  66. (let ((pop (interface-clients int)))
  67. (if pop
  68. (add-to-population! thing pop))))
  69. ;; Adding a late name.
  70. (define (note-interface-name! int name)
  71. (if (and name (not (interface-name int)))
  72. (set-interface-name! int name)))
  73. ;;----------------
  74. ;; Simple interfaces. ITEMS is a list of items of the form:
  75. ;; - <name> ; use the default type
  76. ;; - (<name> <type>) ; use <type>
  77. ;; - ((<name> ...) <type>) ; use <type> for each <name>
  78. ;;
  79. ;; We make a table of the names and use it appropriately.
  80. (define (make-simple-interface name items)
  81. (let ((table (make-simple-interface-table items)))
  82. (make-interface (lambda (name)
  83. (let ((type (table-ref table name)))
  84. (if type
  85. (values name type)
  86. (values #f #f))))
  87. (lambda (proc)
  88. (table-walk (lambda (name type)
  89. (proc name name type))
  90. table))
  91. name)))
  92. (define (make-simple-interface-table items)
  93. (let ((table (make-symbol-table)))
  94. (for-each (lambda (item)
  95. (if (pair? item)
  96. (let ((name (car item))
  97. (type (cadr item)))
  98. (if (or (null? name)
  99. (pair? name))
  100. (for-each (lambda (name)
  101. (table-set! table name type))
  102. name)
  103. (table-set! table name type)))
  104. (table-set! table item undeclared-type)))
  105. items)
  106. (make-table-immutable! table)
  107. table))
  108. ;;----------------
  109. ;; Compound interfaces
  110. ;;
  111. ;; A compound interface is the union of a set of existing interfaces.
  112. ;; To do lookups or walks we walk down the list of included interfaces.
  113. (define (make-compound-interface name . ints)
  114. (let ((int (make-interface (lambda (name)
  115. (let loop ((ints ints))
  116. (if (null? ints)
  117. (values #f #f)
  118. (receive (new-name type)
  119. (interface-ref (car ints) name)
  120. (if new-name
  121. (values new-name type)
  122. (loop (cdr ints)))))))
  123. (lambda (proc)
  124. (for-each (lambda (int)
  125. (for-each-declaration proc int))
  126. ints))
  127. name)))
  128. (for-each (lambda (i)
  129. (note-reference-to-interface! i int))
  130. ints)
  131. int))
  132. ;;----------------
  133. ;; Modified interfaces.
  134. ;;
  135. ;; We return a procedure that uses COMMANDS to modify any interface it is
  136. ;; passed. We parse the commands first so that errors are detected before
  137. ;; the structure that exports the modified interace is installed anywhere.
  138. ;;
  139. ;; Commands are:
  140. ;; (prefix <symbol>)
  141. ;; Add <symbol> to the beginning of every name in INTERFACE.
  142. ;; (expose <symbol> ...)
  143. ;; Export only those names in INTERFACE that are listed.
  144. ;; (hide <symbol> ...)
  145. ;; Do not export any of the names listed.
  146. ;; (alias (<old> <new>) ...)
  147. ;; Make name <old> also visible as <new>.
  148. ;; (rename (<old> <new>) ...)
  149. ;; Make name <old> visible as <new> but not as <old>.
  150. ;; The commands are interpreted last-to-first. Thus
  151. ;; ((expose foo:bar) (prefix foo:))
  152. ;; and
  153. ;; ((prefix foo:) (expose bar))
  154. ;; both make BAR visible as FOO:BAR but
  155. ;; ((expose bar) (prefix foo:))
  156. ;; does not allow any names to be seen.
  157. (define (make-modified-interface-maker commands)
  158. (if (and (list? commands)
  159. (every okay-command? commands))
  160. (receive (alist hidden default)
  161. (process-commands commands)
  162. (lambda (interface)
  163. (let ((lookup (make-lookup alist hidden default interface))
  164. (walker (make-interface-walker alist hidden default interface)))
  165. (let ((int (make-interface lookup walker #f)))
  166. (note-reference-to-interface! interface int)
  167. int))))
  168. (assertion-violation 'make-modified-interface-maker
  169. "badly-formed structure modifiers" commands)))
  170. ;; We process COMMANDS and compute three values:
  171. ;; - an alist mapping visible names to their real names in the package
  172. ;; - a list of names that are hidden (these may also appear in the alist;
  173. ;; the hiding overrides the alist).
  174. ;; - a default, which applies to all other names:
  175. ;; = #f, there are no other visible names
  176. ;; = #t, all other names are visible
  177. ;; = <symbol>, names beginning with this prefix are visible
  178. ;;
  179. ;; We just loop over the commands, dispatching on the type of command.
  180. (define (process-commands commands)
  181. (let loop ((alist '())
  182. (hidden '())
  183. (default #t)
  184. (commands (reverse commands)))
  185. (if (null? commands)
  186. (values (filter (lambda (pair)
  187. (not (memq (car pair) hidden)))
  188. alist)
  189. hidden
  190. default)
  191. (receive (alist hidden default)
  192. (let ((proc (case (caar commands)
  193. ((prefix) process-prefix)
  194. ((expose) process-expose)
  195. ((hide) process-hide)
  196. ((alias) process-alias)
  197. ((rename) process-rename))))
  198. (proc (cdar commands) alist hidden default))
  199. (loop alist hidden default (cdr commands))))))
  200. ;; Checks that COMMAND is properly formed.
  201. (define (okay-command? command)
  202. (and (list? command)
  203. (pair? command)
  204. (symbol? (car command))
  205. (pair? (cdr command))
  206. (let ((args (cdr command)))
  207. (case (car command)
  208. ((prefix)
  209. (and (symbol? (car args))
  210. (null? (cdr args))))
  211. ((expose hide)
  212. (every symbol? args))
  213. ((alias rename)
  214. (every (lambda (spec)
  215. (and (list? spec)
  216. (= 2 (length spec))
  217. (symbol? (car spec))
  218. (symbol? (cadr spec))))
  219. args))
  220. (else
  221. #f)))))
  222. ;; We add the prefix to the names in ALIST and HIDDEN. If DEFAULT is already
  223. ;; a prefix we add this one to it, otherwise the prefix is the new default.
  224. (define (process-prefix args alist hidden default)
  225. (let ((prefix (car args)))
  226. (values (map (lambda (pair)
  227. (cons (symbol-append prefix (car pair))
  228. (cdr pair)))
  229. alist)
  230. (map (lambda (name)
  231. (symbol-append prefix name))
  232. hidden)
  233. (cond ((symbol? default)
  234. (symbol-append default prefix))
  235. ((not default)
  236. #f)
  237. (else
  238. prefix)))))
  239. ;; We make a new ALIST with the exposed names and with package names are looked
  240. ;; up in the current state. Then we start again with no hidden names and no
  241. ;; default.
  242. (define (process-expose args alist hidden default)
  243. (values (let loop ((args args) (new-alist '()))
  244. (if (null? args)
  245. (reverse new-alist)
  246. (let* ((name (car args))
  247. (pname (interface-lookup name alist hidden default)))
  248. (loop (cdr args)
  249. (if pname
  250. (cons (cons name pname)
  251. new-alist)
  252. new-alist)))))
  253. '()
  254. #f))
  255. ;; Just add the names to the hidden list.
  256. (define (process-hide args alist hidden default)
  257. (values alist
  258. (append args hidden)
  259. default))
  260. ;; Add the new aliases to ALIST.
  261. (define (process-alias args alist hidden default)
  262. (values (append (map (lambda (spec)
  263. (cons (cadr spec)
  264. (interface-lookup (car spec) alist hidden default)))
  265. args)
  266. alist)
  267. hidden
  268. default))
  269. ;; Add the new aliases to ALIST and add the old names to HIDDEN.
  270. (define (process-rename args alist hidden default)
  271. (values (append (map (lambda (spec)
  272. (cons (cadr spec)
  273. (interface-lookup (car spec) alist hidden default)))
  274. args)
  275. alist)
  276. (append (map car args) hidden)
  277. default))
  278. ;;----------------
  279. ;; Look up a name, returning the name by which it is known in the base structure.
  280. ;; - If it is in HIDDEN then it is not exported.
  281. ;; - If there is an alias, then return the alias.
  282. ;; - If there is no default the name is not exported.
  283. ;; - A default of #T means every name is passed through.
  284. ;; - Otherwise, check that NAME begins with the default and return the
  285. ;; suffix after the default.
  286. (define (interface-lookup name alist hidden default)
  287. (cond ((memq name hidden)
  288. #f)
  289. ((assq name alist)
  290. => cdr)
  291. ((not default)
  292. #f)
  293. ((eq? default #t)
  294. name)
  295. ((prefix-match? (symbol->string name)
  296. (symbol->string default))
  297. (remove-prefix (symbol->string name)
  298. (symbol->string default)))
  299. (else
  300. #f)))
  301. ;; Curried version of MAKE-LOOKUP for making the INTERFACE-REF method for
  302. ;; modified structures.
  303. (define (make-lookup alist hidden default interface)
  304. (lambda (name)
  305. (let ((alias (interface-lookup name alist hidden default)))
  306. (if alias
  307. (interface-ref interface alias)
  308. (values #f #f)))))
  309. ;; True if NAME begins with PREFIX (and is not just PREFIX).
  310. (define (prefix-match? name prefix)
  311. (and (< (string-length prefix)
  312. (string-length name))
  313. (let loop ((i 0))
  314. (cond ((= i (string-length prefix))
  315. #t)
  316. ((char=? (string-ref name i)
  317. (string-ref prefix i))
  318. (loop (+ i 1)))
  319. (else
  320. #f)))))
  321. ;; Return the portion of NAME that follows PREFIX.
  322. (define (remove-prefix name prefix)
  323. (string->symbol (substring name
  324. (string-length prefix)
  325. (string-length name))))
  326. ;;----------------
  327. ;; Return a procedure for walking over the declarations in a modified interface.
  328. ;; There are two helpers, depending on whether names are passed on by default.
  329. (define (make-interface-walker alist hidden default interface)
  330. (lambda (proc)
  331. (if default
  332. (walk-default proc alist hidden default interface))
  333. (walk-alist proc alist hidden interface)))
  334. ;; If there is a default we need to walk over the declarations in the base
  335. ;; interface and pass on the ones that are not hidden.
  336. (define (walk-default proc alist hidden default interface)
  337. (for-each-declaration
  338. (lambda (name base-name type)
  339. (let ((new-name
  340. (cond ((symbol? default)
  341. (symbol-append default name))
  342. (else name))))
  343. (if (not (memq new-name hidden))
  344. (proc new-name
  345. base-name
  346. type))))
  347. interface))
  348. ;; With no default, all of the names are in the ALIST and we do not need to
  349. ;; walk over the declarations in the base interface.
  350. (define (walk-alist proc alist hidden interface)
  351. (for-each (lambda (pair)
  352. (receive (base-name type)
  353. (interface-ref interface (cdr pair))
  354. (let ((new-name (car pair)))
  355. (if (and base-name (not (memq new-name hidden)))
  356. (proc new-name
  357. base-name
  358. type)))))
  359. alist))