[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Writing structures to a file
- To: BYRON at MIT-ML
- Subject: Writing structures to a file
- From: Robert W. Kerns <RWK at MIT-MC>
- Date: Wed, 4 Feb 81 09:26:00 GMT
- Cc: BUG-LISP at MIT-ML
- Original-date: 4 February 1981 04:26-EST
From: BYRON@MIT-ML
Date: Tue, 21 Jan 81 17:15:37 GMT
Original-Date: 01/21/81 13:15:37 EDT
Subject: Re: Writing structures to a file
To: (BUG LISP) at MIT-ML
What is the canonical way to save DEFVST structures in a file.
Does code exist to parse the #{} syntax?
No code currently exists to parse the #{} syntax. However, it
wouldn't be that hard to write, and if you do so you should let
us know so we can steal it.
Another way of dealing with the problem is to define a method
to output code to produce the objects. For example:
;; Everything not handled specially, just print
(DEFMETHOD* (:OUTPUT OBJECT-CLASS) (SELF FILE)
(PRINT SELF FILE))
;; Unaesthetic, but workable
(DEFMETHOD* (:OUTPUT PAIR-CLASS) (SELF FILE)
(PRINC "(" FILE)
(SEND (CAR SELF) ':OUTPUT FILE)
(PRINC " . " FILE)
(SEND (CDR SELF) ':OUTPUT FILE)
(PRINC ") " FILE))
(DEFVST MYFROB SLOT-A SLOT-B)
(DEFMETHOD* (:OUTPUT MYFROB-CLASS) (SELF FILE)
(PRINC "#.(CONS-A-MYFROB SLOT-A '" FILE)
(SEND (MYFROB-SLOT-A SELF) ':OUTPUT FILE)
(PRINC " SLOT-B '" FILE)
(SEND (MYFROB-SLOT-B SELF) ':OUTPUT FILE)
(PRINC ")
" FILE))
and then to output stuff to the file, instead of using
(PRINT <stuff> <file>), do
(SEND <stuff> ':OUTPUT <file>). This will print code to
the file to reproduce <stuff> when read back in. If you don't
mind always seeing your objects looking like this, you can just do
(DEFMETHOD* (:PRINT-SELF MYFROB-CLASS) (SELF FILE LEVEL SLASHIFY-P)
(IF (NOT SLASHIFY-P) ;PRINC case, do as usual
(SEND-AS STRUCT-CLASS SELF FILE LEVEL SLASHIFY-P)
;; Otherwise we output a form to be evaluated to get our object
(PRINC "#." FILE)
(PRINT-OBJECT `(CONS-A-MYFROB SLOT-A ,(MYFROB-SLOT-A SELF)
SLOT-B ,(MYFROB-SLOT-B SELF))
LEVEL SLASHIFY-P FILE)))
Thus, PRINC will print using the #{} syntax, but PRINT will print using
the #.(CONS-A-MYFROB ....) syntax.