2 * FML interpreter. Europagate, 1995
5 * Revision 1.2 1995/02/06 15:23:26 adam
6 * Added some more relational operators (le,ne,ge). Added increment
7 * and decrement operators. Function index changed, so that first
8 * element is 1 - not 0. Function fml_atom_val edited.
10 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
11 * First version of the FML interpreter. It's slow and memory isn't
12 * freed properly. In particular, the FML nodes aren't released yet.
22 #define FML_ATOM_CHUNK 1024
23 #define FML_NODE_CHUNK 1024
25 struct fml_node *fml_node_alloc (Fml fml)
29 if (! fml->node_free_list)
33 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
36 (*fml->err_handle)(FML_ERR_NOMEM);
39 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
43 n = fml->node_free_list;
44 fml->node_free_list = n->p[1];
45 n->p[0] = n->p[1] = NULL;
50 static struct fml_atom *atom_malloc (Fml fml)
54 if (! fml->atom_free_list)
58 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
61 (*fml->err_handle)(FML_ERR_NOMEM);
64 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
68 fa = fml->atom_free_list;
69 fml->atom_free_list = fa->next;
73 static void atom_delete (Fml fml, struct fml_atom *a)
75 a->next = fml->atom_free_list;
76 fml->atom_free_list = a;
79 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
81 struct fml_atom *a0, *a1;
83 a0 = a1 = atom_malloc (fml);
86 memcpy (&a1->buf, &a->buf, FML_ATOM_CHUNK);
90 a1 = a1->next = atom_malloc (fml);
96 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
99 struct fml_atom *a, *a0;
101 a0 = a = atom_malloc (fml);
102 strncpy (a->buf, str, FML_ATOM_BUF);
103 while (strlen (str+soff) >= FML_ATOM_BUF)
107 an = atom_malloc (fml);
109 soff += FML_ATOM_BUF;
110 strncpy (an->buf, str+soff, FML_ATOM_BUF);
117 struct fml_node *fml_mk_list (Fml fml, struct fml_node *fn)
121 struct fml_node *fn2;
123 fn2 = fml_node_alloc (fml);
125 fn2->p[0] = fn->p[0];
132 int fml_atom_str (struct fml_atom *a, char *str)
140 memcpy (str+len, a->buf, FML_ATOM_BUF);
145 strcpy (str+len, a->buf);
146 len += strlen(str+len);
150 void fml_atom_strx (struct fml_atom *a, char *str, int max)
155 while (a->next && len < max - 2*FML_ATOM_BUF)
157 memcpy (str+len, a->buf, FML_ATOM_BUF);
161 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
162 str[len+FML_ATOM_BUF-1] = '\0';
166 int fml_atom_val (struct fml_atom *a)
168 static char arg[256];
171 return atoi (a->buf);
172 fml_atom_strx (a, arg, 200);
176 void fml_node_delete (Fml fml, struct fml_node *fn)
182 atom_delete (fml, fn->p[0]);
184 fml_node_delete (fml, fn->p[0]);
187 fn->p[1] = fml->node_free_list;
188 fml->node_free_list = fn;
194 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
196 struct fml_node *fn0, *fn1;
200 fn1 = fn0 = fml_node_alloc (fml);
206 fn1->p[0] = atom_copy (fml, fn->p[0]);
209 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
213 fn1 = fn1->p[1] = fml_node_alloc (fml);