Minor changes.
[egate.git] / fml / fmlrel.c
1 /*
2  * Copyright (c) 1995, the EUROPAGATE consortium (see below).
3  *
4  * The EUROPAGATE consortium members are:
5  *
6  *    University College Dublin
7  *    Danmarks Teknologiske Videnscenter
8  *    An Chomhairle Leabharlanna
9  *    Consejo Superior de Investigaciones Cientificas
10  *
11  * Permission to use, copy, modify, distribute, and sell this software and
12  * its documentation, in whole or in part, for any purpose, is hereby granted,
13  * provided that:
14  *
15  * 1. This copyright and permission notice appear in all copies of the
16  * software and its documentation. Notices of copyright or attribution
17  * which appear at the beginning of any file must remain unchanged.
18  *
19  * 2. The names of EUROPAGATE or the project partners may not be used to
20  * endorse or promote products derived from this software without specific
21  * prior written permission.
22  *
23  * 3. Users of this software (implementors and gateway operators) agree to
24  * inform the EUROPAGATE consortium of their use of the software. This
25  * information will be used to evaluate the EUROPAGATE project and the
26  * software, and to plan further developments. The consortium may use
27  * the information in later publications.
28  * 
29  * 4. Users of this software agree to make their best efforts, when
30  * documenting their use of the software, to acknowledge the EUROPAGATE
31  * consortium, and the role played by the software in their work.
32  *
33  * THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT WARRANTY OF ANY KIND,
34  * EXPRESS, IMPLIED, OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
35  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
36  * IN NO EVENT SHALL THE EUROPAGATE CONSORTIUM OR ITS MEMBERS BE LIABLE
37  * FOR ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF
38  * ANY KIND, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA
39  * OR PROFITS, WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND
40  * ON ANY THEORY OF LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE
41  * USE OR PERFORMANCE OF THIS SOFTWARE.
42  *
43  */
44 /*
45  * FML interpreter. Europagate, 1995
46  *
47  * $Log: fmlrel.c,v $
48  * Revision 1.4  1995/05/16 09:39:34  adam
49  * LICENSE.
50  *
51  * Revision 1.3  1995/02/23  08:32:06  adam
52  * Changed header.
53  *
54  * Revision 1.1  1995/02/09  14:33:37  adam
55  * Split source fml.c and define relevant build-in functions in separate
56  * files. New operators mult, div, not, llen implemented.
57  *
58  */
59
60 #include <assert.h>
61 #include <stdlib.h>
62 #include <stdio.h>
63
64 #include "fmlp.h"
65
66 static struct fml_node *fml_exec_gt (Fml fml, struct fml_node *l,
67                                      struct fml_node *r)
68 {
69     int left_val, right_val;
70     struct fml_node *fn;
71     fml_lr_values (fml, l, &left_val, r, &right_val);
72     if (left_val > right_val)
73     {
74         fn = fml_node_alloc (fml);
75         fn->is_atom = 1;
76         fn->p[0] = fml_atom_alloc (fml, "1");
77     }
78     else
79         fn = NULL;
80     return fn;
81 }
82
83 static struct fml_node *fml_exec_lt (Fml fml, struct fml_node *l,
84                                      struct fml_node *r)
85 {
86     int left_val, right_val;
87     struct fml_node *fn;
88     fml_lr_values (fml, l, &left_val, r, &right_val);
89     if (left_val < right_val)
90     {
91         fn = fml_node_alloc (fml);
92         fn->is_atom = 1;
93         fn->p[0] = fml_atom_alloc (fml, "1");
94     }
95     else
96         fn = NULL;
97     return fn;
98 }
99
100 static struct fml_node *fml_exec_eq (Fml fml, struct fml_node *l,
101                                      struct fml_node *r)
102 {
103     int left_val, right_val;
104     struct fml_node *fn;
105     fml_lr_values (fml, l, &left_val, r, &right_val);
106     if (left_val == right_val)
107     {
108         fn = fml_node_alloc (fml);
109         fn->is_atom = 1;
110         fn->p[0] = fml_atom_alloc (fml, "1");
111     }
112     else
113         fn = NULL;
114     return fn;
115 }
116
117 static struct fml_node *fml_exec_ne (Fml fml, struct fml_node *l,
118                                      struct fml_node *r)
119 {
120     int left_val, right_val;
121     struct fml_node *fn;
122     fml_lr_values (fml, l, &left_val, r, &right_val);
123     if (left_val != right_val)
124     {
125         fn = fml_node_alloc (fml);
126         fn->is_atom = 1;
127         fn->p[0] = fml_atom_alloc (fml, "1");
128     }
129     else
130         fn = NULL;
131     return fn;
132 }
133
134 static struct fml_node *fml_exec_le (Fml fml, struct fml_node *l,
135                                      struct fml_node *r)
136 {
137     int left_val, right_val;
138     struct fml_node *fn;
139     fml_lr_values (fml, l, &left_val, r, &right_val);
140     if (left_val <= right_val)
141     {
142         fn = fml_node_alloc (fml);
143         fn->is_atom = 1;
144         fn->p[0] = fml_atom_alloc (fml, "1");
145     }
146     else
147         fn = NULL;
148     return fn;
149 }
150
151 static struct fml_node *fml_exec_ge (Fml fml, struct fml_node *l,
152                                      struct fml_node *r)
153 {
154     int left_val, right_val;
155     struct fml_node *fn;
156     fml_lr_values (fml, l, &left_val, r, &right_val);
157     if (left_val >= right_val)
158     {
159         fn = fml_node_alloc (fml);
160         fn->is_atom = 1;
161         fn->p[0] = fml_atom_alloc (fml, "1");
162     }
163     else
164         fn = NULL;
165     return fn;
166 }
167
168 static struct fml_node *fml_exec_and (Fml fml, struct fml_node *l,
169                                       struct fml_node *r)
170 {
171     if (l && r)
172     {
173         fml_node_delete (fml, l);
174         return r;
175     }
176     fml_node_delete (fml, l);
177     fml_node_delete (fml, r);
178     return NULL;
179 }
180
181 static struct fml_node *fml_exec_or (Fml fml, struct fml_node *l,
182                                       struct fml_node *r)
183 {
184     if (r)
185     {
186         fml_node_delete (fml, l);
187         return r;
188     }
189     return l;
190 }
191
192 static struct fml_node *fml_exec_not (Fml fml, struct fml_node **lp, 
193                                       struct token *tp)
194 {
195     struct fml_node *fn;
196     fml_cmd_lex (lp, tp);
197
198     fn = fml_expr_term (fml, lp, tp);
199     if (fn)
200     {
201         fml_node_delete (fml, fn);
202         return NULL;
203     }
204     fn = fml_node_alloc (fml);
205     fn->is_atom = 1;
206     fn->p[0] = fml_atom_alloc (fml, "1");
207     return fn;
208 }
209
210 void fml_rel_init (Fml fml)
211 {
212     struct fml_sym_info *sym_info;
213
214     sym_info = fml_sym_add (fml->sym_tab, "gt");
215     sym_info->kind = FML_CBINARY;
216     sym_info->binary = fml_exec_gt;
217     sym_info = fml_sym_add (fml->sym_tab, "lt");
218     sym_info->kind = FML_CBINARY;
219     sym_info->binary = fml_exec_lt;
220     sym_info = fml_sym_add (fml->sym_tab, "eq");
221     sym_info->kind = FML_CBINARY;
222     sym_info->binary = fml_exec_eq;
223
224     sym_info = fml_sym_add (fml->sym_tab, "ge");
225     sym_info->kind = FML_CBINARY;
226     sym_info->binary = fml_exec_ge;
227     sym_info = fml_sym_add (fml->sym_tab, "le");
228     sym_info->kind = FML_CBINARY;
229     sym_info->binary = fml_exec_le;
230     sym_info = fml_sym_add (fml->sym_tab, "ne");
231     sym_info->kind = FML_CBINARY;
232     sym_info->binary = fml_exec_ne;
233
234     sym_info = fml_sym_add (fml->sym_tab, "and");
235     sym_info->kind = FML_CBINARY;
236     sym_info->binary = fml_exec_and;
237     sym_info = fml_sym_add (fml->sym_tab, "or");
238     sym_info->kind = FML_CBINARY;
239     sym_info->binary = fml_exec_or;
240
241     sym_info = fml_sym_add (fml->sym_tab, "not");
242     sym_info->kind = FML_CPREFIX;
243     sym_info->prefix = fml_exec_not;
244 }