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, /* , */
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, /* ; */
90 PRINTABLE|FLOATC, /* E */
113 PRINTABLE|BACKSLASH, /* \ */
117 PRINTABLE|SPECIAL, /* ` */
122 PRINTABLE|FLOATC, /* e */
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 #define AO_SCHEME_TOKEN_MAX 128
249 static char token_string[AO_SCHEME_TOKEN_MAX];
250 static int32_t token_int;
251 static int token_len;
252 static float token_float;
254 static inline void add_token(int c) {
255 if (c && token_len < AO_SCHEME_TOKEN_MAX - 1)
256 token_string[token_len++] = c;
259 static inline void del_token(void) {
264 static inline void end_token(void) {
265 token_string[token_len] = '\0';
273 static const struct namedfloat namedfloats[] = {
274 { .name = "+inf.0", .value = INFINITY },
275 { .name = "-inf.0", .value = -INFINITY },
276 { .name = "+nan.0", .value = NAN },
277 { .name = "-nan.0", .value = NAN },
280 #define NUM_NAMED_FLOATS (sizeof namedfloats / sizeof namedfloats[0])
290 if (lex_class & ENDOFFILE)
293 if (lex_class & WHITE)
296 if (lex_class & COMMENT) {
297 while ((c = lexc()) != '\n') {
298 if (lex_class & ENDOFFILE)
304 if (lex_class & (SPECIAL|DOTC)) {
325 return UNQUOTE_SPLICING;
332 if (lex_class & POUND) {
349 alphabetic = (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'));
350 if (token_len == 0) {
365 token_int = token_string[0];
366 else if (!strcmp(token_string, "space"))
368 else if (!strcmp(token_string, "newline"))
370 else if (!strcmp(token_string, "tab"))
372 else if (!strcmp(token_string, "return"))
374 else if (!strcmp(token_string, "formfeed"))
377 ao_scheme_error(AO_SCHEME_INVALID, "invalid character token #\\%s", token_string);
383 if (lex_class & STRINGC) {
386 if (lex_class & BACKSLASH)
388 if (lex_class & (STRINGC|ENDOFFILE)) {
395 if (lex_class & PRINTABLE) {
409 if (!(lex_class & NUMBER)) {
413 if (!(lex_class & INTEGER))
415 if (token_len != epos &&
423 if (c == '.' && epos != 0)
425 if (c == 'e' || c == 'E') {
429 epos = token_len + 1;
431 if (lex_class & DIGIT) {
434 token_int = token_int * 10 + c - '0';
439 if ((lex_class & (NOTNAME)) && (c != '.' || !isfloat)) {
441 // if (lex_class & ENDOFFILE)
445 if (isint && hasdigit) {
447 token_int = -token_int;
450 if (isfloat && hasdigit) {
451 token_float = strtof(token_string, NULL);
454 for (u = 0; u < NUM_NAMED_FLOATS; u++)
455 if (!strcmp(namedfloats[u].name, token_string)) {
456 token_float = namedfloats[u].value;
466 static inline int lex(void)
468 int parse_token = _lex();
469 RDBGI("token %d (%s)\n", parse_token, token_string);
473 static int parse_token;
475 int ao_scheme_read_list;
476 struct ao_scheme_cons *ao_scheme_read_cons;
477 struct ao_scheme_cons *ao_scheme_read_cons_tail;
478 struct ao_scheme_cons *ao_scheme_read_stack;
479 static int ao_scheme_read_state;
481 #define READ_IN_QUOTE 0x01
482 #define READ_SAW_DOT 0x02
483 #define READ_DONE_DOT 0x04
484 #define READ_SAW_VECTOR 0x08
487 push_read_stack(int read_state)
489 RDBGI("push read stack %p 0x%x\n", ao_scheme_read_cons, read_state);
491 if (ao_scheme_read_list) {
492 ao_scheme_read_stack = ao_scheme_cons_cons(ao_scheme_cons_poly(ao_scheme_read_cons),
493 ao_scheme__cons(ao_scheme_int_poly(read_state),
494 ao_scheme_cons_poly(ao_scheme_read_stack)));
495 if (!ao_scheme_read_stack)
498 ao_scheme_read_state = read_state;
499 ao_scheme_read_cons = NULL;
500 ao_scheme_read_cons_tail = NULL;
508 if (ao_scheme_read_list) {
509 ao_scheme_read_cons = ao_scheme_poly_cons(ao_scheme_read_stack->car);
510 ao_scheme_read_stack = ao_scheme_poly_cons(ao_scheme_read_stack->cdr);
511 read_state = ao_scheme_poly_int(ao_scheme_read_stack->car);
512 ao_scheme_read_stack = ao_scheme_poly_cons(ao_scheme_read_stack->cdr);
513 for (ao_scheme_read_cons_tail = ao_scheme_read_cons;
514 ao_scheme_read_cons_tail && ao_scheme_read_cons_tail->cdr;
515 ao_scheme_read_cons_tail = ao_scheme_poly_cons(ao_scheme_read_cons_tail->cdr))
518 ao_scheme_read_cons = 0;
519 ao_scheme_read_cons_tail = 0;
520 ao_scheme_read_stack = 0;
521 read_state = ao_scheme_read_state;
524 RDBGI("pop read stack %p %d\n", ao_scheme_read_cons, read_state);
531 struct ao_scheme_atom *atom;
534 ao_poly v = AO_SCHEME_NIL;
536 ao_scheme_read_list = 0;
538 ao_scheme_read_cons = ao_scheme_read_cons_tail = ao_scheme_read_stack = 0;
541 while (parse_token == OPEN || parse_token == OPEN_VECTOR) {
542 if (parse_token == OPEN_VECTOR)
543 read_state |= READ_SAW_VECTOR;
544 if (!push_read_stack(read_state))
545 return AO_SCHEME_NIL;
546 ao_scheme_read_list++;
551 switch (parse_token) {
554 if (ao_scheme_read_list)
555 ao_scheme_error(AO_SCHEME_EOF, "unexpected end of file");
556 return _ao_scheme_atom_eof;
559 atom = ao_scheme_atom_intern(token_string);
561 v = ao_scheme_atom_poly(atom);
566 v = ao_scheme_integer_poly(token_int);
569 v = ao_scheme_float_get(token_float);
572 if (token_string[0] == 't')
573 v = _ao_scheme_bool_true;
575 v = _ao_scheme_bool_false;
578 string = ao_scheme_string_copy(token_string);
580 v = ao_scheme_string_poly(string);
587 case UNQUOTE_SPLICING:
588 if (!push_read_stack(read_state))
589 return AO_SCHEME_NIL;
590 ao_scheme_read_list++;
591 read_state = READ_IN_QUOTE;
592 switch (parse_token) {
594 v = _ao_scheme_atom_quote;
597 v = _ao_scheme_atom_quasiquote;
600 v = _ao_scheme_atom_unquote;
602 case UNQUOTE_SPLICING:
603 v = _ao_scheme_atom_unquote2dsplicing;
608 if (!ao_scheme_read_list) {
612 v = ao_scheme_cons_poly(ao_scheme_read_cons);
613 --ao_scheme_read_list;
614 read_state = pop_read_stack();
615 if (read_state & READ_SAW_VECTOR)
616 v = ao_scheme_vector_poly(ao_scheme_list_to_vector(ao_scheme_poly_cons(v)));
619 if (!ao_scheme_read_list) {
620 ao_scheme_error(AO_SCHEME_INVALID, ". outside of cons");
621 return AO_SCHEME_NIL;
623 if (!ao_scheme_read_cons) {
624 ao_scheme_error(AO_SCHEME_INVALID, ". first in cons");
625 return AO_SCHEME_NIL;
627 read_state |= READ_SAW_DOT;
631 /* loop over QUOTE ends */
633 if (!ao_scheme_read_list)
636 if (read_state & READ_DONE_DOT) {
637 ao_scheme_error(AO_SCHEME_INVALID, ". not last in cons");
638 return AO_SCHEME_NIL;
641 if (read_state & READ_SAW_DOT) {
642 read_state |= READ_DONE_DOT;
643 ao_scheme_read_cons_tail->cdr = v;
645 struct ao_scheme_cons *read = ao_scheme_cons_cons(v, AO_SCHEME_NIL);
647 return AO_SCHEME_NIL;
649 if (ao_scheme_read_cons_tail)
650 ao_scheme_read_cons_tail->cdr = ao_scheme_cons_poly(read);
652 ao_scheme_read_cons = read;
653 ao_scheme_read_cons_tail = read;
656 if (!(read_state & READ_IN_QUOTE) || !ao_scheme_read_cons->cdr)
659 v = ao_scheme_cons_poly(ao_scheme_read_cons);
660 --ao_scheme_read_list;
661 read_state = pop_read_stack();