(ctree-find-calist): Delete duplicated result.
[elisp/apel.git] / calist.el
1 ;;; calist.el --- Condition functions
2
3 ;; Copyright (C) 1998 MORIOKA Tomohiko.
4
5 ;; Author: MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: condition, alist, tree
7
8 ;; This file is part of APEL (A Portable Emacs Library).
9
10 ;; This program is free software; you can redistribute it and/or
11 ;; modify it under the terms of the GNU General Public License as
12 ;; published by the Free Software Foundation; either version 2, or (at
13 ;; your option) any later version.
14
15 ;; This program is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Code:
26
27 (eval-when-compile (require 'cl))
28
29 (defvar calist-field-match-method-obarray [nil])
30
31 (defun define-calist-field-match-method (field-type function)
32   "Set field-match-method for FIELD-TYPE to FUNCTION."
33   (fset (intern (symbol-name field-type) calist-field-match-method-obarray)
34         function))
35
36 (defun calist-default-field-match-method (calist field-type field-value)
37   (let ((s-field (assoc field-type calist)))
38     (cond ((null s-field)
39            (cons (cons field-type field-value) calist)
40            )
41           ((eq field-value t)
42            calist)
43           ((equal (cdr s-field) field-value)
44            calist))))
45
46 (defsubst calist-field-match-method (field-type)
47   (condition-case nil
48       (symbol-function
49        (intern-soft
50         (symbol-name field-type) calist-field-match-method-obarray))
51     (error (symbol-function 'calist-default-field-match-method))
52     ))
53
54 (defsubst calist-field-match (calist field-type field-value)
55   (funcall (calist-field-match-method field-type)
56            calist field-type field-value))
57
58 (defun ctree-match-calist (rule-tree alist)
59   "Return matched condition-alist if ALIST matches RULE-TREE."
60   (if (null rule-tree)
61       alist
62     (let ((type (car rule-tree))
63           (choices (cdr rule-tree))
64           default)
65       (catch 'tag
66         (while choices
67           (let* ((choice (car choices))
68                  (choice-value (car choice)))
69             (if (eq choice-value t)
70                 (setq default choice)
71               (let ((ret-alist (calist-field-match alist type (car choice))))
72                 (if ret-alist
73                     (throw 'tag
74                            (if (cdr choice)
75                                (ctree-match-calist (cdr choice) ret-alist)
76                              ret-alist))
77                   ))))
78           (setq choices (cdr choices)))
79         (if default
80             (let ((ret-alist (calist-field-match alist type t)))
81               (if ret-alist
82                   (if (cdr default)
83                       (ctree-match-calist (cdr default) ret-alist)
84                     ret-alist))))
85         ))))
86
87 (defun ctree-match-calist-partially (rule-tree alist)
88   "Return matched condition-alist if ALIST matches RULE-TREE."
89   (if (null rule-tree)
90       alist
91     (let ((type (car rule-tree))
92           (choices (cdr rule-tree))
93           default)
94       (catch 'tag
95         (while choices
96           (let* ((choice (car choices))
97                  (choice-value (car choice)))
98             (if (eq choice-value t)
99                 (setq default choice)
100               (let ((ret-alist (calist-field-match alist type (car choice))))
101                 (if ret-alist
102                     (throw 'tag
103                            (if (cdr choice)
104                                (ctree-match-calist-partially
105                                 (cdr choice) ret-alist)
106                              ret-alist))
107                   ))))
108           (setq choices (cdr choices)))
109         (if default
110             (let ((ret-alist (calist-field-match alist type t)))
111               (if ret-alist
112                   (if (cdr default)
113                       (ctree-match-calist-partially (cdr default) ret-alist)
114                     ret-alist)))
115           (calist-field-match alist type t))
116         ))))
117
118 (defun ctree-find-calist (rule-tree alist &optional all)
119   "Return list of condition-alist which matches ALIST in RULE-TREE.
120 If optional argument ALL is specified, default rules are not ignored
121 even if other rules are matched for ALIST."
122   (if (null rule-tree)
123       (list alist)
124     (let ((type (car rule-tree))
125           (choices (cdr rule-tree))
126           default dest)
127       (while choices
128         (let* ((choice (car choices))
129                (choice-value (car choice)))
130           (if (eq choice-value t)
131               (setq default choice)
132             (let ((ret-alist (calist-field-match alist type (car choice))))
133               (if ret-alist
134                   (if (cdr choice)
135                       (setq dest
136                             (append (ctree-find-calist
137                                      (cdr choice) ret-alist all)
138                                     dest))
139                     (or (member ret-alist dest)
140                         (setq dest (cons ret-alist dest)))
141                     )))))
142         (setq choices (cdr choices)))
143       (or (and (not all) dest)
144           (if default
145               (let ((ret-alist (calist-field-match alist type t)))
146                 (if ret-alist
147                     (if (cdr default)
148                         (setq dest
149                               (append (ctree-find-calist
150                                        (cdr default) ret-alist all)
151                                       dest))
152                       (or (member ret-alist dest)
153                           (setq dest (cons ret-alist dest)))
154                       ))))
155           )
156       dest)))
157
158 (defun calist-to-ctree (calist)
159   "Convert condition-alist CALIST to condition-tree."
160   (if calist
161       (let* ((cell (car calist)))
162         (cons (car cell)
163               (list (cons (cdr cell)
164                           (calist-to-ctree (cdr calist))
165                           ))))))
166
167 (defun ctree-add-calist-strictly (ctree calist)
168   "Add condition CALIST to condition-tree CTREE without default clause."
169   (cond ((null calist) ctree)
170         ((null ctree)
171          (calist-to-ctree calist)
172          )
173         (t
174          (let* ((type (car ctree))
175                 (values (cdr ctree))
176                 (ret (assoc type calist)))
177            (if ret
178                (catch 'tag
179                  (while values
180                    (let ((cell (car values)))
181                      (if (equal (car cell)(cdr ret))
182                          (throw 'tag
183                                 (setcdr cell
184                                         (ctree-add-calist-strictly
185                                          (cdr cell)
186                                          (delete ret (copy-alist calist)))
187                                         ))))
188                    (setq values (cdr values)))
189                  (setcdr ctree (cons (cons (cdr ret)
190                                            (calist-to-ctree
191                                             (delete ret (copy-alist calist))))
192                                      (cdr ctree)))
193                  )
194              (catch 'tag
195                (while values
196                  (let ((cell (car values)))
197                    (setcdr cell
198                            (ctree-add-calist-strictly (cdr cell) calist))
199                    )
200                  (setq values (cdr values))))
201              )
202            ctree))))
203
204 (defun ctree-add-calist-with-default (ctree calist)
205   "Add condition CALIST to condition-tree CTREE with default clause."
206   (cond ((null calist) ctree)
207         ((null ctree)
208          (let* ((cell (car calist))
209                 (type (car cell))
210                 (value (cdr cell)))
211            (cons type
212                  (list '(t)
213                        (cons value (calist-to-ctree (cdr calist)))))
214            ))
215         (t
216          (let* ((type (car ctree))
217                 (values (cdr ctree))
218                 (ret (assoc type calist)))
219            (if ret
220                (catch 'tag
221                  (while values
222                    (let ((cell (car values)))
223                      (if (equal (car cell)(cdr ret))
224                          (throw 'tag
225                                 (setcdr cell
226                                         (ctree-add-calist-with-default
227                                          (cdr cell)
228                                          (delete ret (copy-alist calist)))
229                                         ))))
230                    (setq values (cdr values)))
231                  (setcdr ctree (list* '(t)
232                                       (cons (cdr ret)
233                                             (calist-to-ctree
234                                              (delete ret (copy-alist calist))))
235                                       (cdr ctree)))
236                  )
237              (catch 'tag
238                (while values
239                  (let ((cell (car values)))
240                    (setcdr cell
241                            (ctree-add-calist-with-default (cdr cell) calist))
242                    )
243                  (setq values (cdr values)))
244                (let ((elt (cons t (calist-to-ctree calist))))
245                  (or (member elt (cdr ctree))
246                      (setcdr ctree (cons elt (cdr ctree)))
247                      )))
248              )
249            ctree))))
250
251 (defun ctree-set-calist-strictly (ctree-var calist)
252   "Set condition CALIST in CTREE-VAR without default clause."
253   (set ctree-var
254        (ctree-add-calist-strictly (symbol-value ctree-var) calist)))
255
256 (defun ctree-set-calist-with-default (ctree-var calist)
257   "Set condition CALIST to CTREE-VAR with default clause."
258   (set ctree-var
259        (ctree-add-calist-with-default (symbol-value ctree-var) calist)))
260
261
262 ;;; @ end
263 ;;;
264
265 (provide 'calist)
266
267 ;;; calist.el ends here