save.scm 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  1. ;;; installed-scm-file
  2. ;;;; Copyright (C) 2000,2001,2002, 2006 Free Software Foundation, Inc.
  3. ;;;;
  4. ;;;; This library is free software; you can redistribute it and/or
  5. ;;;; modify it under the terms of the GNU Lesser General Public
  6. ;;;; License as published by the Free Software Foundation; either
  7. ;;;; version 2.1 of the License, or (at your option) any later version.
  8. ;;;;
  9. ;;;; This library is distributed in the hope that it will be useful,
  10. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. ;;;; Lesser General Public License for more details.
  13. ;;;;
  14. ;;;; You should have received a copy of the GNU Lesser General Public
  15. ;;;; License along with this library; if not, write to the Free Software
  16. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. ;;;;
  18. (define-module (oop goops save)
  19. :use-module (oop goops internal)
  20. :use-module (oop goops util)
  21. :re-export (make-unbound)
  22. :export (save-objects load-objects restore
  23. enumerate! enumerate-component!
  24. write-readably write-component write-component-procedure
  25. literal? readable make-readable))
  26. ;;;
  27. ;;; save-objects ALIST PORT [EXCLUDED] [USES]
  28. ;;;
  29. ;;; ALIST ::= ((NAME . OBJECT) ...)
  30. ;;;
  31. ;;; Save OBJECT ... to PORT so that when the data is read and evaluated
  32. ;;; OBJECT ... are re-created under names NAME ... .
  33. ;;; Exclude any references to objects in the list EXCLUDED.
  34. ;;; Add a (use-modules . USES) line to the top of the saved text.
  35. ;;;
  36. ;;; In some instances, when `save-object' doesn't know how to produce
  37. ;;; readable syntax for an object, you can explicitly register read
  38. ;;; syntax for an object using the special form `readable'.
  39. ;;;
  40. ;;; Example:
  41. ;;;
  42. ;;; The function `foo' produces an object of obscure structure.
  43. ;;; Only `foo' can construct such objects. Because of this, an
  44. ;;; object such as
  45. ;;;
  46. ;;; (define x (vector 1 (foo)))
  47. ;;;
  48. ;;; cannot be saved by `save-objects'. But if you instead write
  49. ;;;
  50. ;;; (define x (vector 1 (readable (foo))))
  51. ;;;
  52. ;;; `save-objects' will happily produce the necessary read syntax.
  53. ;;;
  54. ;;; To add new read syntax, hang methods on `enumerate!' and
  55. ;;; `write-readably'.
  56. ;;;
  57. ;;; enumerate! OBJECT ENV
  58. ;;; Should call `enumerate-component!' (which takes same args) on
  59. ;;; each component object. Should return #t if the composite object
  60. ;;; can be written as a literal. (`enumerate-component!' returns #t
  61. ;;; if the component is a literal.
  62. ;;;
  63. ;;; write-readably OBJECT PORT ENV
  64. ;;; Should write a readable representation of OBJECT to PORT.
  65. ;;; Should use `write-component' to print each component object.
  66. ;;; Use `literal?' to decide if a component is a literal.
  67. ;;;
  68. ;;; Utilities:
  69. ;;;
  70. ;;; enumerate-component! OBJECT ENV
  71. ;;;
  72. ;;; write-component OBJECT PATCHER PORT ENV
  73. ;;; PATCHER is an expression which, when evaluated, stores OBJECT
  74. ;;; into its current location.
  75. ;;;
  76. ;;; Example:
  77. ;;;
  78. ;;; (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
  79. ;;;
  80. ;;; write-component is a macro.
  81. ;;;
  82. ;;; literal? COMPONENT ENV
  83. ;;;
  84. (define-method (immediate? (o <top>)) #f)
  85. (define-method (immediate? (o <null>)) #t)
  86. (define-method (immediate? (o <number>)) #t)
  87. (define-method (immediate? (o <boolean>)) #t)
  88. (define-method (immediate? (o <symbol>)) #t)
  89. (define-method (immediate? (o <char>)) #t)
  90. (define-method (immediate? (o <keyword>)) #t)
  91. ;;; enumerate! OBJECT ENVIRONMENT
  92. ;;;
  93. ;;; Return #t if object is a literal.
  94. ;;;
  95. (define-method (enumerate! (o <top>) env) #t)
  96. (define-method (write-readably (o <top>) file env)
  97. ;;(goops-error "No read-syntax defined for object `~S'" o)
  98. (write o file) ;doesn't catch bugs, but is much more flexible
  99. )
  100. ;;;
  101. ;;; Readables
  102. ;;;
  103. (if (or (not (defined? 'readables))
  104. (not readables))
  105. (define readables (make-weak-key-hash-table 61)))
  106. (define readable
  107. (procedure->memoizing-macro
  108. (lambda (exp env)
  109. `(make-readable ,(cadr exp) ',(copy-tree (cadr exp))))))
  110. (define (make-readable obj expr)
  111. (hashq-set! readables obj expr)
  112. obj)
  113. (define (readable-expression obj)
  114. `(readable ,(hashq-ref readables obj)))
  115. (define (readable? obj)
  116. (hashq-get-handle readables obj))
  117. ;;;
  118. ;;; Strings
  119. ;;;
  120. (define-method (enumerate! (o <string>) env) #f)
  121. ;;;
  122. ;;; Vectors
  123. ;;;
  124. (define-method (enumerate! (o <vector>) env)
  125. (or (not (vector? o))
  126. (let ((literal? #t))
  127. (array-for-each (lambda (o)
  128. (if (not (enumerate-component! o env))
  129. (set! literal? #f)))
  130. o)
  131. literal?)))
  132. (define-method (write-readably (o <vector>) file env)
  133. (if (not (vector? o))
  134. (write o file)
  135. (let ((n (vector-length o)))
  136. (if (zero? n)
  137. (display "#()" file)
  138. (let ((not-literal? (not (literal? o env))))
  139. (display (if not-literal?
  140. "(vector "
  141. "#(")
  142. file)
  143. (if (and not-literal?
  144. (literal? (vector-ref o 0) env))
  145. (display #\' file))
  146. (write-component (vector-ref o 0)
  147. `(vector-set! ,o 0 ,(vector-ref o 0))
  148. file
  149. env)
  150. (do ((i 1 (+ 1 i)))
  151. ((= i n))
  152. (display #\space file)
  153. (if (and not-literal?
  154. (literal? (vector-ref o i) env))
  155. (display #\' file))
  156. (write-component (vector-ref o i)
  157. `(vector-set! ,o ,i ,(vector-ref o i))
  158. file
  159. env))
  160. (display #\) file))))))
  161. ;;;
  162. ;;; Arrays
  163. ;;;
  164. (define-method (enumerate! (o <array>) env)
  165. (enumerate-component! (shared-array-root o) env))
  166. (define (make-mapper array)
  167. (let* ((dims (array-dimensions array))
  168. (n (array-rank array))
  169. (indices (reverse (if (<= n 11)
  170. (list-tail '(t s r q p n m l k j i) (- 11 n))
  171. (let loop ((n n)
  172. (ls '()))
  173. (if (zero? n)
  174. ls
  175. (loop (- n 1)
  176. (cons (gensym "i") ls))))))))
  177. `(lambda ,indices
  178. (+ ,(shared-array-offset array)
  179. ,@(map (lambda (ind dim inc)
  180. `(* ,inc ,(if (pair? dim) `(- ,ind ,(car dim)) ind)))
  181. indices
  182. (array-dimensions array)
  183. (shared-array-increments array))))))
  184. (define (write-array prefix o not-literal? file env)
  185. (letrec ((inner (lambda (n indices)
  186. (if (not (zero? n))
  187. (let ((el (apply array-ref o
  188. (reverse (cons 0 indices)))))
  189. (if (and not-literal?
  190. (literal? el env))
  191. (display #\' file))
  192. (write-component
  193. el
  194. `(array-set! ,o ,el ,@indices)
  195. file
  196. env)))
  197. (do ((i 1 (+ 1 i)))
  198. ((= i n))
  199. (display #\space file)
  200. (let ((el (apply array-ref o
  201. (reverse (cons i indices)))))
  202. (if (and not-literal?
  203. (literal? el env))
  204. (display #\' file))
  205. (write-component
  206. el
  207. `(array-set! ,o ,el ,@indices)
  208. file
  209. env))))))
  210. (display prefix file)
  211. (let loop ((dims (array-dimensions o))
  212. (indices '()))
  213. (cond ((null? (cdr dims))
  214. (inner (car dims) indices))
  215. (else
  216. (let ((n (car dims)))
  217. (do ((i 0 (+ 1 i)))
  218. ((= i n))
  219. (if (> i 0)
  220. (display #\space file))
  221. (display prefix file)
  222. (loop (cdr dims) (cons i indices))
  223. (display #\) file))))))
  224. (display #\) file)))
  225. (define-method (write-readably (o <array>) file env)
  226. (let ((root (shared-array-root o)))
  227. (cond ((literal? o env)
  228. (if (not (vector? root))
  229. (write o file)
  230. (begin
  231. (display #\# file)
  232. (display (array-rank o) file)
  233. (write-array #\( o #f file env))))
  234. ((binding? root env)
  235. (display "(make-shared-array " file)
  236. (if (literal? root env)
  237. (display #\' file))
  238. (write-component root
  239. (goops-error "write-readably(<array>): internal error")
  240. file
  241. env)
  242. (display #\space file)
  243. (display (make-mapper o) file)
  244. (for-each (lambda (dim)
  245. (display #\space file)
  246. (display dim file))
  247. (array-dimensions o))
  248. (display #\) file))
  249. (else
  250. (display "(list->uniform-array " file)
  251. (display (array-rank o) file)
  252. (display " '() " file)
  253. (write-array "(list " o file env)))))
  254. ;;;
  255. ;;; Pairs
  256. ;;;
  257. ;;; These methods have more complex structure than is required for
  258. ;;; most objects, since they take over some of the logic of
  259. ;;; `write-component'.
  260. ;;;
  261. (define-method (enumerate! (o <pair>) env)
  262. (let ((literal? (enumerate-component! (car o) env)))
  263. (and (enumerate-component! (cdr o) env)
  264. literal?)))
  265. (define-method (write-readably (o <pair>) file env)
  266. (let ((proper? (let loop ((ls o))
  267. (or (null? ls)
  268. (and (pair? ls)
  269. (not (binding? (cdr ls) env))
  270. (loop (cdr ls))))))
  271. (1? (or (not (pair? (cdr o)))
  272. (binding? (cdr o) env)))
  273. (not-literal? (not (literal? o env)))
  274. (infos '())
  275. (refs (ref-stack env)))
  276. (display (cond ((not not-literal?) #\()
  277. (proper? "(list ")
  278. (1? "(cons ")
  279. (else "(cons* "))
  280. file)
  281. (if (and not-literal?
  282. (literal? (car o) env))
  283. (display #\' file))
  284. (write-component (car o) `(set-car! ,o ,(car o)) file env)
  285. (do ((ls (cdr o) (cdr ls))
  286. (prev o ls))
  287. ((or (not (pair? ls))
  288. (binding? ls env))
  289. (if (not (null? ls))
  290. (begin
  291. (if (not not-literal?)
  292. (display " ." file))
  293. (display #\space file)
  294. (if (and not-literal?
  295. (literal? ls env))
  296. (display #\' file))
  297. (write-component ls `(set-cdr! ,prev ,ls) file env)))
  298. (display #\) file))
  299. (display #\space file)
  300. (set! infos (cons (object-info ls env) infos))
  301. (push-ref! ls env) ;*fixme* optimize
  302. (set! (visiting? (car infos)) #t)
  303. (if (and not-literal?
  304. (literal? (car ls) env))
  305. (display #\' file))
  306. (write-component (car ls) `(set-car! ,ls ,(car ls)) file env)
  307. )
  308. (for-each (lambda (info)
  309. (set! (visiting? info) #f))
  310. infos)
  311. (set! (ref-stack env) refs)
  312. ))
  313. ;;;
  314. ;;; Objects
  315. ;;;
  316. ;;; Doesn't yet handle unbound slots
  317. ;; Don't export this function! This is all very temporary.
  318. ;;
  319. (define (get-set-for-each proc class)
  320. (for-each (lambda (slotdef g-n-s)
  321. (let ((g-n-s (cddr g-n-s)))
  322. (cond ((integer? g-n-s)
  323. (proc (standard-get g-n-s) (standard-set g-n-s)))
  324. ((not (memq (slot-definition-allocation slotdef)
  325. '(#:class #:each-subclass)))
  326. (proc (car g-n-s) (cadr g-n-s))))))
  327. (class-slots class)
  328. (slot-ref class 'getters-n-setters)))
  329. (define (access-for-each proc class)
  330. (for-each (lambda (slotdef g-n-s)
  331. (let ((g-n-s (cddr g-n-s))
  332. (a (slot-definition-accessor slotdef)))
  333. (cond ((integer? g-n-s)
  334. (proc (slot-definition-name slotdef)
  335. (and a (generic-function-name a))
  336. (standard-get g-n-s)
  337. (standard-set g-n-s)))
  338. ((not (memq (slot-definition-allocation slotdef)
  339. '(#:class #:each-subclass)))
  340. (proc (slot-definition-name slotdef)
  341. (and a (generic-function-name a))
  342. (car g-n-s)
  343. (cadr g-n-s))))))
  344. (class-slots class)
  345. (slot-ref class 'getters-n-setters)))
  346. (define restore
  347. (procedure->memoizing-macro
  348. (lambda (exp env)
  349. "(restore CLASS (SLOT-NAME1 ...) EXP1 ...)"
  350. `(let ((o (,%allocate-instance ,(cadr exp) '())))
  351. (for-each (lambda (name val)
  352. (,slot-set! o name val))
  353. ',(caddr exp)
  354. (list ,@(cdddr exp)))
  355. o))))
  356. (define-method (enumerate! (o <object>) env)
  357. (get-set-for-each (lambda (get set)
  358. (let ((val (get o)))
  359. (if (not (unbound? val))
  360. (enumerate-component! val env))))
  361. (class-of o))
  362. #f)
  363. (define-method (write-readably (o <object>) file env)
  364. (let ((class (class-of o)))
  365. (display "(restore " file)
  366. (display (class-name class) file)
  367. (display " (" file)
  368. (let ((slotdefs
  369. (filter (lambda (slotdef)
  370. (not (or (memq (slot-definition-allocation slotdef)
  371. '(#:class #:each-subclass))
  372. (and (slot-bound? o (slot-definition-name slotdef))
  373. (excluded?
  374. (slot-ref o (slot-definition-name slotdef))
  375. env)))))
  376. (class-slots class))))
  377. (if (not (null? slotdefs))
  378. (begin
  379. (display (slot-definition-name (car slotdefs)) file)
  380. (for-each (lambda (slotdef)
  381. (display #\space file)
  382. (display (slot-definition-name slotdef) file))
  383. (cdr slotdefs)))))
  384. (display #\) file)
  385. (access-for-each (lambda (name aname get set)
  386. (display #\space file)
  387. (let ((val (get o)))
  388. (cond ((unbound? val)
  389. (display '(make-unbound) file))
  390. ((excluded? val env))
  391. (else
  392. (if (literal? val env)
  393. (display #\' file))
  394. (write-component val
  395. (if aname
  396. `(set! (,aname ,o) ,val)
  397. `(slot-set! ,o ',name ,val))
  398. file env)))))
  399. class)
  400. (display #\) file)))
  401. ;;;
  402. ;;; Classes
  403. ;;;
  404. ;;; Currently, we don't support reading in class objects
  405. ;;;
  406. (define-method (enumerate! (o <class>) env) #f)
  407. (define-method (write-readably (o <class>) file env)
  408. (display (class-name o) file))
  409. ;;;
  410. ;;; Generics
  411. ;;;
  412. ;;; Currently, we don't support reading in generic functions
  413. ;;;
  414. (define-method (enumerate! (o <generic>) env) #f)
  415. (define-method (write-readably (o <generic>) file env)
  416. (display (generic-function-name o) file))
  417. ;;;
  418. ;;; Method
  419. ;;;
  420. ;;; Currently, we don't support reading in methods
  421. ;;;
  422. (define-method (enumerate! (o <method>) env) #f)
  423. (define-method (write-readably (o <method>) file env)
  424. (goops-error "No read-syntax for <method> defined"))
  425. ;;;
  426. ;;; Environments
  427. ;;;
  428. (define-class <environment> ()
  429. (object-info #:accessor object-info
  430. #:init-form (make-hash-table 61))
  431. (excluded #:accessor excluded
  432. #:init-form (make-hash-table 61))
  433. (pass-2? #:accessor pass-2?
  434. #:init-value #f)
  435. (ref-stack #:accessor ref-stack
  436. #:init-value '())
  437. (objects #:accessor objects
  438. #:init-value '())
  439. (pre-defines #:accessor pre-defines
  440. #:init-value '())
  441. (locals #:accessor locals
  442. #:init-value '())
  443. (stand-ins #:accessor stand-ins
  444. #:init-value '())
  445. (post-defines #:accessor post-defines
  446. #:init-value '())
  447. (patchers #:accessor patchers
  448. #:init-value '())
  449. (multiple-bound #:accessor multiple-bound
  450. #:init-value '())
  451. )
  452. (define-method (initialize (env <environment>) initargs)
  453. (next-method)
  454. (cond ((get-keyword #:excluded initargs #f)
  455. => (lambda (excludees)
  456. (for-each (lambda (e)
  457. (hashq-create-handle! (excluded env) e #f))
  458. excludees)))))
  459. (define-method (object-info o env)
  460. (hashq-ref (object-info env) o))
  461. (define-method ((setter object-info) o env x)
  462. (hashq-set! (object-info env) o x))
  463. (define (excluded? o env)
  464. (hashq-get-handle (excluded env) o))
  465. (define (add-patcher! patcher env)
  466. (set! (patchers env) (cons patcher (patchers env))))
  467. (define (push-ref! o env)
  468. (set! (ref-stack env) (cons o (ref-stack env))))
  469. (define (pop-ref! env)
  470. (set! (ref-stack env) (cdr (ref-stack env))))
  471. (define (container env)
  472. (car (ref-stack env)))
  473. (define-class <object-info> ()
  474. (visiting #:accessor visiting
  475. #:init-value #f)
  476. (binding #:accessor binding
  477. #:init-value #f)
  478. (literal? #:accessor literal?
  479. #:init-value #f)
  480. )
  481. (define visiting? visiting)
  482. (define-method (binding (info <boolean>))
  483. #f)
  484. (define-method (binding o env)
  485. (binding (object-info o env)))
  486. (define binding? binding)
  487. (define-method (literal? (info <boolean>))
  488. #t)
  489. ;;; Note that this method is intended to be used only during the
  490. ;;; writing pass
  491. ;;;
  492. (define-method (literal? o env)
  493. (or (immediate? o)
  494. (excluded? o env)
  495. (let ((info (object-info o env)))
  496. ;; write-component sets all bindings first to #:defining,
  497. ;; then to #:defined
  498. (and (or (not (binding? info))
  499. ;; we might be using `literal?' in a write-readably method
  500. ;; to query about the object being defined
  501. (and (eq? (visiting info) #:defining)
  502. (null? (cdr (ref-stack env)))))
  503. (literal? info)))))
  504. ;;;
  505. ;;; Enumeration
  506. ;;;
  507. ;;; Enumeration has two passes.
  508. ;;;
  509. ;;; Pass 1: Detect common substructure, circular references and order
  510. ;;;
  511. ;;; Pass 2: Detect literals
  512. (define (enumerate-component! o env)
  513. (cond ((immediate? o) #t)
  514. ((readable? o) #f)
  515. ((excluded? o env) #t)
  516. ((pass-2? env)
  517. (let ((info (object-info o env)))
  518. (if (binding? info)
  519. ;; if circular reference, we print as a literal
  520. ;; (note that during pass-2, circular references are
  521. ;; forward references, i.e. *not* yet marked with #:pass-2
  522. (not (eq? (visiting? info) #:pass-2))
  523. (and (enumerate! o env)
  524. (begin
  525. (set! (literal? info) #t)
  526. #t)))))
  527. ((object-info o env)
  528. => (lambda (info)
  529. (set! (binding info) #t)
  530. (if (visiting? info)
  531. ;; circular reference--mark container
  532. (set! (binding (object-info (container env) env)) #t))))
  533. (else
  534. (let ((info (make <object-info>)))
  535. (set! (object-info o env) info)
  536. (push-ref! o env)
  537. (set! (visiting? info) #t)
  538. (enumerate! o env)
  539. (set! (visiting? info) #f)
  540. (pop-ref! env)
  541. (set! (objects env) (cons o (objects env)))))))
  542. (define (write-component-procedure o file env)
  543. "Return #f if circular reference"
  544. (cond ((immediate? o) (write o file) #t)
  545. ((readable? o) (write (readable-expression o) file) #t)
  546. ((excluded? o env) (display #f file) #t)
  547. (else
  548. (let ((info (object-info o env)))
  549. (cond ((not (binding? info)) (write-readably o file env) #t)
  550. ((not (eq? (visiting info) #:defined)) #f) ;forward reference
  551. (else (display (binding info) file) #t))))))
  552. ;;; write-component OBJECT PATCHER FILE ENV
  553. ;;;
  554. (define write-component
  555. (procedure->memoizing-macro
  556. (lambda (exp env)
  557. `(or (write-component-procedure ,(cadr exp) ,@(cdddr exp))
  558. (begin
  559. (display #f ,(cadddr exp))
  560. (add-patcher! ,(caddr exp) env))))))
  561. ;;;
  562. ;;; Main engine
  563. ;;;
  564. (define binding-name car)
  565. (define binding-object cdr)
  566. (define (pass-1! alist env)
  567. ;; Determine object order and necessary bindings
  568. (for-each (lambda (binding)
  569. (enumerate-component! (binding-object binding) env))
  570. alist))
  571. (define (make-local i)
  572. (string->symbol (string-append "%o" (number->string i))))
  573. (define (name-bindings! alist env)
  574. ;; Name top-level bindings
  575. (for-each (lambda (b)
  576. (let ((o (binding-object b)))
  577. (if (not (or (immediate? o)
  578. (readable? o)
  579. (excluded? o env)))
  580. (let ((info (object-info o env)))
  581. (if (symbol? (binding info))
  582. ;; already bound to a variable
  583. (set! (multiple-bound env)
  584. (acons (binding info)
  585. (binding-name b)
  586. (multiple-bound env)))
  587. (set! (binding info)
  588. (binding-name b)))))))
  589. alist)
  590. ;; Name rest of bindings and create stand-in and definition lists
  591. (let post-loop ((ls (objects env))
  592. (post-defs '()))
  593. (cond ((or (null? ls)
  594. (eq? (binding (car ls) env) #t))
  595. (set! (post-defines env) post-defs)
  596. (set! (objects env) ls))
  597. ((not (binding (car ls) env))
  598. (post-loop (cdr ls) post-defs))
  599. (else
  600. (post-loop (cdr ls) (cons (car ls) post-defs)))))
  601. (let pre-loop ((ls (reverse (objects env)))
  602. (i 0)
  603. (pre-defs '())
  604. (locs '())
  605. (sins '()))
  606. (if (null? ls)
  607. (begin
  608. (set! (pre-defines env) (reverse pre-defs))
  609. (set! (locals env) (reverse locs))
  610. (set! (stand-ins env) (reverse sins)))
  611. (let ((info (object-info (car ls) env)))
  612. (cond ((not (binding? info))
  613. (pre-loop (cdr ls) i pre-defs locs sins))
  614. ((boolean? (binding info))
  615. ;; local
  616. (set! (binding info) (make-local i))
  617. (pre-loop (cdr ls)
  618. (+ 1 i)
  619. pre-defs
  620. (cons (car ls) locs)
  621. sins))
  622. ((null? locs)
  623. (pre-loop (cdr ls)
  624. i
  625. (cons (car ls) pre-defs)
  626. locs
  627. sins))
  628. (else
  629. (let ((real-name (binding info)))
  630. (set! (binding info) (make-local i))
  631. (pre-loop (cdr ls)
  632. (+ 1 i)
  633. pre-defs
  634. (cons (car ls) locs)
  635. (acons (binding info) real-name sins)))))))))
  636. (define (pass-2! env)
  637. (set! (pass-2? env) #t)
  638. (for-each (lambda (o)
  639. (let ((info (object-info o env)))
  640. (set! (literal? info) (enumerate! o env))
  641. (set! (visiting info) #:pass-2)))
  642. (append (pre-defines env)
  643. (locals env)
  644. (post-defines env))))
  645. (define (write-define! name val literal? file)
  646. (display "(define " file)
  647. (display name file)
  648. (display #\space file)
  649. (if literal? (display #\' file))
  650. (write val file)
  651. (display ")\n" file))
  652. (define (write-empty-defines! file env)
  653. (for-each (lambda (stand-in)
  654. (write-define! (cdr stand-in) #f #f file))
  655. (stand-ins env))
  656. (for-each (lambda (o)
  657. (write-define! (binding o env) #f #f file))
  658. (post-defines env)))
  659. (define (write-definition! prefix o file env)
  660. (display prefix file)
  661. (let ((info (object-info o env)))
  662. (display (binding info) file)
  663. (display #\space file)
  664. (if (literal? info)
  665. (display #\' file))
  666. (push-ref! o env)
  667. (set! (visiting info) #:defining)
  668. (write-readably o file env)
  669. (set! (visiting info) #:defined)
  670. (pop-ref! env)
  671. (display #\) file)))
  672. (define (write-let*-head! file env)
  673. (display "(let* (" file)
  674. (write-definition! "(" (car (locals env)) file env)
  675. (for-each (lambda (o)
  676. (write-definition! "\n (" o file env))
  677. (cdr (locals env)))
  678. (display ")\n" file))
  679. (define (write-rebindings! prefix bindings file env)
  680. (for-each (lambda (patch)
  681. (display prefix file)
  682. (display (cdr patch) file)
  683. (display #\space file)
  684. (display (car patch) file)
  685. (display ")\n" file))
  686. bindings))
  687. (define (write-definitions! selector prefix file env)
  688. (for-each (lambda (o)
  689. (write-definition! prefix o file env)
  690. (newline file))
  691. (selector env)))
  692. (define (write-patches! prefix file env)
  693. (for-each (lambda (patch)
  694. (display prefix file)
  695. (display (let name-objects ((patcher patch))
  696. (cond ((binding patcher env)
  697. => (lambda (name)
  698. (cond ((assq name (stand-ins env))
  699. => cdr)
  700. (else name))))
  701. ((pair? patcher)
  702. (cons (name-objects (car patcher))
  703. (name-objects (cdr patcher))))
  704. (else patcher)))
  705. file)
  706. (newline file))
  707. (reverse (patchers env))))
  708. (define (write-immediates! alist file)
  709. (for-each (lambda (b)
  710. (if (immediate? (binding-object b))
  711. (write-define! (binding-name b)
  712. (binding-object b)
  713. #t
  714. file)))
  715. alist))
  716. (define (write-readables! alist file env)
  717. (let ((written '()))
  718. (for-each (lambda (b)
  719. (cond ((not (readable? (binding-object b))))
  720. ((assq (binding-object b) written)
  721. => (lambda (p)
  722. (set! (multiple-bound env)
  723. (acons (cdr p)
  724. (binding-name b)
  725. (multiple-bound env)))))
  726. (else
  727. (write-define! (binding-name b)
  728. (readable-expression (binding-object b))
  729. #f
  730. file)
  731. (set! written (acons (binding-object b)
  732. (binding-name b)
  733. written)))))
  734. alist)))
  735. (define-method (save-objects (alist <pair>) (file <string>) . rest)
  736. (let ((port (open-output-file file)))
  737. (apply save-objects alist port rest)
  738. (close-port port)
  739. *unspecified*))
  740. (define-method (save-objects (alist <pair>) (file <output-port>) . rest)
  741. (let ((excluded (if (>= (length rest) 1) (car rest) '()))
  742. (uses (if (>= (length rest) 2) (cadr rest) '())))
  743. (let ((env (make <environment> #:excluded excluded)))
  744. (pass-1! alist env)
  745. (name-bindings! alist env)
  746. (pass-2! env)
  747. (if (not (null? uses))
  748. (begin
  749. (write `(use-modules ,@uses) file)
  750. (newline file)))
  751. (write-immediates! alist file)
  752. (if (null? (locals env))
  753. (begin
  754. (write-definitions! post-defines "(define " file env)
  755. (write-patches! "" file env))
  756. (begin
  757. (write-definitions! pre-defines "(define " file env)
  758. (write-empty-defines! file env)
  759. (write-let*-head! file env)
  760. (write-rebindings! " (set! " (stand-ins env) file env)
  761. (write-definitions! post-defines " (set! " file env)
  762. (write-patches! " " file env)
  763. (display " )\n" file)))
  764. (write-readables! alist file env)
  765. (write-rebindings! "(define " (reverse (multiple-bound env)) file env))))
  766. (define-method (load-objects (file <string>))
  767. (let* ((port (open-input-file file))
  768. (objects (load-objects port)))
  769. (close-port port)
  770. objects))
  771. (define-method (load-objects (file <input-port>))
  772. (let ((m (make-module)))
  773. (module-use! m the-scm-module)
  774. (module-use! m %module-public-interface)
  775. (save-module-excursion
  776. (lambda ()
  777. (set-current-module m)
  778. (let loop ((sexp (read file)))
  779. (if (not (eof-object? sexp))
  780. (begin
  781. (eval sexp m)
  782. (loop (read file)))))))
  783. (module-map (lambda (name var)
  784. (cons name (variable-ref var)))
  785. m)))