-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdraw-tree.el
168 lines (143 loc) · 5.1 KB
/
draw-tree.el
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
;; -*- lexical-binding: t -*-
;; draw-tree.el
;; Draw tree structure of cons cells
;; Ported to Emacs Lisp from Nils M Holm's Scheme 9 from Empty Space's
;; Function Library (http://www.t3x.org/s9fes/draw-tree.scm.html) and
;; its Common Lisp port written by CBaggers
;; (https://github.com/cbaggers/draw-cons-tree)
;; Giulio Pietroiusti, 2022
;; Placed in the Public Domain
;; Example:
;;
;; (draw-tree '((a) (b . c) (d e))) ==>
;; "
;; [o|o]---[o|o]---[o|/]
;; | | |
;; [o|/] | [o|o]---[o|/]
;; | | | |
;; a | d e
;; |
;; [o|o]--- c
;; |
;; b
;; "
(require 'cl-lib)
(defvar draw-tree-result "" "holds return value of draw-tree")
(defconst draw-tree-*nothing* (cons 'N '()))
(defconst draw-tree-*visited* (cons 'V '()))
(defun draw-tree-emptyp (x) (eq x draw-tree-*nothing*))
(defun draw-tree-visitedp (x) (eq (car x) draw-tree-*visited*))
(defun draw-tree-mark-visited (x) (cons draw-tree-*visited* x))
(defun draw-tree-members-of (x) (cdr x))
(defun draw-tree-donep (x)
(and (consp x) (draw-tree-visitedp x) (null (cdr x))))
(defun draw-tree-draw-fixed-string (s)
(let* ((b (make-string 8 ?\s))
(k (length s))
(s (if (> k 7) (substring s 0 7) s))
(s (if (< k 3) (concat " " s) s))
(k (length s)))
(setq draw-tree-result
(concat draw-tree-result (concat s (substring b 0 (- 8 k)))))))
(defun draw-tree-draw-atom (n)
(cond ((null n)
(draw-tree-draw-fixed-string "()"))
((symbolp n)
(draw-tree-draw-fixed-string (symbol-name n)))
((numberp n)
(draw-tree-draw-fixed-string (number-to-string n)))
((stringp n)
(draw-tree-draw-fixed-string (concat "\"" n "\"")))
((char-displayable-p n)
(draw-tree-draw-fixed-string (concat "\\" (string n))))
((not (eq n nil))
(draw-tree-draw-fixed-string "t"))
((eq n nil)
(draw-tree-draw-fixed-string "nil"))
(t
(error "draw-tree-draw-atom: unknown type" n))))
(defun draw-tree-draw-conses (n)
(cl-labels ((draw-tree-draw-conses (n r)
(cond ((not (consp n))
(draw-tree-draw-atom n)
(nreverse r))
((null (cdr n))
(setq draw-tree-result (concat draw-tree-result "[o|/]"))
(nreverse (cons (car n) r)))
(t
(setq draw-tree-result (concat draw-tree-result "[o|o]---"))
(draw-tree-draw-conses (cdr n) (cons (car n) r))))))
(draw-tree-draw-conses n '())))
(defun draw-tree-draw-bars (n)
(cl-labels ((draw-tree-draw-bars (n)
(cond ((not (consp n)) nil)
((draw-tree-emptyp (car n))
(draw-tree-draw-fixed-string "")
(draw-tree-draw-bars (cdr n)))
((and (consp (car n))
(draw-tree-visitedp (car n)))
(draw-tree-draw-bars (draw-tree-members-of (car n)))
(draw-tree-draw-bars (cdr n)))
(t
(draw-tree-draw-fixed-string "|")
(draw-tree-draw-bars (cdr n))))))
(draw-tree-draw-bars (draw-tree-members-of n))))
(defun draw-tree-skip-empty (n)
(if (and (consp n)
(or (draw-tree-emptyp (car n))
(draw-tree-donep (car n))))
(draw-tree-skip-empty (cdr n))
n))
(defun remove-trailing-nothing (n)
(reverse (draw-tree-skip-empty (reverse n))))
(defun draw-tree-all-verticalp (n)
(or (not (consp n))
(and (null (cdr n))
(draw-tree-all-verticalp (car n)))))
(defun draw-tree-draw-members (n)
(cl-labels ((draw-tree-draw-members (n r)
(cond ((not (consp n))
(draw-tree-mark-visited
(remove-trailing-nothing
(reverse r))))
((draw-tree-emptyp (car n))
(draw-tree-draw-fixed-string "")
(draw-tree-draw-members (cdr n)
(cons draw-tree-*nothing* r)))
((not (consp (car n)))
(draw-tree-draw-atom (car n))
(draw-tree-draw-members (cdr n)
(cons draw-tree-*nothing* r)))
((null (cdr n))
(draw-tree-draw-members (cdr n)
(cons (draw-tree-draw-final (car n)) r)))
((draw-tree-all-verticalp (car n))
(draw-tree-draw-fixed-string "[o|/]")
(draw-tree-draw-members (cdr n)
(cons (caar n) r)))
(t
(draw-tree-draw-fixed-string "|")
(draw-tree-draw-members (cdr n)
(cons (car n) r))))))
(draw-tree-draw-members (draw-tree-members-of n) '())))
(defun draw-tree-draw-final (n)
(cond ((not (consp n))
(draw-tree-draw-atom n)
draw-tree-*nothing*)
((draw-tree-visitedp n)
(draw-tree-draw-members n))
(t
(draw-tree-mark-visited (draw-tree-draw-conses n)))))
(defun draw-tree (n)
(setq draw-tree-result "\n")
(cl-labels ((draw-tree (n)
(if (not (draw-tree-donep n))
(progn (setq draw-tree-result (concat draw-tree-result "\n"))
(draw-tree-draw-bars n)
(setq draw-tree-result (concat draw-tree-result "\n"))
(draw-tree (draw-tree-draw-members n))))))
(if (not (consp n))
(draw-tree-draw-atom n)
(draw-tree (draw-tree-mark-visited (draw-tree-draw-conses n)))))
(setq draw-tree-result (concat draw-tree-result "\n"))
draw-tree-result)