2 * FML interpreter. Europagate, 1995
5 * Revision 1.5 1995/02/09 14:37:18 adam
6 * Removed .depend from cvs. Removed function fml_mk_list.
8 * Revision 1.4 1995/02/09 14:33:37 adam
9 * Split source fml.c and define relevant build-in functions in separate
10 * files. New operators mult, div, not, llen implemented.
12 * Revision 1.3 1995/02/09 13:07:15 adam
13 * Nodes are freed now. Many bugs fixed.
15 * Revision 1.2 1995/02/06 15:23:26 adam
16 * Added some more relational operators (le,ne,ge). Added increment
17 * and decrement operators. Function index changed, so that first
18 * element is 1 - not 0. Function fml_atom_val edited.
20 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
21 * First version of the FML interpreter. It's slow and memory isn't
22 * freed properly. In particular, the FML nodes aren't released yet.
32 #define FML_ATOM_CHUNK 1024
33 #define FML_NODE_CHUNK 1024
35 static int no_nodes = 0;
36 static int no_atoms = 0;
38 struct fml_node *fml_node_alloc (Fml fml)
42 if (! fml->node_free_list)
46 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
49 (*fml->err_handle)(FML_ERR_NOMEM);
52 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
56 n = fml->node_free_list;
57 fml->node_free_list = n->p[1];
58 n->p[0] = n->p[1] = NULL;
64 static struct fml_atom *atom_malloc (Fml fml)
68 if (! fml->atom_free_list)
72 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
75 (*fml->err_handle)(FML_ERR_NOMEM);
78 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
82 fa = fml->atom_free_list;
83 fml->atom_free_list = fa->next;
88 static void atom_delete (Fml fml, struct fml_atom *a)
90 a->next = fml->atom_free_list;
91 fml->atom_free_list = a;
95 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
97 struct fml_atom *a0, *a1;
99 a0 = a1 = atom_malloc (fml);
102 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
106 a1 = a1->next = atom_malloc (fml);
112 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
115 struct fml_atom *a, *a0;
117 a0 = a = atom_malloc (fml);
118 strncpy (a->buf, str, FML_ATOM_BUF);
119 while (strlen (str+soff) >= FML_ATOM_BUF)
123 an = atom_malloc (fml);
125 soff += FML_ATOM_BUF;
126 strncpy (an->buf, str+soff, FML_ATOM_BUF);
133 int fml_atom_str (struct fml_atom *a, char *str)
141 memcpy (str+len, a->buf, FML_ATOM_BUF);
146 strcpy (str+len, a->buf);
147 len += strlen(str+len);
151 void fml_atom_strx (struct fml_atom *a, char *str, int max)
156 while (a->next && len < max - 2*FML_ATOM_BUF)
158 memcpy (str+len, a->buf, FML_ATOM_BUF);
162 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
163 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 struct fml_node *fml_mk_node_val (Fml fml, int val)
181 sprintf (arg, "%d", val);
182 fn = fml_node_alloc (fml);
184 fn->p[0] = fml_atom_alloc (fml, arg);
188 void fml_node_delete (Fml fml, struct fml_node *fn)
194 atom_delete (fml, fn->p[0]);
196 fml_node_delete (fml, fn->p[0]);
199 fn->p[1] = fml->node_free_list;
200 fml->node_free_list = fn;
207 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
209 struct fml_node *fn0, *fn1;
213 fn1 = fn0 = fml_node_alloc (fml);
219 fn1->p[0] = atom_copy (fml, fn->p[0]);
222 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
226 fn1 = fn1->p[1] = fml_node_alloc (fml);
231 void fml_node_stat (Fml fml)
234 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);