2 * FML interpreter. Europagate, 1995
5 * Revision 1.9 1995/02/27 09:01:20 adam
6 * Regular expression support. Argument passing by name option. New FML
9 * Revision 1.8 1995/02/23 08:32:05 adam
12 * Revision 1.6 1995/02/10 18:15:52 adam
13 * FML function 'strcmp' implemented. This function can be used to
14 * test for existence of MARC fields.
16 * Revision 1.5 1995/02/09 14:37:18 adam
17 * Removed .depend from cvs. Removed function fml_mk_list.
19 * Revision 1.4 1995/02/09 14:33:37 adam
20 * Split source fml.c and define relevant build-in functions in separate
21 * files. New operators mult, div, not, llen implemented.
23 * Revision 1.3 1995/02/09 13:07:15 adam
24 * Nodes are freed now. Many bugs fixed.
26 * Revision 1.2 1995/02/06 15:23:26 adam
27 * Added some more relational operators (le,ne,ge). Added increment
28 * and decrement operators. Function index changed, so that first
29 * element is 1 - not 0. Function fml_atom_val edited.
31 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
32 * First version of the FML interpreter. It's slow and memory isn't
33 * freed properly. In particular, the FML nodes aren't released yet.
43 #define FML_ATOM_CHUNK 1024
44 #define FML_NODE_CHUNK 1024
46 static int no_nodes = 0;
47 static int no_atoms = 0;
49 struct fml_node *fml_node_alloc (Fml fml)
53 if (! fml->node_free_list)
57 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
60 (*fml->err_handle)(FML_ERR_NOMEM);
63 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
67 n = fml->node_free_list;
68 fml->node_free_list = n->p[1];
69 n->p[0] = n->p[1] = NULL;
75 static struct fml_atom *atom_malloc (Fml fml)
79 if (! fml->atom_free_list)
83 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
86 (*fml->err_handle)(FML_ERR_NOMEM);
89 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
93 fa = fml->atom_free_list;
94 fml->atom_free_list = fa->next;
99 static void atom_delete (Fml fml, struct fml_atom *a)
101 a->next = fml->atom_free_list;
102 fml->atom_free_list = a;
106 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
108 struct fml_atom *a0, *a1;
110 a0 = a1 = atom_malloc (fml);
113 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
117 a1 = a1->next = atom_malloc (fml);
123 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
126 struct fml_atom *a, *a0;
128 a0 = a = atom_malloc (fml);
129 strncpy (a->buf, str, FML_ATOM_BUF);
130 while (strlen (str+soff) >= FML_ATOM_BUF)
134 an = atom_malloc (fml);
136 soff += FML_ATOM_BUF;
137 strncpy (an->buf, str+soff, FML_ATOM_BUF);
144 int fml_atom_cmp (Fml fml, struct fml_atom *a1, struct fml_atom *a2)
149 n = strncmp (a1->buf, a2->buf, FML_ATOM_BUF);
162 int fml_atom_str (struct fml_atom *a, char *str)
170 memcpy (str+len, a->buf, FML_ATOM_BUF);
175 strcpy (str+len, a->buf);
176 len += strlen(str+len);
180 void fml_atom_strx (struct fml_atom *a, char *str, int max)
185 while (a->next && len < max - 2*FML_ATOM_BUF)
187 memcpy (str+len, a->buf, FML_ATOM_BUF);
191 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
192 str[len+FML_ATOM_BUF-1] = '\0';
195 int fml_atom_len (struct fml_atom *a)
205 len += strlen (a->buf);
210 int fml_atom_val (struct fml_atom *a)
212 static char arg[256];
215 return atoi (a->buf);
216 fml_atom_strx (a, arg, 200);
220 struct fml_node *fml_mk_node_val (Fml fml, int val)
225 sprintf (arg, "%d", val);
226 fn = fml_node_alloc (fml);
228 fn->p[0] = fml_atom_alloc (fml, arg);
232 void fml_node_delete (Fml fml, struct fml_node *fn)
238 atom_delete (fml, fn->p[0]);
240 fml_node_delete (fml, fn->p[0]);
243 fn->p[1] = fml->node_free_list;
244 fml->node_free_list = fn;
251 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
253 struct fml_node *fn0, *fn1;
257 fn1 = fn0 = fml_node_alloc (fml);
263 fn1->p[0] = atom_copy (fml, fn->p[0]);
266 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
270 fn1 = fn1->p[1] = fml_node_alloc (fml);
275 void fml_node_stat (Fml fml)
278 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);