2 * FML interpreter. Europagate, 1995
5 * Revision 1.6 1995/02/10 18:15:52 adam
6 * FML function 'strcmp' implemented. This function can be used to
7 * test for existence of MARC fields.
9 * Revision 1.5 1995/02/09 14:37:18 adam
10 * Removed .depend from cvs. Removed function fml_mk_list.
12 * Revision 1.4 1995/02/09 14:33:37 adam
13 * Split source fml.c and define relevant build-in functions in separate
14 * files. New operators mult, div, not, llen implemented.
16 * Revision 1.3 1995/02/09 13:07:15 adam
17 * Nodes are freed now. Many bugs fixed.
19 * Revision 1.2 1995/02/06 15:23:26 adam
20 * Added some more relational operators (le,ne,ge). Added increment
21 * and decrement operators. Function index changed, so that first
22 * element is 1 - not 0. Function fml_atom_val edited.
24 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
25 * First version of the FML interpreter. It's slow and memory isn't
26 * freed properly. In particular, the FML nodes aren't released yet.
36 #define FML_ATOM_CHUNK 1024
37 #define FML_NODE_CHUNK 1024
39 static int no_nodes = 0;
40 static int no_atoms = 0;
42 struct fml_node *fml_node_alloc (Fml fml)
46 if (! fml->node_free_list)
50 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
53 (*fml->err_handle)(FML_ERR_NOMEM);
56 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
60 n = fml->node_free_list;
61 fml->node_free_list = n->p[1];
62 n->p[0] = n->p[1] = NULL;
68 static struct fml_atom *atom_malloc (Fml fml)
72 if (! fml->atom_free_list)
76 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
79 (*fml->err_handle)(FML_ERR_NOMEM);
82 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
86 fa = fml->atom_free_list;
87 fml->atom_free_list = fa->next;
92 static void atom_delete (Fml fml, struct fml_atom *a)
94 a->next = fml->atom_free_list;
95 fml->atom_free_list = a;
99 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
101 struct fml_atom *a0, *a1;
103 a0 = a1 = atom_malloc (fml);
106 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
110 a1 = a1->next = atom_malloc (fml);
116 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
119 struct fml_atom *a, *a0;
121 a0 = a = atom_malloc (fml);
122 strncpy (a->buf, str, FML_ATOM_BUF);
123 while (strlen (str+soff) >= FML_ATOM_BUF)
127 an = atom_malloc (fml);
129 soff += FML_ATOM_BUF;
130 strncpy (an->buf, str+soff, FML_ATOM_BUF);
137 int fml_atom_cmp (Fml fml, struct fml_atom *a1, struct fml_atom *a2)
142 n = strncmp (a1->buf, a2->buf, FML_ATOM_BUF);
155 int fml_atom_str (struct fml_atom *a, char *str)
163 memcpy (str+len, a->buf, FML_ATOM_BUF);
168 strcpy (str+len, a->buf);
169 len += strlen(str+len);
173 void fml_atom_strx (struct fml_atom *a, char *str, int max)
178 while (a->next && len < max - 2*FML_ATOM_BUF)
180 memcpy (str+len, a->buf, FML_ATOM_BUF);
184 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
185 str[len+FML_ATOM_BUF-1] = '\0';
188 int fml_atom_val (struct fml_atom *a)
190 static char arg[256];
193 return atoi (a->buf);
194 fml_atom_strx (a, arg, 200);
198 struct fml_node *fml_mk_node_val (Fml fml, int val)
203 sprintf (arg, "%d", val);
204 fn = fml_node_alloc (fml);
206 fn->p[0] = fml_atom_alloc (fml, arg);
210 void fml_node_delete (Fml fml, struct fml_node *fn)
216 atom_delete (fml, fn->p[0]);
218 fml_node_delete (fml, fn->p[0]);
221 fn->p[1] = fml->node_free_list;
222 fml->node_free_list = fn;
229 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
231 struct fml_node *fn0, *fn1;
235 fn1 = fn0 = fml_node_alloc (fml);
241 fn1->p[0] = atom_copy (fml, fn->p[0]);
244 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
248 fn1 = fn1->p[1] = fml_node_alloc (fml);
253 void fml_node_stat (Fml fml)
256 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);