save.scm 24 KB

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