+#define AO_FEC_CRC_INIT 0xffff
+
+static inline uint16_t
+ao_fec_crc_byte(uint8_t byte, uint16_t crc)
+{
+ uint8_t bit;
+
+ for (bit = 0; bit < 8; bit++) {
+ if (((crc & 0x8000) >> 8) ^ (byte & 0x80))
+ crc = (crc << 1) ^ 0x8005;
+ else
+ crc = (crc << 1);
+ byte <<= 1;
+ }
+ return crc;
+}
+
+uint16_t
+ao_fec_crc(const uint8_t *bytes, uint8_t len)
+{
+ uint16_t crc = AO_FEC_CRC_INIT;
+
+ while (len--)
+ crc = ao_fec_crc_byte(*bytes++, crc);
+ return crc;
+}
+
+struct ao_lisp_macro_stack {
+ struct ao_lisp_macro_stack *next;
+ ao_poly p;
+};
+
+struct ao_lisp_macro_stack *macro_stack;
+
+int
+ao_lisp_macro_push(ao_poly p)
+{
+ struct ao_lisp_macro_stack *m = macro_stack;
+
+ while (m) {
+ if (m->p == p)
+ return 1;
+ m = m->next;
+ }
+ m = malloc (sizeof (struct ao_lisp_macro_stack));
+ m->p = p;
+ m->next = macro_stack;
+ macro_stack = m;
+ return 0;
+}
+
+void
+ao_lisp_macro_pop(void)
+{
+ struct ao_lisp_macro_stack *m = macro_stack;
+
+ macro_stack = m->next;
+ free(m);
+}
+
+#define DBG_MACRO 0
+#if DBG_MACRO
+int macro_scan_depth;
+
+void indent(void)
+{
+ int i;
+ for (i = 0; i < macro_scan_depth; i++)
+ printf(" ");
+}
+#define MACRO_DEBUG(a) a
+#else
+#define MACRO_DEBUG(a)
+#endif
+
+ao_poly
+ao_has_macro(ao_poly p);
+
+ao_poly
+ao_macro_test_get(ao_poly atom)
+{
+ ao_poly *ref = ao_lisp_atom_ref(ao_lisp_frame_global, atom);
+ if (ref)
+ return *ref;
+ return AO_LISP_NIL;
+}
+
+ao_poly
+ao_is_macro(ao_poly p)
+{
+ struct ao_lisp_builtin *builtin;
+ struct ao_lisp_lambda *lambda;
+ ao_poly ret;
+
+ MACRO_DEBUG(indent(); printf ("is macro "); ao_lisp_poly_print(p); printf("\n"); ++macro_scan_depth);
+ switch (ao_lisp_poly_type(p)) {
+ case AO_LISP_ATOM:
+ if (ao_lisp_macro_push(p))
+ ret = AO_LISP_NIL;
+ else {
+ if (ao_is_macro(ao_macro_test_get(p)))
+ ret = p;
+ else
+ ret = AO_LISP_NIL;
+ ao_lisp_macro_pop();
+ }
+ break;
+ case AO_LISP_CONS:
+ ret = ao_has_macro(p);
+ break;
+ case AO_LISP_BUILTIN:
+ builtin = ao_lisp_poly_builtin(p);
+ if ((builtin->args & AO_LISP_FUNC_MASK) == AO_LISP_FUNC_MACRO)
+ ret = p;
+ else
+ ret = 0;
+ break;
+
+ case AO_LISP_LAMBDA:
+ lambda = ao_lisp_poly_lambda(p);
+ if (lambda->args == AO_LISP_FUNC_MACRO)
+ ret = p;
+ else
+ ret = ao_has_macro(lambda->code);
+ break;
+ default:
+ ret = AO_LISP_NIL;
+ break;
+ }
+ MACRO_DEBUG(--macro_scan_depth; indent(); printf ("... "); ao_lisp_poly_print(ret); printf("\n"));
+ return ret;
+}
+
+ao_poly
+ao_has_macro(ao_poly p)
+{
+ struct ao_lisp_cons *cons;
+ struct ao_lisp_lambda *lambda;
+ ao_poly m;
+
+ if (p == AO_LISP_NIL)
+ return AO_LISP_NIL;
+
+ MACRO_DEBUG(indent(); printf("has macro "); ao_lisp_poly_print(p); printf("\n"); ++macro_scan_depth);
+ switch (ao_lisp_poly_type(p)) {
+ case AO_LISP_LAMBDA:
+ lambda = ao_lisp_poly_lambda(p);
+ p = ao_has_macro(lambda->code);
+ break;
+ case AO_LISP_CONS:
+ cons = ao_lisp_poly_cons(p);
+ if ((p = ao_is_macro(cons->car)))
+ break;
+
+ cons = ao_lisp_poly_cons(cons->cdr);
+ p = AO_LISP_NIL;
+ while (cons) {
+ m = ao_has_macro(cons->car);
+ if (m) {
+ p = m;
+ break;
+ }
+ cons = ao_lisp_poly_cons(cons->cdr);
+ }
+ break;
+
+ default:
+ p = AO_LISP_NIL;
+ break;
+ }
+ MACRO_DEBUG(--macro_scan_depth; indent(); printf("... "); ao_lisp_poly_print(p); printf("\n"));
+ return p;
+}
+
+int
+ao_lisp_read_eval_abort(void)
+{
+ ao_poly in, out = AO_LISP_NIL;
+ for(;;) {
+ in = ao_lisp_read();
+ if (in == _ao_lisp_atom_eof)
+ break;
+ out = ao_lisp_eval(in);
+ if (ao_lisp_exception)
+ return 0;
+ ao_lisp_poly_print(out);
+ putchar ('\n');
+ }
+ return 1;
+}
+
+static FILE *in;
+static FILE *out;
+
+int
+ao_lisp_getc(void)
+{
+ return getc(in);
+}
+
+static const struct option options[] = {
+ { .name = "out", .has_arg = 1, .val = 'o' },
+ { 0, 0, 0, 0 }
+};
+
+static void usage(char *program)
+{
+ fprintf(stderr, "usage: %s [--out=<output>] [input]\n", program);
+ exit(1);
+}
+