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.
15 #include "ao_scheme.h"
16 #include "ao_scheme_read.h"
20 static const uint16_t lex_classes[128] = {
53 PRINTABLE|WHITE, /* */
55 PRINTABLE|STRINGC, /* " */
60 PRINTABLE|SPECIAL, /* ' */
61 PRINTABLE|SPECIAL, /* ( */
62 PRINTABLE|SPECIAL, /* ) */
64 PRINTABLE|SIGN, /* + */
65 PRINTABLE|SPECIAL_QUASI, /* , */
66 PRINTABLE|SIGN, /* - */
67 PRINTABLE|DOTC|FLOATC, /* . */
69 PRINTABLE|DIGIT, /* 0 */
70 PRINTABLE|DIGIT, /* 1 */
71 PRINTABLE|DIGIT, /* 2 */
72 PRINTABLE|DIGIT, /* 3 */
73 PRINTABLE|DIGIT, /* 4 */
74 PRINTABLE|DIGIT, /* 5 */
75 PRINTABLE|DIGIT, /* 6 */
76 PRINTABLE|DIGIT, /* 7 */
77 PRINTABLE|DIGIT, /* 8 */
78 PRINTABLE|DIGIT, /* 9 */
80 PRINTABLE|COMMENT, /* ; */
86 PRINTABLE|HEX_LETTER, /* A */
87 PRINTABLE|HEX_LETTER, /* B */
88 PRINTABLE|HEX_LETTER, /* C */
89 PRINTABLE|HEX_LETTER, /* D */
90 PRINTABLE|FLOATC|HEX_LETTER,/* E */
91 PRINTABLE|HEX_LETTER, /* F */
113 PRINTABLE|BACKSLASH, /* \ */
117 PRINTABLE|SPECIAL_QUASI, /* ` */
118 PRINTABLE|HEX_LETTER, /* a */
119 PRINTABLE|HEX_LETTER, /* b */
120 PRINTABLE|HEX_LETTER, /* c */
121 PRINTABLE|HEX_LETTER, /* d */
122 PRINTABLE|FLOATC|HEX_LETTER,/* e */
123 PRINTABLE|HEX_LETTER, /* f */
151 static int lex_unget_c;
161 c = ao_scheme_getc();
173 static uint16_t lex_class;
183 lex_class = ENDOFFILE;
186 lex_class = lex_classes[c];
188 } while (lex_class & IGNORE);
201 lex_class = ENDOFFILE;
234 if (c < '0' || '7' < c) {
238 v = (v << 3) + c - '0';
247 #ifndef AO_SCHEME_TOKEN_MAX
248 #define AO_SCHEME_TOKEN_MAX 128
251 static char token_string[AO_SCHEME_TOKEN_MAX];
252 static int32_t token_int;
253 static int token_len;
255 static inline void add_token(int c) {
256 if (c && token_len < AO_SCHEME_TOKEN_MAX - 1)
257 token_string[token_len++] = c;
260 static inline void del_token(void) {
265 static inline void end_token(void) {
266 token_string[token_len] = '\0';
269 #ifdef AO_SCHEME_FEATURE_FLOAT
270 static float token_float;
277 static const struct namedfloat namedfloats[] = {
278 { .name = "+inf.0", .value = INFINITY },
279 { .name = "-inf.0", .value = -INFINITY },
280 { .name = "+nan.0", .value = NAN },
281 { .name = "-nan.0", .value = NAN },
284 #define NUM_NAMED_FLOATS (sizeof namedfloats / sizeof namedfloats[0])
296 if ((lex_class & HEX_DIGIT) == 0) {
302 if ('0' <= c && c <= '9')
305 cval = (c | ('a' - 'A')) - 'a' + 10;
306 token_int = token_int * base + cval;
319 if (lex_class & ENDOFFILE)
322 if (lex_class & WHITE)
325 if (lex_class & COMMENT) {
326 while ((c = lexc()) != '\n') {
327 if (lex_class & ENDOFFILE)
333 if (lex_class & (SPECIAL|DOTC)) {
347 #ifdef AO_SCHEME_FEATURE_QUASI
355 return UNQUOTE_SPLICING;
374 #ifdef AO_SCHEME_FEATURE_VECTOR
382 alphabetic = (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'));
383 if (token_len == 0) {
398 token_int = token_string[0];
399 else if (!strcmp(token_string, "space"))
401 else if (!strcmp(token_string, "newline"))
403 else if (!strcmp(token_string, "tab"))
405 else if (!strcmp(token_string, "return"))
407 else if (!strcmp(token_string, "formfeed"))
410 ao_scheme_error(AO_SCHEME_INVALID, "invalid character token #\\%s", token_string);
415 return parse_int(16);
422 if (lex_class & STRINGC) {
425 if (lex_class & BACKSLASH)
427 if (lex_class & (STRINGC|ENDOFFILE)) {
434 if (lex_class & PRINTABLE) {
435 #ifdef AO_SCHEME_FEATURE_FLOAT
445 if (!(lex_class & NUMBER)) {
447 #ifdef AO_SCHEME_FEATURE_FLOAT
451 #ifdef AO_SCHEME_FEATURE_FLOAT
452 if (!(lex_class & INTEGER))
454 if (token_len != epos &&
463 #ifdef AO_SCHEME_FEATURE_FLOAT
464 if (c == '.' && epos != 0)
466 if (c == 'e' || c == 'E') {
470 epos = token_len + 1;
473 if (lex_class & DIGIT) {
476 token_int = token_int * 10 + c - '0';
481 if ((lex_class & (NOTNAME))
482 #ifdef AO_SCHEME_FEATURE_FLOAT
483 && (c != '.' || !isfloat)
486 #ifdef AO_SCHEME_FEATURE_FLOAT
489 // if (lex_class & ENDOFFILE)
493 if (isint && hasdigit) {
495 token_int = -token_int;
498 #ifdef AO_SCHEME_FEATURE_FLOAT
499 if (isfloat && hasdigit) {
500 token_float = strtof(token_string, NULL);
503 for (u = 0; u < NUM_NAMED_FLOATS; u++)
504 if (!strcmp(namedfloats[u].name, token_string)) {
505 token_float = namedfloats[u].value;
516 static inline int lex(void)
518 int parse_token = _lex();
519 RDBGI("token %d \"%s\"\n", parse_token, token_string);
523 static int parse_token;
525 int ao_scheme_read_list;
526 struct ao_scheme_cons *ao_scheme_read_cons;
527 struct ao_scheme_cons *ao_scheme_read_cons_tail;
528 struct ao_scheme_cons *ao_scheme_read_stack;
529 static int ao_scheme_read_state;
531 #define READ_IN_QUOTE 0x01
532 #define READ_SAW_DOT 0x02
533 #define READ_DONE_DOT 0x04
534 #define READ_SAW_VECTOR 0x08
537 push_read_stack(int read_state)
539 RDBGI("push read stack %p 0x%x\n", ao_scheme_read_cons, read_state);
541 if (ao_scheme_read_list) {
542 ao_scheme_read_stack = ao_scheme_cons_cons(ao_scheme_cons_poly(ao_scheme_read_cons),
543 ao_scheme_cons(ao_scheme_int_poly(read_state),
544 ao_scheme_cons_poly(ao_scheme_read_stack)));
545 if (!ao_scheme_read_stack)
548 ao_scheme_read_state = read_state;
549 ao_scheme_read_cons = NULL;
550 ao_scheme_read_cons_tail = NULL;
558 if (ao_scheme_read_list) {
559 ao_scheme_read_cons = ao_scheme_poly_cons(ao_scheme_read_stack->car);
560 ao_scheme_read_stack = ao_scheme_poly_cons(ao_scheme_read_stack->cdr);
561 read_state = ao_scheme_poly_int(ao_scheme_read_stack->car);
562 ao_scheme_read_stack = ao_scheme_poly_cons(ao_scheme_read_stack->cdr);
563 for (ao_scheme_read_cons_tail = ao_scheme_read_cons;
564 ao_scheme_read_cons_tail && ao_scheme_read_cons_tail->cdr;
565 ao_scheme_read_cons_tail = ao_scheme_poly_cons(ao_scheme_read_cons_tail->cdr))
568 read_state = ao_scheme_read_state;
569 ao_scheme_read_cons = NULL;
570 ao_scheme_read_cons_tail = NULL;
571 ao_scheme_read_stack = NULL;
572 ao_scheme_read_state = 0;
575 RDBGI("pop read stack %p %d\n", ao_scheme_read_cons, read_state);
579 #ifdef AO_SCHEME_FEATURE_VECTOR
580 #define is_open(t) ((t) == OPEN || (t) == OPEN_VECTOR)
582 #define is_open(t) ((t) == OPEN)
588 struct ao_scheme_atom *atom;
589 struct ao_scheme_string *string;
591 ao_poly v = AO_SCHEME_NIL;
593 ao_scheme_read_list = 0;
595 ao_scheme_read_cons = ao_scheme_read_cons_tail = ao_scheme_read_stack = NULL;
598 while (is_open(parse_token)) {
599 #ifdef AO_SCHEME_FEATURE_VECTOR
600 if (parse_token == OPEN_VECTOR)
601 read_state |= READ_SAW_VECTOR;
603 if (!push_read_stack(read_state))
604 return AO_SCHEME_NIL;
605 ao_scheme_read_list++;
610 switch (parse_token) {
613 if (ao_scheme_read_list)
614 ao_scheme_error(AO_SCHEME_EOF, "unexpected end of file");
615 return _ao_scheme_atom_eof;
618 atom = ao_scheme_atom_intern(token_string);
620 v = ao_scheme_atom_poly(atom);
625 v = ao_scheme_integer_poly(token_int);
627 #ifdef AO_SCHEME_FEATURE_FLOAT
629 v = ao_scheme_float_get(token_float);
633 if (token_string[0] == 't')
634 v = _ao_scheme_bool_true;
636 v = _ao_scheme_bool_false;
639 string = ao_scheme_string_make(token_string);
641 v = ao_scheme_string_poly(string);
646 #ifdef AO_SCHEME_FEATURE_QUASI
649 case UNQUOTE_SPLICING:
651 if (!push_read_stack(read_state))
652 return AO_SCHEME_NIL;
653 ao_scheme_read_list++;
654 read_state = READ_IN_QUOTE;
655 switch (parse_token) {
657 v = _ao_scheme_atom_quote;
659 #ifdef AO_SCHEME_FEATURE_QUASI
661 v = _ao_scheme_atom_quasiquote;
664 v = _ao_scheme_atom_unquote;
666 case UNQUOTE_SPLICING:
667 v = _ao_scheme_atom_unquote2dsplicing;
673 if (!ao_scheme_read_list) {
677 v = ao_scheme_cons_poly(ao_scheme_read_cons);
678 --ao_scheme_read_list;
679 read_state = pop_read_stack();
680 #ifdef AO_SCHEME_FEATURE_VECTOR
681 if (read_state & READ_SAW_VECTOR) {
682 v = ao_scheme_vector_poly(ao_scheme_list_to_vector(ao_scheme_poly_cons(v)));
683 read_state &= ~READ_SAW_VECTOR;
688 if (!ao_scheme_read_list) {
689 ao_scheme_error(AO_SCHEME_INVALID, ". outside of cons");
690 return AO_SCHEME_NIL;
692 if (!ao_scheme_read_cons) {
693 ao_scheme_error(AO_SCHEME_INVALID, ". first in cons");
694 return AO_SCHEME_NIL;
696 read_state |= READ_SAW_DOT;
700 /* loop over QUOTE ends */
702 if (!ao_scheme_read_list)
705 if (read_state & READ_DONE_DOT) {
706 ao_scheme_error(AO_SCHEME_INVALID, ". not last in cons");
707 return AO_SCHEME_NIL;
710 if (read_state & READ_SAW_DOT) {
711 read_state |= READ_DONE_DOT;
712 ao_scheme_read_cons_tail->cdr = v;
714 struct ao_scheme_cons *read = ao_scheme_cons_cons(v, AO_SCHEME_NIL);
716 return AO_SCHEME_NIL;
718 if (ao_scheme_read_cons_tail)
719 ao_scheme_read_cons_tail->cdr = ao_scheme_cons_poly(read);
721 ao_scheme_read_cons = read;
722 ao_scheme_read_cons_tail = read;
725 if (!(read_state & READ_IN_QUOTE) || !ao_scheme_read_cons->cdr)
728 v = ao_scheme_cons_poly(ao_scheme_read_cons);
729 --ao_scheme_read_list;
730 read_state = pop_read_stack();