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/>.
22 SCM fdisplay_ (SCM, int, int);
25 display_helper (SCM x, int cont, char* sep, int fd, int write_p)
29 return cell_unspecified;
30 g_depth = g_depth - 1;
37 fdputc (VALUE (x), fd);
43 case '\0': fdputs ("nul", fd); break;
44 case '\a': fdputs ("alarm", fd); break;
45 case '\b': fdputs ("backspace", fd); break;
46 case '\t': fdputs ("tab", fd); break;
47 case '\n': fdputs ("newline", fd); break;
48 case '\v': fdputs ("vtab", fd); break;
49 case '\f': fdputs ("page", fd); break;
51 // case '\r': fdputs ("return", fd); break;
52 case 13: fdputs ("return", fd); break;
53 case ' ': fdputs ("space", fd); break;
54 default: fdputc (VALUE (x), fd);
61 fdputs ("#<closure ", fd);
62 display_helper (CDR (x), cont, "", fd, 0);
68 fdputs ("#<procedure ", fd);
70 if (FUNCTION (x).name != 0)
71 p = FUNCTION (x).name;
74 fdputs (itoa (CDR (x)), fd);
76 fdputs (itoa (x), fd);
82 fdputs ("#<macro ", fd);
83 display_helper (CDR (x), cont, "", fd, 0);
89 fdputs ("#<variable ", fd);
90 display_helper (CAR (VARIABLE (x)), cont, "", fd, 0);
96 fdputs (itoa (VALUE (x)), fd);
103 if (CAR (x) == cell_circular
104 && CADR (x) != cell_closure)
106 fdputs ("(*circ* . ", fd);
109 while (x != cell_nil && i++ < 10)
111 fdisplay_ (CAAR (x), fd, write_p); fdputs (" ", fd);
114 fdputs (" ...)", fd);
118 if (x && x != cell_nil)
119 fdisplay_ (CAR (x), fd, write_p);
120 if (CDR (x) && TYPE (CDR (x)) == TPAIR)
121 display_helper (CDR (x), 1, " ", fd, write_p);
122 else if (CDR (x) && CDR (x) != cell_nil)
124 if (TYPE (CDR (x)) != TPAIR)
126 fdisplay_ (CDR (x), fd, write_p);
139 if (TYPE (x) == TPORT)
141 fdputs ("#<port ", fd);
142 fdputs (itoa (PORT (x)), fd);
145 if (TYPE (x) == TKEYWORD)
147 if ((write_p && TYPE (x) == TSTRING) || TYPE (x) == TPORT)
150 while (t && t != cell_nil)
152 switch (write_p ? VALUE (CAR (t)) : -1)
154 case '\0': fdputs ("\\0", fd); break;
155 case '\a': fdputs ("\\a", fd); break;
156 case '\b': fdputs ("\\b", fd); break;
157 case '\t': fdputs ("\\t", fd); break;
158 case '\v': fdputs ("\\v", fd); break;
159 case '\n': fdputs ("\\n", fd); break;
160 case '\f': fdputs ("\\f", fd); break;
163 case 13: fdputs ("\\r", fd); break;
164 case 27: fdputs ("\\e", fd); break;
166 //case '\r': fdputs ("\\r", fd); break;
168 //case '\e': fdputs ("\\e", fd); break;
170 case '\\': fdputs ("\\\\", fd); break;
171 case '"': fdputs ("\\\"", fd); break;
173 fdputc (VALUE (CAR (t)), fd);
177 if ((write_p && TYPE (x) == TSTRING) || TYPE (x) == TPORT)
179 if (TYPE (x) == TPORT)
187 for (int i = 0; i < LENGTH (x); i++)
191 fdisplay_ (VECTOR (x) + i, fd, write_p);
199 fdputs (itoa (TYPE (x)), fd);
201 fdputs (itoa (x), fd);
213 return display_helper (x, 0, "", g_stdout, 0);
217 display_error_ (SCM x)
220 return display_helper (x, 0, "", STDERR, 0);
224 display_port_ (SCM x, SCM p)
226 assert (TYPE (p) == TNUMBER);
227 return fdisplay_ (x, VALUE (p), 0);
234 return display_helper (x, 0, "", g_stdout, 1);
241 return display_helper (x, 0, "", STDERR, 1);
245 write_port_ (SCM x, SCM p)
247 assert (TYPE (p) == TNUMBER);
248 return fdisplay_ (x, VALUE (p), 1);
252 fdisplay_ (SCM x, int fd, int write_p) ///((internal))
255 return display_helper (x, 0, "", fd, write_p);
259 exit_ (SCM x) ///((name . "exit"))
261 assert (TYPE (x) == TNUMBER);
266 xassq (SCM x, SCM a) ///for speed in core only
268 while (a != cell_nil && x != CDAR (a))
270 return a != cell_nil ? CAR (a) : cell_f;
282 while (a != cell_nil && v != VALUE (CAR (a)))
289 while (a != cell_nil && v != STRING (CAR (a)))
296 while (a != cell_nil && x != CAR (a))
299 return a != cell_nil ? a : cell_f;
303 equal2_p (SCM a, SCM b)
308 if (TYPE (a) == TPAIR && TYPE (b) == TPAIR)
310 if (equal2_p (CAR (a), CAR (b)) == cell_t)
318 if (TYPE (a) == TSTRING && TYPE (b) == TSTRING)
324 if (TYPE (a) == TVECTOR && TYPE (b) == TVECTOR)
326 if (LENGTH (a) != LENGTH (b))
328 for (int i=0; i < LENGTH (a); i++)
330 SCM ai = VECTOR (a) + i;
331 SCM bi = VECTOR (b) + i;
332 if (TYPE (ai) == TREF)
334 if (TYPE (bi) == TREF)
336 if (equal2_p (ai, bi) == cell_f)
347 while (x != cell_nil && CDR (x) != cell_nil)