2 * FML interpreter. Europagate, 1995
5 * Revision 1.1 1995/02/06 13:48:09 adam
16 #define FML_ATOM_CHUNK 1024
17 #define FML_NODE_CHUNK 1024
19 struct fml_node *fml_node_alloc (Fml fml)
23 if (! fml->node_free_list)
27 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
30 (*fml->err_handle)(FML_ERR_NOMEM);
33 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
37 n = fml->node_free_list;
38 fml->node_free_list = n->p[1];
39 n->p[0] = n->p[1] = NULL;
44 static struct fml_atom *atom_malloc (Fml fml)
48 if (! fml->atom_free_list)
52 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
55 (*fml->err_handle)(FML_ERR_NOMEM);
58 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
62 fa = fml->atom_free_list;
63 fml->atom_free_list = fa->next;
67 static void atom_delete (Fml fml, struct fml_atom *a)
69 a->next = fml->atom_free_list;
70 fml->atom_free_list = a;
73 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
75 struct fml_atom *a0, *a1;
77 a0 = a1 = atom_malloc (fml);
80 memcpy (&a1->buf, &a->buf, FML_ATOM_CHUNK);
84 a1 = a1->next = atom_malloc (fml);
90 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
93 struct fml_atom *a, *a0;
95 a0 = a = atom_malloc (fml);
96 strncpy (a->buf, str, FML_ATOM_BUF);
97 while (strlen (str+soff) >= FML_ATOM_BUF)
101 an = atom_malloc (fml);
103 soff += FML_ATOM_BUF;
104 strncpy (an->buf, str+soff, FML_ATOM_BUF);
111 struct fml_node *fml_mk_list (Fml fml, struct fml_node *fn)
115 struct fml_node *fn2;
117 fn2 = fml_node_alloc (fml);
119 fn2->p[0] = fn->p[0];
126 int fml_atom_str (struct fml_atom *a, char *str)
134 memcpy (str+len, a->buf, FML_ATOM_BUF);
139 strcpy (str+len, a->buf);
140 len += strlen(str+len);
144 void fml_atom_strx (struct fml_atom *a, char *str, int max)
149 while (a->next && len < max - 2*FML_ATOM_BUF)
151 memcpy (str+len, a->buf, FML_ATOM_BUF);
155 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
156 str[len+FML_ATOM_BUF-1] = '\0';
159 int fml_atom_val (struct fml_atom *a)
162 return atoi (a->buf);
165 void fml_node_delete (Fml fml, struct fml_node *fn)
171 atom_delete (fml, fn->p[0]);
173 fml_node_delete (fml, fn->p[0]);
176 fn->p[1] = fml->node_free_list;
177 fml->node_free_list = fn;
183 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
185 struct fml_node *fn0, *fn1;
189 fn1 = fn0 = fml_node_alloc (fml);
195 fn1->p[0] = atom_copy (fml, fn->p[0]);
198 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
202 fn1 = fn1->p[1] = fml_node_alloc (fml);