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"
19 static const uint16_t lex_classes[128] = {
52 PRINTABLE|WHITE, /* */
54 PRINTABLE|STRINGC, /* " */
55 PRINTABLE|POUND, /* # */
59 PRINTABLE|SPECIAL, /* ' */
60 PRINTABLE|SPECIAL, /* ( */
61 PRINTABLE|SPECIAL, /* ) */
63 PRINTABLE|SIGN, /* + */
65 PRINTABLE|SIGN, /* - */
66 PRINTABLE|DOTC|FLOATC, /* . */
68 PRINTABLE|DIGIT, /* 0 */
69 PRINTABLE|DIGIT, /* 1 */
70 PRINTABLE|DIGIT, /* 2 */
71 PRINTABLE|DIGIT, /* 3 */
72 PRINTABLE|DIGIT, /* 4 */
73 PRINTABLE|DIGIT, /* 5 */
74 PRINTABLE|DIGIT, /* 6 */
75 PRINTABLE|DIGIT, /* 7 */
76 PRINTABLE|DIGIT, /* 8 */
77 PRINTABLE|DIGIT, /* 9 */
79 PRINTABLE|COMMENT, /* ; */
89 PRINTABLE|FLOATC, /* E */
112 PRINTABLE|BACKSLASH, /* \ */
121 PRINTABLE|FLOATC, /* e */
150 static int lex_unget_c;
172 static uint16_t lex_class;
182 lex_class = ENDOFFILE;
185 lex_class = lex_classes[c];
187 } while (lex_class & IGNORE);
200 lex_class = ENDOFFILE;
233 if (c < '0' || '7' < c) {
237 v = (v << 3) + c - '0';
246 #define AO_LISP_TOKEN_MAX 32
248 static char token_string[AO_LISP_TOKEN_MAX];
249 static int32_t token_int;
250 static int token_len;
251 static float token_float;
253 static inline void add_token(int c) {
254 if (c && token_len < AO_LISP_TOKEN_MAX - 1)
255 token_string[token_len++] = c;
258 static inline void del_token(void) {
263 static inline void end_token(void) {
264 token_string[token_len] = '\0';
272 static const struct namedfloat namedfloats[] = {
273 { .name = "+inf.0", .value = INFINITY },
274 { .name = "-inf.0", .value = -INFINITY },
275 { .name = "+nan.0", .value = NAN },
276 { .name = "-nan.0", .value = NAN },
279 #define NUM_NAMED_FLOATS (sizeof namedfloats / sizeof namedfloats[0])
289 if (lex_class & ENDOFFILE)
292 if (lex_class & WHITE)
295 if (lex_class & COMMENT) {
296 while ((c = lexc()) != '\n') {
297 if (lex_class & ENDOFFILE)
303 if (lex_class & (SPECIAL|DOTC)) {
319 if (lex_class & POUND) {
334 alphabetic = (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'));
335 if (token_len == 0) {
350 token_int = token_string[0];
351 else if (!strcmp(token_string, "space"))
353 else if (!strcmp(token_string, "newline"))
355 else if (!strcmp(token_string, "tab"))
357 else if (!strcmp(token_string, "return"))
359 else if (!strcmp(token_string, "formfeed"))
362 ao_lisp_error(AO_LISP_INVALID, "invalid character token #\\%s", token_string);
368 if (lex_class & STRINGC) {
371 if (lex_class & BACKSLASH)
373 if (lex_class & (STRINGC|ENDOFFILE)) {
380 if (lex_class & PRINTABLE) {
394 if (!(lex_class & NUMBER)) {
398 if (!(lex_class & INTEGER))
400 if (token_len != epos &&
408 if (c == '.' && epos != 0)
410 if (c == 'e' || c == 'E') {
414 epos = token_len + 1;
416 if (lex_class & DIGIT) {
419 token_int = token_int * 10 + c - '0';
424 if ((lex_class & (NOTNAME)) && (c != '.' || !isfloat)) {
426 // if (lex_class & ENDOFFILE)
430 if (isint && hasdigit) {
432 token_int = -token_int;
435 if (isfloat && hasdigit) {
436 token_float = atof(token_string);
439 for (u = 0; u < NUM_NAMED_FLOATS; u++)
440 if (!strcmp(namedfloats[u].name, token_string)) {
441 token_float = namedfloats[u].value;
451 static inline int lex(void)
453 int parse_token = _lex();
454 DBGI("token %d (%s)\n", parse_token, token_string);
458 static int parse_token;
460 struct ao_lisp_cons *ao_lisp_read_cons;
461 struct ao_lisp_cons *ao_lisp_read_cons_tail;
462 struct ao_lisp_cons *ao_lisp_read_stack;
464 #define READ_IN_QUOTE 0x01
465 #define READ_SAW_DOT 0x02
466 #define READ_DONE_DOT 0x04
469 push_read_stack(int cons, int read_state)
471 DBGI("push read stack %p 0x%x\n", ao_lisp_read_cons, read_state);
474 ao_lisp_read_stack = ao_lisp_cons_cons(ao_lisp_cons_poly(ao_lisp_read_cons),
475 ao_lisp__cons(ao_lisp_int_poly(read_state),
476 ao_lisp_cons_poly(ao_lisp_read_stack)));
477 if (!ao_lisp_read_stack)
480 ao_lisp_read_cons = NULL;
481 ao_lisp_read_cons_tail = NULL;
486 pop_read_stack(int cons)
490 ao_lisp_read_cons = ao_lisp_poly_cons(ao_lisp_read_stack->car);
491 ao_lisp_read_stack = ao_lisp_poly_cons(ao_lisp_read_stack->cdr);
492 read_state = ao_lisp_poly_int(ao_lisp_read_stack->car);
493 ao_lisp_read_stack = ao_lisp_poly_cons(ao_lisp_read_stack->cdr);
494 for (ao_lisp_read_cons_tail = ao_lisp_read_cons;
495 ao_lisp_read_cons_tail && ao_lisp_read_cons_tail->cdr;
496 ao_lisp_read_cons_tail = ao_lisp_poly_cons(ao_lisp_read_cons_tail->cdr))
499 ao_lisp_read_cons = 0;
500 ao_lisp_read_cons_tail = 0;
501 ao_lisp_read_stack = 0;
504 DBGI("pop read stack %p %d\n", ao_lisp_read_cons, read_state);
511 struct ao_lisp_atom *atom;
520 ao_lisp_read_cons = ao_lisp_read_cons_tail = ao_lisp_read_stack = 0;
523 while (parse_token == OPEN) {
524 if (!push_read_stack(cons, read_state))
531 switch (parse_token) {
535 ao_lisp_error(AO_LISP_EOF, "unexpected end of file");
536 return _ao_lisp_atom_eof;
539 atom = ao_lisp_atom_intern(token_string);
541 v = ao_lisp_atom_poly(atom);
546 v = ao_lisp_integer_poly(token_int);
549 v = ao_lisp_float_get(token_float);
552 if (token_string[0] == 't')
553 v = _ao_lisp_bool_true;
555 v = _ao_lisp_bool_false;
558 string = ao_lisp_string_copy(token_string);
560 v = ao_lisp_string_poly(string);
565 if (!push_read_stack(cons, read_state))
568 read_state = READ_IN_QUOTE;
569 v = _ao_lisp_atom_quote;
576 v = ao_lisp_cons_poly(ao_lisp_read_cons);
578 read_state = pop_read_stack(cons);
582 ao_lisp_error(AO_LISP_INVALID, ". outside of cons");
585 if (!ao_lisp_read_cons) {
586 ao_lisp_error(AO_LISP_INVALID, ". first in cons");
589 read_state |= READ_SAW_DOT;
593 /* loop over QUOTE ends */
598 if (read_state & READ_DONE_DOT) {
599 ao_lisp_error(AO_LISP_INVALID, ". not last in cons");
603 if (read_state & READ_SAW_DOT) {
604 read_state |= READ_DONE_DOT;
605 ao_lisp_read_cons_tail->cdr = v;
607 struct ao_lisp_cons *read = ao_lisp_cons_cons(v, AO_LISP_NIL);
611 if (ao_lisp_read_cons_tail)
612 ao_lisp_read_cons_tail->cdr = ao_lisp_cons_poly(read);
614 ao_lisp_read_cons = read;
615 ao_lisp_read_cons_tail = read;
618 if (!(read_state & READ_IN_QUOTE) || !ao_lisp_read_cons->cdr)
621 v = ao_lisp_cons_poly(ao_lisp_read_cons);
623 read_state = pop_read_stack(cons);