1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) 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/>.
24 read_input_file_env_ (SCM e, SCM a)
28 return cons (e, read_input_file_env_ (read_env (a), a));
32 read_input_file_env (SCM a)
36 if (assq_ref_env (cell_symbol_read_input_file, r0) != cell_undefined)
37 return apply (cell_symbol_read_input_file, cell_nil, r0);
39 return read_input_file_env_ (read_env (r0), r0);
43 reader_read_line_comment (int c)
47 return reader_read_line_comment (getchar ());
50 SCM reader_read_block_comment (int s, int c);
51 SCM reader_read_hash (int c, SCM a);
52 SCM reader_read_list (int c, SCM a);
55 reader_identifier_p (int c)
57 return (c > ' ' && c <= '~' && c != '"' && c != ';' && c != '(' && c != ')' && c != EOF);
61 reader_end_of_word_p (int c)
63 return (c == '"' || c == ';' || c == '(' || c == ')' || isspace (c) || c == EOF);
67 reader_read_identifier_or_number (int c)
73 if (c == '+' && isdigit (peekchar ()))
75 else if (c == '-' && isdigit (peekchar ()))
87 if (reader_end_of_word_p (c))
92 return MAKE_NUMBER (n);
94 while (!reader_end_of_word_p (c))
101 return lookup_symbol_ (cstring_to_list (buf));
105 reader_read_sexp_ (int c, SCM a)
113 reader_read_line_comment (c);
118 return reader_read_sexp_ (getchar (), a);
120 return reader_read_list (getchar (), a);
124 return reader_read_hash (getchar (), a);
126 return cons (cell_symbol_quasiquote,
127 cons (reader_read_sexp_ (getchar (), a), cell_nil));
129 if (peekchar () == '@')
132 return cons (cell_symbol_unquote_splicing,
133 cons (reader_read_sexp_ (getchar (), a), cell_nil));
135 return cons (cell_symbol_unquote,
136 cons (reader_read_sexp_ (getchar (), a), cell_nil));
138 return cons (cell_symbol_quote,
139 cons (reader_read_sexp_ (getchar (), a), cell_nil));
141 return reader_read_string ();
143 if (!reader_identifier_p (peekchar ()))
146 return reader_read_identifier_or_number (c);
151 reader_eat_whitespace (int c)
156 return reader_eat_whitespace (reader_read_line_comment (c));
157 if (c == '#' && (peekchar () == '!' || peekchar () == '|'))
160 reader_read_block_comment (c, getchar ());
161 return reader_eat_whitespace (getchar ());
167 reader_read_list (int c, SCM a)
169 c = reader_eat_whitespace (c);
173 error (cell_symbol_not_a_pair, MAKE_STRING (cstring_to_list ("EOF in list")));
175 SCM s = reader_read_sexp_ (c, a);
177 return CAR (reader_read_list (getchar (), a));
178 return cons (s, reader_read_list (getchar (), a));
184 return reader_read_sexp_ (getchar (), a);
188 reader_read_block_comment (int s, int c)
190 if (c == s && peekchar () == '#') return getchar ();
191 return reader_read_block_comment (s, getchar ());
195 reader_read_hash (int c, SCM a)
200 reader_read_block_comment (c, getchar ());
201 return reader_read_sexp_ (getchar (), a);
203 reader_read_block_comment (c, getchar ());
204 return reader_read_sexp_ (getchar (), a);
210 if (peekchar () == '@')
213 return cons (cell_symbol_unsyntax_splicing,
214 cons (reader_read_sexp_ (getchar (), a),
217 return cons (cell_symbol_unsyntax,
218 cons (reader_read_sexp_ (getchar (), a), cell_nil));
220 return cons (cell_symbol_syntax,
221 cons (reader_read_sexp_ (getchar (), a), cell_nil));
223 return cons (cell_symbol_quasisyntax,
224 cons (reader_read_sexp_ (getchar (), a), cell_nil));
226 return MAKE_KEYWORD (CAR (reader_read_sexp_ (getchar (), a)));
228 return reader_read_octal ();
230 return reader_read_hex ();
232 return reader_read_character ();
234 return list_to_vector (reader_read_list (getchar (), a));
236 reader_read_sexp_ (getchar (), a);
237 return reader_read_sexp_ (getchar (), a);
239 return reader_read_sexp_ (getchar (), a);
243 reader_read_sexp (SCM c, SCM s, SCM a)
245 return reader_read_sexp_ (VALUE (c), a);
249 reader_read_character ()
252 if (c >= '0' && c <= '7'
253 && peekchar () >= '0' && peekchar () <= '7')
256 while (peekchar () >= '0' && peekchar () <= '7')
259 c += getchar () - '0';
262 else if (((c >= 'a' && c <= 'z')
264 && ((peekchar () >= 'a' && peekchar () <= 'z')
265 || peekchar () == '*'))
270 while ((peekchar () >= 'a' && peekchar () <= 'z')
271 || peekchar () == '*')
276 if (!strcmp (buf, "*eof*")) c = EOF;
277 else if (!strcmp (buf, "nul")) c = '\0';
278 else if (!strcmp (buf, "alarm")) c = '\a';
279 else if (!strcmp (buf, "backspace")) c = '\b';
280 else if (!strcmp (buf, "tab")) c = '\t';
281 else if (!strcmp (buf, "linefeed")) c = '\n';
282 else if (!strcmp (buf, "newline")) c = '\n';
283 else if (!strcmp (buf, "vtab")) c = '\v';
284 else if (!strcmp (buf, "page")) c = '\f';
287 else if (!strcmp (buf, "return")) c = 13;
288 else if (!strcmp (buf, "esc")) c = 27;
290 else if (!strcmp (buf, "return")) c = '\r';
291 //Nyacc crash else if (!strcmp (buf, "esc")) c = '\e';
293 else if (!strcmp (buf, "space")) c = ' ';
295 #if 1 // Nyacc uses old abbrevs
296 else if (!strcmp (buf, "bel")) c = '\a';
297 else if (!strcmp (buf, "bs")) c = '\b';
298 else if (!strcmp (buf, "ht")) c = '\t';
299 else if (!strcmp (buf, "vt")) c = '\v';
303 else if (!strcmp (buf, "cr")) c = 13;
305 else if (!strcmp (buf, "cr")) c = '\r';
307 #endif // Nyacc uses old abbrevs
311 eputs ("char not supported: ");
315 assert (!"char not supported");
319 return MAKE_CHAR (c);
328 if (c == '-') {s = -1;getchar (); c = peekchar ();}
329 while (c >= '0' && c <= '7')
336 return MAKE_NUMBER (s*n);
345 if (c == '-') {s = -1;getchar (); c = peekchar ();}
346 while ((c >= '0' && c <= '9')
347 || (c >= 'A' && c <= 'F')
348 || (c >= 'a' && c <= 'f'))
351 if (c >= 'a') n += c - 'a' + 10;
352 else if (c >= 'A') n += c - 'A' + 10;
357 return MAKE_NUMBER (s*n);
361 reader_read_string ()
369 if (c == '"' || i > 1022)
372 lst = append2 (lst, cstring_to_list (buf));
380 if (p == '\\' || p == '"')
381 buf[i++] = getchar ();
395 assert (!"EOF in string");
401 return MAKE_STRING (lst);
413 char *p = (char*)g_cells;
417 putchar (g_stack >> 8);
418 putchar (g_stack % 256);
422 eputs ("program r2=");
427 for (int i=0; i<g_free * sizeof (struct scm); i++)