2 * FML interpreter. Europagate, 1995
5 * Revision 1.4 1995/02/09 14:33:37 adam
6 * Split source fml.c and define relevant build-in functions in separate
7 * files. New operators mult, div, not, llen implemented.
9 * Revision 1.3 1995/02/09 13:07:15 adam
10 * Nodes are freed now. Many bugs fixed.
12 * Revision 1.2 1995/02/06 15:23:26 adam
13 * Added some more relational operators (le,ne,ge). Added increment
14 * and decrement operators. Function index changed, so that first
15 * element is 1 - not 0. Function fml_atom_val edited.
17 * Revision 1.1.1.1 1995/02/06 13:48:10 adam
18 * First version of the FML interpreter. It's slow and memory isn't
19 * freed properly. In particular, the FML nodes aren't released yet.
29 #define FML_ATOM_CHUNK 1024
30 #define FML_NODE_CHUNK 1024
32 static int no_nodes = 0;
33 static int no_atoms = 0;
35 struct fml_node *fml_node_alloc (Fml fml)
39 if (! fml->node_free_list)
43 n = fml->node_free_list = malloc (sizeof(*n) * FML_NODE_CHUNK);
46 (*fml->err_handle)(FML_ERR_NOMEM);
49 for (i = FML_ATOM_CHUNK-1; --i >= 0; n++)
53 n = fml->node_free_list;
54 fml->node_free_list = n->p[1];
55 n->p[0] = n->p[1] = NULL;
61 static struct fml_atom *atom_malloc (Fml fml)
65 if (! fml->atom_free_list)
69 fa = fml->atom_free_list = malloc (sizeof(*fa) * FML_ATOM_CHUNK);
72 (*fml->err_handle)(FML_ERR_NOMEM);
75 for (i = FML_ATOM_CHUNK-1; --i >= 0; fa++)
79 fa = fml->atom_free_list;
80 fml->atom_free_list = fa->next;
85 static void atom_delete (Fml fml, struct fml_atom *a)
87 a->next = fml->atom_free_list;
88 fml->atom_free_list = a;
92 static struct fml_atom *atom_copy (Fml fml, struct fml_atom *a)
94 struct fml_atom *a0, *a1;
96 a0 = a1 = atom_malloc (fml);
99 memcpy (&a1->buf, &a->buf, FML_ATOM_BUF);
103 a1 = a1->next = atom_malloc (fml);
109 struct fml_atom *fml_atom_alloc (Fml fml, char *str)
112 struct fml_atom *a, *a0;
114 a0 = a = atom_malloc (fml);
115 strncpy (a->buf, str, FML_ATOM_BUF);
116 while (strlen (str+soff) >= FML_ATOM_BUF)
120 an = atom_malloc (fml);
122 soff += FML_ATOM_BUF;
123 strncpy (an->buf, str+soff, FML_ATOM_BUF);
130 struct fml_node *fml_mk_list (Fml fml, struct fml_node *fn)
134 struct fml_node *fn2;
136 fn2 = fml_node_alloc (fml);
138 fn2->p[0] = fn->p[0];
145 int fml_atom_str (struct fml_atom *a, char *str)
153 memcpy (str+len, a->buf, FML_ATOM_BUF);
158 strcpy (str+len, a->buf);
159 len += strlen(str+len);
163 void fml_atom_strx (struct fml_atom *a, char *str, int max)
168 while (a->next && len < max - 2*FML_ATOM_BUF)
170 memcpy (str+len, a->buf, FML_ATOM_BUF);
174 strncpy (str+len, a->buf, FML_ATOM_BUF-1);
175 str[len+FML_ATOM_BUF-1] = '\0';
178 int fml_atom_val (struct fml_atom *a)
180 static char arg[256];
183 return atoi (a->buf);
184 fml_atom_strx (a, arg, 200);
188 struct fml_node *fml_mk_node_val (Fml fml, int val)
193 sprintf (arg, "%d", val);
194 fn = fml_node_alloc (fml);
196 fn->p[0] = fml_atom_alloc (fml, arg);
200 void fml_node_delete (Fml fml, struct fml_node *fn)
206 atom_delete (fml, fn->p[0]);
208 fml_node_delete (fml, fn->p[0]);
211 fn->p[1] = fml->node_free_list;
212 fml->node_free_list = fn;
219 struct fml_node *fml_node_copy (Fml fml, struct fml_node *fn)
221 struct fml_node *fn0, *fn1;
225 fn1 = fn0 = fml_node_alloc (fml);
231 fn1->p[0] = atom_copy (fml, fn->p[0]);
234 fn1->p[0] = fml_node_copy (fml, fn->p[0]);
238 fn1 = fn1->p[1] = fml_node_alloc (fml);
243 void fml_node_stat (Fml fml)
246 printf ("<<node=%d, atom=%d>>", no_nodes, no_atoms);