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;
35 if (TYPE (x) != TPAIR) return MAKE_NUMBER (-1);
38 return MAKE_NUMBER (n);
42 list (SCM x) ///((arity . n))
48 exit_ (SCM x) ///((name . "exit"))
50 assert (TYPE (x) == TNUMBER);
55 append (SCM x) ///((arity . n))
57 if (x == cell_nil) return cell_nil;
58 if (cdr (x) == cell_nil) return car (x);
59 return append2 (car (x), append (cdr (x)));
64 // string_to_cstring (SCM s)
66 // static char buf[1024];
69 // while (s != cell_nil)
71 // *p++ = VALUE (car (s));
79 error (SCM key, SCM x)
82 if ((throw = assq_ref_env (cell_symbol_throw, r0)) != cell_undefined)
83 return apply (throw, cons (key, cons (x, cell_nil)), r0);
88 assert_defined (SCM x, SCM e)
90 if (e == cell_undefined) return error (cell_symbol_unbound_variable, x);
95 check_formals (SCM f, SCM formals, SCM args)
97 int flen = (TYPE (formals) == TNUMBER) ? VALUE (formals) : VALUE (length (formals));
98 int alen = VALUE (length (args));
99 if (alen != flen && alen != -1 && flen != -1)
102 sprintf (buf, "apply: wrong number of arguments; expected: %d, got: %d: ", flen, alen);
103 SCM e = MAKE_STRING (cstring_to_list (buf));
104 return error (cell_symbol_wrong_number_of_args, cons (e, f));
106 return cell_unspecified;
110 check_apply (SCM f, SCM e)
112 char const* type = 0;
113 if (f == cell_f || f == cell_t) type = "bool";
114 if (f == cell_nil) type = "nil";
115 if (f == cell_unspecified) type = "*unspecified*";
116 if (f == cell_undefined) type = "*undefined*";
117 if (TYPE (f) == TCHAR) type = "char";
118 if (TYPE (f) == TNUMBER) type = "number";
119 if (TYPE (f) == TSTRING) type = "string";
124 sprintf (buf, "cannot apply: %s:", type);
125 fprintf (stderr, " [");
127 fprintf (stderr, "]\n");
128 SCM e = MAKE_STRING (cstring_to_list (buf));
129 return error (cell_symbol_wrong_type_arg, cons (e, f));
131 return cell_unspecified;
147 *p-- = '0' + (x % 10);
161 fputs ("program r2=", stderr);
163 fputs ("\n", stderr);
169 char *p = (char*)g_cells;
173 fputc (g_stack >> 8, stdout);
174 fputc (g_stack % 256, stdout);
175 if (getenv ("MES_HACK"))
177 TYPE (9) = 0x2d2d2d2d;
178 CAR (9) = 0x2d2d2d2d;
179 CDR (9) = 0x3e3e3e3e;
186 CAR (11) = 0x58585858;
194 CAR (11) = 0x58585858;
197 TYPE (14) = 0x3c3c3c3c;
198 CAR (14) = 0x2d2d2d2d;
199 CDR (14) = 0x2d2d2d2d;
203 for (int i=0; i<g_free * sizeof(struct scm); i++)
204 fputc (*p++, stdout);
209 load_env (SCM a) ///((internal))
212 if (getenv ("MES_MINI"))
213 g_stdin = fopen ("mini-0.mes", "r");
216 g_stdin = fopen ("module/mes/read-0.mes", "r");
217 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mes", "r");
219 if (!g_function) r0 = mes_builtins (r0);
220 r2 = read_input_file_env (r0);
226 bload_env (SCM a) ///((internal))
229 g_stdin = fopen ("mini-0.mo", "r");
231 g_stdin = fopen ("module/mes/read-0.mo", "r");
232 g_stdin = g_stdin ? g_stdin : fopen (PREFIX "module/mes/read-0.mo", "r");
235 char *p = (char*)g_cells;
236 assert (getchar () == 'M');
237 assert (getchar () == 'E');
238 assert (getchar () == 'S');
239 g_stack = getchar () << 8;
240 g_stack += getchar ();
247 g_free = (p-(char*)g_cells) / sizeof (struct scm);
251 r0 = mes_builtins (r0);