(ctree-match-calist-all): New function.
[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-all (rule-tree alist)
88   "Return matched condition-alist if ALIST matches RULE-TREE."
89   (if (null rule-tree)
90       (list alist)
91     (let ((type (car rule-tree))
92           (choices (cdr rule-tree))
93           default dest)
94       (while choices
95         (let* ((choice (car choices))
96                (choice-value (car choice)))
97           (if (eq choice-value t)
98               (setq default choice)
99             (let ((ret-alist (calist-field-match alist type (car choice))))
100               (if ret-alist
101                   (if (cdr choice)
102                       (setq dest
103                             (append
104                              (ctree-match-calist-all (cdr choice) ret-alist)
105                              dest))
106                     (setq dest (cons ret-alist dest))
107                     )))))
108         (setq choices (cdr choices)))
109       (or dest
110           (if default
111               (let ((ret-alist (calist-field-match alist type t)))
112                 (if ret-alist
113                     (if (cdr default)
114                         (ctree-match-calist-all (cdr default) ret-alist)
115                       (list ret-alist)
116                       ))))))))
117
118 (defun calist-to-ctree (calist)
119   "Convert condition-alist CALIST to condition-tree."
120   (if calist
121       (let* ((cell (car calist)))
122         (cons (car cell)
123               (list (cons (cdr cell)
124                           (calist-to-ctree (cdr calist))
125                           ))))))
126
127 (defun ctree-add-calist-strictly (ctree calist)
128   "Add condition CALIST to condition-tree CTREE without default clause."
129   (cond ((null calist) ctree)
130         ((null ctree)
131          (calist-to-ctree calist)
132          )
133         (t
134          (let* ((type (car ctree))
135                 (values (cdr ctree))
136                 (ret (assoc type calist)))
137            (if ret
138                (catch 'tag
139                  (while values
140                    (let ((cell (car values)))
141                      (if (equal (car cell)(cdr ret))
142                          (throw 'tag
143                                 (setcdr cell
144                                         (ctree-add-calist-strictly
145                                          (cdr cell)
146                                          (delete ret (copy-alist calist)))
147                                         ))))
148                    (setq values (cdr values)))
149                  (setcdr ctree (cons (cons (cdr ret)
150                                            (calist-to-ctree
151                                             (delete ret (copy-alist calist))))
152                                      (cdr ctree)))
153                  )
154              (catch 'tag
155                (while values
156                  (let ((cell (car values)))
157                    (setcdr cell
158                            (ctree-add-calist-strictly (cdr cell) calist))
159                    )
160                  (setq values (cdr values))))
161              )
162            ctree))))
163
164 (defun ctree-add-calist-with-default (ctree calist)
165   "Add condition CALIST to condition-tree CTREE with default clause."
166   (cond ((null calist) ctree)
167         ((null ctree)
168          (let* ((cell (car calist))
169                 (type (car cell))
170                 (value (cdr cell)))
171            (cons type
172                  (list '(t)
173                        (cons value (calist-to-ctree (cdr calist)))))
174            ))
175         (t
176          (let* ((type (car ctree))
177                 (values (cdr ctree))
178                 (ret (assoc type calist)))
179            (if ret
180                (catch 'tag
181                  (while values
182                    (let ((cell (car values)))
183                      (if (equal (car cell)(cdr ret))
184                          (throw 'tag
185                                 (setcdr cell
186                                         (ctree-add-calist-with-default
187                                          (cdr cell)
188                                          (delete ret (copy-alist calist)))
189                                         ))))
190                    (setq values (cdr values)))
191                  (setcdr ctree (list* '(t)
192                                       (cons (cdr ret)
193                                             (calist-to-ctree
194                                              (delete ret (copy-alist calist))))
195                                       (cdr ctree)))
196                  )
197              (catch 'tag
198                (while values
199                  (let ((cell (car values)))
200                    (setcdr cell
201                            (ctree-add-calist-with-default (cdr cell) calist))
202                    )
203                  (setq values (cdr values)))
204                (let ((elt (cons t (calist-to-ctree calist))))
205                  (or (member elt (cdr ctree))
206                      (setcdr ctree (cons elt (cdr ctree)))
207                      )))
208              )
209            ctree))))
210
211 (defun ctree-set-calist-strictly (ctree-var calist)
212   "Set condition CALIST in CTREE-VAR without default clause."
213   (set ctree-var
214        (ctree-add-calist-strictly (symbol-value ctree-var) calist)))
215
216 (defun ctree-set-calist-with-default (ctree-var calist)
217   "Set condition CALIST to CTREE-VAR with default clause."
218   (set ctree-var
219        (ctree-add-calist-with-default (symbol-value ctree-var) calist)))
220
221
222 ;;; @ end
223 ;;;
224
225 (provide 'calist)
226
227 ;;; calist.el ends here