2 * Copyright © 2016 Keith Packard <keithp@keithp.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
16 #include "ao_lisp_read.h"
18 static const uint16_t lex_classes[128] = {
51 PRINTABLE|WHITE, /* */
53 PRINTABLE|STRINGC, /* " */
54 PRINTABLE|POUND, /* # */
58 PRINTABLE|SPECIAL, /* ' */
59 PRINTABLE|SPECIAL, /* ( */
60 PRINTABLE|SPECIAL, /* ) */
62 PRINTABLE|SIGN, /* + */
64 PRINTABLE|SIGN, /* - */
65 PRINTABLE|SPECIAL, /* . */
67 PRINTABLE|DIGIT, /* 0 */
68 PRINTABLE|DIGIT, /* 1 */
69 PRINTABLE|DIGIT, /* 2 */
70 PRINTABLE|DIGIT, /* 3 */
71 PRINTABLE|DIGIT, /* 4 */
72 PRINTABLE|DIGIT, /* 5 */
73 PRINTABLE|DIGIT, /* 6 */
74 PRINTABLE|DIGIT, /* 7 */
75 PRINTABLE|DIGIT, /* 8 */
76 PRINTABLE|DIGIT, /* 9 */
78 PRINTABLE|COMMENT, /* ; */
111 PRINTABLE|BACKSLASH, /* \ */
143 PRINTABLE|VBAR, /* | */
145 PRINTABLE|TWIDDLE, /* ~ */
149 static int lex_unget_c;
210 if (c < '0' || '7' < c) {
214 v = (v << 3) + c - '0';
223 static uint16_t lex_class;
232 lex_class = ENDOFFILE;
236 lex_class = lex_classes[c];
237 if (lex_class & BACKSLASH) {
240 lex_class = ENDOFFILE;
242 lex_class = PRINTABLE;
245 } while (lex_class & IGNORE);
249 #define AO_LISP_TOKEN_MAX 32
251 static char token_string[AO_LISP_TOKEN_MAX];
252 static int token_int;
253 static int token_len;
255 static inline void add_token(int c) {
256 if (c && token_len < AO_LISP_TOKEN_MAX - 1)
257 token_string[token_len++] = c;
260 static inline void end_token(void) {
261 token_string[token_len] = '\0';
272 if (lex_class & ENDOFFILE)
275 if (lex_class & WHITE)
278 if (lex_class & COMMENT) {
279 while ((c = lexc()) != '\n') {
280 if (lex_class & ENDOFFILE)
286 if (lex_class & SPECIAL) {
302 if (lex_class & TWIDDLE) {
306 if (lex_class & POUND) {
318 if (lex_class & STRINGC) {
321 if (lex_class & (STRINGC|ENDOFFILE)) {
328 if (lex_class & PRINTABLE) {
338 if (!(lex_class & NUMBER)) {
341 if (token_len != 0 &&
348 if (lex_class & DIGIT) {
351 token_int = token_int * 10 + c - '0';
356 if (lex_class & (NOTNAME)) {
357 // if (lex_class & ENDOFFILE)
361 if (isnum && hasdigit) {
363 token_int = -token_int;
374 static inline int lex(void)
376 int parse_token = _lex();
377 DBGI("token %d (%s)\n", parse_token, token_string);
381 static int parse_token;
383 struct ao_lisp_cons *ao_lisp_read_cons;
384 struct ao_lisp_cons *ao_lisp_read_cons_tail;
385 struct ao_lisp_cons *ao_lisp_read_stack;
387 #define READ_IN_QUOTE 0x01
388 #define READ_SAW_DOT 0x02
389 #define READ_DONE_DOT 0x04
392 push_read_stack(int cons, int read_state)
394 DBGI("push read stack %p 0x%x\n", ao_lisp_read_cons, read_state);
397 ao_lisp_read_stack = ao_lisp_cons_cons(ao_lisp_cons_poly(ao_lisp_read_cons),
398 ao_lisp__cons(ao_lisp_int_poly(read_state),
399 ao_lisp_cons_poly(ao_lisp_read_stack)));
400 if (!ao_lisp_read_stack)
403 ao_lisp_read_cons = NULL;
404 ao_lisp_read_cons_tail = NULL;
409 pop_read_stack(int cons)
413 ao_lisp_read_cons = ao_lisp_poly_cons(ao_lisp_read_stack->car);
414 ao_lisp_read_stack = ao_lisp_poly_cons(ao_lisp_read_stack->cdr);
415 read_state = ao_lisp_poly_int(ao_lisp_read_stack->car);
416 ao_lisp_read_stack = ao_lisp_poly_cons(ao_lisp_read_stack->cdr);
417 for (ao_lisp_read_cons_tail = ao_lisp_read_cons;
418 ao_lisp_read_cons_tail && ao_lisp_read_cons_tail->cdr;
419 ao_lisp_read_cons_tail = ao_lisp_poly_cons(ao_lisp_read_cons_tail->cdr))
422 ao_lisp_read_cons = 0;
423 ao_lisp_read_cons_tail = 0;
424 ao_lisp_read_stack = 0;
427 DBGI("pop read stack %p %d\n", ao_lisp_read_cons, read_state);
434 struct ao_lisp_atom *atom;
443 ao_lisp_read_cons = ao_lisp_read_cons_tail = ao_lisp_read_stack = 0;
446 while (parse_token == OPEN) {
447 if (!push_read_stack(cons, read_state))
454 switch (parse_token) {
458 ao_lisp_error(AO_LISP_EOF, "unexpected end of file");
459 return _ao_lisp_atom_eof;
462 atom = ao_lisp_atom_intern(token_string);
464 v = ao_lisp_atom_poly(atom);
469 v = ao_lisp_int_poly(token_int);
472 if (token_string[0] == 't')
473 v = _ao_lisp_bool_true;
475 v = _ao_lisp_bool_false;
478 string = ao_lisp_string_copy(token_string);
480 v = ao_lisp_string_poly(string);
485 if (!push_read_stack(cons, read_state))
488 read_state |= READ_IN_QUOTE;
489 v = _ao_lisp_atom_quote;
496 v = ao_lisp_cons_poly(ao_lisp_read_cons);
498 read_state = pop_read_stack(cons);
502 ao_lisp_error(AO_LISP_INVALID, ". outside of cons");
505 if (!ao_lisp_read_cons) {
506 ao_lisp_error(AO_LISP_INVALID, ". first in cons");
509 read_state |= READ_SAW_DOT;
513 /* loop over QUOTE ends */
518 if (read_state & READ_DONE_DOT) {
519 ao_lisp_error(AO_LISP_INVALID, ". not last in cons");
523 if (read_state & READ_SAW_DOT) {
524 read_state |= READ_DONE_DOT;
525 ao_lisp_read_cons_tail->cdr = v;
527 struct ao_lisp_cons *read = ao_lisp_cons_cons(v, AO_LISP_NIL);
531 if (ao_lisp_read_cons_tail)
532 ao_lisp_read_cons_tail->cdr = ao_lisp_cons_poly(read);
534 ao_lisp_read_cons = read;
535 ao_lisp_read_cons_tail = read;
538 if (!(read_state & READ_IN_QUOTE) || !ao_lisp_read_cons->cdr)
541 v = ao_lisp_cons_poly(ao_lisp_read_cons);
543 read_state = pop_read_stack(cons);