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/>.
23 read_input_file_env_ (SCM e, SCM a)
25 if (e == cell_nil) return e;
26 return cons (e, read_input_file_env_ (read_env (a), a));
30 read_input_file_env (SCM a)
33 if (assq_ref_cache (cell_symbol_read_input_file, r0) != cell_undefined)
34 return apply_env (cell_symbol_read_input_file, cell_nil, r0);
35 return read_input_file_env_ (read_env (r0), r0);
39 read_line_comment (int c)
41 if (c == '\n') return c;
42 return read_line_comment (getchar ());
46 read_word (int c, SCM w, SCM a)
48 if (c == EOF && w == cell_nil) return cell_nil;
49 if (c == '\t') return read_word ('\n', w, a);
50 if (c == '\f') return read_word ('\n', w, a);
51 if (c == '\n' && w == cell_nil) return read_word (getchar (), w, a);
52 if (c == '\n' && VALUE (car (w)) == '.' && cdr (w) == cell_nil) return cell_dot;
53 if (c == EOF || c == '\n') return lookup_ (w, a);
54 if (c == ' ') return read_word ('\n', w, a);
55 if (c == '(' && w == cell_nil) return read_list (a);
56 if (c == '(') {ungetchar (c); return lookup_ (w, a);}
57 if (c == ')' && w == cell_nil) {ungetchar (c); return cell_nil;}
58 if (c == ')') {ungetchar (c); return lookup_ (w, a);}
59 if (c == ';') {read_line_comment (c); return read_word ('\n', w, a);}
60 return read_word (getchar (), append2 (w, cons (MAKE_CHAR (c), cell_nil)), a);
64 eat_whitespace (int c)
66 while (c == ' ' || c == '\t' || c == '\n' || c == '\f') c = getchar ();
67 if (c == ';') return eat_whitespace (read_line_comment (c));
75 c = eat_whitespace (c);
76 if (c == ')') return cell_nil;
77 SCM w = read_word (c, cell_nil, a);
79 return car (read_list (a));
80 return cons (w, read_list (a));
86 return read_word (getchar (), cell_nil, a);
90 lookup_ (SCM s, SCM a)
92 if (isdigit (VALUE (car (s))) || (VALUE (car (s)) == '-' && cdr (s) != cell_nil)) {
95 if (VALUE (car (s)) == '-') {
100 while (p != cell_nil && isdigit (VALUE (car (p)))) {
102 n += VALUE (car (p)) - '0';
105 if (p == cell_nil) return MAKE_NUMBER (n * sign);
108 SCM x = lookup_symbol_ (s);
109 return x ? x : make_symbol_ (s);
113 list_of_char_equal_p (SCM a, SCM b)
115 while (a != cell_nil && b != cell_nil && VALUE (car (a)) == VALUE (car (b))) {
116 assert (TYPE (car (a)) == CHAR);
117 assert (TYPE (car (b)) == CHAR);
121 return (a == cell_nil && b == cell_nil) ? cell_t : cell_f;
125 lookup_symbol_ (SCM s)
129 if (list_of_char_equal_p (STRING (car (x)), s) == cell_t) break;