2 * FML interpreter. Europagate, 1995
5 * Revision 1.8 1995/02/23 08:32:05 adam
8 * Revision 1.6 1995/02/10 18:15:52 adam
9 * FML function 'strcmp' implemented. This function can be used to
10 * test for existence of MARC fields.
12 * Revision 1.5 1995/02/09 14:37:18 adam
13 * Removed .depend from cvs. Removed function fml_mk_list.
15 * Revision 1.4 1995/02/09 14:33:37 adam
16 * Split source fml.c and define relevant build-in functions in separate
17 * files. New operators mult, div, not, llen implemented.
19 * Revision 1.3 1995/02/09 13:07:15 adam
20 * Nodes are freed now. Many bugs fixed.
22 * Revision 1.2 1995/02/06 15:23:26 adam
23 * Added some more relational operators (le,ne,ge). Added increment
24 * and decrement operators. Function index changed, so that first
25 * element is 1 - not 0. Function fml_atom_val edited.
27 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
28 * First version of the FML interpreter. It's slow and memory isn't
29 * freed properly. In particular, the FML nodes aren't released yet.
39 #define FML_ATOM_CHUNK 1024
40 #define FML_NODE_CHUNK 1024
42 static int no_nodes = 0;
43 static int no_atoms = 0;
45 struct fml_node *fml_node_alloc (Fml fml)
49 if (! fml->node_free_list)
53 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
56 (*fml->err_handle)(FML_ERR_NOMEM);
59 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
63 n = fml->node_free_list;
64 fml->node_free_list = n->p[1];
65 n->p[0] = n->p[1] = NULL;
71 static struct fml_atom *atom_malloc (Fml fml)
75 if (! fml->atom_free_list)
79 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
82 (*fml->err_handle)(FML_ERR_NOMEM);
85 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
89 fa = fml->atom_free_list;
90 fml->atom_free_list = fa->next;
95 static void atom_delete (Fml fml, struct fml_atom *a)
97 a->next = fml->atom_free_list;
98 fml->atom_free_list = a;
102 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
104 struct fml_atom *a0, *a1;
106 a0 = a1 = atom_malloc (fml);
109 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
113 a1 = a1->next = atom_malloc (fml);
119 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
122 struct fml_atom *a, *a0;
124 a0 = a = atom_malloc (fml);
125 strncpy (a->buf, str, FML_ATOM_BUF);
126 while (strlen (str+soff) >= FML_ATOM_BUF)
130 an = atom_malloc (fml);
132 soff += FML_ATOM_BUF;
133 strncpy (an->buf, str+soff, FML_ATOM_BUF);
140 int fml_atom_cmp (Fml fml, struct fml_atom *a1, struct fml_atom *a2)
145 n = strncmp (a1->buf, a2->buf, FML_ATOM_BUF);
158 int fml_atom_str (struct fml_atom *a, char *str)
166 memcpy (str+len, a->buf, FML_ATOM_BUF);
171 strcpy (str+len, a->buf);
172 len += strlen(str+len);
176 void fml_atom_strx (struct fml_atom *a, char *str, int max)
181 while (a->next && len < max - 2*FML_ATOM_BUF)
183 memcpy (str+len, a->buf, FML_ATOM_BUF);
187 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
188 str[len+FML_ATOM_BUF-1] = '\0';
191 int fml_atom_val (struct fml_atom *a)
193 static char arg[256];
196 return atoi (a->buf);
197 fml_atom_strx (a, arg, 200);
201 struct fml_node *fml_mk_node_val (Fml fml, int val)
206 sprintf (arg, "%d", val);
207 fn = fml_node_alloc (fml);
209 fn->p[0] = fml_atom_alloc (fml, arg);
213 void fml_node_delete (Fml fml, struct fml_node *fn)
219 atom_delete (fml, fn->p[0]);
221 fml_node_delete (fml, fn->p[0]);
224 fn->p[1] = fml->node_free_list;
225 fml->node_free_list = fn;
232 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
234 struct fml_node *fn0, *fn1;
238 fn1 = fn0 = fml_node_alloc (fml);
244 fn1->p[0] = atom_copy (fml, fn->p[0]);
247 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
251 fn1 = fn1->p[1] = fml_node_alloc (fml);
256 void fml_node_stat (Fml fml)
259 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);