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, /* ; */
90 PRINTABLE|FLOATC, /* E */
113 PRINTABLE|BACKSLASH, /* \ */
117 PRINTABLE|SPECIAL_QUASI, /* ` */
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 #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])
295 if (lex_class & ENDOFFILE)
298 if (lex_class & WHITE)
301 if (lex_class & COMMENT) {
302 while ((c = lexc()) != '\n') {
303 if (lex_class & ENDOFFILE)
309 if (lex_class & (SPECIAL|DOTC)) {
323 #ifdef AO_SCHEME_FEATURE_QUASI
331 return UNQUOTE_SPLICING;
339 if (lex_class & POUND) {
350 #ifdef AO_SCHEME_FEATURE_VECTOR
358 alphabetic = (('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'));
359 if (token_len == 0) {
374 token_int = token_string[0];
375 else if (!strcmp(token_string, "space"))
377 else if (!strcmp(token_string, "newline"))
379 else if (!strcmp(token_string, "tab"))
381 else if (!strcmp(token_string, "return"))
383 else if (!strcmp(token_string, "formfeed"))
386 ao_scheme_error(AO_SCHEME_INVALID, "invalid character token #\\%s", token_string);
392 if (lex_class & STRINGC) {
395 if (lex_class & BACKSLASH)
397 if (lex_class & (STRINGC|ENDOFFILE)) {
404 if (lex_class & PRINTABLE) {
405 #ifdef AO_SCHEME_FEATURE_FLOAT
415 if (!(lex_class & NUMBER)) {
417 #ifdef AO_SCHEME_FEATURE_FLOAT
421 #ifdef AO_SCHEME_FEATURE_FLOAT
422 if (!(lex_class & INTEGER))
424 if (token_len != epos &&
433 #ifdef AO_SCHEME_FEATURE_FLOAT
434 if (c == '.' && epos != 0)
436 if (c == 'e' || c == 'E') {
440 epos = token_len + 1;
443 if (lex_class & DIGIT) {
446 token_int = token_int * 10 + c - '0';
451 if ((lex_class & (NOTNAME))
452 #ifdef AO_SCHEME_FEATURE_FLOAT
453 && (c != '.' || !isfloat)
456 #ifdef AO_SCHEME_FEATURE_FLOAT
459 // if (lex_class & ENDOFFILE)
463 if (isint && hasdigit) {
465 token_int = -token_int;
468 #ifdef AO_SCHEME_FEATURE_FLOAT
469 if (isfloat && hasdigit) {
470 token_float = strtof(token_string, NULL);
473 for (u = 0; u < NUM_NAMED_FLOATS; u++)
474 if (!strcmp(namedfloats[u].name, token_string)) {
475 token_float = namedfloats[u].value;
486 static inline int lex(void)
488 int parse_token = _lex();
489 RDBGI("token %d (%s)\n", parse_token, token_string);
493 static int parse_token;
495 int ao_scheme_read_list;
496 struct ao_scheme_cons *ao_scheme_read_cons;
497 struct ao_scheme_cons *ao_scheme_read_cons_tail;
498 struct ao_scheme_cons *ao_scheme_read_stack;
499 static int ao_scheme_read_state;
501 #define READ_IN_QUOTE 0x01
502 #define READ_SAW_DOT 0x02
503 #define READ_DONE_DOT 0x04
504 #define READ_SAW_VECTOR 0x08
507 push_read_stack(int read_state)
509 RDBGI("push read stack %p 0x%x\n", ao_scheme_read_cons, read_state);
511 if (ao_scheme_read_list) {
512 ao_scheme_read_stack = ao_scheme_cons_cons(ao_scheme_cons_poly(ao_scheme_read_cons),
513 ao_scheme_cons(ao_scheme_int_poly(read_state),
514 ao_scheme_cons_poly(ao_scheme_read_stack)));
515 if (!ao_scheme_read_stack)
518 ao_scheme_read_state = read_state;
519 ao_scheme_read_cons = NULL;
520 ao_scheme_read_cons_tail = NULL;
528 if (ao_scheme_read_list) {
529 ao_scheme_read_cons = ao_scheme_poly_cons(ao_scheme_read_stack->car);
530 ao_scheme_read_stack = ao_scheme_poly_cons(ao_scheme_read_stack->cdr);
531 read_state = ao_scheme_poly_int(ao_scheme_read_stack->car);
532 ao_scheme_read_stack = ao_scheme_poly_cons(ao_scheme_read_stack->cdr);
533 for (ao_scheme_read_cons_tail = ao_scheme_read_cons;
534 ao_scheme_read_cons_tail && ao_scheme_read_cons_tail->cdr;
535 ao_scheme_read_cons_tail = ao_scheme_poly_cons(ao_scheme_read_cons_tail->cdr))
538 ao_scheme_read_cons = 0;
539 ao_scheme_read_cons_tail = 0;
540 ao_scheme_read_stack = 0;
541 read_state = ao_scheme_read_state;
544 RDBGI("pop read stack %p %d\n", ao_scheme_read_cons, read_state);
548 #ifdef AO_SCHEME_FEATURE_VECTOR
549 #define is_open(t) ((t) == OPEN || (t) == OPEN_VECTOR)
551 #define is_open(t) ((t) == OPEN)
557 struct ao_scheme_atom *atom;
558 struct ao_scheme_string *string;
560 ao_poly v = AO_SCHEME_NIL;
562 ao_scheme_read_list = 0;
564 ao_scheme_read_cons = ao_scheme_read_cons_tail = ao_scheme_read_stack = 0;
567 while (is_open(parse_token)) {
568 #ifdef AO_SCHEME_FEATURE_VECTOR
569 if (parse_token == OPEN_VECTOR)
570 read_state |= READ_SAW_VECTOR;
572 if (!push_read_stack(read_state))
573 return AO_SCHEME_NIL;
574 ao_scheme_read_list++;
579 switch (parse_token) {
582 if (ao_scheme_read_list)
583 ao_scheme_error(AO_SCHEME_EOF, "unexpected end of file");
584 return _ao_scheme_atom_eof;
587 atom = ao_scheme_atom_intern(token_string);
589 v = ao_scheme_atom_poly(atom);
594 v = ao_scheme_integer_poly(token_int);
596 #ifdef AO_SCHEME_FEATURE_FLOAT
598 v = ao_scheme_float_get(token_float);
602 if (token_string[0] == 't')
603 v = _ao_scheme_bool_true;
605 v = _ao_scheme_bool_false;
608 string = ao_scheme_string_make(token_string);
610 v = ao_scheme_string_poly(string);
615 #ifdef AO_SCHEME_FEATURE_QUASI
618 case UNQUOTE_SPLICING:
620 if (!push_read_stack(read_state))
621 return AO_SCHEME_NIL;
622 ao_scheme_read_list++;
623 read_state = READ_IN_QUOTE;
624 switch (parse_token) {
626 v = _ao_scheme_atom_quote;
628 #ifdef AO_SCHEME_FEATURE_QUASI
630 v = _ao_scheme_atom_quasiquote;
633 v = _ao_scheme_atom_unquote;
635 case UNQUOTE_SPLICING:
636 v = _ao_scheme_atom_unquote2dsplicing;
642 if (!ao_scheme_read_list) {
646 v = ao_scheme_cons_poly(ao_scheme_read_cons);
647 --ao_scheme_read_list;
648 read_state = pop_read_stack();
649 #ifdef AO_SCHEME_FEATURE_VECTOR
650 if (read_state & READ_SAW_VECTOR)
651 v = ao_scheme_vector_poly(ao_scheme_list_to_vector(ao_scheme_poly_cons(v)));
655 if (!ao_scheme_read_list) {
656 ao_scheme_error(AO_SCHEME_INVALID, ". outside of cons");
657 return AO_SCHEME_NIL;
659 if (!ao_scheme_read_cons) {
660 ao_scheme_error(AO_SCHEME_INVALID, ". first in cons");
661 return AO_SCHEME_NIL;
663 read_state |= READ_SAW_DOT;
667 /* loop over QUOTE ends */
669 if (!ao_scheme_read_list)
672 if (read_state & READ_DONE_DOT) {
673 ao_scheme_error(AO_SCHEME_INVALID, ". not last in cons");
674 return AO_SCHEME_NIL;
677 if (read_state & READ_SAW_DOT) {
678 read_state |= READ_DONE_DOT;
679 ao_scheme_read_cons_tail->cdr = v;
681 struct ao_scheme_cons *read = ao_scheme_cons_cons(v, AO_SCHEME_NIL);
683 return AO_SCHEME_NIL;
685 if (ao_scheme_read_cons_tail)
686 ao_scheme_read_cons_tail->cdr = ao_scheme_cons_poly(read);
688 ao_scheme_read_cons = read;
689 ao_scheme_read_cons_tail = read;
692 if (!(read_state & READ_IN_QUOTE) || !ao_scheme_read_cons->cdr)
695 v = ao_scheme_cons_poly(ao_scheme_read_cons);
696 --ao_scheme_read_list;
697 read_state = pop_read_stack();