altos/scheme: Split tests out from build sources
[fw/altos] / src / scheme / ao_scheme_make_const.c
index e34792c400c82ef15db87c52382b71167701bc62..8561bf0b47d39302846f452ea3a7a4427a3ae6d4 100644 (file)
@@ -125,9 +125,9 @@ ao_scheme_macro_pop(void)
 
 #define DBG_MACRO 0
 #if DBG_MACRO
-int macro_scan_depth;
+static int macro_scan_depth;
 
-void indent(void)
+static void indent(void)
 {
        int i;
        for (i = 0; i < macro_scan_depth; i++)
@@ -157,7 +157,7 @@ ao_is_macro(ao_poly p)
        struct ao_scheme_lambda *lambda;
        ao_poly ret;
 
-       MACRO_DEBUG(indent(); printf ("is macro "); ao_scheme_poly_write(p); printf("\n"); ++macro_scan_depth);
+       MACRO_DEBUG(indent(); ao_scheme_printf ("is macro %v\n", p); ++macro_scan_depth);
        switch (ao_scheme_poly_type(p)) {
        case AO_SCHEME_ATOM:
                if (ao_scheme_macro_push(p))
@@ -192,7 +192,7 @@ ao_is_macro(ao_poly p)
                ret = AO_SCHEME_NIL;
                break;
        }
-       MACRO_DEBUG(--macro_scan_depth; indent(); printf ("... "); ao_scheme_poly_write(ret); printf("\n"));
+       MACRO_DEBUG(--macro_scan_depth; indent(); ao_scheme_printf ("... %v\n", ret););
        return ret;
 }
 
@@ -207,11 +207,11 @@ ao_has_macro(ao_poly p)
        if (p == AO_SCHEME_NIL)
                return AO_SCHEME_NIL;
 
-       MACRO_DEBUG(indent(); printf("has macro "); ao_scheme_poly_write(p); printf("\n"); ++macro_scan_depth);
+       MACRO_DEBUG(indent(); ao_scheme_printf("has macro %v\n", p); ++macro_scan_depth);
        switch (ao_scheme_poly_type(p)) {
        case AO_SCHEME_LAMBDA:
                lambda = ao_scheme_poly_lambda(p);
-               p = ao_has_macro(lambda->code);
+               p = ao_has_macro(ao_scheme_poly_cons(lambda->code)->cdr);
                break;
        case AO_SCHEME_CONS:
                cons = ao_scheme_poly_cons(p);
@@ -235,7 +235,7 @@ ao_has_macro(ao_poly p)
                p = AO_SCHEME_NIL;
                break;
        }
-       MACRO_DEBUG(--macro_scan_depth; indent(); printf("... "); ao_scheme_poly_write(p); printf("\n"));
+       MACRO_DEBUG(--macro_scan_depth; indent(); ao_scheme_printf("... %v\n", p));
        return p;
 }
 
@@ -270,18 +270,19 @@ ao_scheme_seen_builtin(struct ao_scheme_builtin *b)
 }
 
 static int
-ao_scheme_read_eval_abort(void)
+ao_scheme_read_eval_abort(FILE *read_file)
 {
-       ao_poly in, out = AO_SCHEME_NIL;
+       ao_poly in;
+
        for(;;) {
-               in = ao_scheme_read();
+               in = ao_scheme_read(read_file);
                if (in == _ao_scheme_atom_eof)
                        break;
-               out = ao_scheme_eval(in);
-               if (ao_scheme_exception)
+               (void) ao_scheme_eval(in);
+               if (ao_scheme_exception) {
+                       ao_scheme_fprintf(stderr, "make_const failed on %v\n", in);
                        return 0;
-               ao_scheme_poly_write(out, true);
-               putchar ('\n');
+               }
        }
        return 1;
 }
@@ -307,8 +308,11 @@ ao_scheme_add_feature(struct feature **list, char *name)
 }
 
 static bool
-ao_scheme_has_feature(struct feature *list, const char *name)
+_ao_scheme_has_feature(struct feature *list, const char *name, bool skip_undef)
 {
+       if (skip_undef && !strcmp(name, "UNDEF"))
+               return false;
+
        while (list) {
                if (!strcmp(list->name, name))
                        return true;
@@ -317,6 +321,18 @@ ao_scheme_has_feature(struct feature *list, const char *name)
        return false;
 }
 
+static bool
+ao_scheme_has_undef(struct feature *list)
+{
+       return _ao_scheme_has_feature(list, "UNDEF", false);
+}
+
+static bool
+ao_scheme_has_feature(struct feature *list, const char *name)
+{
+       return _ao_scheme_has_feature(list, name, true);
+}
+
 static void
 ao_scheme_add_features(struct feature **list, const char *names)
 {
@@ -424,16 +440,26 @@ main(int argc, char **argv)
                        a = ao_scheme_atom_intern((char *) atoms[an].name);
        }
 
-       if (argv[optind]){
+       while (argv[optind]) {
                in = fopen(argv[optind], "r");
                if (!in) {
                        perror(argv[optind]);
                        exit(1);
                }
+               if (!ao_scheme_read_eval_abort(in)) {
+                       fprintf(stderr, "eval failed\n");
+                       exit(1);
+               }
+               fclose(in);
+               optind++;
        }
-       if (!ao_scheme_read_eval_abort()) {
-               fprintf(stderr, "eval failed\n");
-               exit(1);
+
+       if (!ao_scheme_has_undef(enable) && ao_scheme_has_undef(disable)) {
+               struct ao_scheme_cons cons;
+
+               cons.car = _ao_scheme_atom_undef;
+               cons.cdr = AO_SCHEME_NIL;
+               ao_scheme_do_undef(&cons);
        }
 
        /* Reduce to referenced values */
@@ -444,10 +470,10 @@ main(int argc, char **argv)
 
                val = ao_has_macro(vals->vals[f].val);
                if (val != AO_SCHEME_NIL) {
-                       printf("error: function %s contains unresolved macro: ",
-                              ao_scheme_poly_atom(vals->vals[f].atom)->name);
-                       ao_scheme_poly_write(val, true);
-                       printf("\n");
+                       fprintf(stderr, "error: function %s contains unresolved macro: ",
+                               ao_scheme_poly_atom(vals->vals[f].atom)->name);
+                       ao_scheme_poly_write(stderr, val, true);
+                       fprintf(stderr, "\n");
                        exit(1);
                }