2 * FML interpreter. Europagate, 1995
5 * Revision 1.3 1995/02/09 13:07:15 adam
6 * Nodes are freed now. Many bugs fixed.
8 * Revision 1.2 1995/02/06 15:23:26 adam
9 * Added some more relational operators (le,ne,ge). Added increment
10 * and decrement operators. Function index changed, so that first
11 * element is 1 - not 0. Function fml_atom_val edited.
13 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
14 * First version of the FML interpreter. It's slow and memory isn't
15 * freed properly. In particular, the FML nodes aren't released yet.
25 #define FML_ATOM_CHUNK 1024
26 #define FML_NODE_CHUNK 1024
28 static int no_nodes = 0;
29 static int no_atoms = 0;
31 struct fml_node *fml_node_alloc (Fml fml)
35 if (! fml->node_free_list)
39 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
42 (*fml->err_handle)(FML_ERR_NOMEM);
45 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
49 n = fml->node_free_list;
50 fml->node_free_list = n->p[1];
51 n->p[0] = n->p[1] = NULL;
57 static struct fml_atom *atom_malloc (Fml fml)
61 if (! fml->atom_free_list)
65 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
68 (*fml->err_handle)(FML_ERR_NOMEM);
71 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
75 fa = fml->atom_free_list;
76 fml->atom_free_list = fa->next;
81 static void atom_delete (Fml fml, struct fml_atom *a)
83 a->next = fml->atom_free_list;
84 fml->atom_free_list = a;
88 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
90 struct fml_atom *a0, *a1;
92 a0 = a1 = atom_malloc (fml);
95 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
99 a1 = a1->next = atom_malloc (fml);
105 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
108 struct fml_atom *a, *a0;
110 a0 = a = atom_malloc (fml);
111 strncpy (a->buf, str, FML_ATOM_BUF);
112 while (strlen (str+soff) >= FML_ATOM_BUF)
116 an = atom_malloc (fml);
118 soff += FML_ATOM_BUF;
119 strncpy (an->buf, str+soff, FML_ATOM_BUF);
126 struct fml_node *fml_mk_list (Fml fml, struct fml_node *fn)
130 struct fml_node *fn2;
132 fn2 = fml_node_alloc (fml);
134 fn2->p[0] = fn->p[0];
141 int fml_atom_str (struct fml_atom *a, char *str)
149 memcpy (str+len, a->buf, FML_ATOM_BUF);
154 strcpy (str+len, a->buf);
155 len += strlen(str+len);
159 void fml_atom_strx (struct fml_atom *a, char *str, int max)
164 while (a->next && len < max - 2*FML_ATOM_BUF)
166 memcpy (str+len, a->buf, FML_ATOM_BUF);
170 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
171 str[len+FML_ATOM_BUF-1] = '\0';
175 int fml_atom_val (struct fml_atom *a)
177 static char arg[256];
180 return atoi (a->buf);
181 fml_atom_strx (a, arg, 200);
185 void fml_node_delete (Fml fml, struct fml_node *fn)
191 atom_delete (fml, fn->p[0]);
193 fml_node_delete (fml, fn->p[0]);
196 fn->p[1] = fml->node_free_list;
197 fml->node_free_list = fn;
204 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
206 struct fml_node *fn0, *fn1;
210 fn1 = fn0 = fml_node_alloc (fml);
216 fn1->p[0] = atom_copy (fml, fn->p[0]);
219 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
223 fn1 = fn1->p[1] = fml_node_alloc (fml);
228 void fml_node_stat (Fml fml)
231 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);