save.scm 24 KB

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