1 /* -*-comment-start: "//";comment-end:""-*-
2 * GNU Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
4 * Copyright © 2018 Jeremiah Orians <jeremiah@pdp10.guru>
6 * This file is part of GNU Mes.
8 * GNU Mes is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
13 * GNU Mes is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
24 #define MAX_STRING 4096
27 read_input_file_env_ (SCM e, SCM a)
31 return cons (e, read_input_file_env_ (read_env (a), a));
35 read_input_file_env (SCM a)
38 return read_input_file_env_ (read_env (r0), r0);
42 reader_read_line_comment (int c)
50 error (cell_symbol_system_error,
51 MAKE_STRING (cstring_to_list ("reader_read_line_comment")));
54 SCM reader_read_block_comment (int s, int c);
55 SCM reader_read_hash (int c, SCM a);
56 SCM reader_read_list (int c, SCM a);
59 reader_identifier_p (int c)
61 return (c > ' ' && c <= '~' && c != '"' && c != ';' && c != '(' && c != ')' && c != EOF);
65 reader_end_of_word_p (int c)
67 return (c == '"' || c == ';' || c == '(' || c == ')' || isspace (c) || c == EOF);
71 reader_read_identifier_or_number (int c)
77 if (c == '+' && isdigit (peekchar ()))
79 else if (c == '-' && isdigit (peekchar ()))
91 if (reader_end_of_word_p (c))
96 return MAKE_NUMBER (n);
98 /* Fallthrough: Note that `4a', `+1b' are identifiers */
99 while (!reader_end_of_word_p (c))
106 return cstring_to_symbol (buf);
110 reader_read_sexp_ (int c, SCM a)
117 c = reader_read_line_comment (c);
120 if ((c == ' ') || (c == '\t') || (c == '\n') || (c == '\f'))
126 return reader_read_list (readchar (), a);
130 return reader_read_hash (readchar (), a);
132 return cons (cell_symbol_quasiquote,
133 cons (reader_read_sexp_ (readchar (), a), cell_nil));
136 if (peekchar () == '@')
139 return cons (cell_symbol_unquote_splicing,
140 cons (reader_read_sexp_ (readchar (), a), cell_nil));
142 return cons (cell_symbol_unquote,
143 cons (reader_read_sexp_ (readchar (), a), cell_nil));
146 return cons (cell_symbol_quote,
147 cons (reader_read_sexp_ (readchar (), a), cell_nil));
149 return reader_read_string ();
150 if (c == '.' && (!reader_identifier_p (peekchar ())))
152 return reader_read_identifier_or_number (c);
156 reader_eat_whitespace (int c)
161 return reader_eat_whitespace (reader_read_line_comment (c));
162 if (c == '#' && (peekchar () == '!' || peekchar () == '|'))
165 reader_read_block_comment (c, readchar ());
166 return reader_eat_whitespace (readchar ());
172 reader_read_list (int c, SCM a)
174 c = reader_eat_whitespace (c);
178 error (cell_symbol_not_a_pair, MAKE_STRING (cstring_to_list ("EOF in list")));
180 SCM s = reader_read_sexp_ (c, a);
182 return CAR (reader_read_list (readchar (), a));
183 return cons (s, reader_read_list (readchar (), a));
189 return reader_read_sexp_ (readchar (), a);
193 reader_read_block_comment (int s, int c)
195 if (c == s && peekchar () == '#') return readchar ();
196 return reader_read_block_comment (s, readchar ());
200 reader_read_hash (int c, SCM a)
204 reader_read_block_comment (c, readchar ());
205 return reader_read_sexp_ (readchar (), a);
209 reader_read_block_comment (c, readchar ());
210 return reader_read_sexp_ (readchar (), a);
218 if (peekchar () == '@')
221 return cons (cell_symbol_unsyntax_splicing,
222 cons (reader_read_sexp_ (readchar (), a), cell_nil));
225 return cons (cell_symbol_unsyntax,
226 cons (reader_read_sexp_ (readchar (), a), cell_nil));
229 return cons (cell_symbol_syntax,
230 cons (reader_read_sexp_ (readchar (), a), cell_nil));
232 return cons (cell_symbol_quasisyntax,
233 cons (reader_read_sexp_ (readchar (), a), cell_nil));
235 return MAKE_KEYWORD (CAR (reader_read_sexp_ (readchar (), a)));
237 return reader_read_binary ();
239 return reader_read_octal ();
241 return reader_read_hex ();
243 return reader_read_character ();
245 return list_to_vector (reader_read_list (readchar (), a));
248 reader_read_sexp_ (readchar (), a);
249 return reader_read_sexp_ (readchar (), a);
251 return reader_read_sexp_ (readchar (), a);
255 reader_read_sexp (SCM c, SCM s, SCM a)
257 return reader_read_sexp_ (VALUE (c), a);
261 reader_read_character ()
266 if (c >= '0' && c <= '7'
267 && p >= '0' && p <= '7')
270 while (p >= '0' && p <= '7')
273 c += readchar () - '0';
277 else if (((c >= 'a' && c <= 'z')
279 && ((p >= 'a' && p <= 'z')
285 while ((p >= 'a' && p <= 'z')
288 buf[i] = readchar ();
293 if (!strcmp (buf, "*eof*")) c = EOF;
294 else if (!strcmp (buf, "nul")) c = '\0';
295 else if (!strcmp (buf, "alarm")) c = '\a';
296 else if (!strcmp (buf, "backspace")) c = '\b';
297 else if (!strcmp (buf, "tab")) c = '\t';
298 else if (!strcmp (buf, "linefeed")) c = '\n';
299 else if (!strcmp (buf, "newline")) c = '\n';
300 else if (!strcmp (buf, "vtab")) c = '\v';
301 else if (!strcmp (buf, "page")) c = '\f';
304 else if (!strcmp (buf, "return")) c = 13;
305 else if (!strcmp (buf, "esc")) c = 27;
307 else if (!strcmp (buf, "return")) c = '\r';
308 //Nyacc crash else if (!strcmp (buf, "esc")) c = '\e';
310 else if (!strcmp (buf, "space")) c = ' ';
312 #if 1 // Nyacc uses old abbrevs
313 else if (!strcmp (buf, "bel")) c = '\a';
314 else if (!strcmp (buf, "bs")) c = '\b';
315 else if (!strcmp (buf, "ht")) c = '\t';
316 else if (!strcmp (buf, "vt")) c = '\v';
320 else if (!strcmp (buf, "cr")) c = 13;
322 else if (!strcmp (buf, "cr")) c = '\r';
324 #endif // Nyacc uses old abbrevs
328 eputs ("char not supported: ");
331 error (cell_symbol_system_error,
332 MAKE_STRING (cstring_to_list ("char not supported")));
335 return MAKE_CHAR (c);
339 reader_read_binary ()
350 while (c == '0' || c == '1')
359 return MAKE_NUMBER (n);
374 while (c >= '0' && c <= '7')
383 return MAKE_NUMBER (n);
398 while ((c >= '0' && c <= '9')
399 || (c >= 'A' && c <= 'F')
400 || (c >= 'a' && c <= 'f'))
404 n = n + c - 'a' + 10;
406 n = n + c - 'A' + 10;
414 return MAKE_NUMBER (n);
418 reader_read_string ()
430 if (c == '\\' || c == '"')
431 lst = cons (MAKE_CHAR (c), lst);
433 lst = cons (MAKE_CHAR ('\0'), lst);
435 lst = cons (MAKE_CHAR ('\a'), lst);
437 lst = cons (MAKE_CHAR ('\b'), lst);
439 lst = cons (MAKE_CHAR ('\t'), lst);
441 lst = cons (MAKE_CHAR ('\n'), lst);
443 lst = cons (MAKE_CHAR ('\v'), lst);
445 lst = cons (MAKE_CHAR ('\f'), lst);
448 // lst = cons (MAKE_CHAR ('\r'), lst);
449 lst = cons (MAKE_CHAR (13), lst);
452 // lst = cons (MAKE_CHAR ('\e'), lst);
453 lst = cons (MAKE_CHAR (27), lst);
456 SCM x = reader_read_hex ();
457 lst = cons (MAKE_CHAR (VALUE (x)), lst);
461 lst = cons (MAKE_CHAR (c), lst);
464 return MAKE_STRING (reverse_x_ (lst, cell_nil));
476 char *p = (char*)g_cells;
480 putchar (g_stack >> 8);
481 putchar (g_stack % 256);
483 // See HACKING, simple crafted dump for tiny-mes.c
484 if (g_tiny || getenv ("MES_TINY"))
486 eputs ("dumping TINY\n");
488 TYPE (9) = 0x2d2d2d2d;
489 CAR (9) = 0x2d2d2d2d;
490 CDR (9) = 0x3e3e3e3e;
497 CAR (11) = 0x58585858;
505 CAR (11) = 0x58585858;
508 TYPE (14) = 0x3c3c3c3c;
509 CAR (14) = 0x2d2d2d2d;
510 CDR (14) = 0x2d2d2d2d;
514 else if (g_debug > 1)
516 eputs ("program r2=");
522 for (i=0; i<g_free * sizeof (struct scm); i = i + 1)