#include "ao_lisp.h"
#include <stdio.h>
-static struct ao_lisp_cons *list;
-static char *string;
+static FILE *ao_lisp_file;
+static int newline = 1;
+
+static char save_file[] = "lisp.image";
+
+int
+ao_lisp_os_save(void)
+{
+ FILE *save = fopen(save_file, "w");
+
+ if (!save) {
+ perror(save_file);
+ return 0;
+ }
+ fwrite(ao_lisp_pool, 1, AO_LISP_POOL_TOTAL, save);
+ fclose(save);
+ return 1;
+}
+
+int
+ao_lisp_os_restore_save(struct ao_lisp_os_save *save, int offset)
+{
+ FILE *restore = fopen(save_file, "r");
+ size_t ret;
+
+ if (!restore) {
+ perror(save_file);
+ return 0;
+ }
+ fseek(restore, offset, SEEK_SET);
+ ret = fread(save, sizeof (struct ao_lisp_os_save), 1, restore);
+ fclose(restore);
+ if (ret != 1)
+ return 0;
+ return 1;
+}
+
+int
+ao_lisp_os_restore(void)
+{
+ FILE *restore = fopen(save_file, "r");
+ size_t ret;
+
+ if (!restore) {
+ perror(save_file);
+ return 0;
+ }
+ ret = fread(ao_lisp_pool, 1, AO_LISP_POOL_TOTAL, restore);
+ fclose(restore);
+ if (ret != AO_LISP_POOL_TOTAL)
+ return 0;
+ return 1;
+}
+
+int
+ao_lisp_getc(void)
+{
+ int c;
+
+ if (ao_lisp_file)
+ return getc(ao_lisp_file);
+
+ if (newline) {
+ printf("> ");
+ newline = 0;
+ }
+ c = getchar();
+ if (c == '\n')
+ newline = 1;
+ return c;
+}
int
main (int argc, char **argv)
{
- int i, j;
- struct ao_lisp_atom *atom;
- ao_lisp_poly poly;
- ao_lisp_root_add(&ao_lisp_cons_type, (void **) &list);
- ao_lisp_root_add(&ao_lisp_string_type, (void **) &string);
-
- /* allocator test */
- for (j = 0; j < 10; j++) {
- list = 0;
- string = ao_lisp_string_new(0);
- for (i = 0; i < 7; i++) {
- string = ao_lisp_string_cat(string, "a");
- list = ao_lisp_cons(ao_lisp_string_poly(string), list);
- list = ao_lisp_cons(ao_lisp_int_poly(i), list);
- atom = ao_lisp_atom_intern("ant");
- atom->val = ao_lisp_cons_poly(list);
- list = ao_lisp_cons(ao_lisp_atom_poly(atom), list);
+ while (*++argv) {
+ ao_lisp_file = fopen(*argv, "r");
+ if (!ao_lisp_file) {
+ perror(*argv);
+ exit(1);
}
- ao_lisp_poly_print(ao_lisp_cons_poly(list));
- printf("\n");
+ ao_lisp_read_eval_print();
+ fclose(ao_lisp_file);
+ ao_lisp_file = NULL;
}
+ ao_lisp_read_eval_print();
- atom = ao_lisp_atom_intern("ant");
- atom->val = ao_lisp_string_poly(ao_lisp_string_cat("hello world", ""));
-
- list = ao_lisp_cons(ao_lisp_atom_poly(ao_lisp_atom_intern("plus")),
- ao_lisp_cons(ao_lisp_cons_poly(ao_lisp_cons(ao_lisp_atom_poly(ao_lisp_atom_intern("plus")),
- ao_lisp_cons(ao_lisp_int_poly(3),
- ao_lisp_cons(ao_lisp_int_poly(4), NULL)))),
- ao_lisp_cons(ao_lisp_int_poly(2), NULL)));
- printf("list: ");
- ao_lisp_poly_print(ao_lisp_cons_poly(list));
- printf ("\n");
- ao_lisp_poly_print(ao_lisp_eval(ao_lisp_cons_poly(list)));
- printf ("\n");
-
- while ((poly = ao_lisp_read())) {
- poly = ao_lisp_eval(poly);
- ao_lisp_poly_print(poly);
- putchar ('\n');
- fflush(stdout);
- }
+ printf ("collects: full: %d incremental %d\n",
+ ao_lisp_collects[AO_LISP_COLLECT_FULL],
+ ao_lisp_collects[AO_LISP_COLLECT_INCREMENTAL]);
+
+ printf ("freed: full %d incremental %d\n",
+ ao_lisp_freed[AO_LISP_COLLECT_FULL],
+ ao_lisp_freed[AO_LISP_COLLECT_INCREMENTAL]);
+
+ printf("loops: full %d incremental %d\n",
+ ao_lisp_loops[AO_LISP_COLLECT_FULL],
+ ao_lisp_loops[AO_LISP_COLLECT_INCREMENTAL]);
+
+ printf("loops per collect: full %f incremental %f\n",
+ (double) ao_lisp_loops[AO_LISP_COLLECT_FULL] /
+ (double) ao_lisp_collects[AO_LISP_COLLECT_FULL],
+ (double) ao_lisp_loops[AO_LISP_COLLECT_INCREMENTAL] /
+ (double) ao_lisp_collects[AO_LISP_COLLECT_INCREMENTAL]);
+
+ printf("freed per collect: full %f incremental %f\n",
+ (double) ao_lisp_freed[AO_LISP_COLLECT_FULL] /
+ (double) ao_lisp_collects[AO_LISP_COLLECT_FULL],
+ (double) ao_lisp_freed[AO_LISP_COLLECT_INCREMENTAL] /
+ (double) ao_lisp_collects[AO_LISP_COLLECT_INCREMENTAL]);
+ printf("freed per loop: full %f incremental %f\n",
+ (double) ao_lisp_freed[AO_LISP_COLLECT_FULL] /
+ (double) ao_lisp_loops[AO_LISP_COLLECT_FULL],
+ (double) ao_lisp_freed[AO_LISP_COLLECT_INCREMENTAL] /
+ (double) ao_lisp_loops[AO_LISP_COLLECT_INCREMENTAL]);
}