2 * FML interpreter. Europagate, 1995
5 * Revision 1.11 1995/03/02 10:18:48 adam
8 * Revision 1.10 1995/03/02 08:06:03 adam
9 * Fml function strsub implemented. New test files marc[45].fml.
10 * New test options in fmltest.
12 * Revision 1.9 1995/02/27 09:01:20 adam
13 * Regular expression support. Argument passing by name option. New FML
16 * Revision 1.8 1995/02/23 08:32:05 adam
19 * Revision 1.6 1995/02/10 18:15:52 adam
20 * FML function 'strcmp' implemented. This function can be used to
21 * test for existence of MARC fields.
23 * Revision 1.5 1995/02/09 14:37:18 adam
24 * Removed .depend from cvs. Removed function fml_mk_list.
26 * Revision 1.4 1995/02/09 14:33:37 adam
27 * Split source fml.c and define relevant build-in functions in separate
28 * files. New operators mult, div, not, llen implemented.
30 * Revision 1.3 1995/02/09 13:07:15 adam
31 * Nodes are freed now. Many bugs fixed.
33 * Revision 1.2 1995/02/06 15:23:26 adam
34 * Added some more relational operators (le,ne,ge). Added increment
35 * and decrement operators. Function index changed, so that first
36 * element is 1 - not 0. Function fml_atom_val edited.
38 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
39 * First version of the FML interpreter. It's slow and memory isn't
40 * freed properly. In particular, the FML nodes aren't released yet.
50 #define FML_ATOM_CHUNK 1024
51 #define FML_NODE_CHUNK 1024
53 static int no_nodes = 0;
54 static int no_atoms = 0;
56 struct fml_node *fml_node_alloc (Fml fml)
60 if (! fml->node_free_list)
64 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
67 (*fml->err_handle)(FML_ERR_NOMEM);
70 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
74 n = fml->node_free_list;
75 fml->node_free_list = n->p[1];
76 n->p[0] = n->p[1] = NULL;
82 static struct fml_atom *atom_malloc (Fml fml)
86 if (! fml->atom_free_list)
90 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
93 (*fml->err_handle)(FML_ERR_NOMEM);
96 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
100 fa = fml->atom_free_list;
101 fml->atom_free_list = fa->next;
106 static void atom_delete (Fml fml, struct fml_atom *a)
108 a->next = fml->atom_free_list;
109 fml->atom_free_list = a;
113 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
115 struct fml_atom *a0, *a1;
117 a0 = a1 = atom_malloc (fml);
120 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
124 a1 = a1->next = atom_malloc (fml);
130 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
133 struct fml_atom *a, *a0;
135 a0 = a = atom_malloc (fml);
136 strncpy (a->buf, str, FML_ATOM_BUF);
137 while (strlen (str+soff) >= FML_ATOM_BUF)
141 an = atom_malloc (fml);
143 soff += FML_ATOM_BUF;
144 strncpy (an->buf, str+soff, FML_ATOM_BUF);
151 int fml_atom_cmp (Fml fml, struct fml_atom *a1, struct fml_atom *a2)
156 n = strncmp (a1->buf, a2->buf, FML_ATOM_BUF);
169 int fml_atom_str (struct fml_atom *a, char *str)
177 memcpy (str+len, a->buf, FML_ATOM_BUF);
181 for (i=0; i<FML_ATOM_BUF && a->buf[i]; i++)
185 memcpy (str+len, a->buf, i);
192 void fml_atom_strx (struct fml_atom *a, char *str, int max)
197 while (a->next && len < max - 2*FML_ATOM_BUF)
199 memcpy (str+len, a->buf, FML_ATOM_BUF);
203 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
204 str[len+FML_ATOM_BUF-1] = '\0';
207 int fml_atom_len (struct fml_atom *a)
217 len += strlen (a->buf);
222 int fml_atom_val (struct fml_atom *a)
224 static char arg[256];
227 return atoi (a->buf);
228 fml_atom_strx (a, arg, 200);
232 struct fml_node *fml_mk_node_val (Fml fml, int val)
237 sprintf (arg, "%d", val);
238 fn = fml_node_alloc (fml);
240 fn->p[0] = fml_atom_alloc (fml, arg);
244 void fml_node_delete (Fml fml, struct fml_node *fn)
250 atom_delete (fml, fn->p[0]);
252 fml_node_delete (fml, fn->p[0]);
255 fn->p[1] = fml->node_free_list;
256 fml->node_free_list = fn;
263 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
265 struct fml_node *fn0, *fn1;
269 fn1 = fn0 = fml_node_alloc (fml);
275 fn1->p[0] = atom_copy (fml, fn->p[0]);
278 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
282 fn1 = fn1->p[1] = fml_node_alloc (fml);
287 void fml_node_stat (Fml fml)
290 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);
293 struct fml_atom *fml_atom_strsub (Fml fml, struct fml_atom *a, int o, int l)
295 static char buf[512];
298 int ol = fml_atom_len (a);
302 cp = malloc (ol + 1);
307 fml_atom_str (a, buf);
310 an = fml_atom_alloc (fml, buf+o);