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, /* " */
56 PRINTABLE|POUND, /* # */
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;
363 if (lex_class & POUND) {
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 ao_scheme_read_cons = 0;
569 ao_scheme_read_cons_tail = 0;
570 ao_scheme_read_stack = 0;
571 read_state = ao_scheme_read_state;
574 RDBGI("pop read stack %p %d\n", ao_scheme_read_cons, read_state);
578 #ifdef AO_SCHEME_FEATURE_VECTOR
579 #define is_open(t) ((t) == OPEN || (t) == OPEN_VECTOR)
581 #define is_open(t) ((t) == OPEN)
587 struct ao_scheme_atom *atom;
588 struct ao_scheme_string *string;
590 ao_poly v = AO_SCHEME_NIL;
592 ao_scheme_read_list = 0;
594 ao_scheme_read_cons = ao_scheme_read_cons_tail = ao_scheme_read_stack = 0;
597 while (is_open(parse_token)) {
598 #ifdef AO_SCHEME_FEATURE_VECTOR
599 if (parse_token == OPEN_VECTOR)
600 read_state |= READ_SAW_VECTOR;
602 if (!push_read_stack(read_state))
603 return AO_SCHEME_NIL;
604 ao_scheme_read_list++;
609 switch (parse_token) {
612 if (ao_scheme_read_list)
613 ao_scheme_error(AO_SCHEME_EOF, "unexpected end of file");
614 return _ao_scheme_atom_eof;
617 atom = ao_scheme_atom_intern(token_string);
619 v = ao_scheme_atom_poly(atom);
624 v = ao_scheme_integer_poly(token_int);
626 #ifdef AO_SCHEME_FEATURE_FLOAT
628 v = ao_scheme_float_get(token_float);
632 if (token_string[0] == 't')
633 v = _ao_scheme_bool_true;
635 v = _ao_scheme_bool_false;
638 string = ao_scheme_string_make(token_string);
640 v = ao_scheme_string_poly(string);
645 #ifdef AO_SCHEME_FEATURE_QUASI
648 case UNQUOTE_SPLICING:
650 if (!push_read_stack(read_state))
651 return AO_SCHEME_NIL;
652 ao_scheme_read_list++;
653 read_state = READ_IN_QUOTE;
654 switch (parse_token) {
656 v = _ao_scheme_atom_quote;
658 #ifdef AO_SCHEME_FEATURE_QUASI
660 v = _ao_scheme_atom_quasiquote;
663 v = _ao_scheme_atom_unquote;
665 case UNQUOTE_SPLICING:
666 v = _ao_scheme_atom_unquote2dsplicing;
672 if (!ao_scheme_read_list) {
676 v = ao_scheme_cons_poly(ao_scheme_read_cons);
677 --ao_scheme_read_list;
678 read_state = pop_read_stack();
679 #ifdef AO_SCHEME_FEATURE_VECTOR
680 if (read_state & READ_SAW_VECTOR)
681 v = ao_scheme_vector_poly(ao_scheme_list_to_vector(ao_scheme_poly_cons(v)));
685 if (!ao_scheme_read_list) {
686 ao_scheme_error(AO_SCHEME_INVALID, ". outside of cons");
687 return AO_SCHEME_NIL;
689 if (!ao_scheme_read_cons) {
690 ao_scheme_error(AO_SCHEME_INVALID, ". first in cons");
691 return AO_SCHEME_NIL;
693 read_state |= READ_SAW_DOT;
697 /* loop over QUOTE ends */
699 if (!ao_scheme_read_list)
702 if (read_state & READ_DONE_DOT) {
703 ao_scheme_error(AO_SCHEME_INVALID, ". not last in cons");
704 return AO_SCHEME_NIL;
707 if (read_state & READ_SAW_DOT) {
708 read_state |= READ_DONE_DOT;
709 ao_scheme_read_cons_tail->cdr = v;
711 struct ao_scheme_cons *read = ao_scheme_cons_cons(v, AO_SCHEME_NIL);
713 return AO_SCHEME_NIL;
715 if (ao_scheme_read_cons_tail)
716 ao_scheme_read_cons_tail->cdr = ao_scheme_cons_poly(read);
718 ao_scheme_read_cons = read;
719 ao_scheme_read_cons_tail = read;
722 if (!(read_state & READ_IN_QUOTE) || !ao_scheme_read_cons->cdr)
725 v = ao_scheme_cons_poly(ao_scheme_read_cons);
726 --ao_scheme_read_list;
727 read_state = pop_read_stack();