2 * FML interpreter. Europagate, 1995
5 * Revision 1.10 1995/03/02 08:06:03 adam
6 * Fml function strsub implemented. New test files marc[45].fml.
7 * New test options in fmltest.
9 * Revision 1.9 1995/02/27 09:01:20 adam
10 * Regular expression support. Argument passing by name option. New FML
13 * Revision 1.8 1995/02/23 08:32:05 adam
16 * Revision 1.6 1995/02/10 18:15:52 adam
17 * FML function 'strcmp' implemented. This function can be used to
18 * test for existence of MARC fields.
20 * Revision 1.5 1995/02/09 14:37:18 adam
21 * Removed .depend from cvs. Removed function fml_mk_list.
23 * Revision 1.4 1995/02/09 14:33:37 adam
24 * Split source fml.c and define relevant build-in functions in separate
25 * files. New operators mult, div, not, llen implemented.
27 * Revision 1.3 1995/02/09 13:07:15 adam
28 * Nodes are freed now. Many bugs fixed.
30 * Revision 1.2 1995/02/06 15:23:26 adam
31 * Added some more relational operators (le,ne,ge). Added increment
32 * and decrement operators. Function index changed, so that first
33 * element is 1 - not 0. Function fml_atom_val edited.
35 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
36 * First version of the FML interpreter. It's slow and memory isn't
37 * freed properly. In particular, the FML nodes aren't released yet.
47 #define FML_ATOM_CHUNK 1024
48 #define FML_NODE_CHUNK 1024
50 static int no_nodes = 0;
51 static int no_atoms = 0;
53 struct fml_node *fml_node_alloc (Fml fml)
57 if (! fml->node_free_list)
61 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
64 (*fml->err_handle)(FML_ERR_NOMEM);
67 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
71 n = fml->node_free_list;
72 fml->node_free_list = n->p[1];
73 n->p[0] = n->p[1] = NULL;
79 static struct fml_atom *atom_malloc (Fml fml)
83 if (! fml->atom_free_list)
87 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
90 (*fml->err_handle)(FML_ERR_NOMEM);
93 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
97 fa = fml->atom_free_list;
98 fml->atom_free_list = fa->next;
103 static void atom_delete (Fml fml, struct fml_atom *a)
105 a->next = fml->atom_free_list;
106 fml->atom_free_list = a;
110 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
112 struct fml_atom *a0, *a1;
114 a0 = a1 = atom_malloc (fml);
117 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
121 a1 = a1->next = atom_malloc (fml);
127 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
130 struct fml_atom *a, *a0;
132 a0 = a = atom_malloc (fml);
133 strncpy (a->buf, str, FML_ATOM_BUF);
134 while (strlen (str+soff) >= FML_ATOM_BUF)
138 an = atom_malloc (fml);
140 soff += FML_ATOM_BUF;
141 strncpy (an->buf, str+soff, FML_ATOM_BUF);
148 int fml_atom_cmp (Fml fml, struct fml_atom *a1, struct fml_atom *a2)
153 n = strncmp (a1->buf, a2->buf, FML_ATOM_BUF);
166 int fml_atom_str (struct fml_atom *a, char *str)
174 memcpy (str+len, a->buf, FML_ATOM_BUF);
179 strcpy (str+len, a->buf);
180 len += strlen(str+len);
184 void fml_atom_strx (struct fml_atom *a, char *str, int max)
189 while (a->next && len < max - 2*FML_ATOM_BUF)
191 memcpy (str+len, a->buf, FML_ATOM_BUF);
195 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
196 str[len+FML_ATOM_BUF-1] = '\0';
199 int fml_atom_len (struct fml_atom *a)
209 len += strlen (a->buf);
214 int fml_atom_val (struct fml_atom *a)
216 static char arg[256];
219 return atoi (a->buf);
220 fml_atom_strx (a, arg, 200);
224 struct fml_node *fml_mk_node_val (Fml fml, int val)
229 sprintf (arg, "%d", val);
230 fn = fml_node_alloc (fml);
232 fn->p[0] = fml_atom_alloc (fml, arg);
236 void fml_node_delete (Fml fml, struct fml_node *fn)
242 atom_delete (fml, fn->p[0]);
244 fml_node_delete (fml, fn->p[0]);
247 fn->p[1] = fml->node_free_list;
248 fml->node_free_list = fn;
255 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
257 struct fml_node *fn0, *fn1;
261 fn1 = fn0 = fml_node_alloc (fml);
267 fn1->p[0] = atom_copy (fml, fn->p[0]);
270 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
274 fn1 = fn1->p[1] = fml_node_alloc (fml);
279 void fml_node_stat (Fml fml)
282 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);
285 struct fml_atom *fml_atom_strsub (Fml fml, struct fml_atom *a, int o, int l)
287 static char buf[512];
290 int ol = fml_atom_len (a);
294 cp = malloc (ol + 1);
299 fml_atom_str (a, buf);
302 an = fml_atom_alloc (fml, buf+o);