+Better naming for main parser functions
[lineal.git] / src / save-restore.lisp
blob602e5bdf17360d6f3ae855ca8afd99b69c721855
2 (in-package :lineal)
4 (defgeneric persist-to (v strm))
5 (defmethod persist-to ((k number) strm)
6 (format strm " ~S" k))
8 (defmethod persist-to ((u tuple) strm)
9 (format strm " (vcat~{ ~S~})" (tuple-elems u)))
11 (defmethod persist-to ((a mtrix) strm)
12 (format strm " (vcat~{ (cat~{ ~S~})~})"
13 (mtrix-elems a)))
15 (defun persist-var-to (k strm)
16 (format strm "(store ~S" k)
17 (persist-to
18 (symbol-value (find-symbol k :lineal.client-vars))
19 strm)
20 (princ ")" strm)
21 (terpri strm))
23 ;V Write all variables in *clent-vars* to the stream V
24 ;V in a special format which uses lineal's calculator V
25 ;V syntax. For example, if we have a V
26 ;V number a, vector b, and matrix c, where V
27 ; a = 17
28 ; b = (1, 2, 3)
29 ; [1 2]
30 ; c = [3 4]
31 ;V the data written to the stream is V
32 ; (store "a" 17)
33 ; (store "b" (vcat 1 2 3))
34 ; (store "c" (vcat (cat 1 2) (cat 3 4))))
36 (defun save-to-stream (strm)
37 (loop :for k :in
38 (append *saved-numbers*
39 *saved-tuples* *saved-matrices*)
40 :do (persist-var-to k strm)))
42 (defun restore-from-stream (strm)
43 (let ((result (process-input-from-stream strm nil)))
44 (if (stringp result)
45 (format nil "ERROR ~A" result)
46 "Variables successfully restored.")))
48 ;V Save a session capture file.V
49 (defun local-capture
50 (&optional
51 (file (make-pathname :name "captured_session")))
52 (with-open-file
53 (strm file :direction :output
54 :if-exists :supersede)
55 (save-to-stream strm)))
57 ;V Restore from a capture file.V
58 (defun local-restore
59 (&optional
60 (file (make-pathname :name "captured_session")))
61 (handler-case
62 (with-open-file (strm file :direction :input
63 :if-does-not-exist nil)
64 (if strm (restore-from-stream strm)
65 "File does not exist."))
66 (error (condit)
67 (declare (ignore condit))
68 "Something went horribly wrong.")))