altos/lisp: Add non-cons cdr support
[fw/altos] / src / lisp / ao_lisp_read.c
index ea98b9767cb59882ca13e34ffeb343b623995c46..550f62c2c19d94c3c5431ea2fa0eb0fedf1a330e 100644 (file)
@@ -62,7 +62,7 @@ static const uint16_t lex_classes[128] = {
        PRINTABLE|SIGN,         /* + */
        PRINTABLE,              /* , */
        PRINTABLE|SIGN,         /* - */
-       PRINTABLE,              /* . */
+       PRINTABLE|DOTC,         /* . */
        PRINTABLE,              /* / */
        PRINTABLE|DIGIT,        /* 0 */
        PRINTABLE|DIGIT,        /* 1 */
@@ -156,19 +156,7 @@ lex_get()
                c = lex_unget_c;
                lex_unget_c = 0;
        } else {
-#if AO_LISP_ALTOS
-               static uint8_t  at_eol;
-
-               if (at_eol) {
-                       ao_cmd_readline();
-                       at_eol = 0;
-               }
-               c = ao_cmd_lex();
-               if (c == '\n')
-                       at_eol = 1;
-#else
-               c = getchar();
-#endif
+               c = ao_lisp_getc();
        }
        return c;
 }
@@ -188,8 +176,6 @@ lex_quoted (void)
        int     count;
 
        c = lex_get();
-//     if (jumping)
-//             return nil;
        if (c == EOF)
                return EOF;
        c &= 0x7f;
@@ -218,8 +204,6 @@ lex_quoted (void)
                count = 1;
                while (count <= 3) {
                        c = lex_get();
-//                     if (jumping)
-//                             return nil;
                        if (c == EOF)
                                return EOF;
                        c &= 0x7f;
@@ -278,7 +262,7 @@ static inline void end_token(void) {
 }
 
 static int
-lex(void)
+_lex(void)
 {
        int     c;
 
@@ -286,13 +270,19 @@ lex(void)
        for (;;) {
                c = lexc();
                if (lex_class & ENDOFFILE)
-                       return AO_LISP_NIL;
+                       return END;
 
-//             if (jumping)
-//                     return nil;
                if (lex_class & WHITE)
                        continue;
 
+               if (lex_class & COMMENT) {
+                       while ((c = lexc()) != '\n') {
+                               if (lex_class & ENDOFFILE)
+                                       return END;
+                       }
+                       continue;
+               }
+
                if (lex_class & (BRA|KET|QUOTEC)) {
                        add_token(c);
                        end_token();
@@ -305,6 +295,11 @@ lex(void)
                                return QUOTE;
                        }
                }
+               if (lex_class & (DOTC)) {
+                       add_token(c);
+                       end_token();
+                       return DOT;
+               }
                if (lex_class & TWIDDLE) {
                        token_int = lexc();
                        return NUM;
@@ -312,8 +307,6 @@ lex(void)
                if (lex_class & STRINGC) {
                        for (;;) {
                                c = lexc();
-//                             if (jumping)
-//                                     return nil;
                                if (lex_class & (STRINGC|ENDOFFILE)) {
                                        end_token();
                                        return STRING;
@@ -349,8 +342,6 @@ lex(void)
                                }
                                add_token (c);
                                c = lexc ();
-//                             if (jumping)
-//                                     return nil;
                                if (lex_class & (NOTNAME)) {
 //                                     if (lex_class & ENDOFFILE)
 //                                             clearerr (f);
@@ -369,41 +360,92 @@ lex(void)
        }
 }
 
+static inline int lex(void)
+{
+       int     parse_token = _lex();
+       DBGI("token %d (%s)\n", parse_token, token_string);
+       return parse_token;
+}
+
 static int parse_token;
-static uint8_t                 been_here;
-static struct ao_lisp_cons     *read_cons;
-static struct ao_lisp_cons     *read_cons_tail;
-static struct ao_lisp_cons     *read_stack;
 
-static ao_poly
-read_item(void)
+struct ao_lisp_cons    *ao_lisp_read_cons;
+struct ao_lisp_cons    *ao_lisp_read_cons_tail;
+struct ao_lisp_cons    *ao_lisp_read_stack;
+
+#define READ_IN_QUOTE  0x01
+#define READ_SAW_DOT   0x02
+#define READ_DONE_DOT  0x04
+
+static int
+push_read_stack(int cons, int read_state)
+{
+       DBGI("push read stack %p 0x%x\n", ao_lisp_read_cons, read_state);
+       DBG_IN();
+       if (cons) {
+               ao_lisp_read_stack = ao_lisp_cons_cons(ao_lisp_cons_poly(ao_lisp_read_cons),
+                                                      ao_lisp__cons(ao_lisp_int_poly(read_state),
+                                                                    ao_lisp_cons_poly(ao_lisp_read_stack)));
+               if (!ao_lisp_read_stack)
+                       return 0;
+       }
+       ao_lisp_read_cons = NULL;
+       ao_lisp_read_cons_tail = NULL;
+       return 1;
+}
+
+static int
+pop_read_stack(int cons)
+{
+       int     read_state = 0;
+       if (cons) {
+               ao_lisp_read_cons = ao_lisp_poly_cons(ao_lisp_read_stack->car);
+               ao_lisp_read_stack = ao_lisp_poly_cons(ao_lisp_read_stack->cdr);
+               read_state = ao_lisp_poly_int(ao_lisp_read_stack->car);
+               ao_lisp_read_stack = ao_lisp_poly_cons(ao_lisp_read_stack->cdr);
+               for (ao_lisp_read_cons_tail = ao_lisp_read_cons;
+                    ao_lisp_read_cons_tail && ao_lisp_read_cons_tail->cdr;
+                    ao_lisp_read_cons_tail = ao_lisp_poly_cons(ao_lisp_read_cons_tail->cdr))
+                       ;
+       } else {
+               ao_lisp_read_cons = 0;
+               ao_lisp_read_cons_tail = 0;
+               ao_lisp_read_stack = 0;
+       }
+       DBG_OUT();
+       DBGI("pop read stack %p %d\n", ao_lisp_read_cons, read_state);
+       return read_state;
+}
+
+ao_poly
+ao_lisp_read(void)
 {
        struct ao_lisp_atom     *atom;
        char                    *string;
        int                     cons;
+       int                     read_state;
        ao_poly                 v;
 
-       if (!been_here) {
-               ao_lisp_root_add(&ao_lisp_cons_type, &read_cons);
-               ao_lisp_root_add(&ao_lisp_cons_type, &read_cons_tail);
-               ao_lisp_root_add(&ao_lisp_cons_type, &read_stack);
-       }
 
        cons = 0;
-       read_cons = read_cons_tail = read_stack = 0;
+       read_state = 0;
+       ao_lisp_read_cons = ao_lisp_read_cons_tail = ao_lisp_read_stack = 0;
        for (;;) {
+               parse_token = lex();
                while (parse_token == OPEN) {
-                       if (cons++)
-                               read_stack = ao_lisp_cons_cons(ao_lisp_cons_poly(read_cons), read_stack);
-                       read_cons = NULL;
-                       read_cons_tail = NULL;
+                       if (!push_read_stack(cons, read_state))
+                               return AO_LISP_NIL;
+                       cons++;
+                       read_state = 0;
                        parse_token = lex();
                }
 
                switch (parse_token) {
-               case ENDOFFILE:
+               case END:
                default:
-                       v = AO_LISP_NIL;
+                       if (cons)
+                               ao_lisp_error(AO_LISP_EOF, "unexpected end of file");
+                       return _ao_lisp_atom_eof;
                        break;
                case NAME:
                        atom = ao_lisp_atom_intern(token_string);
@@ -422,40 +464,67 @@ read_item(void)
                        else
                                v = AO_LISP_NIL;
                        break;
+               case QUOTE:
+                       if (!push_read_stack(cons, read_state))
+                               return AO_LISP_NIL;
+                       cons++;
+                       read_state |= READ_IN_QUOTE;
+                       v = _ao_lisp_atom_quote;
+                       break;
                case CLOSE:
-                       if (cons)
-                               v = ao_lisp_cons_poly(read_cons);
-                       else
+                       if (!cons) {
                                v = AO_LISP_NIL;
-                       if (--cons) {
-                               read_cons = ao_lisp_poly_cons(read_stack->car);
-                               read_stack = ao_lisp_poly_cons(read_stack->cdr);
-                               for (read_cons_tail = read_cons;
-                                    read_cons_tail && read_cons_tail->cdr;
-                                    read_cons_tail = ao_lisp_poly_cons(read_cons_tail->cdr))
-                                       ;
+                               break;
                        }
+                       v = ao_lisp_cons_poly(ao_lisp_read_cons);
+                       --cons;
+                       read_state = pop_read_stack(cons);
                        break;
+               case DOT:
+                       if (!cons) {
+                               ao_lisp_error(AO_LISP_INVALID, ". outside of cons");
+                               return AO_LISP_NIL;
+                       }
+                       if (!ao_lisp_read_cons) {
+                               ao_lisp_error(AO_LISP_INVALID, ". first in cons");
+                               return AO_LISP_NIL;
+                       }
+                       read_state |= READ_SAW_DOT;
+                       continue;
                }
 
-               if (!cons)
-                       break;
+               /* loop over QUOTE ends */
+               for (;;) {
+                       if (!cons)
+                               return v;
+
+                       if (read_state & READ_DONE_DOT) {
+                               ao_lisp_error(AO_LISP_INVALID, ". not last in cons");
+                               return AO_LISP_NIL;
+                       }
 
-               struct ao_lisp_cons     *read = ao_lisp_cons_cons(v, NULL);
-               if (read_cons_tail)
-                       read_cons_tail->cdr = ao_lisp_cons_poly(read);
-               else
-                       read_cons = read;
-               read_cons_tail = read;
+                       if (read_state & READ_SAW_DOT) {
+                               read_state |= READ_DONE_DOT;
+                               ao_lisp_read_cons_tail->cdr = v;
+                       } else {
+                               struct ao_lisp_cons     *read = ao_lisp_cons_cons(v, AO_LISP_NIL);
+                               if (!read)
+                                       return AO_LISP_NIL;
 
-               parse_token = lex();
+                               if (ao_lisp_read_cons_tail)
+                                       ao_lisp_read_cons_tail->cdr = ao_lisp_cons_poly(read);
+                               else
+                                       ao_lisp_read_cons = read;
+                               ao_lisp_read_cons_tail = read;
+                       }
+
+                       if (!(read_state & READ_IN_QUOTE) || !ao_lisp_read_cons->cdr)
+                               break;
+
+                       v = ao_lisp_cons_poly(ao_lisp_read_cons);
+                       --cons;
+                       read_state = pop_read_stack(cons);
+               }
        }
        return v;
 }
-
-ao_poly
-ao_lisp_read(void)
-{
-       parse_token = lex();
-       return read_item();
-}