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 xassq (SCM x, SCM a) ///for speed in core only
24 while (a != cell_nil && x != CDAR (a)) a = CDR (a);
25 return a != cell_nil ? CAR (a) : cell_f;
33 // while (x != cell_nil)
36 // if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
39 // return MAKE_NUMBER (n);
43 exit_ (SCM x) ///((name . "exit"))
45 assert (TYPE (x) == TNUMBER);
50 append (SCM x) ///((arity . n))
52 if (x == cell_nil) return cell_nil;
53 if (cdr (x) == cell_nil) return car (x);
54 return append2 (car (x), append (cdr (x)));
59 // string_to_cstring (SCM s)
61 // static char buf[1024];
64 // while (s != cell_nil)
66 // *p++ = VALUE (car (s));
74 // error (SCM key, SCM x)
77 // if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
78 // return apply (throw, cons (key, cons (x, cell_nil)), r0);
83 assert_defined (SCM x, SCM e) ///(internal)
85 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
90 check_formals (SCM f, SCM formals, SCM args) ///((internal))
92 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
93 int alen = VALUE (length (args));
94 if (alen != flen && alen != -1 && flen != -1)
97 sprintf (buf, "apply: wrong number of arguments; expected: %d, got: %d: ", flen, alen);
98 SCM e = MAKE_STRING (cstring_to_list (buf));
99 return error (cell_symbol_wrong_number_of_args, cons (e, f));
101 return cell_unspecified;
105 check_apply (SCM f, SCM e)
107 char const* type = 0;
108 if (f == cell_f || f == cell_t) type = "bool";
109 if (f == cell_nil) type = "nil";
110 if (f == cell_unspecified) type = "*unspecified*";
111 if (f == cell_undefined) type = "*undefined*";
112 if (TYPE (f) == TCHAR) type = "char";
113 if (TYPE (f) == TNUMBER) type = "number";
114 if (TYPE (f) == TSTRING) type = "string";
119 sprintf (buf, "cannot apply: %s:", type);
120 fprintf (stderr, " [");
122 fprintf (stderr, "]\n");
123 SCM e = MAKE_STRING (cstring_to_list (buf));
124 return error (cell_symbol_wrong_type_arg, cons (e, f));
126 return cell_unspecified;
142 *p-- = '0' + (x % 10);
156 fputs ("program r2=", stderr);
158 fputs ("\n", stderr);
164 char *p = (char*)g_cells;
168 fputc (g_stack >> 8, stdout);
169 fputc (g_stack % 256, stdout);
170 // See HACKING, simple crafted dump for tiny-mes.c
171 if (getenv ("MES_TINY"))
173 TYPE (9) = 0x2d2d2d2d;
174 CAR (9) = 0x2d2d2d2d;
175 CDR (9) = 0x3e3e3e3e;
182 CAR (11) = 0x58585858;
190 CAR (11) = 0x58585858;
193 TYPE (14) = 0x3c3c3c3c;
194 CAR (14) = 0x2d2d2d2d;
195 CDR (14) = 0x2d2d2d2d;
199 for (int i=0; i<g_free * sizeof(struct scm); i++)
200 fputc (*p++, stdout);
205 load_env (SCM a) ///((internal))
208 g_stdin = fopen ("module/mes/read-0.mes", "r");
209 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mes", "r");
210 if (!g_function) r0 = mes_builtins (r0);
211 r2 = read_input_file_env (r0);
217 bload_env (SCM a) ///((internal))
220 g_stdin = fopen ("module/mes/read-0-32.mo", "r");
222 g_stdin = fopen ("module/mes/read-0.mo", "r");
223 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
226 char *p = (char*)g_cells;
227 assert (getchar () == 'M');
228 assert (getchar () == 'E');
229 assert (getchar () == 'S');
230 g_stack = getchar () << 8;
231 g_stack += getchar ();
238 g_free = (p-(char*)g_cells) / sizeof (struct scm);
242 r0 = mes_builtins (r0);