altos/lisp: Add 'else' sematics to cond
[fw/altos] / src / lisp / ao_lisp_make_const.c
index 416a95d91fa4ddfa4be7d624696bae2c661db4cf..826c98b948e829acefbee5d02cf49c62faa634fb 100644 (file)
@@ -34,44 +34,8 @@ struct builtin_func {
        int     func;
 };
 
-struct builtin_func funcs[] = {
-       { .name = "eval",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_eval },
-       { .name = "read",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_read },
-       { .name = "lambda",     .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_lambda },
-       { .name = "lexpr",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_lexpr },
-       { .name = "nlambda",    .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_nlambda },
-       { .name = "macro",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_macro },
-       { .name = "car",        .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_car },
-       { .name = "cdr",        .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_cdr },
-       { .name = "cons",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_cons },
-       { .name = "last",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_last },
-       { .name = "length",     .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_length },
-       { .name = "quote",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_quote },
-       { .name = "set",        .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_set },
-       { .name = "setq",       .args = AO_LISP_FUNC_MACRO,     .func = builtin_setq },
-       { .name = "cond",       .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_cond },
-       { .name = "progn",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_progn },
-       { .name = "while",      .args = AO_LISP_FUNC_NLAMBDA,   .func = builtin_while },
-       { .name = "print",      .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_print },
-       { .name = "patom",      .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_patom },
-       { .name = "+",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_plus },
-       { .name = "-",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_minus },
-       { .name = "*",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_times },
-       { .name = "/",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_divide },
-       { .name = "%",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_mod },
-       { .name = "=",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_equal },
-       { .name = "<",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_less },
-       { .name = ">",          .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_greater },
-       { .name = "<=",         .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_less_equal },
-       { .name = ">=",         .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_greater_equal },
-       { .name = "pack",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_pack },
-       { .name = "unpack",     .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_unpack },
-       { .name = "flush",      .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_flush },
-       { .name = "delay",      .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_delay },
-       { .name = "led",        .args = AO_LISP_FUNC_F_LEXPR,   .func = builtin_led },
-       { .name = "save",       .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_save },
-       { .name = "restore",    .args = AO_LISP_FUNC_F_LAMBDA,  .func = builtin_restore },
-};
+#define AO_LISP_BUILTIN_CONSTS
+#include "ao_lisp_builtin.h"
 
 #define N_FUNC (sizeof funcs / sizeof funcs[0])
 
@@ -136,6 +100,7 @@ ao_lisp_macro_push(ao_poly p)
        m->p = p;
        m->next = macro_stack;
        macro_stack = m;
+       return 0;
 }
 
 void
@@ -147,7 +112,7 @@ ao_lisp_macro_pop(void)
        free(m);
 }
 
-#define DBG_MACRO 1
+#define DBG_MACRO 0
 #if DBG_MACRO
 int macro_scan_depth;
 
@@ -305,8 +270,8 @@ main(int argc, char **argv)
        ao_poly val;
        struct ao_lisp_atom     *a;
        struct ao_lisp_builtin  *b;
-       int     in_atom;
-       char    *out_name;
+       int     in_atom = 0;
+       char    *out_name = NULL;
        int     c;
 
        in = stdin;
@@ -323,6 +288,10 @@ main(int argc, char **argv)
                }
        }
 
+       /* Boolean values #f and #t */
+       ao_lisp_bool_get(0);
+       ao_lisp_bool_get(1);
+
        for (f = 0; f < (int) N_FUNC; f++) {
                b = ao_lisp_make_builtin(funcs[f].func, funcs[f].args);
                a = ao_lisp_atom_intern(funcs[f].name);
@@ -330,18 +299,14 @@ main(int argc, char **argv)
                                 ao_lisp_builtin_poly(b));
        }
 
-       /* boolean constants */
-       ao_lisp_atom_set(ao_lisp_atom_poly(ao_lisp_atom_intern("nil")),
-                        AO_LISP_NIL);
-       a = ao_lisp_atom_intern("t");
-       ao_lisp_atom_set(ao_lisp_atom_poly(a),
-                        ao_lisp_atom_poly(a));
-
        /* end of file value */
        a = ao_lisp_atom_intern("eof");
        ao_lisp_atom_set(ao_lisp_atom_poly(a),
                         ao_lisp_atom_poly(a));
 
+       /* 'else' */
+       a = ao_lisp_atom_intern("else");
+
        if (argv[optind]){
                in = fopen(argv[optind], "r");
                if (!in) {
@@ -355,9 +320,9 @@ main(int argc, char **argv)
        }
 
        /* Reduce to referenced values */
-       ao_lisp_collect();
+       ao_lisp_collect(AO_LISP_COLLECT_FULL);
 
-       for (f = 0; f < ao_lisp_frame_num(ao_lisp_frame_global); f++) {
+       for (f = 0; f < ao_lisp_frame_global->num; f++) {
                val = ao_has_macro(ao_lisp_frame_global->vals[f].val);
                if (val != AO_LISP_NIL) {
                        printf("error: function %s contains unresolved macro: ",
@@ -384,6 +349,8 @@ main(int argc, char **argv)
        fprintf(out, "#define ao_builtin_frame 0x%04x\n", ao_lisp_frame_poly(ao_lisp_frame_global));
        fprintf(out, "#define ao_lisp_const_checksum ((uint16_t) 0x%04x)\n", ao_fec_crc(ao_lisp_const, ao_lisp_top));
 
+       fprintf(out, "#define _ao_lisp_bool_false 0x%04x\n", ao_lisp_bool_poly(ao_lisp_false));
+       fprintf(out, "#define _ao_lisp_bool_true 0x%04x\n", ao_lisp_bool_poly(ao_lisp_true));
 
        for (a = ao_lisp_atoms; a; a = ao_lisp_poly_atom(a->next)) {
                char    *n = a->name, c;
@@ -397,7 +364,7 @@ main(int argc, char **argv)
                fprintf(out, "  0x%04x\n", ao_lisp_atom_poly(a));
        }
        fprintf(out, "#ifdef AO_LISP_CONST_BITS\n");
-       fprintf(out, "const uint8_t ao_lisp_const[] = {");
+       fprintf(out, "const uint8_t ao_lisp_const[AO_LISP_POOL_CONST] __attribute((aligned(4))) = {");
        for (o = 0; o < ao_lisp_top; o++) {
                uint8_t c;
                if ((o & 0xf) == 0)