1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016 Jan Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of Mes.
7 * Mes is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
12 * Mes is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
22 read_line_comment (int c)
24 if (c == '\n') return c;
25 return read_line_comment (getchar ());
29 read_word (int c, SCM w, SCM a)
31 if (c == EOF && w == cell_nil) return cell_nil;
32 if (c == '\t') return read_word ('\n', w, a);
33 if (c == '\f') return read_word ('\n', w, a);
34 if (c == '\n' && w == cell_nil) return read_word (getchar (), w, a);
35 if (c == '\n' && VALUE (car (w)) == '.' && cdr (w) == cell_nil) return cell_dot;
36 if (c == EOF || c == '\n') return lookup_ (w, a);
37 if (c == ' ') return read_word ('\n', w, a);
38 if (c == '(' && w == cell_nil) return read_list (a);
39 if (c == '(') {ungetchar (c); return lookup_ (w, a);}
40 if (c == ')' && w == cell_nil) {ungetchar (c); return cell_nil;}
41 if (c == ')') {ungetchar (c); return lookup_ (w, a);}
42 if (c == ';') {read_line_comment (c); return read_word ('\n', w, a);}
43 return read_word (getchar (), append2 (w, cons (MAKE_CHAR (c), cell_nil)), a);
47 eat_whitespace (int c)
49 while (c == ' ' || c == '\t' || c == '\n' || c == '\f') c = getchar ();
50 if (c == ';') return eat_whitespace (read_line_comment (c));
58 c = eat_whitespace (c);
59 if (c == ')') return cell_nil;
60 SCM w = read_word (c, cell_nil, a);
62 return car (read_list (a));
63 return cons (w, read_list (a));
69 return read_word (getchar (), cell_nil, a);
73 lookup_ (SCM s, SCM a)
75 if (isdigit (VALUE (car (s))) || (VALUE (car (s)) == '-' && cdr (s) != cell_nil)) {
78 if (VALUE (car (s)) == '-') {
83 while (p != cell_nil && isdigit (VALUE (car (p)))) {
85 n += VALUE (car (p)) - '0';
88 if (p == cell_nil) return MAKE_NUMBER (n * sign);
91 SCM x = lookup_symbol_ (s);
92 return x ? x : make_symbol_ (s);
96 list_of_char_equal_p (SCM a, SCM b)
98 while (a != cell_nil && b != cell_nil && VALUE (car (a)) == VALUE (car (b))) {
99 assert (TYPE (car (a)) == CHAR);
100 assert (TYPE (car (b)) == CHAR);
104 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
108 lookup_symbol_ (SCM s)
112 if (list_of_char_equal_p (STRING (car (x)), s) == cell_t) break;