-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathserializing.lisp
58 lines (51 loc) · 2.2 KB
/
serializing.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
;;;; This file is one of components of CL-YACLYAML system, licenced under GPL, see COPYING for details
(in-package #:cl-yaclyaml)
;;; Converting representation graph into representation tree.
(defparameter node-aliases (make-hash-table :test #'eq))
(defparameter alias-count 0)
(defparameter encountered-nodes (make-hash-table :test #'eq))
(defun %nserialize (node)
;; (format t "encountered nodes: ~a~%" (hash->assoc encountered-nodes))
;; (format t "node: ~a~%" node)
;; (format t "~a~%" (multiple-value-list (gethash node encountered-nodes)))
(let ((it (gethash node node-aliases)))
(if it
(values it t)
(if (gethash node encountered-nodes)
;; need to create an alias node
(progn ;; (format t "Encountered node for at least second time.~%")
(let ((alias-name (format nil "a~a" (incf alias-count))))
;; KLUDGE: here we assume that :PROPERTIES is the first assoc-element
;; and content is the second
(setf (car node) `(:properties (:anchor . ,alias-name) ,.(cdar node)))
(values (setf (gethash node node-aliases) `(:alias . ,alias-name))
t)))
(progn ;; (format t "Encountered node for the first time. ")
(setf (gethash node encountered-nodes) t)
(cond ((scalar-p node)
;; (format t "scalar~%")
nil)
((mapping-p node)
;; (format t "mapping~%")
(iter (for map-entry in (cddr (assoc :content node)))
(multiple-value-bind (it got) (%nserialize (car map-entry))
(if got
(setf (car map-entry) it)))
(multiple-value-bind (it got) (%nserialize (cdr map-entry))
(if got
(setf (cdr map-entry) it)))))
;; sequence
(t
;; (format t "sequence~%")
(iter (for subnode on (cdr (assoc :content node)))
(multiple-value-bind (it got) (%nserialize (car subnode))
(if got
(setf (car subnode) it))))))
(values node nil))))))
(defun nserialize (representation-graph)
"Destructively convert representation graph (which may contain loops) to representation tree,
where all shared-ness is represented via alias-nodes."
(let ((node-aliases (make-hash-table :test #'eq))
(encountered-nodes (make-hash-table :test #'eq))
(alias-count 0))
(%nserialize representation-graph)))