(ctree-match-calist): Prefer normal choice than default choice.
[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 calist-to-ctree (calist)
88   "Convert condition-alist CALIST to condition-tree."
89   (if calist
90       (let* ((cell (car calist)))
91         (cons (car cell)
92               (list (cons (cdr cell)
93                           (calist-to-ctree (cdr calist))
94                           ))))))
95
96 (defun ctree-add-calist-strictly (ctree calist)
97   "Add condition CALIST to condition-tree CTREE without default clause."
98   (cond ((null calist) ctree)
99         ((null ctree)
100          (calist-to-ctree calist)
101          )
102         (t
103          (let* ((type (car ctree))
104                 (values (cdr ctree))
105                 (ret (assoc type calist)))
106            (if ret
107                (catch 'tag
108                  (while values
109                    (let ((cell (car values)))
110                      (if (equal (car cell)(cdr ret))
111                          (throw 'tag
112                                 (setcdr cell
113                                         (ctree-add-calist-strictly
114                                          (cdr cell)
115                                          (delete ret (copy-alist calist)))
116                                         ))))
117                    (setq values (cdr values)))
118                  (setcdr ctree (cons (cons (cdr ret)
119                                            (calist-to-ctree
120                                             (delete ret (copy-alist calist))))
121                                      (cdr ctree)))
122                  )
123              (catch 'tag
124                (while values
125                  (let ((cell (car values)))
126                    (setcdr cell
127                            (ctree-add-calist-strictly (cdr cell) calist))
128                    )
129                  (setq values (cdr values))))
130              )
131            ctree))))
132
133 (defun ctree-add-calist-with-default (ctree calist)
134   "Add condition CALIST to condition-tree CTREE with default clause."
135   (cond ((null calist) ctree)
136         ((null ctree)
137          (let* ((cell (car calist))
138                 (type (car cell))
139                 (value (cdr cell)))
140            (cons type
141                  (list '(t)
142                        (cons value (calist-to-ctree (cdr calist)))))
143            ))
144         (t
145          (let* ((type (car ctree))
146                 (values (cdr ctree))
147                 (ret (assoc type calist)))
148            (if ret
149                (catch 'tag
150                  (while values
151                    (let ((cell (car values)))
152                      (if (equal (car cell)(cdr ret))
153                          (throw 'tag
154                                 (setcdr cell
155                                         (ctree-add-calist-with-default
156                                          (cdr cell)
157                                          (delete ret (copy-alist calist)))
158                                         ))))
159                    (setq values (cdr values)))
160                  (setcdr ctree (list* '(t)
161                                       (cons (cdr ret)
162                                             (calist-to-ctree
163                                              (delete ret (copy-alist calist))))
164                                       (cdr ctree)))
165                  )
166              (catch 'tag
167                (while values
168                  (let ((cell (car values)))
169                    (setcdr cell
170                            (ctree-add-calist-with-default (cdr cell) calist))
171                    )
172                  (setq values (cdr values)))
173                (let ((elt (cons t (calist-to-ctree calist))))
174                  (or (member elt (cdr ctree))
175                      (setcdr ctree (cons elt (cdr ctree)))
176                      )))
177              )
178            ctree))))
179
180 (defun ctree-set-calist-strictly (ctree-var calist)
181   "Set condition CALIST in CTREE-VAR without default clause."
182   (set ctree-var
183        (ctree-add-calist-strictly (symbol-value ctree-var) calist)))
184
185 (defun ctree-set-calist-with-default (ctree-var calist)
186   "Set condition CALIST to CTREE-VAR with default clause."
187   (set ctree-var
188        (ctree-add-calist-with-default (symbol-value ctree-var) calist)))
189
190
191 ;;; @ end
192 ;;;
193
194 (provide 'calist)
195
196 ;;; calist.el ends here