2 * FML interpreter. Europagate, 1995
5 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
6 * First version of the FML interpreter. It's slow and memory isn't
7 * freed properly. In particular, the FML nodes aren't released yet.
17 #define FML_ATOM_CHUNK 1024
18 #define FML_NODE_CHUNK 1024
20 struct fml_node *fml_node_alloc (Fml fml)
24 if (! fml->node_free_list)
28 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
31 (*fml->err_handle)(FML_ERR_NOMEM);
34 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
38 n = fml->node_free_list;
39 fml->node_free_list = n->p[1];
40 n->p[0] = n->p[1] = NULL;
45 static struct fml_atom *atom_malloc (Fml fml)
49 if (! fml->atom_free_list)
53 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
56 (*fml->err_handle)(FML_ERR_NOMEM);
59 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
63 fa = fml->atom_free_list;
64 fml->atom_free_list = fa->next;
68 static void atom_delete (Fml fml, struct fml_atom *a)
70 a->next = fml->atom_free_list;
71 fml->atom_free_list = a;
74 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
76 struct fml_atom *a0, *a1;
78 a0 = a1 = atom_malloc (fml);
81 memcpy (&a1->buf, &a->buf, FML_ATOM_CHUNK);
85 a1 = a1->next = atom_malloc (fml);
91 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
94 struct fml_atom *a, *a0;
96 a0 = a = atom_malloc (fml);
97 strncpy (a->buf, str, FML_ATOM_BUF);
98 while (strlen (str+soff) >= FML_ATOM_BUF)
102 an = atom_malloc (fml);
104 soff += FML_ATOM_BUF;
105 strncpy (an->buf, str+soff, FML_ATOM_BUF);
112 struct fml_node *fml_mk_list (Fml fml, struct fml_node *fn)
116 struct fml_node *fn2;
118 fn2 = fml_node_alloc (fml);
120 fn2->p[0] = fn->p[0];
127 int fml_atom_str (struct fml_atom *a, char *str)
135 memcpy (str+len, a->buf, FML_ATOM_BUF);
140 strcpy (str+len, a->buf);
141 len += strlen(str+len);
145 void fml_atom_strx (struct fml_atom *a, char *str, int max)
150 while (a->next && len < max - 2*FML_ATOM_BUF)
152 memcpy (str+len, a->buf, FML_ATOM_BUF);
156 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
157 str[len+FML_ATOM_BUF-1] = '\0';
160 int fml_atom_val (struct fml_atom *a)
163 return atoi (a->buf);
166 void fml_node_delete (Fml fml, struct fml_node *fn)
172 atom_delete (fml, fn->p[0]);
174 fml_node_delete (fml, fn->p[0]);
177 fn->p[1] = fml->node_free_list;
178 fml->node_free_list = fn;
184 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
186 struct fml_node *fn0, *fn1;
190 fn1 = fn0 = fml_node_alloc (fml);
196 fn1->p[0] = atom_copy (fml, fn->p[0]);
199 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
203 fn1 = fn1->p[1] = fml_node_alloc (fml);